Here is another small project of mine: battery operated ESP8266 ESP-01 WiFi thermometer using DS18B20 and ThingSpeak API to collect data.
Before we proceed, you should:
- Know how to program ESP8266 with Arduino IDE
- Know how to run ESP8266 on battery power for months
- Have an account on ThinkSpeak, create a channel and obtain write API key

Required hardware list:
- ESP8266 ESP-01
- DS18B20 digital thermometer
- 2 AA batteries and a holder. I’m using 3D printed from this design
- 4.7kOhm resistor
- some wires and universal PCB. I’m using 50x70mm
Electrical diagram is pretty simple and it takes only few minutes to solder everything into place. Please remember about deep sleep hack. It is required!


Code is also pretty straight forward: connect to WiFi, read temerature from DS18B20, sent temperature to ThingSpeak, go to sleep for 15 minutes, reboot and start again.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266mDNS.h>
#include <ESP8266WiFi.h>
#define ALLOWED_CONNECT_CYCLES 40
#define ALLOWED_READ_CYCLES 80
#define ONE_WIRE_BUS 2 // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
/*
* Update those 3 line to configure WiFi and ThingSpeak project
*/
const String apiKey = "[THINGSPEAK_API_KEY_GOES_HERE]";
const String wifiSSID = "[WIFI_NETWORK_SSID_GOES_HERE]";
const String wifiPassword = "[WIFI_NETWORK_PASSWORD_GOES_HERE]";
#define SLEEP_TIME_SECONDS 900 //900 for 15 minutes
const char* host = "api.thingspeak.com";
void setup() {
/*
* If after changing WiFi settings (SSID or password) ESP8266
* does not want to connect again, uncomment this line and reflash
*/
//ESP.eraseConfig();
WiFi.begin(wifiSSID, wifiPassword);
uint8_t counter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
counter++;
if (counter > ALLOWED_CONNECT_CYCLES) {
ESP.deepSleep(SLEEP_TIME_SECONDS * 1000000);
}
}
WiFiClient client;
const int httpPort = 80;
if (client.connect(host, httpPort)) {
digitalWrite(0, HIGH);
float temperature;
counter = 0;
do {
DS18B20.requestTemperatures();
temperature = DS18B20.getTempCByIndex(0);
counter++;
if (counter > ALLOWED_READ_CYCLES) {
ESP.deepSleep(SLEEP_TIME_SECONDS * 1000000);
}
} while (temperature == 85.0 || temperature == (-127.0));
client.print(String("GET /update?api_key=" + apiKey + "&field2=" + String(temperature)) + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
}
delay(2000);
ESP.deepSleep(SLEEP_TIME_SECONDS * 1000000);
}
void loop() {
}
The only “mysterious” line of code is ESP.eraseConfig();
. By default it is commented out and if everything is working, leave it like this. I have one ESP module that fails to change WiFi network if this line is not in place. I do not fully understand why it solves the problem, but it solves it. Maybe somebody more experience could throw some light on it?
Results can be seen here.

At this moment I have 2 wireless sensors connected to my heating system to monitor when it is working. More is on the way.
Leave a Reply