Becl657c Iot Lab Manual - DR - Martin Joel Rathnam 02.03.2025
Becl657c Iot Lab Manual - DR - Martin Joel Rathnam 02.03.2025
BECL657C
Approved By
Dr. A. Poonguzhali
HOD/ECE
DEPARTMENT OF ELECTRONICS
AND
COMMUNICATION ENGINEERING
DEPARTMENT OF ELECTRONICS AND COMMUNICATION
ENGINEERING
B.E - VI Semester
Laboratory Instructor
1. Ms.Malarvizhi K
Instructor, Dept. of ECE. SSCE- Bengaluru
List of Experiments
Experiment
No. Experiment Name
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 6
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 7
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
2. Sensors and Actuators: Sensors collect data from the environment, measuring variables such as
temperature, humidity, pressure, motion, and more. Actuators are responsible for carrying out actions
based on commands received from the network or other devices, such as turning on/off lights or adjusting
the thermostat.
3. Connectivity: IoT devices rely on various communication technologies to connect to the internet
and other devices. This can include Wi-Fi, cellular networks, Bluetooth, Zigbee, LoRa, or Ethernet,
depending on the device's range, power consumption, and bandwidth requirements.
4. Data Transmission: Data collected by IoT devices is transmitted to central processing units, such as
cloud servers or edge computing platforms. This data can be transmitted in real-time or stored locally for
later transmission, depending on the use case.
5. Data Processing: The collected data is processed to extract valuable insights, trends, and patterns.
This can involve analytics, artificial intelligence, and machine learning to make informed decisions.
6. Cloud/Edge Computing: IoT data can be processed either in the cloud (centralized) or at the edge
(closer to the device). Edge computing reduces latency and allows for real-time decision-making, while cloud
computing offers scalability and accessibility from anywhere.
7. Applications: IoT applications are software programs or interfaces that enable users to interact with
IoT data and control devices. These can take the form of web-based dashboards, mobile apps, or automation
systems.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 8
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
2. Sensing and Data Collection: IoT devices are equipped with sensors that collect data from the
physical world. This data can be environmental data, usage statistics, or other relevant information.
3. Data Analysis: The collected data is analyzed to derive valuable insights, enabling businesses and
individuals to make informed decisions, optimize processes, and improve efficiency.
4. Remote Control and Automation: IoT allows for remote monitoring and control of devices. This
can range from adjusting home thermostat settings from a smartphone to remotely managing
industrial equipment.
Applications of IoT
IoT has a wide range of applications across various industries, including:
•Smart Homes: Home automation, security systems, and energy management.
•Smart Cities: Traffic management, waste management, and environmental monitoring.
•Industrial IoT (IIoT): Predictive maintenance, asset tracking, and process optimization.
• Healthcare: Remote patient monitoring and medical device connectivity.
• Agriculture: Precision farming and livestock monitoring.
• Logistics and Supply Chain: Inventory management and asset tracking.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 9
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
1. Sensing Data: IoT devices are equipped with various sensors that capture data from the physical
world. These sensors can measure parameters like temperature, humidity, pressure, light, motion, and
more. Some devices also include cameras and microphones for visual and audio data.
2. Data Transmission: IoT devices use communication technologies to transmit the data they collect
to other devices or central processing units. The choice of communication method depends on factors such
as range, power consumption, and bandwidth requirements. Common communication technologies
include Wi-Fi, cellular networks (3G, 4G, 5G), Bluetooth, Zigbee, LoRa, and more.
3. Data Processing: The data collected by IoT devices is sent to central processing units, such as cloud
servers or edge computing platforms. Here, the data is processed and analyzed. This processing can include
data filtering, aggregation, storage, and, in many cases, the application of machine learning or artificial
intelligence algorithms to extract meaningful insights from the data.
4. Storage: Processed data is often stored in databases or data lakes, making it accessible for future
analysis and historical reference.
5. Decision-Making: Based on the processed data, IoT systems can make informed decisions. These
decisions can range from simple actions like turning on a light when a motion sensor is triggered to
complex decisions in industrial IoT (IIoT), such as predicting equipment maintenance needs.
6. User Interaction: IoT applications and interfaces enable users to interact with the system. Users
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 10
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
can monitor and control devices, view data through web-based dashboards or mobile apps, and receive
alerts or notifications. Some systems also allow voice control or integration with other software
applications.
7. Automation: IoT can enable automation based on pre-defined rules or data-driven triggers. For
example, a smart thermostat can automatically adjust the temperature based on occupancy and ambient
conditions.
8. Security and Privacy: Security measures are crucial in IoT to protect data, devices, and the
network from unauthorized access and potential vulnerabilities. This includes encryption, authentication,
and access control.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 11
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
INTRODUCTION
There’s an add-on for the Arduino IDE that allows you to program the
ESP32 using the Arduino IDE and its programming language. We’ll show
you how to install the ESP32 board in Arduino IDE.
To install the ESP32 board in your Arduino IDE, follow these next instructions:
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 12
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 13
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
iii. Open the Boards Manager. Go to Tools > Board > Boards Manager…
iv. Search for ESP32 and press install button for the “ESP32 by Espressif Systems“:
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 14
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Plug the ESP32 board to your computer. With your Arduino IDE open, follow these steps:
1. Select your Board in Tools > Board menu (in my case it’s the DOIT ESP32 DEVKIT V1)
2. 2. Select the Port (if you don’t see the COM Port in your Arduino IDE,
you need to install theCP210x USB to UART Bridge VCP Drivers)
3. Open the following example under File > Examples > WiFi (ESP32) > WiFiScan
4. A new sketch opens in your Arduino IDE
5. Press the Upload button in the Arduino IDE. Wait a few seconds while
the code compiles and uploads to your board.
6. If everything went as expected, you should see a “Done uploading.” message.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 15
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 16
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Troubleshooting
If you try to upload a new sketch to your ESP32 and you get this error
message “A fatal error occurred: Failed to connect to ESP32: Timed out…
Connecting…“. It means that your ESP32 is not in flashing/uploading mode.
Having the right board name and COM por selected, follow these steps:
• Press the “Upload” button in the Arduino IDE to upload your sketch:
• After you see the “Connecting….” message in your Arduino IDE,
release the finger from the “BOOT” button:
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 17
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
That’s it. Your ESP32 should have the new sketch running. Press
the “ENABLE” button to restart the ESP32 and run the new
uploaded sketch.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 18
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No: 1
1a. To interface LED/Buzzer with Arduino /Raspberry Pi and write a program to ‘turn ON’
LED for 1 sec after every 2 seconds.
Aim: Select any one development board Example (Arduino / Raspberry Pi) and control
LED using the board.
Connection Details:
PIN CONFIGURATIONS
O/P LED GPIO 2 ARDUINO
Initial Setup:
1. Computer must be connected with internet facility
2. Iot Pro Kit must be connected with USB Cable
Procedure:
1. Write the code.
2. Configure the IoT Kit with the system.
3. Verify the code
4. Execute the code
5. Wait for system to show any errors
6. Debug the errors if any
7. Once again compile the program.
8. Check whether the output is obtained in the Serial Monitor Window.
Pre-Viva Questions:
1. What is LED?
2. What is GPIO?
3. What are sensors?
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 19
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
CODE:
void setup()
{
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs repeatedly forever
void loop()
{
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(LED_BUILTIN, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Result: Hence it is possible to control the LED using the Arduino board.
1. What is IoT?
2. What is LED?
3. What are sensors?
4. What are actuators?
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 20
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
1 b. 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.
Aim: Select any one development board Example (Arduino / Raspberry Pi) and to ‘turn ON’
LED when a push button is pressed or at sensor detection
Connection Details:
PIN CONFIGURATIONS
O/P LED GPIO 2 ARDUINO
Initial Setup:
1. Computer must be connected with internet facility
2. Iot Pro Kit must be connected with USB Cable
Procedure:
1. Write the code.
2. Configure the IoT Kit with the system.
3. Verify the code
4. Execute the code
5. Wait for system to show any errors
6. Debug the errors if any
7. Once again compile the program.
8. Check whether the output is obtained in the Serial Monitor Window.
Pre-Viva Questions:
1. What is LED?
2. What is GPIO?
3. What are sensors?
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 21
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Result : Hence it is possible to control the LED using the Arduino board.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 22
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No: 2
2a. To interface the DHT11 sensor with Arduino /Raspberry Pi and write a program to
print temperature and humidity readings.
Aim: Using Arduino, interface the DHT11 sensor with Arduino /Raspberry Pi and write a program
to print temperature and humidity readings.
Connection Details:
PIN CONFIGURATIONS
DHT 11 DHT 11
O/P DHT Sensor O/P PIN 16
Initial Setup:
1. Computer must be connected with internet facility
2. Iot Pro Kit must be connected with USB Cable
Procedure:
1. Write the code.
2. Configure the IoT Kit with the system.
3. Verify the code
4. Execute the code
5. Wait for system to show any errors
6. Debug the errors if any
7. Once again compile the program.
8. Check whether the output is obtained in the Serial Monitor Window.
Pre-Viva Questions:
1. What is DHT11 Sensor?
2. What GPIO needs to be connected?
3. What are sensors?
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 23
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
CODE:
#include "DHT.h"// Install the DHT library by Adafruit //
#define DHT11PIN 16
DHT dht(DHT11PIN, DHT11);
void setup()
{
Serial.begin(115200);
dht.begin();
}
void loop()
{
float humi = dht.readHumidity();
float temp = dht.readTemperature();
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("ºC ");
Serial.print("Humidity: ");
Serial.println(humi);
delay(1000);
}
1. What is DHT11?
2. Which pin needs to be connected to the sensor output?
3. What is humidity?
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 24
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
2b. To interface OLED with Arduino /Raspberry Pi and write a program to print its temperature
and humidity readings.
Aim: Using Arduino, interface the OLED with Arduino /Raspberry Pi and write a program to print
temperature and humidity readings.
Connection Details:
PIN CONFIGURATIONS
DHT 11 DHT 11
O/P DHT Sensor O/P PIN 16
Initial Setup:
1. Computer must be connected with internet facility
2. Iot Pro Kit must be connected with USB Cable
Procedure:
1. Write the code.
2. Configure the IoT Kit with the system.
3. Verify the code
4. Execute the code
5. Wait for system to show any errors
6. Debug the errors if any
7. Once again compile the program.
8. Check whether the output is obtained in the Serial Monitor Window.
Pre-Viva Questions:
1. What is DHT11 Sensor?
2. What GPIO needs to be connected?
3. What are sensors?
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 25
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 26
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
/* TFT Display */
void Display(void)
{
tft.fillScreen(0x0000); /* BLACK -- Background */
tft.setRotation(1);
tft.setCursor(0, 30);
tft.setTextColor(ST7735_YELLOW);
tft.setTextSize(1);
tft.println("Temp C: ");
tft.setCursor(60, 30);
tft.setTextColor(ST7735_YELLOW);
tft.setTextSize(1);
tft.println(t);
tft.setCursor(0, 90);
tft.setTextColor(ST7735_YELLOW);
tft.setTextSize(1);
tft.println("Hum %: ");
tft.setCursor(60, 90);
tft.setTextColor(ST7735_YELLOW);
tft.setTextSize(1);
tft.println(h);
delay(3000);
}
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 27
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No. 3
To interface motor using relay with Arduino/Raspberry Pi and write a program to 'turn ON'
motor when push button is pressed.
PIN CONFIGURATIONS
BUTTON GPIO 22
RELAY GPIO 27
CODE:
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 28
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Result: Hence the motor was turned on with the press of a button.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 29
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No. 4
This Arduino program uses an analog soil moisture sensor to read soil moisture levels. The
sensor sends an analog signal, which is read by one of the analog input pins on the Arduino.
Wiring:
CODE:
// Pin Definitions
const int soilMoisturePin = A0; // Analog pin connected to the sensor
int soilMoistureValue = 0; // Variable to store the moisture level
void setup()
{ // Start the serial communication
Serial.begin(9600);
pinMode(soilMoisturePin, INPUT); // Set soil moisture pin as input
}
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 30
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
void loop()
{ // Read the analog value from the soil moisture sensor
soilMoistureValue = analogRead(soilMoisturePin); // Print the value to the Serial Monitor
Serial.print("Soil Moisture Value: ");
Serial.println(soilMoistureValue); // Delay for a while before reading again
delay(1000);
}
Explanation:
• The analogRead() function reads the voltage from the sensor (typically between 0 to 1023,
where 0 means very wet soil and 1023 means very dry).
• The serial monitor displays the soil moisture level, and you can observe the value change
based on soil conditions.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 31
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
• LDR Pin:
o The other leg of the LDR connects to an analog input pin (e.g., A0).
o A 10kΩ resistor connects from the LDR’s other leg to GND (Ground).
The LDR works by changing its resistance based on the light it receives. In brighter light, its
resistance decreases, and in darkness, it increases.
CODE:
// Pin Definitions
const int ldrPin = A0; // Pin where the LDR is connected
int ldrValue = 0; // Variable to store the LDR sensor value
void setup()
{
// Start the serial communication
Serial.begin(9600);
pinMode(ldrPin, INPUT); // Set LDR pin as input
}
void loop()
{
// Read the analog value from the LDR sensor
ldrValue = analogRead(ldrPin);
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 32
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Explanation:
1. The analogRead(ldrPin) reads the value from the LDR, which will be between 0 (dark)
and 1023 (bright light).
2. The value is printed on the Serial Monitor so you can see how the light intensity affects
the value.
3. In bright light, the resistance of the LDR decreases, which will cause the analog value to
be lower.
4. In low light or darkness, the resistance increases, and the analog value will be higher.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 33
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment 5
The ultrasonic sensor measures distance by emitting a sound wave and measuring the time it
takes for the wave to reflect back to the sensor.
CODE:
// Pin Definitions
const int trigPin = 9; // Trigger pin of the ultrasonic sensor
const int echoPin = 10; // Echo pin of the ultrasonic sensor
long duration; // Variable to store the duration of the pulse
int distance; // Variable to store the calculated distance
void setup()
{
// Start the serial communication
Serial.begin(9600);
// Set the trigPin as an OUTPUT and echoPin as an INPUT
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
// Ensure the trigPin is LOW for a brief moment
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 34
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Explanation:
1. Trigger Pin (Trig): The trigger pin sends a short HIGH pulse (10 microseconds) to initiate
the ultrasonic sensor’s measurement.
2. Echo Pin (Echo): The echo pin receives the reflected sound wave from the obstacle. It
stays HIGH for the duration of the travel time of the sound wave.
3. pulseIn() Function: The pulseIn(echoPin, HIGH) function measures how long the echo pin
stays HIGH, which corresponds to the time taken by the sound wave to travel to the
object and back.
4. Distance Calculation: The time (duration) is used to calculate the distance based on the
speed of sound (approximately 0.034 cm per microsecond). The division by 2 accounts for
the round trip of the sound wave.
5. Serial Monitor: The calculated distance is displayed in centimeters on the Serial Monitor,
updated every half second (delay(500)).
2. Open the Serial Monitor (Tools → Serial Monitor) and you should see the distance in
centimeters displayed, updating every 500 milliseconds.
3. Move an object in front of the sensor, and watch the distance value change in response.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 35
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Notes:
• The accuracy of the distance depends on the object’s material and the angle at which it
reflects the sound wave.
• You can adjust the delay(500) to change the update rate. For more frequent updates, you
can reduce this value, but be careful not to overload the sensor with too many readings
too quickly.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 36
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No: 6
Create any cloud platform account, explore IoT services and register a thing on the
Platform
Initial Setup:
1. Computer must be connected with internet facility
2. Iot Pro Kit must be connected with USB Cable
Procedure:
Step 1: Go to https://thingspeak.com.
Step 4: After the creation of channel ,go to the API Keys and copy the Write API
Key.
Step 5: And now paste the Write API Key in your code and your channel number
in place of “myChannelNumber” and “myWriteAPIKey”
Step 6: After pasting the API key and Channel number, now you can upload the code
to ESP32.
Step 7: Now you can see the IoT services and register a thing on the platform.
Pre-Viva Questions:
1. What is a cloud?
2. Which cloud platform an we create an account?
3. What are the services we can access through the cloud?
Observations: It is possible to Create a cloud platform account, explore IoT services and to
register a thing on the Platform.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 37
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 38
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Hence it is possible to create a cloud platform account and explored the IoT services and registered
a thing on the Platform.
Initial Setup:
1. Computer must be connected with internet facility
2. Iot Pro Kit must be connected with USB Cable
Procedure:
Step 1: After pasting the API key and Channel number, now you can upload the code to
ESP32.
Step 2: Now you can view the temperature and humidity data in the form of graph in
your channel i.e. in the private view.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 39
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
PIN CONFIGURATION
DHT 11 ESP 32
O/P GPIO 4
CODE:
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 40
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, passphrase);
delay(500); Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
// ---initialize dht11
dht.begin();
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop ()
{
if ((millis() - lastTime) > timerDelay)
{
delay(2500);
// Reading temperature or humidity takes about 250 milliseconds!
if(x == 200)
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 41
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Pre-Viva Questions:
1. How can sensor data be pushed to the cloud?
2. What is the need to push sensor data to the cloud?
3. Can the data pushed to the cloud be recovered?
1. What is Bluetooth?
2. What is humidity?
3. Which pin needs to be connected to the sensor output?
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 42
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No: 7
Access the data pushed from sensor to cloud and apply any analytics or visualization services
Aim: To Access the data pushed from sensor to cloud and apply any analytics or visualization
services.
Initial Setup:
1. Computer must be connected with internet facility
2. Iot Pro Kit must be connected with USB Cable
Procedure:
PIN CONFIGURATION
DHT 11 ESP 32
O/P GPIO 4
CODE:
#include <WiFi.h> // Install the WiFi library//
#include "ThingSpeak.h"
char* ssid = "SSID"; // your network SSID (name) //
char* pass ="PASSWORD" ; // your network password/
WiFiClient client;
int field[2] = {1,2};
unsigned long channelNumber = YOUR CHANNEL NUMBER;
const char * readAPIKey = "API KEY";
void setup()
{
Serial.begin(115200);// Initialize serial
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo native USB portonly
}
WiFi.mode(WIFI_STA);
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 43
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Serial.println();
delay(20000); // no need to fetch too often
}
Pre-Viva Questions:
1. How can sensor data be retrieved to the cloud?
2. What is the need to retrieve sensor data to the cloud?
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 44
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No: 8
To interface Bluetooth with Arduino/Raspberry Pi and write a program to turn LED ON/OFF
when '1/0' is received from smartphone using Bluetooth.
Apparatus:IoT Pro Kit, wires, USB Cable connector.
Initial Setup:
1. Computer must be connected with internet facility
2. Iot Pro Kit must be connected with USB Cable
Procedure:
1. Write the code.
2. Configure the IoT Kit with the system.
3. Verify the code
4. Execute the code
5. Wait for system to show any errors
6. Debug the errors if any
7. Once again compile the program.
8. Check whether the output is obtained in the Serial Monitor Window.
Pre-Viva Questions:
1. Which application is used in mobile for connecting Bluetooth with IoT Kit?
2. Can we control an actuator from a mobile?
3. What are actuators?
Observations: It is possible to control actuators connected to the development board using Bluetooth
CODE:
#include <BluetoothSerial.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menu config` to and enableit
#endif
#define ledPin 2
BluetoothSerial SerialBT;
String message = "";
char incomingChar;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 45
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Result : Hence it is possible to control actuators connected to the development board using
Bluetooth.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 46
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No.9
Arduino to MQTT
• GND → GND
Required Libraries:
Install these libraries in the Arduino IDE via Sketch → Include Library → Manage Libraries.
#include <ESP8266WiFi.h> // For ESP8266 Wi-Fi module (use ESP32WiFi.h for ESP32)
#include <PubSubClient.h>
#include <DHT.h>
// Wi-Fi credentials
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
// DHT Sensor
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT11 or DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(115200);
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 47
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
void reconnect()
{
while (!client.connected())
{
if (client.connect("ArduinoClient"))
{
Serial.println("Connected to MQTT Broker");
} else {
delay(5000);
}
}
}
void loop()
{
if (!client.connected())
{
reconnect();
}
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 48
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
client.publish(mqtt_topic, temperatureStr);
Serial.print("Temperature: ");
Serial.println(temperature);
client.loop();
delay(10000); // Delay for 10 seconds
}
Explanation:
• Wi-Fi Connection: The ESP8266 or ESP32 connects to your Wi-Fi network using the
provided credentials.
• MQTT Communication: The code connects to the MQTT broker, and every 10 seconds, it
reads the temperature from the DHT sensor and publishes it to the topic
home/temperature.
PIN CONFIGURATION
DHT 11 ESP 32
O/P GPIO 4
#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
DHTdht(DHTPIN, DHTTYPE);
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 49
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
void setupMQTT()
mqttClient.setServer(mqttServer, mqttPort);
mqttClient.setCallback(callback);
void reconnect()
while (!mqttClient.connected())
if (mqttClient.connect(clientId.c_str()))
Serial.println("Connected.");
// subscribe to topic
mqttClient.subscribe("esp32/message");
void setup()
Serial.begin(9600);
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 50
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Serial.println("DHT11 test!");
dht.begin();
WiFi.begin(ssid, password);
delay(500);
Serial.print(".");
pinMode(2, OUTPUT);
setupMQTT();
void loop()
if (!mqttClient.connected())reconnect();
mqttClient.loop();
long previous_time = 0;
previous_time = now;
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
return;
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 51
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
char tempString[8];
dtostrf(t, 1, 2, tempString);
Serial.print("Temperature: ");
Serial.println(tempString);
mqttClient.publish("esp32/temperature", tempString);
char humString[8];
dtostrf(h, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
mqttClient.publish("esp32/humidity", humString);
delay(2000);
Serial.print("Callback - ");
Serial.print("Message:");
String messageTemp;
Serial.println();
if (String(topic) == "esp32/message")
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 52
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
if(messageTemp == "on")
Serial.println("on");
digitalWrite(2, HIGH);
Serial.println("off");
digitalWrite(2, LOW);
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 53
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Step 3: After creating Workspace add a new broker, by specifying the host and port and save it
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 54
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 55
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Step 6: After subscribing the topics, now we are able to see the temperature and humidity data.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 56
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No. 10
Write a program to create a UDP server on Arduino/Raspberry Pi and respond with humidity
data to the UDP client when requested.
• GND → GND
Required Libraries:
CODE:
// Wi-Fi credentials
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
// DHT Sensor
#define DHTPIN 2 // Pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT11 or DHT22
DHT dht(DHTPIN, DHTTYPE);
// UDP parameters
WiFiUDP udp;
unsigned int localPort = 4210; // Local port to listen for incoming requests
char incomingPacket[255]; // Buffer for incoming packets
void setup() {
// Start serial communication
Serial.begin(115200);
// Connect to Wi-Fi
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 57
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
void loop() {
// Check if a UDP packet is available
int packetSize = udp.parsePacket();
if (packetSize) {
// Read the incoming packet
int len = udp.read(incomingPacket, 255);
if (len > 0) {
incomingPacket[len] = 0; // Null-terminate the string
}
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 58
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
udp.write(humidityStr.c_str());
udp.endPacket();
Serial.print("Sent humidity: ");
Serial.println(humidityStr);
}
}
}
Explanation:
1. Wi-Fi Connection:
o The WiFi.begin(ssid, password) connects the ESP8266 (or ESP32) to the specified
Wi-Fi network. The while (WiFi.status() != WL_CONNECTED) loop waits until
the connection is established.
2. DHT Sensor:
o The DHT11 or DHT22 sensor reads the humidity using the dht.readHumidity()
function. If the reading fails, NaN (Not a Number) is returned.
3. UDP Communication:
o The WiFiUDP udp object is used to send and receive UDP packets.
o The humidity data is sent in a string format with two decimal places
(String(humidity, 2)).
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 59
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
CODE:
#include <WiFi.h>
#include
<WiFiUdp.h>
#include <DHT.h>
// Replace with your network
credentials
const char* ssid = "SSID";
const char* password = "PASSWORD";
// Replace with your DHT sensor type
and pin
#define DHT_PIN 4
#define DHT_TYPE DHT11
// UDP server port
#define UDP_PORT 5000
// Create an instance of the DHT sensor
DHT dht(DHT_PIN, DHT_TYPE);
WiFiUDP udp;
float humidity = 0.0;
void setup()
{
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Print the ESP32 IP address
Serial.print("ESP32 IP address: ");
Serial.println(WiFi.localIP());
// Start the UDP server
udp.begin(UDP_PORT);
Serial.println("UDP server started");
// Initialize DHT
sensor
dht.begin();
}
void loop()
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 60
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
if (request == "get_humidity")
{
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 61
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Result: Hence the program to create UDP server on Arduino/Raspberry Pi and respond with
humidity data to UDP client when requested was successfully achieved.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 62
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No. 11
Write a program to create a TCP server on Arduino /Raspberry Pi and respond with humidity
data to the TCP client when requested.
// Wi-Fi credentials
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
// DHT Sensor
#define DHTPIN 2 // Pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT11 or DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
// Start serial communication
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 63
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Serial.println("Connected to WiFi");
// Start the TCP server
server.begin();
Serial.println("TCP server started");
// Initialize the DHT sensor
dht.begin();
}
void loop()
{
// Check for incoming clients
WiFiClient client = server.available();
if (client)
{
Serial.println("Client connected");
// Read the data sent by the client
String request = "";
while (client.available())
{
char c = client.read();
request += c;
}
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 64
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
}
}
// Close the connection
client.stop();
Serial.println("Client disconnected");
}
}
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 65
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
PIN CONFIGURATION
DHT 11 ESP 32
O/P GPIO 4
CODE:
#include <WiFi.h>
#include
<WiFiClient.h>
#include <DHT.h>
// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "PASSWORD";
// Replace with your DHT sensor type and pin number
#define DHT_PIN 4
#define DHT_TYPE DHT11
DHT dht(DHT_PIN, DHT_TYPE);
WiFiServer server(8888); // TCP server port
void setup()
{
Serial.begin(115200);
delay(10);
// Connect to Wi-Fi
Serial.println();
Serial.print("Connecting to
"); Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
dht.begin();
server.begin();
}
void loop()
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 66
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
{
WiFiClient client = server.available();
if (client)
{
Serial.println("New client connected");
// Wait for data from client
while
(client.connected())
{
if (client.available())
{
String request = client.readStringUntil('\r');client.flush();
if (request.indexOf("/humidity") != -1)
{
// Read humidity data from DHT sensor
float humidity = dht.readHumidity();
if (isnan(humidity))
{
Serial.println("Failed to read humidity from DHT sensor");
client.println("Failed to read humidity from DHT sensor");
}
else
{
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
client.print("Humidity: ");
client.print(humidity);
client.println("%");
}
}
// Close the connection
client.stop();
Serial.println("Client disconnected");
}
}
}
}
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 67
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 68
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
4. The ESP32 TCP server should respond with the humidity data read
from the DHT sensor, which should be displayed in your Serial
Monitor or in the output of the curl command in the command line.
5. That's it! You have successfully set up and tested a TCP server on
your ESP32 board to respond with humidity data when requested by a
TCP client.
Pre-Viva Questions:
1. Who is a client?
2. What is socket comunication?
Observations: It is possible to Read data from a sensor and send it to a requesting client. (using socket
communication).
Result : Hence it is possible to read data from a sensor and send it to a requesting client. (using
socket communication).
1. Who is a client?
2. What is socket communication?
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 69
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Experiment No.12
Write a program on Arduino / Raspberry Pi to subscribe to the MQTT broker for temperature
data and print it.
To subscribe to an MQTT broker on Arduino and print temperature data, you'll need to follow
these steps:
1. Set up the MQTT broker: If you don't already have an MQTT broker, you can use a
public one like mqtt.eclipse.org or a private one (e.g., using Mosquitto on your own
server).
2. Use an MQTT client library for Arduino: For this example, we'll use the PubSubClient
library, which is commonly used for MQTT communication with Arduino.
3. Wi-Fi connection: You'll need an Arduino with Wi-Fi capability, such as the ESP8266 or
ESP32.
Prerequisites:
• Arduino Board: ESP8266 or ESP32 (if you're using another board like Arduino Uno, you
would need an external Wi-Fi module like the ESP8266 in AT mode).
• MQTT Broker: Use a public broker like mqtt.eclipse.org or set up your own.
• Temperature data: This example assumes the temperature is published by another device
on an MQTT topic (e.g., home/temperature).
Required Libraries:
// Wi-Fi credentials
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 70
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 71
Regulation – 2022 (CBCS Scheme) INTERNET OF THINGS (IoT) – BECL657C
void setup() {
Serial.begin(115200);
setup_wifi(); // Connect to Wi-Fi
void loop() {
if (!client.connected()) {
reconnect(); // Reconnect if disconnected
}
client.loop(); // Maintain MQTT connection and listen for incoming messages
}
Explanation of the Code:
1. Wi-Fi Connection:
o The setup_wifi() function connects the ESP8266 (or ESP32) to the Wi-Fi network
using the SSID and password.
o The program waits until the Wi-Fi connection is established (WiFi.status()).
2. MQTT Setup:
o The PubSubClient library is used to handle the MQTT connection.
o The MQTT broker information (server, port, topic) is set up. In this case, the topic
home/temperature is subscribed to, which will be used to receive temperature data.
3. Callback Function:
o The callback() function is called when a message is received on the subscribed
MQTT topic. It prints the topic and the received message to the Serial Monitor.
o In this example, the message is expected to be a temperature value in plain text,
and it will be printed to the Serial Monitor.
4. Reconnecting to the Broker:
o The reconnect() function ensures that the Arduino will attempt to reconnect to the
MQTT broker if the connection is lost.
o Once connected, it subscribes to the home/temperature topic to receive updates.
5. MQTT Loop:
o In the loop(), the code ensures that the MQTT connection is maintained and calls
client.loop() to listen for new messages on the subscribed topics.
Prepared by:Dr.Martin Joel Rathnam & Dr.Hosanna Princye P Sri Sairam College of Engineering, Anekal Page No. 72