ThingSpeak Temperature and Humidity IoT Device

In this blog post I will show you how to build a temperature and humidity IoT device with an ESP8266 NodeMCU board and a DHT11 sensor that sends data to a ThingSpeak channel. It is divided in five parts:

  1. Prerequisites
  2. Set Up Android IDE
  3. Prepare the Breadboard
  4. Write the Code
  5. Watch it Working

Prerequisites

There are just a few components need to build this device:

  • ESP8266 NodeMCU board (or a similar ESP8266 board with I/O pins)
  • DHT temperature/humidity sensor (in this case it is a DHT11, but you can also use others, like the DHT22)
  • USB power bank
  • Breadboard (to connect the sensor with the board)

Set Up Android IDE

  1. Download and install the Arduino IDE from arduino.cc
  2. Go to „Preferences“ and insert into „Additional Boards Manager URLs“: http://arduino.esp8266.com/stable/package_esp8266com_index.json
  3. Open the library manager: „Sketch => Include Library => Manage Libraries“
  4. Install the ESP8266 library: „esp8266 by ESP8266 Community“
  5. Install the DHT library: „DHT sensor library by Adafruit“

Prepare the Breadboard

The DHT is a digital sensor and needs to be connected to a GPIO pin of the board. The other 2 pins are for VCC and GND. In my case it fits perfectly on the D4 pin, because there is a 3V and GND pin right beneath.

The sketch above shows the schematic circuit, but with a DHT22. The value of the pull-up resistor is 10kΩ.


And the final breadboard looks like this. There we can plug in the ESP8266 NodeMCU board and the DHT11 sensor.

Write the Code

/*
 ESP8266 DHT11 ThingSpeak Channel

Sends the temperature and humidity values from a DHT11 sensor to a ThingSpeak channel.

What you need:
 - ESP8266 NodeMCU board (or any other board with I/O pins)
 - DHT temperature/humidity sensor
 - USB power bank
 - breadboard (to connect the sensor with the board)

What libraries you need to install first:
 - esp8266 by ESP8266 Community (set "Additional Boards Manager URLs in preferences to http://arduino.esp8266.com/stable/package_esp8266com_index.json)
 - DHT sensor library by Adafruit

Blog Post: https://www.grisa.at/blog/2018/04/23/thingspeak-temperature-and-humidity-iot-device/
*/

#include <ESP8266WiFi.h>
#include <DHT.h>

#define DHTPIN 2 // digital pin the DHT11 is concected to
#define DHTTYPE DHT11 // DHT sensor type has to be defined

WiFiClient client;
DHT dht(DHTPIN, DHTTYPE);

// wifi
const char* ssid = "SSID";
const char* password = "PASSWORD";

// ThingSpeak
const int channelID = CHANNELID;
String writeApiKey = "WRITEAPIKEY";
const char* server = "api.thingspeak.com";
const int interval = 60 * 1000; // send data every 60s

void setup() {
 pinMode(DHTPIN, INPUT_PULLUP); // set DTH pin to input mode
 
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 }
}

void loop() {
 // send data to the ThingSpeak API
 sendData();
 // wait and then post again
 delay(interval);
}

void sendData() {
 if (client.connect(server, 80)) {
 // read sensor values
 float humidity = dht.readHumidity();
 float temperature = dht.readTemperature();

// request body
 String body = "field1=";
 body += String(temperature);
 body += "&field2=";
 body += String(humidity);

// send request
 client.println("POST /update HTTP/1.1");
 client.println("Host: api.thingspeak.com");
 client.println("Connection: close");
 client.println("X-THINGSPEAKAPIKEY: " + writeApiKey);
 client.println("Content-Type: application/x-www-form-urlencoded");
 client.println("Content-Length: " + String(body.length()));
 client.println("");
 client.print(body);
 }
 client.stop();
}

Set the WiFi SSID and password and the ThingSpeak channel ID and the write API key. Then you are ready to upload it to your ESP8266 NodeMCU, to do so your board needs to be in flash-mode. My board activates flash-mode by pressing the „FLASH“ and „RST“ button at the same time.

Watch it Working

Now you should see some Data in your ThingsSpeak channel.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.