Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: IoT Lab. No Comments on DHT11 Sensor with NodeMCU and ThingSpeak
0
(0)

In this article we will learn how to implement DHT11 Sensor with NodeMCU and ThingSpeak. If your are new to Internet of Things (IoT), learn about IoT by visiting our Internet of Things tutorial for beginners.

 

Aim of Experiment

To upload environmental sensor data to cloud server (ThingSpeak)


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

Components Required

  • NodeMCU – 1
  • DHT11 Sensor – 1
  • Breadboard – 1

 

Connections Diagram (Schematic)

DHT11 Sensor with NodeMCU and ThingSpeak

 

Code (C++/Arduino IDE)

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

String apiKey = "APIKEY";
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* server = "api.thingspeak.com";

#define DHTTYPE DHT11

const int DHT_pin = D1;
DHT dht(DHT_pin, DHTTYPE);

WiFiClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.print("Connecting to WiFi...with SSID: ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  Serial.println("Connecting...");

  while(WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WiFi connection successful!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:
  //Read the temperature
  float t = dht.readTemperature();
  delay(10); //necessary for reading correct values
  //Read the humidity
  float h = dht.readHumidity();

  if(client.connect(server, 80)){
    
    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);
    delay(1000); //necessary for posting to ThingSpeak

    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" centi, Humidity ");
    Serial.print(h);
    Serial.println("%");
    Serial.println("Data sent to ThingSpeak");
  }
  client.stop();

  Serial.println("Waiting for 15sec...");
  //ThingSpeak needs min. 15sec delay between each data post
  delay(15000);
}

 

Video Explanation

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *