Some sweet 8266 <3 (Part 1)

The beauty of these little devices is both their ease of use and low cost

This is going to be a some what short project post to cover what I’ve done to help get the few 8266 boards up and running in my house.  The main purpose of these boards will be to monitor the changing temperatures in the various rooms they are going to be positioned in.  I will set them up to use MQTT to squirt the temp values back to a MQTT broker that will ultimately push it out to a database for logging.  I’m getting ahead of myself, lets first focus on simply getting these little puppies up and running.

Other than the 8266’s I also had to pickup a few other items, here’s my shopping list:

Hooking it all up

Getting it all put together is pretty straight forward, here is the pin layout used:

  • 8266’s 3-volt pin to DHT22’s pin 1
  • 8266’s ground pin to DHT22’s pin 4
  • 8266’s data 2 pin to DHT22’s pin 2

Here’s the finished product

Firmware flashing, coding, and testing fun

Getting the board and sensor all connected was of course just the start now I had to get it to communicate to the computer.  I’ve decided to go with using Arduino IDE for coding development since there is a great community ready to help give any help or tips in case I needed it.  Here’s a useful guide from Marginally Clever Robots you can use to get the software environment all setup and ready:

How to setup NodeMCU drivers and Arduino IDE

Once I was able to get it all up and running I just had to get a sketch all setup and ready to flash to the 8266.  Here’s the sketch I used:

#include &lt;esp8266wifi.h&gt;
#include &lt;pubsubclient.h&gt;
#include &lt;dht.h&gt;&lt;/dht.h&gt;&lt;/pubsubclient.h&gt;&lt;/esp8266wifi.h&gt;

#define wifi_ssid "My WiFi is better than yours Mark!" //Add your wifi's ssid between the quotes
#define wifi_password "SuperAwesomePassword!1!" //Then add the wifi password here

#define mqtt_server "" //Bet you guessed this is the IP pointing back to your MQTT server
#define mqtt_user "tempreader" //Your defined username
#define mqtt_password "somepassword" //And that username's password
//Make sure to change the below to match your own defined topics
#define humidity_topic "sensor1/humidity"
#define temperature_topic "sensor1/temperature"

#define DHTTYPE DHT22
#define DHTPIN 4

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  client.setServer(mqtt_server, 1883);

void setup_wifi() {
  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {

  Serial.println("WiFi connected");
  Serial.println("IP address: ");

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    //Cut out from the below loop-- []
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

bool checkBound(float newValue, float prevValue, float maxDiff) {
  return !isnan(newValue) &amp;amp;&amp;amp;
         (newValue &amp;lt; prevValue - maxDiff || newValue &amp;gt; prevValue + maxDiff);

long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 0.5;

void loop() {
  if (!client.connected()) {

  long now = millis();
  if (now - lastMsg &amp;gt; 2000) {
    lastMsg = now;

 // Read temperature as Fahrenheit (isFahrenheit = true)

    dht.readTemperature(true);  // returns F
    float newTemp = dht.readTemperature();
    float newHum = dht.readHumidity();

    if (checkBound(newTemp, temp, diff)) {
      temp = newTemp;
      Serial.print("New temperature:");
      client.publish(temperature_topic, String(temp).c_str(), true);

    if (checkBound(newHum, hum, diff)) {
      hum = newHum;
      Serial.print("New humidity:");
      client.publish(humidity_topic, String(hum).c_str(), true);

Small update, I found that DHTPIN 5 and the defined DHT variables were off.  I’ve since corrected them in the above raw coding, apologies for any confusion there. 

Once that’s all flashed to the dev board I was then able to check that it was running properly by selecting the serial monitor under Tools option.  Here’s expected readout:

It’s failing to connect to the MQTT broker since I haven’t setup that in end yet.  That’ll get covered in the future project posts.  The good thing is that its up and connecting to the WiFi without issue.

That pretty much wraps up part 1 of the Home Temp Monitoring project.  I’ll need to get the broker, database, and choose a web server to present the recorded values in later updates.