I think this is a part of where I went badly wrong. At one point trying to wade through chatgpts assumptions and reality I moved my temp sensor positive to the 5v pin which worked fine with usb but as I now know wouldn’t work from battery (assuming I’m on the right page now) I’ve had a few reconfigurations as I was learning about it from scratch and I think I need to start again with the knowledge I have now and see what happens. Sketch below just for curiosities sake, will update when I can if you’re interested. Currently trying to get it out of my mind to enjoy Christmas with the kids but it keeps gnawing at me
#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Wi-Fi credentials
const char* ssid = “network”;
const char* password = “pword”;
// 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 = “S3-Fridge-1”;
// Pin definitions
#define DS18B20_PIN 6 // GPIO6 for DS18B20
#define BATTERY_PIN 2 // GPIO0 (D0) for ADC battery monitoring
// Voltage divider ratio
#define DIVIDER_RATIO 0.3125
// 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-3.9V range
int raw = analogRead(BATTERY_PIN); // Read raw ADC value
float vOut = (raw / 4095.0) * 3.3; // Convert ADC value to voltage
float vBattery = vOut / DIVIDER_RATIO; // Scale back 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() {
// Initialize DS18B20
sensors.begin();
// Connect to Wi-Fi
connectToWiFi();
// Setup MQTT
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);
// Prepare for deep sleep
esp_sleep_enable_timer_wakeup(SLEEP_TIME); // Enable timer wake-up
delay(1000); // Allow MQTT messages to be sent
esp_deep_sleep_start(); // Enter deep sleep
}
void loop() {
// Not used
}
Thanks again for the replies, huge help to me