You are on page 1of 61

DEPARTMENT OF ELECTRONICS

&
COMMUNICATION ENGINEERING

V SEMESTER LAB MANUAL

(2021 Scheme)

Internet of Things- 21EC581

Prepared By:

Mr.Raviteja Balekai, Assistant Professor, ECE


Mrs.Veena H T, Assistant Professor, ECE

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

Vision and Mission of the Institute

Vision of the Institute

To develop technologically competent, humane and socially


responsible engineers and managers to meet the ever growing
challenges of the Global Environment.

Mission of the Institute

 To provide quality technical and management education by


applying best practices in teaching, learning and with the state
of the art infrastructural facilities.

 To mould engineers and managers with appropriate pedagogy to


develop leadership qualities and skills by imbibing professional
ethics to make them industry ready.
 To develop student-centric institution which evolves and fosters
the talents of budding engineers, managers and entrepreneurs
and prepare them to make a positive contribution to the society.
 To promote Research and consultancy through collaboration with
industries and government organizations.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 1


Internet of Things Lab (21EC581) 5th Sem B.E

Vision, Mission, PEOs & PSOs of the Department

Vision of the Department


To excel in creating technically competent and socially responsible
Electronics & Communication Engineers capable of contributing to the
emerging technology.

Mission of the Department


M1: Imparting effective technical education to excel in Electronics
Communication Engineering.
M2: Adapting appropriate pedagogy to imbibe professionalism in
students.
M3: Inculcating Research culture and thereby bridging the gap between
Academia and Industry.

Program Educational Objectives


PEO1: To deliver Engineering Skills and Knowledge by integrating basic
Engineering concepts with core Electronics and Communication
Engineering to solve the problems of the society.
PEO2: To exhibit technical competency by developing solutions in diverse
areas of Electronics and Communication Engineering.
PEO3: To be receptive to emerging technologies and attain professional
Competency through pursuing Research and life-long learning.

Program Specific 00Outcomes

PSO1: Design Analog and Digital Systems using emerging Technologies.

PSO2: Build different models by applying Technical knowledge in


Communication, Networking, Signal Processing, VLSI and Embedded
Systems along with Programming and Simulation tools for a variety of
applications.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 2


Internet of Things Lab (21EC581) 5th Sem B.E

Program Outcomes

Graduates of Electronics & Communication Engineering by the time of


graduation will demonstrate:

PO1: Engineering knowledge: Apply the knowledge of mathematics,


science, engineering fundamentals, and an engineering specialization to
the solution of complex engineering problems.
PO2: Problem analysis: Identify, formulate, review research literature,
and analyze complex engineering problems reaching substantiated
conclusions using first principles of mathematics, natural sciences, and
engineering sciences.
PO3: Design/development of solutions: Design solutions for complex
engineering problems and design system components or processes that
meet the specified needs with appropriate consideration for the public
health and safety, and the cultural, societal, and environmental
considerations.
PO4: Conduct investigations of complex problems: Use research-
based knowledge and research methods including design of experiments,
analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.

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

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 3


Internet of Things Lab (21EC581) 5th Sem B.E

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.

PO10: Communication: Communicate effectively on complex


engineering activities with the engineering community and with society
at large, such as, being able to comprehend and write effective reports
and design documentation, make effective presentations, and give and
receive clear instructions.

PO11: Project management and finance: Demonstrate knowledge and


understanding of the engineering and management principles and apply
these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments.

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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 4


Internet of Things Lab (21EC581) 5th Sem B.E

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 5


Internet of Things Lab (21EC581) 5th Sem B.E

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 6


Internet of Things Lab (21EC581) 5th Sem B.E

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.

1 ii. To interface Push button/Digital sensor (IR/LDR) with Arduino/Raspberry


