How to Send sensor data to the cloud using NodMcu8266 | IoT Tutorial
We are planning to use the NodMcu8266 to automatically log temperature and humidity measurements within the cloud, and show these measurements inside an online dashboard for this we use the DHT11 sensor for Measure temperature and humidity Data in our Environment. We will complete this project by dividing it into several parts.
Part-1:
Required Component :
4.DHT11
5.Breadboard Power Supply Module
Software Required:
The latest version of the Arduino IDE, which you can get from:
http://www.arduino.cc/en/Main/Software
Follow this link to How to Complet Software Setup Installing the Arduino IDE for the ESP8266 | IoT Tutorial
This book will help you to gain more knowledge of the Internet of Things with ESP8266
Part-2:
After Complet your hardware Collection and Software installation now we have to need waring with sensor and esp8266
Circuit Diagram sensor Data cloud NodMcu8266:
shows how to connect a DHT 11 sensor to NodMcu8266.
Part-3
Now we went to test sensor work properly or not so we have to need a test for sensor
Testing the sensor:
Now we will simply print the value of the temperature inside the Serial monitor of the Arduino IDE. If it has not been done yet
,
install the DHT sensor library using the Arduino IDE library manager.
This is the complete code for this part:
// Libraries
#include "DHT.h"
// Pin
#define DHTPIN D5
// Use DHT11 sensor
#define DHTTYPE DHT11
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);
void setup() {
// Start Serial
Serial.begin(115200);
// Init DHT
dht.begin();
}
void loop() {
// Reading temperature and humidity
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Display data
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
// Wait a few seconds between measurements.
delay(2000);
}
if you see like this interface then test successfully done
Code Overview Part-3 Code:
It starts by including the required libraries:
#include "ESP8266WiFi.h"
#include "DHT.h"
To install those libraries, simply look for them inside the Arduino IDE library manager. Next, we need to set the pin that the DHT sensor is connected to:
#define DHTPIN D5
#define DHTTYPE DHT11
After that, we declare an instance of the DHT sensor:
DHT dht(DHTPIN, DHTTYPE, 15);
We also define two variables that will hold the measurements of the sensor
float temperature;
float humidity;
In the setup() function of the sketch, we initialize the sensor:
dht.begin();
in the loop() function, we make the measurements from the sensor:
humidity = dht.readHumidity();
temperature = dht.readTemperature();
Finally Display data on your serial monitor :
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
Part-4
We are now going to see how to Send the temperature and humidity measurements in the cloud. We will use the Dweet.io cloud service here, which is very convenient for sending data online:
Sensor data send to Dweet.io:
Code:
// Libraries
#include <ESP8266WiFi.h>
#include "DHT.h"
// WiFi parameters
const char* ssid = "Mechatronics";
const char* password = "Pa$$word";
// Pin
#define DHTPIN D5
// Use DHT11 sensor
#define DHTTYPE DHT11
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);
// Host
const char* host = "dweet.io";
void setup() {
// Start Serial
Serial.begin(115200);
delay(10);
// Init DHT
dht.begin();
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// Reading temperature and humidity
int h = dht.readHumidity();
// Read temperature as Celsius
int t = dht.readTemperature();
// This will send the request to the server
client.print(String("GET /dweet/for/sensorvalue?temperature=") + String(t) + "&humidity=" + String(h) + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
// Repeat every 10 seconds
delay(10000);
}
Code overview part-4:
Inside this loop, we connect to the Dweet.io server with:
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
Then, we read the data from the sensor with:
int h = dht.readHumidity();
int t = dht.readTemperature();
After that, we send data to the Dweet.io server with:
client.print(String("GET /dweet/for/sensorvalue?temperature=") +
String(t) + "&humidity=" + String(h) + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
We also print any data received on the serial port with:
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Part-5
Displaying data using Freeboard.io
Now, we would like to really show the recorded data interior a dashboard that can be accessed from anyplace within the world. For that, we are going to utilize a service that I love to utilize along side Dweet.io: Freeboard.io.
Let's get started with using Freeboard.io:
1. First, create an account there by going to:
https://www.freeboard.io/
2. Then, create a new dashboard, and inside this dashboard, create a new data source. Link this data source to your Dweet.io thing that you defined in the NodeMcu8266 code:
3. Now create a new Gauge widget that we will use to display the temperature. Give it a name, and link it to the temperature field of our data source:
This is the final result:
You can then do the same with the humidity measurements and also display them on this dashboard:
There are many ways to improve this extend. You'll essentially include more sensors to the project and display these measurements as well inside the Freeboard.io dashboard.
No comments