Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.
When the temperature log is the product
Unlike a simple alarm, this logger keeps a record: when a fridge for medicines or food stayed within range and when it did not. That is useful for a shop, a clinic room or a dacha cellar. A small battery keeps it recording through a blackout.
Making the record trustworthy
Store each reading with a timestamp in the flash file system, and export it as CSV. Between samples go to deep sleep, with a sample interval of one to five minutes. After a sleep the clock must stay right, so synchronise with NTP when Wi-Fi is available. The DS18B20 is specified to about ±0.5 °C, and you can check each probe in ice water, which should read close to 0 °C.
The screen shows the minimum and maximum since the last reset, as on a professional thermometer.
For medicines the typical range is 2 to 8 °C; set the alarm limits to your own requirements.
Keep the log when the network is gone. Upload when it returns.
Components
ESP32-C3 SuperMini
DS18B20 waterproof probe
OLED 0.96" 128x64 I2C SSD1306
18650 Li-ion cell + holder
TP4056 USB-C Li-ion charger with protection
Wiring
Module
Module pin
ESP32 pin
Note
DS18B20 DATA
1-Wire
GPIO5
4.7 kΩ pull-up to 3V3
OLED SDA / SCL
I2C
GPIO8 / GPIO9
0x3C
Battery
3.7 V Li-ion
TP4056 charger, battery to the 5V pin of the board
use a protected cell; a boost or the 3V3 pin variant needs a low-dropout setup
Libraries
OneWire
DallasTemperature
Adafruit SSD1306
Adafruit GFX Library
Code
#include <WiFi.h>
const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";
bool connectWiFi(uint32_t timeoutMs = 20000) {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
uint32_t t0 = millis();
while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
delay(250);
Serial.print(".");
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.print("IP: ");
Serial.println(WiFi.localIP());
return true;
}
return false;
}
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
const char* TG_TOKEN = "123456:ABC-YOUR-BOT-TOKEN"; // from @BotFather
const char* TG_CHAT = "YOUR_CHAT_ID";
// Keep the text free of double quotes and line breaks (it is inserted into JSON).
bool telegramSend(const String& text) {
if (WiFi.status() != WL_CONNECTED) return false;
WiFiClientSecure client;
client.setInsecure(); // simple; pin the certificate for production
HTTPClient http;
String url = String("https://api.telegram.org/bot") + TG_TOKEN + "/sendMessage";
if (!http.begin(client, url)) return false;
http.addHeader("Content-Type", "application/json");
String body = String("{"chat_id":"") + TG_CHAT + "","text":"" + text + ""}";
int code = http.POST(body);
http.end();
return code == 200;
}
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
OneWire ow(5); DallasTemperature ds(&ow);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const float LIMIT_HIGH = -10.0; // freezer: -10; use 8.0 for a fridge
const uint64_t SLEEP_US = 5ULL * 60ULL * 1000000ULL;
RTC_DATA_ATTR float tMin = 99, tMax = -99; RTC_DATA_ATTR int wakeCount = 0; RTC_DATA_ATTR int alertSent = 0;
void setup() {
Serial.begin(115200); Wire.begin(8, 9); ds.begin(); ds.requestTemperatures();
float t = ds.getTempCByIndex(0); wakeCount++;
if (t > -100) { tMin = min(tMin, t); tMax = max(tMax, t); }
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); oled.clearDisplay(); oled.setTextColor(WHITE);
oled.setTextSize(2); oled.setCursor(0, 0); oled.printf("%.1f C", t);
oled.setTextSize(1); oled.setCursor(0, 28); oled.printf("min %.1f max %.1f", tMin, tMax); oled.setCursor(0, 40); oled.printf("wake #%d", wakeCount); oled.display();
bool bad = t > LIMIT_HIGH || t < -100;
if (bad && alertSent < 3 && connectWiFi()) { telegramSend(String("Cold chain alert: ") + (t < -100 ? "sensor fault" : String(t, 1) + " C")); alertSent++; }
if (!bad) alertSent = 0;
if (wakeCount % 144 == 0 && connectWiFi()) telegramSend(String("Logger alive. Last 12 h min/max: ") + String(tMin, 1) + " / " + String(tMax, 1));
delay(3000); oled.ssd1306_command(SSD1306_DISPLAYOFF);
esp_sleep_enable_timer_wakeup(SLEEP_US); esp_deep_sleep_start();
}
void loop() {}
Build tips
Place the probe in a bottle of water or glycol to avoid false alarms when the door opens.
The OLED is only on briefly each wake to save the battery.
Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.
Comments
No comments yet. Share your experience or ask a question.