Professional Documents
Culture Documents
Iot Lab Manual (21ec581) - Vtu
Iot Lab Manual (21ec581) - Vtu
&
COMMUNICATION ENGINEERING
(2021 Scheme)
Prepared By:
Lab Instructor:
Mr.Rakesh, Technical Staff, ECE
Approved by
Dr. Praveen J
IQAC Director, Professor and Head, ECE
Internet of Things Lab (21EC581) 5th Sem B.E
Program Outcomes
PO5: Modern tool usage: Create, select, and apply appropriate techniques,
resources, and modern engineering and IT tools including prediction and modeling
to complex engineering activities with an understanding of the limitations.
PO6: The engineer and society: Apply reasoning informed by the
contextual knowledge to assess societal, health, safety, legal and cultural
issues and the consequent responsibilities relevant to the professional
engineering practice.
PO7: Environment and sustainability: Understand the impact of the
professional engineering solutions in societal and environmental
contexts, and demonstrate the knowledge of, and need for sustainable
development.
PO8: Ethics: Apply ethical principles and commit to professional ethics
and responsibilities and norms of the engineering practice.
PO9: Individual and team work: Function effectively as an individual,
and as a member or leader in diverse teams, and in multidisciplinary
settings.
PO11: Lifelong learning: Recognize the need for, and have the
preparation and to engage in independent and life-long learning in the
broadest context of technological change.
Exp.No Experiments
i. To interface LED/Buzzer with Arduino/Raspberry Pi and write a program to
turn ON LED for 1 sec after every 2 seconds.
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 2 seconds with
Arduino/ Raspberry Pi.
COMPONENTS REQUIRED:
CONNECTION DIAGRAM:
CODING:
Embedded C
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)
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:
CODING:
Embedded C
void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH)
{
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else
{
// turn LED off:
digitalWrite(ledPin, 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.
2. LDR.
3. LED.
4. Resistor (100KΩ)
5. Connecting cable or USB cable.
6. Breadboard.
7. Jumper wires.
CONNECTION DIAGRAM:
CODING:
Embedded C
if (sensorValue<400)
digitalWrite(13, HIGH);
else
digitalWrite(13, LOW);
delay(1000);
}
RESULT: Controlling LED by interfacing LDR with Arduino UNO has successfully executed.
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
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.
EXPERIMENT – 2
i. To interface DHT11 sensor with Arduino/Raspberry Pi and write a program to
print temperature and humidity readings.
COMPONENTS REQUIRED:
CONNECTION DIAGRAM:
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()
{
You need to install PIP before you can install the Adafruit_DHT module:
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("-------------------------------------------")
RESULT: Interfacing of DHT11 with Arduino UNO to print temperature and humidity
readings has successfully executed.
AIM: Write a program to interface OLED/ I2C LCD with Arduino to print temperature and
humidity readings.
COMPONENTS REQUIRED:
CONNECTION 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.
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.
CODING:
Embedded C
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define dht_dpin 7
#include<dht.h>
dht DHT;
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);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Temp=");
display.print(DHT.temperature); //print the temperature
display.print(" Celsius");
display.display();
// display humidity
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
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define dht_dpin 7
#include<dht.h>
dht DHT;
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.
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:
CODING:
Embedded C
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);
}
}
(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.
EXPERIMENT – 4
To interface Bluetooth with Arduino and write a program to turn LED ON/OFF when
'1'/'0' is received from smartphone using Bluetooth.
AIM: Write a program on ESP32 to turn LED ON/OFF from smartphone using Bluetooth when
'1'/'0' is received.
COMPONENTS REQUIRED:
1. ESP32.
2. LED.
3. Connecting cable or USB cable.
4. Breadboard.
5. Jumper wires.
CONNECTION DIAGRAM:
CODING:
Embedded C
(b) Turning LED ON/OFF operation in ESP32 though smartphone using Bluetooth.
RESULT: LED ON/OFF from smartphone using Bluetooth based on received '1'/'0' data with
ESP32 has successfully executed.
EXPERIMENT – 5
To interface Bluetooth with Arduino/Raspberry Pi and write a program to send sensor
data to smartphone using Bluetooth.
AIM: Write a program on ESP32 to send temperature value using DHT11 sensor to smartphone
using Bluetooth.
COMPONENTS REQUIRED:
1. ESP32.
2. DHT11.
3. Connecting cable or USB cable.
4. Breadboard.
5. Jumper wires.
CONNECTION DIAGRAM:
CODING:
Embedded C
// Interfacing DHT11 with ESP32 and sending data to mobile through Bluetooth
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
byte BTData;
#include "DHT.h"
DHT dht(2,DHT11); //D4 pin
void setup()
{
Serial.begin(115200);
SerialBT.begin();
Serial.println("Bluetooth Started! Ready to pair...");
}
void loop()
{
Serial.print("Temperature in c :");
Serial.println(dht.readTemperature( ));
String temperatureString = String(dht.readTemperature( ));
SerialBT.println(temperatureString);
delay(2000);
}
RESULT: Sending Temperature data using DHT11 sensor to smartphone using Bluetooth with
ESP32 has successfully executed.
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:
CODING:
Embedded C
#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()
{
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 ThingSpeak. There are up to 8 fields in a channel, allowing you to
store up to 8 different
RESULT: Sending temperature and humidity data using DHT11 sensor to Thingspeak cloud
with ESP32 has successfully executed.
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:
CODING:
Embedded C
// Retrieving temperature and humidity data from Thingspeak cloud using ESP32
#include <WiFi.h>
#include <ThingSpeak.h>
WiFiClient client;
void setup()
{
Serial.begin(9600);
ThingSpeak.begin(client);
WiFi.disconnect();
delay(3000);
Serial.println("START");
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");
}
void loop()
{
Serial.print("Temperature=");
Serial.println((ThingSpeak.readIntField(12345,1,"ABCDEFGH")));
Serial.print("Humidity=");
Serial.println((ThingSpeak.readIntField(12345,2,"ABCDEFGH")));
delay(3000);
}
(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.
EXPERIMENT – 8
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:
CODING:
Embedded C
/****************************************************/
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
/************************* WiFi Access Point *********************************/
// 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);
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit MQTT demo"));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
uint32_t x=0;
void loop()
{
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected).
// See the MQTT_connect
// function definition further below.
MQTT_connect();
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);
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!");
}
(c) Displaying temperature and humidity data on serial monitor of Arduino IDE
RESULT: Published temperature and humidity data to MQTT broker with ESP32 has
successfully executed.
EXPERIMENT – 9
Write a program on Arduino/Raspberry Pi to subscribe to MQTT broker for temperature
data and print it.
AIM: Write a program on ESP32 to subscribe to MQTT broker for temperature data and print it.
COMPONENTS REQUIRED:
1. ESP32 - 2 Numbers.
2. DHT11.
3. Connecting cable or USB cable.
4. Breadboard.
5. Jumper wires.
CONNECTION DIAGRAM:
CODING:
Embedded C
#include <ESP8266WiFi.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, ADAFRUIT_SERVER, ADAFRUIT_PORT,
ADAFRUIT_USERNAME, ADAFRUIT_USERNAME, ADAFRUIT_KEY);
void MQTT_connect() {
int8_t ret;
Serial.println("MQTT Connected!");
}
void setup() {
Serial.begin(115200);
delay(10);
// Connect to Wi-Fi
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
mqtt.subscribe(&Temperature);
}
void loop() {
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000)))
{
if (subscription == &Temperature)
{
Serial.print(F("Received temperature value is: "));
Serial.println((char *)Temperature.lastread);
}
}
}
RESULT: Program on ESP32 to subscribe to MQTT broker for temperature data and printing
the data has successfully executed.
EXPERIMENT – 10
Write a program to create UDP server on Arduino/Raspberry Pi and respond with
humidity data to UDP client when requested.
AIM: Write a program to create UDP server on NodeMCU (ESP8266) and respond with
humidity data to UDP client when requested.
COMPONENTS REQUIRED:
1. NodeMCU (ESP8266).
2. DHT11.
3. Connecting cable or USB cable.
4. Breadboard.
5. Jumper wires.
CONNECTION DIAGRAM:
CODING:
Embedded C
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "DHT.h"
DHT dht(14,DHT11); //D5 on board numbering system & 14 GPIO numbering system
WiFiUDP udp;
void setup() {
Serial.begin(115200);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
Serial.println("WiFi connected.");
Serial.println("UDP server started");
udp.begin(udpPort);
}
void loop() {
char packetBuffer[255]; // buffer to hold incoming packet
float readHumidity()
{
(b) Receiving humidity on UDP client based on request to UDP server NodeMCU (ESP8266)
RESULT: Received humidity data to UDP client from UDP server on NodeMCU (ESP8266)
has successfully executed.
EXPERIMENT – 11
Write a program to create TCP server on Arduino/Raspberry Pi and respond with
humidity data to TCP client when requested.
AIM: Write a program to create TCP server on NodeMCU (ESP8266) and respond with
humidity data to TCP client when requested.
COMPONENTS REQUIRED:
1. NodeMCU (ESP8266).
2. DHT11.
3. Connecting cable or USB cable.
4. Breadboard.
5. Jumper wires.
CONNECTION DIAGRAM:
CODING:
Embedded C
#include <ESP8266WiFi.h>
#include "DHT.h"
DHT dht(5,DHT11); //D1 on board numbering system & 5 GPIO numbering system
// Replace with your network credentials
const char* ssid = "GMIT";
const char* password = "Davangere";
void setup() {
Serial.begin(115200);
delay(10);
// 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");
void loop() {
WiFiClient client = server.available(); // Check for a client connection
if (client) {
Serial.println("New client connected");
float humidity = dht.readHumidity();
String response = "Humidity:"; // Replace this with your actual humidity data
(b) Receiving humidity on TCP client based on request to TCP server NodeMCU (ESP8266)
RESULT: Received humidity data to TCP client from TCP server on NodeMCU (ESP8266)
has successfully executed.
EXPERIMENT – 12
To install MySQL database on Raspberry Pi and perform basic SQL queries.
AIM: To perform basic SQL queries by installing MySQL database on Rsapberry Pi.
PROCEDURE:
1. Create:
In CRUD operations, 'C' is an acronym for create, which means to add or insert data into the
SQL table. So, firstly we will create a table using CREATE command and then we will use the
INSERT INTO command to insert rows in the created table.
where,
Prior to the creation of a table in SQL, we need to create a database or select an existing
database. Since we already had a database, we will select the database with the USE command.
To ensure that the table is created as per the column names, data types and sizes which we
have assigned during table creation, we will execute the following query:
Execute the following query to insert multiple records in the employee table:
2. Read:
In CRUD operations, 'R' is an acronym for read, which means retrieving or fetching the data
from the SQL table. So, we will use the SELECT command to fetch the inserted records from the
SQL table. We can retrieve all the records from a table using an asterisk (*) in a SELECT query.
There is also an option of retrieving only those records which satisfy a particular condition by
using the WHERE clause in a SELECT query.
Example :Write a query to fetch all the records stored in the employee table.
Here, an asterisk is used in a SELECT query. This means all the column values for every record
will be retrieved.
You will get the following output after executing the above query:
3. Update:
In CRUD operations, 'U' is an acronym for the update, which means making updates to the
records present in the SQL tables. So, we will use the UPDATE command to make changes in
the data present in tables.
Syntax:
Here in the SELECT query, we have used the SET keyword to update an employee's last name
as 'Bose'. We want to update an employee's last name only for the employee with id 6, so we
have specified this condition using the WHERE clause.
To ensure that an employee's last name with employee id 6 is updated successfully, we will
execute the SELECT query.
4. Delete:
In CRUD operations, 'D' is an acronym for delete, which means removing or deleting the
records from the SQL tables. We can delete all the rows from the SQL tables using the DELETE
query. There is also an option to remove only the specific records that satisfy a particular
condition by using the WHERE clause in a DELETE query.
Example:Write a query to delete the employee record from the employee table whose salary is
above 34000.
Here we have applied the DELETE query on the employee table. We want to delete only the
employee record whose salary is 34000, so we have specified this condition using the WHERE
clause.
execute the SELECT query to ensure that the employee record with salary as 34000 is deleted
successfully.
To remove all the records from the employee table, we will execute the DELETE query on the
employee table.
Execute the SELECT query to ensure that all the records are deleted successfully from
the employee table.
RESULT: Performed basic SQL queries by installing MySQL database on Rsapberry Pi.