0% found this document useful (0 votes)
18 views23 pages

Lecture3 1 Hands-On-Activity-1-Sensor To ThingSpeak Cloud

The document outlines an IoT-based system course (UEC715) focusing on sensor to cloud integration using the DHT11 sensor and ThingSpeak platform. It explains how to set up an API for data publishing, hardware connections, and code implementation for temperature and humidity monitoring. The course also includes practical steps for creating a ThingSpeak account, configuring the channel, and visualizing sensor data in real-time.

Uploaded by

tsingh6be21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views23 pages

Lecture3 1 Hands-On-Activity-1-Sensor To ThingSpeak Cloud

The document outlines an IoT-based system course (UEC715) focusing on sensor to cloud integration using the DHT11 sensor and ThingSpeak platform. It explains how to set up an API for data publishing, hardware connections, and code implementation for temperature and humidity monitoring. The course also includes practical steps for creating a ThingSpeak account, configuring the channel, and visualizing sensor data in real-time.

Uploaded by

tsingh6be21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

IoT based System

UEC715

9/10/2024 IoT based System (UEC715) 1


Subject : IoT-based Systems
Code : UEC715

Unit : #1 [Introduction to IoT]


Lecture : #4 [Sensor to Cloud Integration]

Topics covered:
 DHT11 Sensor
 ThingSpeak

Dr. Amit Mishra

9/10/2024 IoT based System (UEC715) 2


9/10/2024 IoT based System (UEC715) 3
Application Programming Interface (API)

API stands for Application Programming


Interface and it is the link between our software
application and the website. If we want data from
website to use in our application then we need the
API.

Example: Lets assume that the website is a restaurant and our program/application is
customer and the food is data. So to get the food, customer needs to give the dish name to the
waiter from the menu card, along with that the waiter also notes the table number and gives that
information to the kitchen staff.

In similar way our program also requests data from website by posting the information like API
key, topic parameter in the form of Uniform Resource Locator (URL). This URL is API. If the
API key matches the API key given to us by the website then the response is given by the website
and the program gets the data.
9/10/2024 IoT based System (UEC715) 4
ThingSpeak
ThingSpeak is a IoT cloud platform service that allows us to visualize live data streams in the
cloud. We can send data to ThingSpeak from our devices and create instant visualization of live
data.

NOTE: In our project we are just posting the temperature data to Thingspeak server and not
requesting any data. So first lets see how to get API from Thingspeak , publish data using API and
then make the hardware connections.
9/10/2024 IoT based System (UEC715) 5
Components required

• ESP-32 dev board


• DHT-11 temperature and humidity sensor
• Jumper wire (M to M) – 3pcs
• USB micro cable

9/10/2024 IoT based System (UEC715) 6


Getting API from Thingspeak and testing it
First we need to create an account on Thingspeak. Follow the below given steps-
Open the Thingspeak website then click on “sign in” in top right corner.

9/10/2024 IoT based System (UEC715) 7


2. Click on “create one”.

9/10/2024 IoT based System (UEC715) 8


3. Fill all the credentials and click on “continue”. Then you will get a verification link on your
registered email ID . Click on that link. Set your password and your account will be created. Then
you just login to Thingspeak.

9/10/2024 IoT based System (UEC715) 9


4. After you logged in you will see this page. Click on “new channel”

9/10/2024 IoT based System (UEC715) 10


5. Give your channel a name, description, and Field1 name.
• As we are using only one field for temperature.

9/10/2024 IoT based System (UEC715) 11


6. New channel is now created. You will also see a blank chart displayed in private view. Click on
API keys.

9/10/2024 IoT based System (UEC715) 12


7. Under API requests copy this URL. Through this URL we publish data to the channel field

9/10/2024 IoT based System (UEC715) 13


8. Paste this URL in the address bar and assign the field parameter, any number or you can keep
it zero. Then hit “enter”

9/10/2024 IoT based System (UEC715) 14


