Menu Close

NodeMCU Temperature, Humidity data upload on Thingspeak on Arduino IDE

This tutorial of Robo India explains how to store and upload the weather data of DHT11 temperature and humidity sensor on cloud ( ThingSpeak.com) using wifi modem NodeMCU.

Detailed Tutorial

1. Introduction:

This tutorial explains how to log weather data on cloud. ThingSpeak.com is to be used as cloud service provider and sensor DHT11 will be used to measure temperature and humidity data.

This tutorial is for NodeMCU on Arduino IDE. Please note that the same tutorial can be performed on LUA as well.

1.2 Hardware required

S.No.ItemQuantity
1NodeMCU 1
2DHT11 Temperature and Humidity sensor 1
3Jumper Male to female 3

2. Building Circuit

Make connection as mentioned ahead.

S.NO.         NodeMCUDHT11
 1.VinVCC
 2.GNDGND
 3.D3Data Out

Circuit Layout:

3. Getting API Key

1. Go to https://thingspeak.com/ and create an account if you do not have one. Login to your account.

2. Create a new channel by clicking on the button.Enter basic details of the channel.Than Scroll down and save the channel.

3. Channel Id is the identity of your channel. Note down this. Than go to API keys copy and paste this key to a separate notepad file will need it later.

4. Programming:

Once the circuit part is done, NodeMCU is needed to be programmed. Here is the code to run this circuit on NodeMCU.

You may download this code (Arduino Sketch) from here.

// Robo India Tutorial 
// Simple code upload the tempeature and humidity data using thingspeak.com
// Hardware: NodeMCU,DHT11

#include <DHT.h>  // Including library for dht

#include <ESP8266WiFi.h>
 
String apiKey = "Your API of thingsspeak";     //  Enter your Write API key from ThingSpeak

const char *ssid =  "Your wifi Network name";     // replace with your wifi ssid and wpa2 key
const char *pass =  "Network password";
const char* server = "api.thingspeak.com";

#define DHTPIN 0          //pin where the dht11 is connected
 
DHT dht(DHTPIN, DHT11);

WiFiClient client;
 
void setup() 
{
       Serial.begin(115200);
       delay(10);
       dht.begin();
 
       Serial.println("Connecting to ");
       Serial.println(ssid);
 
 
       WiFi.begin(ssid, pass);
 
      while (WiFi.status() != WL_CONNECTED) 
     {
            delay(500);
            Serial.print(".");
     }
      Serial.println("");
      Serial.println("WiFi connected");
 
}
 
void loop() 
{
  
      float h = dht.readHumidity();
      float t = dht.readTemperature();
      
              if (isnan(h) || isnan(t)) 
                 {
                     Serial.println("Failed to read from DHT sensor!");
                      return;
                 }

                         if (client.connect(server,80))   //   "184.106.153.149" or api.thingspeak.com
                      {  
                            
                             String postStr = apiKey;
                             postStr +="&field1=";
                             postStr += String(t);
                             postStr +="&field2=";
                             postStr += String(h);
                             postStr += "\r\n\r\n";
 
                             client.print("POST /update HTTP/1.1\n");
                             client.print("Host: api.thingspeak.com\n");
                             client.print("Connection: close\n");
                             client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
                             client.print("Content-Type: application/x-www-form-urlencoded\n");
                             client.print("Content-Length: ");
                             client.print(postStr.length());
                             client.print("\n\n");
                             client.print(postStr);
 
                             Serial.print("Temperature: ");
                             Serial.print(t);
                             Serial.print(" degrees Celcius, Humidity: ");
                             Serial.print(h);
                             Serial.println("%. Send to Thingspeak.");
                        }
          client.stop();
 
          Serial.println("Waiting...");
  
  // thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
  delay(10000);
}

6. Output

Output of this project is seen on Thingspeak and serial monitor. Open your channel at Thingspeak and output will be shown as mentioned ahead.

Temperature:

Humidity:

On every successful data upload, success message is displayed on serial monitor.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

7 Comments

  1. B PAVAN KALYAN

    Arduino: 1.8.5 (Windows 8.1), Board: “NodeMCU 1.0 (ESP-12E Module), 80 MHz, 4M (1M SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 115200”

    DHT11_using_thingspeak:18: error: ‘DHT11’ was not declared in this scope

    DHT dht(DHTPIN, DHT11);

    ^

    C:\Users\USER\Desktop\DHT11_using_thingspeak\DHT11_using_thingspeak.ino: In function ‘void setup()’:

    DHT11_using_thingspeak:26: error: ‘class DHT’ has no member named ‘begin’

    dht.begin();

    ^

    C:\Users\USER\Desktop\DHT11_using_thingspeak\DHT11_using_thingspeak.ino: In function ‘void loop()’:

    DHT11_using_thingspeak:47: error: ‘class DHT’ has no member named ‘readHumidity’

    float h = dht.readHumidity();

    ^

    DHT11_using_thingspeak:48: error: ‘class DHT’ has no member named ‘readTemperature’

    float t = dht.readTemperature();

    ^

    exit status 1
    ‘DHT11’ was not declared in this scope

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

Leave a Reply