Pi and write a program to turn ON LED when push button is pressed or at
sensor detection.
i. To interface DHT11 sensor with Arduino/Raspberry Pi and write a program
to print temperature and humidity readings.
2
ii. To interface OLED with Arduino/Raspberry Pi and write a program to print
temperature and humidity readings on it.
To interface motor using relay with Arduino/Raspberry Pi and write a program to
3
turn ON motor when push button is pressed.
To interface Bluetooth with Arduino/Raspberry Pi and write a program to turn LED
4
ON/OFF when '1'/'0' is received from smartphone using Bluetooth.
To interface Bluetooth with Arduino/Raspberry Pi and write a program to send
5
sensor data to smartphone using Bluetooth.
Write a program on Arduino/Raspberry Pi to upload temperature and humidity data
6
to thingspeak cloud.
Write a program on Arduino/Raspberry Pi to retrieve temperature and humidity
7
data from thingspeak cloud.
Write a program on Arduino/Raspberry Pi to publish temperature data to MQTT
8
broker.
Write a program on Arduino/Raspberry Pi to subscribe to MQTT broker for
9
temperature data and print it.
Write a program to create UDP server on Arduino/Raspberry Pi and respond with
10
humidity data to UDP client when requested.
Write a program to create TCP server on Arduino/Raspberry Pi and respond with
11
humidity data to TCP client when requested.

12 To install MySQL database on Raspberry Pi and perform basic SQL queries.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 7


Internet of Things Lab (21EC581) 5th Sem B.E

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:

1. Arduino UNO/ Raspberry Pi.


2. LED/ Buzzer.
3. Resistor (220Ω)
4. Connecting cable or USB cable.
5. Breadboard.
6. Jumper wires.

CONNECTION DIAGRAM:

(a) LED (b) Buzzer

(c) LED interfacing with Arduino

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 8


Internet of Things Lab (21EC581) 5th Sem B.E

(d) LED interfacing with Raspberry Pi

CODING:

Embedded C

const int buzzer=13; //led or buzzer


void setup()
{
// initialize digital pin 13 as an output.
pinMode(buzzer, OUTPUT);
}
// the loop function runs over and over again forever
void loop()
{
digitalWrite(buzzer, HIGH); // turn the buzzer on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(buzzer, LOW); // turn the buzzer off by making the voltage LOW
delay(2000); // wait for a two seconds
}

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 9


Internet of Things Lab (21EC581) 5th Sem B.E

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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 10


Internet of Things Lab (21EC581) 5th Sem B.E

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

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 11


Internet of Things Lab (21EC581) 5th Sem B.E

CODING:

Embedded C

const int buttonPin = 4; // the number of the pushbutton pin


const int ledPin = 13; // the number of the LED pin

// variables will change:


int buttonState = 0; // variable for reading the pushbutton status

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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 12


Internet of Things Lab (21EC581) 5th Sem B.E

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:

(b) LDR interfacing with Arduino

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 13


Internet of Things Lab (21EC581) 5th Sem B.E

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()
{
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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 14


Internet of Things Lab (21EC581) 5th Sem B.E

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

(d) IR sensor interfacing with Arduino

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 15


Internet of Things Lab (21EC581) 5th Sem B.E

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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 16


Internet of Things Lab (21EC581) 5th Sem B.E

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

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 17


Internet of Things Lab (21EC581) 5th Sem B.E

(c) 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()
{

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 18


Internet of Things Lab (21EC581) 5th Sem B.E

//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("-------------------------------------------")

RESULT: Interfacing of DHT11 with Arduino UNO to print temperature and humidity
readings has successfully executed.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 19


Internet of Things Lab (21EC581) 5th Sem B.E

ii. To interface OLED/I2C LCD with Arduino/Raspberry Pi and write a program to


print temperature and humidity readings on it.

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:

(d) 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
Arduino IDE.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 20


Internet of Things Lab (21EC581) 5th Sem B.E

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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 21


Internet of Things Lab (21EC581) 5th Sem B.E

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.

(e) I2C LCD interfacing with Arduino UNO

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 22


Internet of Things Lab (21EC581) 5th Sem B.E

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);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Temp=");
display.print(DHT.temperature); //print the temperature
display.print(" Celsius");
display.display();

// display humidity

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 23


Internet of Things Lab (21EC581) 5th Sem B.E

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;

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");

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 24


Internet of Things Lab (21EC581) 5th Sem B.E

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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 25


Internet of Things Lab (21EC581) 5th Sem B.E

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:

(a) Push button interfacing with Arduino

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 26


Internet of Things Lab (21EC581) 5th Sem B.E

(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);
}
}

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 27


