SWHarden.com

The personal website of Scott W Harden

WiFi Temperature Sensor with NodeMCU

An internet-enabled digital thermometer for temperature logging and live time series visualization

I spent the last few years experimenting with designs for internet-enabled temperature sensors so I could monitor the relationship between my indoor, outdoor, garage, and attic temperatures over long time spans. This page documents my favorite implementations using NodeMCU, a USB powered and programmable Arduino-compatible IoT development board with an ESP8266 WiFi module that can be purchased on Amazon in small quantities for about $5 each. The design presented on this page uses a DS18B20 1-Wire thermometer packaged in a weatherproof enclosure sold in small quantities for about $2 each.

Connections

Connecting the 1-Wire sensor only requires adding an external resistor to pull the data pin high. I found it convenient to solder the resistor and sensor wires directly to pins of the NodeMCU development board. It is worth noting that the DS18B20 is available in several different form factors, but the communication protocol and wiring requirements are the same.

Embedded Software

The NodeMCU can be programmed via its micro USB connection using the Arduino IDE. Full source code for this project is available on GitHub, but the following sketch is a trimmed-down version which is likely sufficient for others to replicate this project.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>

OneWire oneWire(D2); // Data wire connected to D2
DallasTemperature sensors(&oneWire);

void setup() {
  sensors.begin();
  WiFi.begin("WIFI_SSID", "WIFI_PASSWORD"); // Customize for your network
  while (WiFi.status() != WL_CONNECTED) {delay(1000);}
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);
}

void loop() {
  // Read temperature
  sensors.requestTemperatures();
  float temp = sensors.getTempFByIndex(0);

  // Prepare the endpoint URL
  String resource = String("https://swharden.com/weather/v1/write/");

  // Create JSON body for the POST request
  String data = String("{")
                + String("\"key\": \"MY_SECRET_API_KEY\",") // Customize for security
                + String("\"sensor\": \"3\",") // Sensor ID for multi-sensor environments
                + String("\"temperature\": \"") + String(temp) + String("\",")
                + String("}");

  // Create a WiFi client that does not use SSL
  WiFiClientSecure client;
  client.setInsecure();
  client.connect("swharden.com", 443);

  // Make the POST request including our custom JSON body
  HTTPClient http;
  http.begin(client, resource);
  http.addHeader("Content-Type", "application/json");
  http.POST(data);
  http.end();

  // Wait one minute before retrying
  int seconds = 60;
  while (seconds--) {delay(1000);}
}

Temperature Dashboard

Users will want to customize their web server architecture, but I achieved a live temperature dashboard using a PHP endpoint that interprets the body of the POST request and logs the time/sensor/temperature as a single line appended to a CSV file. The interactive graphs are achieved using Dygraph, a JavaScript library ideal for interactively displaying time-series data on webpages.

Indoor WiFi Module with Outdoor Temperature Sensor

I am fortunate to already have easy access to an indoor/outdoor pass-through created to run HF and VHF coax cabling from indoor radios to outdoor antennas. This made it easy to house the weather-sensitive NodeMCU module inside while snaking the weather-insensitive temperature sensor outside. This is on the North side of my house too, so it’s always in shadow and it should be a pretty good location for direct air temperature measurement.

Garage Temperature Sensor

The sensor in my garage is perhaps the cleanest build. I kept the USB cable short and only had the tip of the temperature sensor stick out the top. The NodeMCU has blue LEDs on it already, so I drilled a hole in the enclosure (sealed off with clear plastic glued to the underside of the hole) allowing the blinking light to be observed from outside. Short flashes indicate it is waiting until the next measurement, and it turns solid while it’s measuring temperature and submitting the HTTP request.

Weatherproof Design for Outdoor Use

Originally I created a “weatherproof” enclosure to house the NodeMCU microcontroller and temperature sensor, powered using low voltage delivered through landscaping wire typically used for lard lighting. This design used a BMP280 sensor (described below) to measure temperature and barometric pressure. I put “weatherproof” in quotes because after almost two years it failed. The experience was an interesting (and humbling) venture into designing IoT gear intended to stand up against the elements. For what it’s worth, it did make it through two Florida hurricanes.

Alternative Designs to Measure Temperature and Pressure with BMP280

Previous WiFi temperature sensor designs used the BMP280 I2C temperature and pressure sensor which I had on hand from previous precision pressure measurement project, but the software required to read temperature and pressure was surprisingly complex due to the calibration procedure described in the datasheet. In contrast, the design using the SD18B20 digital temperature sensor was extremely easy to implement. There are Arduino sketches on this project’s GitHub repository demonstrating how to use both sensors with NodeMCU.

Additional Resources