Intermittent results from multiple esp32c3

Hi folks, hoping for some help with a project I have going. I have esp32c3 for reading temperature and submitting over WiFi to mqtt broker, using deep sleep then waking every 15 minutes to submit temperature.
Sketch below:

#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Wi-Fi credentials
const char* ssid = "Our Hooseynetwork";
const char* password = "Bumhole1pwd";

// MQTT broker details
const char* mqtt_server = "192.168.1.19";
const char* mqtt_user = "fridge1";
const char* mqtt_password = "fridge1";

// Device hostname
const char* hostname = "C3-Fridge-1";

// Pin definitions
#define DS18B20_PIN 6  // GPIO6 for DS18B20
#define BATTERY_PIN 4  // GPIO4 (D4) for ADC battery monitoring

// Voltage divider resistors
#define R1 220000 // 220kΩ
#define R2 220000 // 220kΩ

// Battery voltage limits
#define BATTERY_MAX 4.2
#define BATTERY_MIN 3.0

// Deep sleep time in microseconds (15 minutes)
#define SLEEP_TIME 900000000

// DS18B20 setup
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);

// Wi-Fi and MQTT clients
WiFiClient espClient;
PubSubClient client(espClient);

// Function to read battery voltage
float readBatteryVoltage() {
    analogSetPinAttenuation(BATTERY_PIN, ADC_11db); // Configure ADC for 0-2.5V range
    int raw = analogRead(BATTERY_PIN); // Read raw ADC value
    float vOut = (raw / 4095.0) * 2.5; // Convert ADC value to voltage
    float vBattery = vOut / 0.5; // Scale to battery voltage
    return vBattery;
}

// Function to calculate battery percentage
int calculateBatteryPercentage(float voltage) {
    float percentage = ((voltage - BATTERY_MIN) / (BATTERY_MAX - BATTERY_MIN)) * 100.0;
    if (percentage > 100) percentage = 100;
    if (percentage < 0) percentage = 0;
    return (int)percentage;
}

// Function to connect to Wi-Fi
void connectToWiFi() {
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    WiFi.setHostname(hostname);

    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
    }
}

// Function to connect to MQTT broker
void connectToMQTT() {
    while (!client.connected()) {
        if (client.connect(hostname, mqtt_user, mqtt_password)) {
            // Connected
        } else {
            delay(5000);
        }
    }
}

void setup() {
    sensors.begin(); // Start DS18B20
    connectToWiFi();
    client.setServer(mqtt_server, 1883);
    connectToMQTT();

    // Read temperature
    sensors.requestTemperatures();
    float temperature = sensors.getTempCByIndex(0);
    if (temperature == DEVICE_DISCONNECTED_C) {
        temperature = -127; // Error value
    }

    // Read battery voltage
    float batteryVoltage = readBatteryVoltage();

    // Calculate battery percentage
    int batteryPercentage = calculateBatteryPercentage(batteryVoltage);

    // Publish data
    char payload[100];
    snprintf(payload, sizeof(payload), "{\"hostname\":\"%s\",\"temperature\":%.2f,\"battery_percentage\":%d}", hostname, temperature, batteryPercentage);
    client.publish("fridge/temperature", payload);

    // Deep sleep
    esp_sleep_enable_timer_wakeup(SLEEP_TIME);
    delay(1000); // Ensure MQTT message is sent
    esp_deep_sleep_start();
}

void loop() {
    // Not used
}

I have 5 of these, all the same hardware and all the same sketch but I’ve changed the host name on each. All of them run once, 2 of them have worked perfectly every 15 minutes for days now but 3 of them hang up after submitting the message once. I can see on my network that they are just connected the whole time while the working ones are only connected for a few seconds every 15 minutes, if I manually disconnect them through my network controller (unifi) they reconnect and run the sketch but hang up again until I disconnect them again when they’ll do it again.
Is it possible my soldering skills have a part to play in this? I did put them together very quickly. Or are there firmware updates for them and that could be the difference? Or am I barking up the wrong tree entirely? Any help would be greatly appreciated. Thanks for reading.

this is just a wild guess… but can you try re-uploading the sketches to the devices… i have experienced when you upload… sometimes the new sketch does not “stick” and if you only make minor revisions… you may not be able to tell it is still running the old sketch… if this makes sence…

maybe add a line of code that prints the version or compile date to the serial monitor so you can not assume they are all running the exact same code

Another lesson for you at my expense :smiley: : never assume your firewall has done what you asked…
I set eset to allow all incoming and outgoings to mosquito broker. Turns out it was blocking the port…but only sometimes… I’ve setup a raspberry pi as the broker just now cause my main workstation has a lot of other things going on and they haven’t skipped a beat since.

Wondering if I should delete this topic?

1 Like

no leave for history sake… it will make its way down soon enough and someone may find it in a google search someday