Internet of Things Lab (21EC581) 5th Sem B.E

(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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 28


Internet of Things Lab (21EC581) 5th Sem B.E

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:

(a) Interfacing LED with ESP32

CODING:

Embedded C

// Interfacing LED with ESP32 and controlling LED through Bluetooth


#include <BluetoothSerial.h>
#define ledPIN 2 //D2 pin
BluetoothSerial SerialBT;
byte BTData;
/* Check if Bluetooth configurations are enabled in the SDK */

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 29


Internet of Things Lab (21EC581) 5th Sem B.E

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)


#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
void setup()
{
pinMode(ledPIN, OUTPUT);
Serial.begin(115200);
SerialBT.begin();
Serial.println("Bluetooth Started! Ready to pair...");
}
void loop()
{
if(SerialBT.available())
{
BTData = SerialBT.read();
Serial.write(BTData);
}
if(BTData == '1')
{
digitalWrite(ledPIN, HIGH);
}
/* If received Character is 0, then turn OFF the LED */
if(BTData == '0')
{
digitalWrite(ledPIN, LOW);
}
}

(b) Turning LED ON/OFF operation in ESP32 though smartphone using Bluetooth.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 30


Internet of Things Lab (21EC581) 5th Sem B.E

(c) Connecting smartphone to ESP32 using Bluetooth.


(d) Controlling LED Turn ON (1)/ OFF (0) through smartphone.

RESULT: LED ON/OFF from smartphone using Bluetooth based on received '1'/'0' data with
ESP32 has successfully executed.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 31


Internet of Things Lab (21EC581) 5th Sem B.E

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:

(a) Interfacing DHT11 with ESP32

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

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 32


Internet of Things Lab (21EC581) 5th Sem B.E

/* Check if Bluetooth configurations are enabled in the SDK */


#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

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);
}

(b) Receiving Temperature data on bluetooth terminal of smart phone

RESULT: Sending Temperature data using DHT11 sensor to smartphone using Bluetooth with
ESP32 has successfully executed.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 33


Internet of Things Lab (21EC581) 5th Sem B.E

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

CODING:

Embedded C

//Interfacing DHT11 to ESP32 to upload temperature and humidity data to


thingspeak cloud

#include <DHT.h>
#include <WiFi.h>
#include <WiFiClient.h>

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 34


Internet of Things Lab (21EC581) 5th Sem B.E

#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

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 35


Internet of Things Lab (21EC581) 5th Sem B.E

// pieces of information in a channel. Here, we write to field 1 and 2.

ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey);


ThingSpeak.writeField(myChannelNumber, 2, humidity, myWriteAPIKey);
delay(3000); // ThingSpeak will only accept updates every 3 seconds.
}

(b) Displaying temperature and humidity data on ThingSpeak cloud

RESULT: Sending temperature and humidity data using DHT11 sensor to Thingspeak cloud
with ESP32 has successfully executed.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 36


Internet of Things Lab (21EC581) 5th Sem B.E

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:

(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()
{

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 37


Internet of Things Lab (21EC581) 5th Sem B.E

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);
}

(b) Available temperature and humidity data on ThingSpeak cloud

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 38


Internet of Things Lab (21EC581) 5th Sem B.E

