DEPARTMENT OF ELECTRONICS & COMMUNICATION
ENGINEERING
LAB MANUAL
IoT Lab (BECL657C)
Prof. Darshan A Bhyratae
Prof. Manju D
Prof. Lakshmi Manasa B
2024-2025
Approved By:
Dr. Jijesh J J
Professor & HoD,
Department of E&CE,
SVCE, Bengaluru
i) Course Objectives:
This course will enable students to
To impart necessary and practical knowledge of components of the Internet of Things
To develop skills required to build real-life IoT-based projects.
ii) Course Outcomes:
Explain the Internet of Things and its hardware and software components.
Interface I/O devices, sensors & communication modules.
Remotely monitor data and control devices.
Develop real-life IoT-based projects.
ABET Student Outcomes
Student Outcomes are outcomes (1) through (7), plus any additional outcomes that may be articulated
by the program.
SO1. An ability to identify, formulate, and solve complex engineering problems by applying principles of
engineering, science, and mathematics
SO2. An ability to apply engineering design to produce solutions that meet specified needs with
consideration of public health, safety, and welfare, as well as global, cultural, social, environmental,
and economic factors
SO3. An ability to communicate effectively with a range of audiences
SO4. An ability to recognize ethical and professional responsibilities in engineering situations and make
informed judgments, which must consider the impact of engineering solutions in global, economic,
environmental, and societal contexts
SO5. An ability to function effectively on a team whose members together provide leadership, create a
collaborative and inclusive environment, establish goals, plan tasks, and meet objectives
SO6. An ability to develop and conduct appropriate experimentation, analyze and interpret data, and use
engineering judgment to draw conclusions
SO7. An ability to acquire and apply new knowledge as needed, using appropriate learning strategies.
Reference:https://www.abet.org/accreditation/accreditation-criteria/criteria-for-accrediting-engineering-programs-
2020-2021/
NBA Programme Outcome
Engineering Graduates will be able to:
PO1: Engineering Knowledge: Apply knowledge of mathematics, natural science, computing,
engineering fundamentals and an engineering specialization as specified in WK1 to WK4 respectively
to develop to the solution of complex engineering problems.
PO2: Problem Analysis: Identify, formulate, review research literature and analyze complex
engineering problems reaching substantiated conclusions with consideration for sustainable
development. (WK1 to WK4)
PO3: Design/Development of Solutions: Design creative solutions for complex engineering problems
and design/develop systems/components/processes to meet identified needs with consideration for the
public health and safety, whole-life cost, net zero carbon, culture, society and environment as
required. (WK5)
PO4: Conduct Investigations of Complex Problems: Conduct investigations of complex engineering
problems using research-based knowledge including design of experiments, modelling, analysis &
interpretation of data to provide valid conclusions. (WK8).
PO5: Engineering Tool Usage: Create, select and apply appropriate techniques, resources and modern
engineering & IT tools, including prediction and modelling recognizing their limitations to solve
complex engineering problems. (WK2 and WK6)
PO6: The Engineer and The World: Analyze and evaluate societal and environmental aspects while
solving complex engineering problems for its impact on sustainability with reference to economy,
health, safety, legal framework, culture and environment. (WK1, WK5, and WK7).
PO7: Ethics: Apply ethical principles and commit to professional ethics, human values, diversity and
inclusion; adhere to national & international laws. (WK9)
PO8: Individual and Collaborative Team work: Function effectively as an individual, and as a member
or leader in diverse/multi-disciplinary teams.
PO9: Communication: Communicate effectively and inclusively within the engineering community
and society at large, such as being able to comprehend and write effective reports and design
documentation, make effective presentations considering cultural, language, and learning differences
PO10: Project Management and Finance: Apply knowledge and understanding of engineering
management principles and economic decision-making and apply these to one’s own work, as a
member and leader in a team, and to manage projects and in multidisciplinary environments.
PO11: Life-Long Learning: Recognize the need for, and have the preparation and ability for i)
independent and life-long learning ii) adaptability to new and emerging technologies and iii) critical
thinking in the broadest context of technological change. (WK8)
Program Specific Outcomes (PSOs)
PSO1: Apply knowledge and skills to design and develop innovative embedded systems and IoT
solutions, addressing real-world challenges and practical applications.
PSO2: Integrate and implement hardware and software techniques to analyze, optimize, and create
solutions for signal processing, advanced communication, and VLSI system development.
Knowledge and Attitude Profile (WK)
WK1: A systematic, theory-based understanding of the natural sciences applicable to the discipline and
awareness of relevant social sciences.
WK2: Conceptually-based mathematics, numerical analysis, data analysis, statistics and formal aspects
of computer and information science to support detailed analysis and modelling applicable to the
discipline.
WK3: A systematic, theory-based formulation of engineering fundamentals required in the engineering
discipline.
WK4: Engineering specialist knowledge that provides theoretical frameworks and bodies of knowledge
for the accepted practice areas in the engineering discipline; much is at theforefront of the discipline.
WK5: Knowledge, including efficient resource use, environmental impacts, whole-life cost, re-use of
resources, net zero carbon, and similar concepts, that supports engineering design and operations in a
practice area.
WK6: Knowledge of engineering practice (technology) in the practice areas in the engineering
discipline.
WK7: Knowledge of the role of engineering in society and identified issues in engineering practice in
the discipline, such as the professional responsibility of an engineer to public safety and sustainable
development.
WK8: Engagement with selected knowledge in the current research literature of the discipline,
awareness of the power of critical thinking and creative approaches to evaluate emerging issues.
WK9: Ethics, inclusive behavior and conduct. Knowledge of professional ethics, responsibilities, and
norms of engineering practice. Awareness of the need for diversity by reason of ethnicity, gender, age,
physical ability etc. with mutual understanding andrespect, and of inclusive attitudes
Reference:https://www.nbaind.org/files/2-SAR-UG-EG-T2-14-1-2025_Format_20250115122255.pdf
CO-PO & PSO Mapping
CO \ CKL
PO
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PSO1 PSO2
WKL 1-4 1-4 5 8 2,6 1,5,7 9 - - - 8 3 3
CO1
-2 -4 -5 -4 -5 -2 -2 -2 1 -2 -2 -2 -2
CO2
2 0 2 1 -1 -1 2 2 1 2 2 2 2
CO3
2 0 2 1 -1 -1 2 2 1 2 2 2 2
CO4
2 0 2 1 -1 -1 2 2 1 2 2 2 2
CO5
2 0 2 1 -1 -1 2 2 1 2 2 2 2
3 Strong Correlation
2 Moderate Correlation
1 Weak / No Correlation
CMap:
Course syllabus
Sl.No. Experiments
i) To interface LED/Buzzer with Arduino /Raspberry Pi and write a program to ‘turn ON’
1. LED for 1 sec after every 2 seconds.
ii) To interface the Push button/Digital sensor (IR/LDR) with Arduino /Raspberry Pi and
write a program to ‘turn ON’ LED when a push button is pressed or at sensor detection.
i) To interface the DHT11 sensor with Arduino /Raspberry Pi and write a
2. program to print temperature and humidity readings.
ii) To interface OLED with Arduino /Raspberry Pi and write a program to
print its temperature and humidity readings.
To interface the motor using a relay with Arduino /Raspberry Pi and write a program to ‘turn
3. ON’ the motor when a push button is pressed.
i) Write an Arduino/Raspberry Pi program to interface the Soil Moisture
4. Sensor.
ii) Write an Arduino/Raspberry Pi program to interface the LDR/Photo
Sensor.
5. Write a program to interface an Ultrasonic Sensor with Arduino /Raspberry
Pi.
Write a program on Arduino/Raspberry Pi to upload temperature and
6. humidity data to thingspeak cloud.
Write a program on Arduino/Raspberry Pi to retrieve temperature and
7. humidity data from thingspeak cloud.
8. Write a program to interface LED using Telegram App.
9. Write a program on Arduino/Raspberry Pi to publish temperature data to the
MQTT broker.
Write a program to create a UDP server on Arduino/Raspberry Pi and
10. respond with humidity
data to the UDP client when requested.
Write a program to create a TCP server on Arduino /Raspberry Pi and
11. respond with humidity
data to the TCP client when requested.
Write a program on Arduino / Raspberry Pi to subscribe to the MQTT broker
12. for temperature
data and print it.
LIST OF EXTRA PROGRAMS APART FROM THE CURRICULUM
LIST OF EXTRA PROGRAMS APART FROM THE CURRICULUM
Sl.No Programs Attainment of
POs
1. Create a mobile app to control an actuator. PO3, PO5, PO9,
PO11, PO12
PSO1
2. PO3, PO5, PO9,
Access the data pushed from sensor to cloud and apply
PO11, PO12
any data analytics or visualization services
PSO1
3. PO3, PO5, PO9,
Build simple IoT based project PO11, PO12
PSO1
Programme Coordinator
EXPERIMENT – 1
i) To interface LED/Buzzer with Arduino/Raspberry Pi and write a program to turn
ON LED for 1 sec after every 2 seconds.
AIM: Write a program to turn ON LED/ Buzzer for 1 sec after every seconds with
Arduino/ Raspberry Pi.
COMPONENTS REQUIRED:
Arduino UNO/ Raspberry Pi.
LED/ Buzzer.
Resistor (220Ω)
Connecting cable or USB cable.
Breadboard.
Jumper wires.
(a) LED (b) Buzzer
(c) LED interfacing with Arduino
IoT (Internet of Things) Lab- BECL657C
(d)LED interfacing with Raspberry Pi
CODING:
Int LED =13;
void setup()
{
pinMode(LED,OUTPUT);
}
void loop()
{
digitalWrite(LED,HIGH);
delay(1000);
digitalWrite(LED,LOW);
delay(1000);
}
Department of ECE SVCE Bengaluru Page 10
IoT (Internet of Things) Lab- BECL657C
Python code
import time
# blinking function
def blink(pin):
GPIO.output(pin,GPIO.HIGH)
time.sleep(1)
GPIO.output(pin,GPIO.LOW)
time.sleep(2)
return
# to use Raspberry Pi board pin numbers
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# set up GPIO output channel
GPIO.setup(17,GPIO.OUT)
# blink GPIO17 10 times
for i in range(0,10):
blink(17)
GPIO.cleanup()
RESULT: LED/Buzzer is successfully controlled by Arduino UNO/ Raspberry Pi Board.
Department of ECE SVCE Bengaluru Page 11
IoT (Internet of Things) Lab- BECL657C
II.To interface Push button/Digital sensor (IR/LDR) with Arduino UNO and write a
program to turn ON LED when push button is pressed or at sensor detection.
CASE 1:
AIM: Write a program to turn ON LED when push button is pressed using Arduino
UNO.
COMPONENTS REQUIRED:
1. Arduino UNO.
2. Push button.
3. LED.
4. Resistor (10KΩ)
5. Connecting cable or USB cable.
6. Breadboard.
7. Jumper wires.
CONNECTION DIAGRAM:
a) Push button interfacing with Arduino
Department of ECE SVCE Bengaluru Page 12
IoT (Internet of Things) Lab- BECL657C
CODING:
Embedded C
#define LED 8
#define pushbutton 7
void setup()
{
pinMode(LED,OUTPUT);
pinMode(pushbutton,INPUT);// put your setup code here, to run once:
}
void loop()
{
if (digitalRead(pushbutton)==HIGH)
{
digitalWrite(LED,HIGH);
}
else
{
digitalWrite(LED,LOW);
}
}
RESULT: Controlling LED by pressing push button using Arduino UNO has
successfully executed.
CASE -2
AIM: Write a program to turn ON LED at sensor (LDR) detection using Arduino UNO.
COMPONENTS REQUIRED:
1. Arduino UNO.
Department of ECE SVCE Bengaluru Page 13
IoT (Internet of Things) Lab- BECL657C
2. LDR.
3. LED.
4. Resistor (100KΩ)
5. Connecting cable or USB cable.
6. Breadboard.
7. Jumper wires.
CONNECTION DIAGRAM:
(b) LDR interfacing with Arduino
CODING:
Embedded C
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor int led=13;
void setup()
{
Department of ECE SVCE Bengaluru Page 14
IoT (Internet of Things) Lab- BECL657C
Serial.begin(9600); //sets serial port for communication pinMode(13,
OUTPUT);
}
void loop()
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
if (sensorValue<400)
digitalWrite(13, HIGH);
else
digitalWrite(13, LOW);
delay(1000);
}
RESULT: Controlling LED by interfacing LDR with Arduino UNO has successfully executed.
Department of ECE SVCE Bengaluru Page 15
IoT (Internet of Things) Lab- BECL657C
CASE -3
AIM: Write a program to turn ON LED at sensor (IR) detection using Arduino UNO.
COMPONENTS REQUIRED:
1. Arduino UNO.
2. IR.
3. LED.
4. Resistor (220Ω)
5. Connecting cable or USB cable.
6. Breadboard.
7. Jumper wires.
CONNECTION DIAGRAM:
c) IR sensor
IR sensor interfacing with Arduino
Department of ECE SVCE Bengaluru Page 16
IoT (Internet of Things) Lab- BECL657C
CODING:
Embedded C
int IR=9;
int IRvalue;
int LED=13;
void setup()
{
Serial.begin(9600); pinMode(IR, INPUT);
pinMode(LED, OUTPUT); // LED Pin Output
}
void loop()
{
IRvalue=digitalRead(IR); if (IRvalue==1)
{
digitalWrite(LED, HIGH); // LED High Serial.println("Object detected");
}
else
{
digitalWrite(LED, LOW);
Serial.println("Object is not detected");
}
delay(1000);
}
RESULT: Object detection by interfacing IR with Arduino UNO has successfully executed
Department of ECE SVCE Bengaluru Page 17
IoT (Internet of Things) Lab- BECL657C
EXPERIMENT – 2
i. To interface DHT11 sensor with Arduino/Raspberry Pi and write a
program to print temperature and humidity readings.
AIM: Write a program to interface DHT11 with Arduino/Raspberry Pi to print temperature
and humidity readings.
COMPONENTS REQUIRED:
1. Arduino UNO/ Raspberry Pi.
2. DHT11.
3. Connecting cable or USB cable.
4. Jumper wires.
CONNECTION DIAGRAM:
a) DHT11 sensor
b) DHT11 interfacing with Arduino
Department of ECE SVCE Bengaluru Page 18
IoT (Internet of Things) Lab- BECL657C
a) DHT11 interfacing with Raspberry pi
Step 1: Install the library for DHT in Arduino IDE.
Open Arduino IDE and navigate to Sketch > Include Library > Manage
Libraries.
Search for “DHTlib” and install the “DHTlib” library in the Arduino IDE.
CODING:
Embedded C
#define dht_dpin 2
#include<dht.h> dht DHT;
void setup()
{
Serial.begin(9600);
Serial.println("Humidity and temperature\n\n");
}
void loop()
{
Department of ECE SVCE Bengaluru Page 19
IoT (Internet of Things) Lab- BECL657C
//This is the "heart" of the program.
DHT.read11(dht_dpin);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("%
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(1000);
}
You need to install PIP before you can install the Adafruit_DHT module: sudo pip3
install Adafruit_DHT
Python code
import time
#import sys
import Adafruit_DHT
while(1):
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
# Sensor ,
GPIO Pin print(" ")
print('Temp:{0:0.1f} C, Humidity:{1:0.1f} %'.format(temperature, humidity))
print(" ")
Department of ECE SVCE Bengaluru Page 20
IoT (Internet of Things) Lab- BECL657C
RESULT: Interfacing of DHT11 with Arduino UNO to print temperature and humidity readings
has successfully executed.
II.To interface OLED/I2C LCD with Arduino/Raspberry Pi and write a program to print
temperature and humidity readings on it.
Department of ECE SVCE Bengaluru Page 21
IoT (Internet of Things) Lab- BECL657C
AIM: Write a program to interface OLED/ I2C LCD with Arduino to print temperature and
humidity readings.
COMPONENTS REQUIRED:
1. Arduino UNO/ Raspberry Pi.
2. DHT11.
3. (0.96 Inch) I2C/IIC 4pin OLED Display Module / I2C LCD .
4. Connecting cable or USB cable.
5. Jumper wires.
CONNECTION DIAGRAM:
c) I2C OLED interfacing with Arduino UNO
Steps to interface I2C OLED with Arduino:
Step 1: Install the library for OLED display in Arduino IDE.
Open Arduino IDE and navigate to Sketch > Include Library > Manage
Libraries.
Search for “SSD1306” and install the “SSD1306” library from Adafruit in the
ArduinoIDE.
Department of ECE SVCE Bengaluru Page 22
IoT (Internet of Things) Lab- BECL657C
Step 2: Import “Adafruit_GFX.h” & “Adafruit_SSD1306.h” header files in the code.Define header file
in the code #include <Adafruit_GFX.h>,
#include <Adafruit_SSD1306.h>
Step 3: Connect I2C OLED display device to Arduino as per the circuit diagram.
Initially run I2C scanner which will find the address of I2C module that is connected to
Arduino. In this case address of I2C OLED module is 0x3C.
Steps to interface I2C LCD with Arduino:
Step 1: Install the library for LCD display in Arduino IDE.
Open Arduino IDE and navigate to Sketch > Include Library
> Manage Libraries.
Search for “LiquidCrystal I2C” and install the “LiquidCrystal I2C” library
in the Arduino IDE.
Department of ECE SVCE Bengaluru Page 23
IoT (Internet of Things) Lab- BECL657C
Step 2: Import “LiquidCrystal_I2C.h” header file in the code.
Define header file in the code #include <LiquidCrystal_I2C.h> .
Step 3: Connect I2C LCD display device to Arduino as per the circuit diagram.
Initially run I2C scanner which will find the address of I2C module that is
connected to Arduino. In this case address of I2C LCD module is 0x27.
I2C LCD interfacing with Arduino UNO
Department of ECE SVCE Bengaluru Page 24
IoT (Internet of Things) Lab- BECL657C
CODING:
Embedded C
Displaying Temperature and Humidity in the OLED Display with Arduino
#include <Wire.h>
#include<Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define dht_dpin 7
#include<dht.h>
dht DHT;
#define SCREEN_WIDTH 128 // OLED display width, in pixels #define
SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// The (-1) parameter means that your OLED display doesn’t have a RESET pin.
// If your OLED display does have a RESET pin, it should be connected to a GPIO.
// In that case, you should pass the GPIO number as a parameter.
void setup()
{
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop()
{
delay(5000);
//clear display
display.clearDisplay();
// display temperature
DHT.read11(dht_dpin);
Department of ECE SVCE Bengaluru Page 25
IoT (Internet of Things) Lab- BECL657C
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Temp=");
display.print(DHT.temperature); //print the temperature
display.print(" Celsius");
display.display();
display.setTextSize(1);
display.setCursor(0, 35);
display.print("Humidity=");
display.print(DHT.humidity); //print the humidity
display.print("%");
display.display();
}
f) Displaying Temperature and Humidity values on I2C OLED using Arduino UNO
Displaying Temperature and Humidity in the I2C LCD with Arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C
lcd(0x27,16,2);
#define dht_dpin 7
#include<dht.h>
dht DHT;
Department of ECE SVCE Bengaluru Page 26
IoT (Internet of Things) Lab- BECL657C
void setup()
{
Serial.begin(9600);
lcd.begin();
}
void loop()
{
DHT.read11(dht_dpin);
lcd.setCursor(0, 0);//(Col,ROW) lcd.print("Temp=");
lcd.print(DHT.temperature); //print the temperature
lcd.print("Celsius");
lcd.setCursor(0, 1);//(Col,ROW)
lcd.print("Humidity=");
lcd.print(DHT.humidity);
//print the humidity lcd.print("%");
delay(2000);
}
g)Displaying Temperature and Humidity values on I2C LCD using Arduino UNO
RESULT: Interfacing of OLED/ I2C LCD and DHT11 with Arduino UNO to print temperature
and humidity readings has successfully executed.
Department of ECE SVCE Bengaluru Page 27
IoT (Internet of Things) Lab- BECL657C
EXPERIMENT – 3
To interface motor using relay with Arduino/Raspberry Pi and write a program to turn
ON motor when push button is pressed.
AIM: Write a program to turn ON motor when push button is pressed using relay with Arduino
UNO.
COMPONENTS REQUIRED:
1. Arduino UNO.
2. Relay.
3. Resistor (10kΩ).
4. Motor.
5. Connecting cable or USB cable.
6. Breadboard.
7. Jumper wires.
CONNECTION DIAGRAM:
Fig a Push button interfacing with Arduino
Department of ECE SVCE Bengaluru Page 28
IoT (Internet of Things) Lab- BECL657C
Fig b Interfacing motor with relay using Arduino
CODING:
Embedded C
#define RELAY_PIN 8 // Digital pin connected to the relay module
#define BUTTON_PIN 2 // Digital pin connected to the push button
void setup()
{
Serial.begin(9600);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop()
{
int buttonState = digitalRead(BUTTON_PIN);
if(buttonState == HIGH)
{
// Button is pressed, turn ON the motor via relay
digitalWrite(RELAY_PIN, HIGH);
delay(1000); // Delay for motor ON time (1 second in this case)
}
else
{
// Button is not pressed, turn OFF the motor via relay
digitalWrite(RELAY_PIN, LOW);
}
}
Department of ECE SVCE Bengaluru Page 29
IoT (Internet of Things) Lab- BECL657C
Fig c: Interfacing motor with relay and push button using Arduino
RESULT: Motor turn ON/OFF operation when push button is pressed using relay with Arduino
UNO has successfully executed.
Department of ECE SVCE Bengaluru Page 30
IoT (Internet of Things) Lab- BECL657C
EXPERIMENT – 6
Write a program on Arduino/Raspberry Pi to upload temperature and
humidity data to Thingspeak cloud.
AIM: Write a program on ESP32 to upload temperature and humidity value using DHT11 sensor to
Thingspeak cloud
COMPONENTS REQUIRED:
1. ESP32.
2. DHT11.
3. Connecting cable or USB cable.
4. Breadboard.
5. Jumper wires.
CONNECTION DIAGRAM:
a) Interfacing DHT11 with ESP32
Department of ECE SVCE Bengaluru Page 31
IoT (Internet of Things) Lab- BECL657C
CODING:
//Interfacing DHT11 to ESP32 to upload temperature and humidity data tothingspeak cloud
#include <DHT.h>
#include<WiFi.h>
#include<WiFiClient.h>
#include <ThingSpeak.h>
#include <Adafruit_Sensor.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "GMIT";
const char* password = "DAVANGERE";
WiFiClient client;
unsigned long myChannelNumber = 12345;
const char * myWriteAPIKey = "ABCDEFGH";
uint8_t temperature, humidity;
void setup()
{
Serial.begin(9600);
dht.begin();
delay(10);
// Connect to 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");
// Print the IP address
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop()
Department of ECE SVCE Bengaluru Page 32
IoT (Internet of Things) Lab- BECL657C
{
temperature = dht.readTemperature();
humidity = dht.readHumidity();
Serial.print("Temperature Value is :");
Serial.print(temperature);
Serial.println("C");
Serial.print("Humidity Value is :");
Serial.print(humidity);
Serial.println("%");
// Write to Things peak. There are up to 8 fields in a channel, allowing you to store up to 8
different
ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 2, humidity, myWriteAPIKey);
delay(3000); // ThingSpeak will only accept updates every 3 seconds.
}
RESULT:
SendingtemperatureandhumiditydatausingDHT11sensortoThingspeakcloudwithESP32 has
successfullyexecuted.
Department of ECE SVCE Bengaluru Page 33
IoT (Internet of Things) Lab- BECL657C
EXPERIMENT – 7
Write a program on Arduino/Raspberry Pi to retrieve temperature and humidity data
from Thingspeak cloud.
AIM: Write a program on ESP32 to retrieve temperature and humidity values using DHT11
sensor from Thingspeak cloud.
COMPONENTS REQUIRED:
1. ESP32.
2. DHT11.
3. Connecting cable or USB cable.
4. Breadboard.
5. Jumper wires.
CONNECTION DIAGRAM:
Department of ECE SVCE Bengaluru Page 34
IoT (Internet of Things) Lab- BECL657C
Fig a) Interfacing DHT11 with ESP32
CODING:
Embedded C
// Retrieving temperature and humidity data from Thingspeak cloud using ESP32 #include <WiFi.h>
#include <ThingSpeak.h>
WiFiClient client;
void setup()
{
WiFi.begin("GMIT","DAVANGERE");
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(300);
Serial.print("..");
}
Serial.println("Your IP is");
Serial.println((WiFi.localIP().toString()));
Serial.println("Connected");
}
voidloop()
{
Serial.print("Temperature=");
Serial.println((ThingSpeak.readIntField(12345,1,"ABCDEFGH")));
Serial.print("Humidity=");
Serial.println((ThingSpeak.readIntField(12345,2,"ABCDEFGH")));
delay(3000);
}
Serial.begin(9600);
ThingSpeak.begin(client);
WiFi.disconnect();
delay(3000);
Fig b) Available temperature and humidity data on ThingSpeak cloud
Department of ECE SVCE Bengaluru Page 35
IoT (Internet of Things) Lab- BECL657C
Serial.println("START");
Fig c) Displaying temperature and humidity data on serial monitor of Arduino IDE from
Thingspeak cloud
RESULT: Retrieving temperature and humidity data from Thingspeak cloud with ESP32
has successfully executed.
Department of ECE SVCE Bengaluru Page 36
IoT (Internet of Things) Lab- BECL657C
EXPERIMENT – 7
Write a program on Arduino/Raspberry Pi to publish temperature data to MQTT broker.
AIM: Write a program on ESP32 to publish temperature and humidity values using DHT11
sensor to MQTT broker.
COMPONENTS REQUIRED:
1. ESP32.
2. DHT11.
3. Connecting cable or USB cable.
4. Breadboard.
5. Jumper wires.
CONNECTION DIAGRAM:
Fig 1 Interfacing DHT11 with ESP32
Department of ECE SVCE Bengaluru Page 37
IoT (Internet of Things) Lab- BECL657C
CODING:
Embedded C
/****************************************************/
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "GMIT"
#define WLAN_PASS "Davangere"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "ABCDE"
#define AIO_KEY "A1B2C3D4E5F6G7"
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME,
AIO_KEY);
/****************************** Feeds ***************************************/
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish sensor_1 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/Temperature"); /// Temperature Value
Adafruit_MQTT_Publish sensor_2 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/Humidity"); /// Humidity Value
// Setup a feed called 'onoff' for subscribing to changes.
//Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME
"/feeds/onoff");
/*************************** Sketch Code ************************************/
void MQTT_connect();
void setup()
{
Serial.begin(115200); delay(10);
Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point.
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
Department of ECE SVCE Bengaluru Page 38
IoT (Internet of Things) Lab- BECL657C
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//SetupMQTTsubscriptionforonofffeed.
//mqtt.subscribe(&onoffbutton);
}
uint32_tx=0;voidloop()
{
//Ensuretheconnectiontothe MQTTserverisalive (thiswillmakethefirst
//connectionandautomaticallyreconnectwhendisconnected).
//SeetheMQTT_connect
//functiondefinitionfurtherbelow.
MQTT_connect();
//Nowwecanpublishstuff!
Serial.print(F("\nSending Sensor's Value "));
DHTdht(4,DHT11);//D4onboardnumberingsystem
Serial.print("Sensor 1 ");
Serial.println((dht.readTemperature( )));
delay(3000);
Serial.print("...");
float Value = dht.readTemperature( );
if(!sensor_1.publish(Value))
{
Serial.println(F("Failed"));
}
else
{
Serial.println(F("OK!"));
}
Serial.print("Sensor 2 ");
Serial.println((dht.readHumidity( )));
Serial.print("...");
int Value1 = dht.readHumidity( );
delay(3000);
if (! sensor_2.publish(Value1))
{
Serial.println(F("Failed"));
}
else
{
Serial.println(F("OK!"));
}
delay(3000);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect()
{
Department of ECE SVCE Bengaluru Page 39
IoT (Internet of Things) Lab- BECL657C
int8_t ret;
// Stop if already connected.
if (mqtt.connected())
{
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me while (1);
}
}
Serial.println("MQTT Connected!");
}
Fig 2 Displaying temperature and humidity data on AdafruitIO platform
Fig 3 Displaying temperature and humidity data on serial monitor of ArduinoIDE
Department of ECE SVCE Bengaluru Page 40
IoT (Internet of Things) Lab- BECL657C
RESULT:Published temperature and humidity data to MQTT broker with ESP32 has successfully
executed.
Department of ECE SVCE Bengaluru Page 41