Updated date:

Publish DHT11 Sensor Data to Adafruit IO Platform Using ESP8266

Tim is currently doing research in Internet of Things (IoT). His desire to spread the concept, ideas, and experience of IoT.

Send Data DHT11 Data to AIO platform With MQTT

In this tutorial, I will show you how to send temperature and humidity data from the DHT11 sensor to Adafruit IO (AIO) platform via the MQTT protocol. I will develop a sensor node that acts as an MQTT client and publishes the data. On the AIO platform the published data will be displayed using graphs.

Components Required

  • NodeMCU X 1
  • DTH11 Sensor X 1
  • Jumper Wires X 3 (Male-To-Male)
  • Breadboard X 1

Let's Begin

In this tutorial, I am using the DHT11 sensor which has three pins, DATA, VCC, and GND as shown in the following figure. You can also use the other DHT11 sensor which has 4 pins.

This DHT11 sensor has 3 pins: Data, VCC, and GND.

This DHT11 sensor has 3 pins: Data, VCC, and GND.

1. Circuit

Connect the DATA pin of the DHT11 sensor to the NodeMCU D6 pin and VCC, GND to Vin, and GND pins respectively of NodeMCU. The circuit diagram is shown below.

Circuit diagram.

Circuit diagram.

2. Arduino Code

Now upload the following Arduino code to NodeMCU. Please remember to specify your WiFi SSID and password and also username and KEY of the Adafruit IO (AIO) platform.

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "YOUR_WIFI_SSID"
#define WLAN_PASS       "YOUR_WIFI_PASSWORD"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "YOUR_AIO_USERNAME"
#define AIO_KEY         "YOUR_AIO_KEY"

/************* DHT11 Setup ********************************************/
#define DHTPIN D6
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temp");
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/hum");


/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
  Serial.begin(115200);
  dht.begin();
  delay(10);

  Serial.println(F("Adafruit MQTT demo"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

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

}

uint32_t x=0;

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  // Read humidity
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();

  //publish temperature and humidity
  Serial.print(F("\nTemperature: "));
  Serial.print(t);
  Serial.print(F("\nHumidity: "));
  Serial.print(h);
  
  temperature.publish(t);
  humidity.publish(h);
    
  delay(60000);

}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

3. Build User Interface

Our next step is to build the user interface (UI) where we can display the data published by the sensor node.

  • After you log in, you will see the AIO_Controller dashboard. If you have not created the dashboard then please do so now.
Adafruit dashboard.

Adafruit dashboard.

The first thing we need is to create the Feeds where our data will be published. The Feeds are the MQTT topics. These topics are then bound to our UI which will display data graphically. In this tutorial, I'll show another method to create feeds.

  • For creating feeds go to Feeds->View All as shown in the following figure.
"Feeds" section in Adafruit dashboard.

"Feeds" section in Adafruit dashboard.

  • Now, click on Actions->Create a New Feed as shown below.
Create a new feed in Adafruit dashboard.

Create a new feed in Adafruit dashboard.

  • You will see the Create New Feed dialog box as shown in the following figure. Name the feed as 'temp'.
Naming your new feed in the Adafruit dashboard.

Naming your new feed in the Adafruit dashboard.

  • Repeat the action and create another feed 'hum' as shown below.
Create another new feed in the Adafruit dashboard.

Create another new feed in the Adafruit dashboard.

Now you will have two feeds 'temp' and 'hum' to store temperature and humidity data as shown below.

Your new feeds will now display their data in the Adafruit dashboard.

Your new feeds will now display their data in the Adafruit dashboard.

  • Now navigate to Dashboard->AIO_Controller.
AIO Controller, located in your Adafruit dashboard.

AIO Controller, located in your Adafruit dashboard.

  • And click the '+' button to add a new block (i.e. Line Chart UI) as shown in the following image.
Creating a new block in the Adafruit dashboard.

Creating a new block in the Adafruit dashboard.

  • Click on Line Chart UI and select the 'temp' feed from the list of feeds in the dialog box.
publish-dht11-sensor-data-to-adafruit-io-platform-using-esp8266
  • Click Next and specify block settings.
Specify your block settings.

Specify your block settings.

  • Similarly, add another Line Chart block by selecting 'hum' feed as shown in the following two images.
Add another Line Chart block.

Add another Line Chart block.

Line Chart block settings.

Line Chart block settings.

You will now see a dashboard with two line chart blocks.

Your two new line chart blocks.

Your two new line chart blocks.

  • Now connect your device and you will see temperature and humidity values published on the UI.
Voila! Now you can see the data on your dashboard.

Voila! Now you can see the data on your dashboard.

This content is accurate and true to the best of the author’s knowledge and is not meant to substitute for formal and individualized advice from a qualified professional.

Related Articles