(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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 39


Internet of Things Lab (21EC581) 5th Sem B.E

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:

(a) Interfacing DHT11 with ESP32

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"

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 40


Internet of Things Lab (21EC581) 5th Sem B.E

/************************* 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 ");

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 41


Internet of Things Lab (21EC581) 5th Sem B.E

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());

// Setup MQTT subscription for onoff feed.


//mqtt.subscribe(&onoffbutton);
}

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();

// Now we can publish stuff!

Serial.print(F("\nSending Sensor's Value "));


DHT dht(4,DHT11); //D4 on board numbering system
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!"));
}

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 42


Internet of Things Lab (21EC581) 5th Sem B.E

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() {
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!");
}

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 43


Internet of Things Lab (21EC581) 5th Sem B.E

(b) Displaying temperature and humidity data on Adafruit IO platform

(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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 44


Internet of Things Lab (21EC581) 5th Sem B.E

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:

(a) Interfacing DHT11 with ESP32 (Publisher)


(b) ESP32 (Subscriber)

CODING:

Embedded C

#include <ESP8266WiFi.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>

#define WIFI_SSID "GMIT"


#define WIFI_PASS "DAVANGERE"
#define ADAFRUIT_SERVER "io.adafruit.com"
#define ADAFRUIT_PORT 1883
#define ADAFRUIT_USERNAME "VTU"

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 45


Internet of Things Lab (21EC581) 5th Sem B.E

#define ADAFRUIT_KEY "A1B2C3D4E5F6G7H8"

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, ADAFRUIT_SERVER, ADAFRUIT_PORT,
ADAFRUIT_USERNAME, ADAFRUIT_USERNAME, ADAFRUIT_KEY);

Adafruit_MQTT_Subscribe Temperature = Adafruit_MQTT_Subscribe(&mqtt,


ADAFRUIT_USERNAME "/feeds/Temperature");

void MQTT_connect() {
int8_t ret;

// Stop if already connected.


if (mqtt.connected()) {
return;
}

Serial.print("Connecting to MQTT... ");

while ((ret = mqtt.connect()) != 0) {


Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}

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();

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 46


Internet of Things Lab (21EC581) 5th Sem B.E

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);
}
}
}

(c) Receiving the temperature data on serial monitor of Arduino IDE

RESULT: Program on ESP32 to subscribe to MQTT broker for temperature data and printing
the data has successfully executed.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 47


Internet of Things Lab (21EC581) 5th Sem B.E

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:

(a) Interfacing DHT11 with NodeMCU

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

const char* ssid = "Ravitej";


const char* password = "sharada10";
const int udpPort = 5219; // UDP port to listen on 5219

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 48


Internet of Things Lab (21EC581) 5th Sem B.E

WiFiUDP udp;

void setup() {
Serial.begin(115200);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(1000);
Serial.println("Connecting to WiFi...");
}

Serial.println("WiFi connected.");
Serial.println("UDP server started");
udp.begin(udpPort);
}

void loop() {
char packetBuffer[255]; // buffer to hold incoming packet

int packetSize = udp.parsePacket();


if (packetSize) {
// receive incoming UDP packet
int len = udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}

// check if the received packet is a request for humidity data


if (strcmp(packetBuffer, "GET_H") == 0) {
// replace the following line with your humidity sensor reading logic
float humidity = readHumidity(); // function to read humidity data

// send humidity data back to the client


udp.beginPacket(udp.remoteIP(), udp.remotePort());
udp.printf("Humidity: %.2f%%", humidity);
udp.endPacket();
}
}
delay(1000); // add a small delay to avoid excessive response
}

float readHumidity()
{

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 49


Internet of Things Lab (21EC581) 5th Sem B.E

// Read data from a DHT sensor


float humidity = dht.readHumidity();
return humidity;

(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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 50


Internet of Things Lab (21EC581) 5th Sem B.E

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:

(a) Interfacing DHT11 with NodeMCU

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";

WiFiServer server(80); // Create a server instance on port 80

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 51


Internet of Things Lab (21EC581) 5th Sem B.E

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");

server.begin(); // Start the server


}

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

// Send the humidity data


client.print(response);
client.print(humidity);

// Close the connection


client.stop();
Serial.println("Client disconnected");
}
}

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 52


Internet of Things Lab (21EC581) 5th Sem B.E

(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.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 53


Internet of Things Lab (21EC581) 5th Sem B.E

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:

CRUD operations act as the foundation of database operations.

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.

 Syntax for table creation:

CREATE TABLE Table_Name (ColumnName1 Datatype, ColumnName2


Datatype,..., ColumnNameN Datatype);

where,

 Table_Name is the name that we want to assign to the table.


 Column_Name is the attributes under which we want to store data of the table.
 Datatype is assigned to each column. Datatype decides the type of data that will be stored
in the respective column.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 54


Internet of Things Lab (21EC581) 5th Sem B.E

 Syntax for insertion of data in table:

INSERT INTO Table_Name (ColumnName1,...., ColumnNameN) VALUES (Value


1,....,Value N),....., (Value 1,....,Value N);

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.

 A query to create a table named employee in the database named dbe.

mysql> CREATE TABLE employee(ID INT PRIMARY KEY, First_Name VARCHAR(


20), Last_Name VARCHAR(20), Salary INT, Email_Id VARCHAR(40));

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:

mysql> DESC employee;

will get the following output:

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 55


Internet of Things Lab (21EC581) 5th Sem B.E

Execute the following query to insert multiple records in the employee table:

INSERT INTO employee(ID, First_Name, Last_Name, Salary, Email_Id) VA


LUES(1, "Neeta", "Korade", 59000, "neetak12@gmail.com"), (2, "Sushma
", "Singh", 62000, "sushsingh67@gmail.com"), (3, "Kavita", "Rathod",
27000, "kavitar09@gmail.com"), (4, "Mrunalini", "Deshmukh", 88000,
"mrunald78@gmail.com"), (5, "Swati", "Patel", 34000, "swatip67@gmail
.com"), (6, "Laxmi", "Kadam", 44000, "laxmik14@gmail.com"), (7, "Lal
ita", "Shah", 66000, "lalita45@gmail.com"), (8, "Savita", "Kulkarni"
, 31000, "savitak56@gmail.com"), (9, "Shravani", "Jaiswal", 38000, "
shravanij39@gmail.com"), (10, "Shweta", "Wagh", 20000, "shwetaw03@gm
ail.com");

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.

 Syntax to fetch all the records:

SELECT *FROM TableName;

 Syntax to fetch records according to the condition:

SELECT *FROM TableName WHERE CONDITION;

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 56


Internet of Things Lab (21EC581) 5th Sem B.E

Example :Write a query to fetch all the records stored in the employee table.

Query:mysql> SELECT *FROM employee;

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:

UPDATE Table_Name SET ColumnName = Value WHERE CONDITION;

Example:Write a query to update an employee's last name as 'Bose', whose employee id is 6.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 57


Internet of Things Lab (21EC581) 5th Sem B.E

Query:mysql> UPDATE employee SET Last_Name = "Bose" WHERE ID = 6;

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.

mysql> SELECT *FROM employee;

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 58


Internet of Things Lab (21EC581) 5th Sem B.E

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.

Syntax to delete all the records:

DELETE FROM TableName;

Syntax to delete records according to the condition:

DELETE FROM TableName WHERE CONDITION;

Example:Write a query to delete the employee record from the employee table whose salary is
above 34000.

Query:mysql> DELETE FROM employee WHERE Salary = 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.

mysql> SELECT *FROM employee;

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 59


Internet of Things Lab (21EC581) 5th Sem B.E

To remove all the records from the employee table, we will execute the DELETE query on the
employee table.

mysql> DELETE FROM employee;

 Execute the SELECT query to ensure that all the records are deleted successfully from
the employee table.

mysql> SELECT *FROM employee;

RESULT: Performed basic SQL queries by installing MySQL database on Rsapberry Pi.

Mr.Raviteja Balekai , Asst. Professor, Dept. of ECE, GMIT, Davangere Page 60

You might also like