9. Now open your channel , go to private view and view your chart. The field value will be updated
to the chart and you will see a red horizontal line corresponding to your value.

• If the value is not updated, try reloading the page.


• This indicates that the API is working and can be used in our program. Now that we have tested
the API, lets make the hardware connections for the project.

9/10/2024 IoT based System (UEC715) 15


9/10/2024 IoT based System (UEC715) 16
Circuit diagram
Make the connections as per below given circuit
diagram.

The DHT11 temperature sensor has 3 pins, ground,


vcc and data. It sends calibrated temperature output
in degree Celsius on the data line.
The data pin is connected on GPIO 5 of ESP-
32. It’s temperature range is from 0 to 50 degree
Celsius. It’s operating voltage is 3V to 5V. So we
can directly give it 3.3V from ESP-32 and get the
room temperature readings.

9/10/2024 IoT based System (UEC715) 17


Once the connections are done. Connect the ESP-32 to COM port through USB cable and open the
Arduino IDE.

Install the DHT-11 library


Open library manager and search for “DHT11” and install the “DHT sensor library by Adafruit”

9/10/2024 IoT based System (UEC715) 18


The DHT-11 sensor will now work with our program as it’s library is installed.
Go to tools and select the board “ESP32 Dev Module” and select the proper com port
according to the device manager.

Now we are done with all setup. Before uploading the code, modify it according to your
WiFi ssid and password. Also assign your “write API key” to the apiKey parameter
which you will get from your Thingspeak channel under API keys.

Upload the given code. Open the serial monitor to see the status. Set baud rate to
"115200". As soon as it will connect to WiFi, It will start publishing room temperature
after interval of 10 seconds. You can change this interval by changing the delay in the
“void loop()”.

9/10/2024 IoT based System (UEC715) 19


Code:
// Library declaration
// Variables to store sensor readings
#include <WiFi.h>
#include "ThingSpeak.h" float temperature;
#include <DHT.h> float temperaturef;
float humidity;
// Wi-Fi Credentials
#define DHTPIN 15
const char* ssid = "TU";
const char* password = " tu@inet1"; DHT dht(DHTPIN, DHT11);

WiFiClient client; // Testing Sensor reading

// ThingSpeak Credentials
void initDHT(){
unsigned long Channel_ID = 2637682; if (isnan(temperature) || isnan(humidity)) {
const char * API_Key = "L4PMUSMH1ZJQ1D00 "; Serial.println("Failed to read from DHT sensor!");
while (1);
}
// Delay }

unsigned long last_time = 0;


unsigned long Delay = 1000;

SSID: Service Set Identifier


9/10/2024 IoT based System (UEC715) 20
Continued…
// Setup serial monitor mode and Thingspeak
// Obtaining a new sensor reading for all fields
void setup() {
temperature = dht.readTemperature();
Serial.begin(115200); Serial.print("Temperature (ºC): ");
dht.begin();
Serial.println(temperature);
WiFi.mode(WIFI_STA); humidity = dht.readHumidity();
ThingSpeak.begin(client);
Serial.print("Humidity (%): ");
} Serial.println(humidity);

void loop() { ThingSpeak.setField(1, temperature);


ThingSpeak.setField(2,humidity );
if ((millis() - last_time) > Delay) {
int Data = ThingSpeak.writeFields(Channel_ID, API_Key);

// Connect or reconnect to WiFi if(Data == 200){


Serial.println("Channel updated successfully!");
if(WiFi.status() != WL_CONNECTED){ }
Serial.print("Connecting..."); else{
while(WiFi.status() != WL_CONNECTED){ Serial.println("Problem updating channel. HTTP error code " +
WiFi.begin(ssid, password); String(Data));
delay(5000); }
} last_time = millis();
Serial.println("\nConnected."); }
}
}
9/10/2024 IoT based System (UEC715) 21
Sensor data visualization on
Thingspeak

9/10/2024 IoT based System (UEC715) 22


9/10/2024 IoT based System (UEC715) 23

You might also like