Off-The-Shelf Hacker: Build a Networked Temperature Sensor with the ESP8266

Last week in Off-the-Shelf Hacker, I talked about some of my projects with the ESP8266 WiFi microcontroller. Let’s take a look at how easy it is to put something together.
Back in 2012, I gave a talk at OSCON, “Hacking Together A Basic Remote Sensor With Arduino, Xbee Radios and Linux.” The hack used an Arduino connected to a Dallas DS18B20 digital thermometer, coupled to a Xbee radio. At the other end of the wireless connection was another Xbee radio connected to my Linux notebook. The Arduino read the Dallas sensor and sent the temperature data over the Xbee link and showed the results in a terminal, on the notebook. The Xbee radios were 60 mW models, with external antennas and a range of about 5,000 feet. Yes, I tested them across a lake.
And here we are today. But this time, I’m using an ESP8266 in place of the Arduino and one of the Xbee radios. We also eliminate the other Xbee radio and can simply access the data over a LAN connection.
Wire It up and Upload the Firmware
You’ll need these parts:
- ESP8266 model 07 — WiFi microcontroller (version 07)
- DS18B20 — Dallas digital temperature sensor
- Breadboard
- 4.7K resistor
- 3.3 regulator
- (1) 10 uF capacitor
- (1) 100 uF capacitor
- Linux notebook
I’ve had pretty good luck with Banggood and the turn around (from China) is about ten days. Be sure to look for “shipping included” deals. You can also source what you need from Adafruit, Sparkfun and other Off-The-Shelf Hacker hardware vendors.
Here’s a shot of the breadboard.

ESP8266 WiFi microcontroller with 18B20 temperature sensor on a breadboard
The 3.3 voltage regulator and two filter capacitors are pretty standard. The arrangement takes the 5.0 volts from the USB/serial line and converts it to the 3.3 volts needed by the ESP8266. Most people will remove the USB/serial connections after programming and simply supply 3.3 to 9 volts to the power input pin on the 3.3-volt regulator for normal operation.
Readers will also note that the DS18B20 digital temperature sensor is operating in “parasitic” mode, meaning the VCC (+ voltage) and ground are connected. This lets you use just the data pin and ground, without having to run an extra wire to the VCC pin (normal mode). Other hackers on the Web say that you’ll get more reliable temperature readings, especially when using long cables to the sensor when it’s wired up in normal mode. I’ve always used parasitic mode, with a basic two-wire (22 gauge) bus cable up to about 50 feet in length, without problems.
The Arduino IDE is fast becoming a standard for programming the Arduino, the ESP8266 and a few other firmware-based devices. A quick way to build a working program is to take code segments out of the examples and link them together in useful ways. Much of the heavy lifting, to make the ESP8266s do things, is detailed in the examples and libraries. I pirated a good portion of my WiFi-slide-scroll program for the Steampunk wireless clicker and hacked in the code to read and print data from the DS18B20 sensor.
A couple of things to note.
On the ESP8266 07 model, be aware that some of the chips are mismarked. Pin GPIO 4 is reversed with pin GPIO 5. I just made the adjustment in the code. If you weren’t aware of this, you’d chase your tail for a while wondering why you’d never get a valid reading. It may be that you have a mismarked chip.
A cool ESP8266 function you’ll definitely want to leverage is the WiFi configuration routine. With this function, the 8266 boots up in access point (AP) mode. It also starts a little Web server, then searches for available APs in the area. Next, it lists those APs and lets you select one. You’ll be asked for a passphrase if the AP is using encryption. Upon entering the right passphrase, the 8266 then connects to the chosen network and starts making temperature data available to networked clients. To get to the Web configuration screen, you simply connect to the 8266 access point, on your notebook or smartphone and point your browser to 192.168.4.1.
How convenient is that?
Here’s the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
#include <OneWire.h> #include <DallasTemperature.h> #include <ESP8266WiFi.h> #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> void configModeCallback (WiFiManager *myWiFiManager) { Serial.println("Entered config mode"); Serial.println(WiFi.softAPIP()); //if you used auto generated SSID, print it Serial.println(myWiFiManager->getConfigPortalSSID()); } // Data wire is plugged into port 2 on the Arduino #define ONE_WIRE_BUS 4 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); /* * The setup function. We only start the sensors here */ WiFiServer server(1337); void printWiFiStatus(); void setup(void) { // start serial port Serial.begin(9600); Serial.println("Dallas Temperature IC Control Library Demo"); WiFiManager wifiManager; // Comment out the reset line, to keep SSIDs and log into APs automatically after configuration wifiManager.resetSettings(); wifiManager.setAPCallback(configModeCallback); if(!wifiManager.autoConnect()) { Serial.println("failed to connect and hit timeout"); ESP.reset(); delay(1000); } // Start up the library sensors.begin(); // Start TCP server. server.begin(); } /* * Main function, get and show the temperature */ void loop(void) { // Check if module is still connected to WiFi. if (WiFi.status() != WL_CONNECTED) { Serial.println("WiFi connected inside void loop"); while (WiFi.status() != WL_CONNECTED) { Serial.println("WiFi.status connected loop"); delay(500); } // Print the new IP to Serial. printWiFiStatus(); } WiFiClient client = server.available(); if (client) { Serial.println("Client connected."); while (client.connected()) { // call sensors.requestTemperatures() to issue a global temperature // request to all devices on the bus sensors.requestTemperatures(); // Send the command to get temperatures // After we got the temperatures, we can print them here. // We use the function ByIndex, and as an example get the temperature from the first sensor only. Serial.println(sensors.getTempFByIndex(0)); client.print(sensors.getTempFByIndex(0)); client.write("\n"); delay(1000); } Serial.println("Client disconnected."); client.stop(); } } void printWiFiStatus() { Serial.println(""); Serial.print("Connected to "); // Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } |
My normal programming procedure is as follows.
- Plug the USB/serial cable into my Linux notebook.
- Start up the Arduino IDE.
- Open the 8266-ds18b20-temp-sensor file (contains the above-mentioned code).
- Disconnect the GPIO 0 wire (purple) from 3.3 volts and connect it to ground.
- Disconnect the TX wire temporarily.
- Short the reset pin wire (blue) to ground, then disconnect.
- Reconnect the TX wire.
After completing these steps, you can hit “upload” in the Arduino IDE to send the firmware from the Linux notebook to the ESP8266 module. The blue wireless LED, on the 8266 should blink as the firmware is uploaded. When it stops flashing, the 8266 should reset. If it doesn’t, simply short the reset pin to ground and disconnect.
For normal operation, return the GPIO 0 wire (ground) from ground to 3.3 volts. You can leave it connected to ground while programming/testing if you like.
After the reset, start up the serial terminal in the Arduino IDE and change the baud rate to 9600.
Next, on a smartphone or your notebook, scan for local access points. Connect to the one with “8266” or “ESP” in its name. Start your browser and go to http://192.168.4.1. You should see the 8266 WiFi configuration screen. Here’s what it looks like on my Samsung Galaxy 5 Active super-phone.

