How to make an IoT based weather monitoring system using Nodemcu and Thingspeak

How to make an IoT based weather monitoring system using Nodemcu and Thingspeak

How to make an IoT based weather monitoring system using Nodemcu and Thingspeak

Hello, welcome back. In this tutorial, we will learn how to make an IoT based weather monitoring system using Nodemcu and Thingspeak. This project is mainly based on IoT technology. Also, if you don’t know about Thingspeak, click this link. Through this system, we can monitor factors such as temperature, humidity, pressure and rainfall. The DHT11, BMP180 and Rain sensors are used for that. Also, we can use this system mainly for farms and greenhouses. If you want to do this project using the Blynk app, click this link.

The process of this system

When this system is powered on, the Nodemcu board connects to the algorithm development system called MATLAB through the Thingspeak cloud. Then, values are obtained from the sensors. Also, these values are sent to the thingspeak app using the internet. Then, we can see the values as a visualization on the screen.

So, let’s do this project step by step. The required components are given below.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect these components. To do this, use the circuit diagram below.

How to make an IoT based weather monitoring system using Nodemcu and Thingspeak

Step 3

Thirdly, we set up the Thingspeak app. To do this, follow the steps below.

  • First, go to the Thingspeak website and create a new account using your email address. Then, click the “New channel” button.
  • Next, enter your project name as you like. Then, activate the four fields and name them Temperature, Humidity, Pressure, and Rainfall respectively. After, click the “Save channel” button.
  • So, now we can see the interface of this project.
How to make an IoT based weather monitoring system using Nodemcu and Thingspeak

Step 4

Now, let’s create the program for this project. It is as follows.

/*Weather monitoring system with Thingspeak.
   Created by the SriTu Hobby team.
   http://srituhobby.com
*/

#include <SFE_BMP180.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include "DHT.h"

DHT dht(D3, DHT11);
SFE_BMP180 bmp;
double T, P;
char status;
WiFiClient client;

String apiKey = "";
const char *ssid =  "";
const char *pass =  "";
const char* server = "api.thingspeak.com";


void setup() {
  Serial.begin(115200);
  delay(10);
  bmp.begin();
  Wire.begin();
  dht.begin();
  WiFi.begin(ssid, pass);


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

void loop() {
  //BMP180 sensor
  status =  bmp.startTemperature();
  if (status != 0) {
    delay(status);
    status = bmp.getTemperature(T);

    status = bmp.startPressure(3);// 0 to 3
    if (status != 0) {
      delay(status);
      status = bmp.getPressure(P, T);
      if (status != 0) {

      }
    }
  }

  //DHT11 sensor
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  //Rain sensor
  int r = analogRead(A0);
  r = map(r, 0, 1024, 0, 100);


  if (client.connect(server, 80)) {
    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(t);
    postStr += "&field2=";
    postStr += String(h);
    postStr += "&field3=";
    postStr += String(P, 2);
    postStr += "&field4=";
    postStr += String(r);
    postStr += "\r\n\r\n\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\n\n");
    client.print(postStr);

    Serial.print("Temperature: ");
    Serial.println(t);
    Serial.print("Humidity: ");
    Serial.println(h);
    Serial.print("absolute pressure: ");
    Serial.print(P, 2);
    Serial.println("mb");
    Serial.print("Rain");
    Serial.println(r);

  }
  client.stop();
  delay(1000);
}

Code explanation

Firstly, library files are included.

#include <SFE_BMP180.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include "DHT.h"

Secondly, objects and variables are created for these libraries.

DHT dht(D3, DHT11);
SFE_BMP180 bmp;
double T, P;
char status;
WiFiClient client;

Thirdly, let’s include the apiKey and WI-FI connection details.

String apiKey = "40BXKV4BEXQGXX5B";
const char *ssid =  "SriTu Hobby";
const char *pass =  "123456";
const char* server = "api.thingspeak.com";

In the setup function,

void setup() {
//The serial monitor and libraries are beginning
  Serial.begin(115200);
  delay(10);
  bmp.begin();
  Wire.begin();
  dht.begin();
  WiFi.begin(ssid, pass);

//This code check WIFI status
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
}

The loop function includes all the main functions. These are described below.

void loop() {
  //BMP180 sensor
  status =  bmp.startTemperature();
  if (status != 0) {
    delay(status);
    status = bmp.getTemperature(T);
//Calculates pressure values
    status = bmp.startPressure(3);// 0 to 3
    if (status != 0) {
      delay(status);
      status = bmp.getPressure(P, T);
      if (status != 0) {

      }
    }
  }

  //DHT11 sensor
//Temperature and humidity values are obtained through the sensor
  float h = dht.readHumidity();
  float t = dht.readTemperature();
//The DHT11 sensor is checked
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  //Rain sensor
//Gets the rainfall values through the sensor
  int r = analogRead(A0);
//Converts these values from 0 to 100
  r = map(r, 0, 1024, 0, 100);

//This code sends sensor values to the Thingspeak app
  if (client.connect(server, 80)) {
    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(t);
    postStr += "&field2=";
    postStr += String(h);
    postStr += "&field3=";
    postStr += String(P, 2);
    postStr += "&field4=";
    postStr += String(r);
    postStr += "\r\n\r\n\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\n\n");
    client.print(postStr);

//These values are printed on the serial monitor
    Serial.print("Temperature: ");
    Serial.println(t);
    Serial.print("Humidity: ");
    Serial.println(h);
    Serial.print("absolute pressure: ");
    Serial.print(P, 2);
    Serial.println("mb");
    Serial.print("Rain");
    Serial.println(r);

  }
  client.stop();
  delay(1000);
}

Step 5

Next, select board and port. After, upload this code to the Nodemcu board.

Step 6

Now, go back to the Thingspeak app and click the private view tab. Ok, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.

How to make an IoT based weather monitoring system using Nodemcu and Thingspeak

How to make an IoT based weather monitoring system using Nodemcu and Thingspeak

Similar Posts

2 Comments

Leave a Reply

Your email address will not be published.