ESP8266 WiFi configuration Web page on my Galaxy 5 Active super-phone.
Select the “Configure WiFi” button. Find and select your local wireless network and enter the passphrase if needed. Push “Save” to restart the 8266. It should connect to your local WiFi and start the little TCP server to stream data over port 1337. Use nmap on your Linux notebook or FING on Android to find the 8266’s IP address.
To retain local access points on the 8266, be sure to comment out the “wifiManager.resetSettings();” line in the code, on the final firmware upload. Otherwise, the 8266 will clear its history of connections to access points, after each power up.
You can view messages and the temperature readings, from the 8266, by activating the serial window anytime the device is connected to the Arduino IDE through the USB/serial line connection.
Grabbing the Data on the Notebook Side
Once the 8266 is connected to your local network, you can capture the temperature readings on a Linux notebook.
Make sure your Linux notebook is connected to the same LAN as your 8266 device. Fire up a terminal and run netcat to view your 8266 temperature data. Here’s the command line I used, with the 108 address being the 8266 module.
1 |
rob% netcat 192.168.1.108 1337 |
You should see the temperature reading, one per line about once per second. You could also pipe the data to a file if you wanted to record a stream of readings over time.
1 |
rob% netcat 192.168.1.108 1337 > temp.txt |
I’ll leave it to readers to add some kind of time-stamp to the data stream. Just use an Arduino time/date function and add the output to the text stream with each temperature reading. Note that DS18B20 can output valid temperature readings once every 750 ms, due to hardware communication limitations. So, getting a reading about once per second ensures reliable data.
How Far We’ve Come
The 8266 temperature sensor is a deceptively simple project. Only a few years ago, it took an Arduino and a couple of XBee radios to get a rudimentary wireless link to a notebook that wasn’t even network-enabled.
Today, you only need one chip. It’s also configured over a temporary access point and WiFi using a Web page. There is actually a little Web server running on the 8266! After initial configuration, it will simply connect to the local LAN and stream the data over a little TCP server. Connect a suitable client and you’ve got a basic data logging system. That’s an awful lot of Internet of Things horsepower in a teeny-tiny package.
Just think of what’s around the corner.
Now go hack yourself something interesting.