IoT Lab Manual-01
IoT Lab Manual-01
Overview
Objectives
This course will enable students to:
1. Preparation: To prepare fundamental knowledge of Internet of Things.
2. Core Competence: To equip students with basic foundation of Internet of Things by basics
of Arduino, Raspberry pi and interfacing with different sensors.
Description
1.0 Learning Objectives
Prerequisites
1. C programming language.
2. Basic operation such as creating file, delete, copy, rename etc. should be known.
3. Arduino/Raspberry operation should be understood.
P a g e 1 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
1. C programming.
Resources Required
1. Arduino IDE.
General Instructions
1. Do not handle any equipment without reading the instructions /Instruction manuals.
2. Observe type of sockets of equipment power to avoid mechanical damage.
3. Do not insert connectors forcefully in the Sockets.
4. Strictly observe the instructions given by the Teacher/ Lab Instructor.
5. It is mandatory to come to lab in a formal dress (Shirts, Trousers, ID card, and Shoes for boys).
Strictly no Jeans for both Girls and Boys.
6. It is mandatory to come with observation book and lab record in which previous experiment
should be written in Record and the present lab‘s experiment in Observation book.
7. Observation book of the present lab experiment should be got corrected on the same day .
8. Record should be corrected on the next scheduled lab session.
9. Mobile Phones should be Switched OFF in the lab session.
10. Students have to come to lab in-time. Late comers are not allowed to enter the lab.
11. Prepare for the viva questions. At the end of the experiment, the lab faculty will ask the viva
12. Questions and marks are allotted accordingly.
13. Bring all the required stationery like graph sheets, pencil & eraser, different color pens etc. for
the lab class.
Introduction
P a g e 2 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
The Arduino UNO is a standard board of Arduino. Here UNO means 'one' in Italian. It was named as
UNO to label the first release of Arduino Software. It was also the first USB board released by
Arduino. It is considered as the powerful board used in various projects. Arduino.cc developed the
Arduino UNO board.
Arduino UNO is based on an ATmega328P microcontroller. It is easy to use compared to other boards,
such as the Arduino Mega board, etc. The board consists of digital and analog Input/output pins (I/O),
shields, and other circuits.
The Arduino UNO includes 6 analog pin inputs, 14 digital pins, a USB connector, a power jack, and an
ICSP (In-Circuit Serial Programming) header. It is programmed based on IDE, which stands for
Integrated Development Environment. It can run on both online and offline platforms.
The IDE is common to all available boards of Arduino.
Let's discuss each component in detail.
P a g e 3 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Power LED Indicator- The ON status of LED shows the power is activated. When the
power is OFF, the LED will not light up.
Digital I/O pins- The digital pins have the value HIGH or LOW. The pins numbered from
D0 to D13 are digital pins.
TX and RX LED's- The successful flow of data is represented by the lighting of these
LED's.
AREF- The Analog Reference (AREF) pin is used to feed a reference voltage to the
Arduino UNO board from the external power supply.
Reset button- It is used to add a Reset button to the connection.
USB- It allows the board to connect to the computer. It is essential for the programming of
the Arduino UNO board.
Crystal Oscillator- The Crystal oscillator has a frequency of 16MHz, which makes the
Arduino UNO a powerful board.
Voltage Regulator- The voltage regulator converts the input voltage to 5V.
GND- Ground pins. The ground pin acts as a pin with zero voltage.
Vin- It is the input voltage.
Analog Pins- The pins numbered from A0 to A5 are analog pins. The function of Analog
pins is to read the analog sensor used in the connection. It can also act as GPIO (General
Purpose Input Output) pins.
P a g e 4 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
I2Cs: 1
Flash Memory: 4 MB
SRAM: 64 KB
Clock Speed: 80 MHz
USB-TTL based on CP2102 is included onboard, Enabling Plug n Play
PCB Antenna
Small Sized module to fit smartly inside your IoT projects
PIN CODE
A0 A0
GPIO 16 D0
GPIO 5 D1
GPIO 4 D2
GPIO 0 D3
GPIO 2 D4
GPIO14 D5
GPIO 12 D6
GPIO 13 D7
GPIO 15 D8
GPIO 9 SD2
GPIO10 SD3
GPIO3 Rx
GPIO1 Tx
P a g e 5 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
CONTENTS
Expt.
Name of experiment
No.
To interface LED/Buzzer with Arduino /Raspberry Pi and write a program to ‘turn ON’
1.a
LED for 1 sec after every 2 seconds.
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
To interface the DHT11 sensor with Arduino /Raspberry Pi and write a program to
2.a
print temperature and humidity readings.
To interface OLED with Arduino /Raspberry Pi and write a program to print its
2.b temperature
and humidity readings.
3 To interface the motor using a relay with Arduino /Raspberry Pi and write a program to
‘turn ON’ the motor when a push button is pressed.
4.a Write an Arduino/Raspberry Pi program to interface the Soil Moisture Sensor.
4.b Write an Arduino/Raspberry Pi program to interface the LDR/Photo Sensor.
5 Write a program to interface an Ultrasonic Sensor with Arduino /Raspberry Pi.
Write a program on Arduino/Raspberry Pi to upload temperature and humidity
6
data to thingspeak cloud.
7
Write a program on Arduino/Raspbeii y Pi to retrieve temperature and humidity
data from thingspeak cloud.
P a g e 6 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
9
Write a program on Arduino/Raspbei i y Pi to publish temperature data to the MQTT
broker.
Write a program to create a UDP server on Arduino/Raspberry Pi and respond with
10 humidity
data to the UDP client when requested.
Write a program to create a TCP server on Arduino /Raspberry Pi and respond with
11 humidity
data to the TCP client when requested.
Write a program on Arduino / Raspberry Pi to subscribe to the MQTT broker for
12 temperature
data and print it.
Evaluation Scheme
Assessment Details (both CIE and SEE)
The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam (SEE) is
50%. The minimum passing mark for the CIE is 40% of the maximum marks (20 marks out of 50)
and for the SEE minimum passing mark is 35% of the maximum marks (18 out of 50 marks). A
student shall be deemed to have satisfied the academic requirements and earned the credits allotted
to each subject/ course if the student secures a minimum of 40% (40 marks out of 100) in the sum
total of the CIE (Continuous Internal Evaluation) and SEE (Semester End Examination) taken
together.
Reference
P a g e 8 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
P a g e 9 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Experiments
Experiment Date
Date Planed Marks
No Conducted
01.a
1a. Theory
3
LED, which stands for Light Emitting Diode, is a semiconductor device that emits light when an
electric current passes through it. LEDs are widely used for various applications due to their energy
efficiency, long lifespan, and versatility. Here are some key points about LEDs.
Basic Operation: LEDs work on the principle of electroluminescence. When electrons and holes
(positive counterparts of electrons) recombine within the semiconductor material, they release energy
in the form of photons, which produces light.
P a g e 10 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
1a. Diagram
4
1b.5 Program
int led=13;
void setup()
{
pinMode(led,OUTPUT);
}
void loop()
{
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);
delay(2000);
}
1a. Output
P a g e 11 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
6
___________________________________________________________________________
___________________________________________________________________________
1a. Conclusion
7
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
Experiment Date
Date Planed Marks
No Conducted
01.b
P a g e 12 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
using pinMode() function. INPUT_PULLUP mode is used to enable the Internal PULL-UP
Resistor. The value of Internal PULL-UP resistor of Arduino Uno is about 20-50KΩ.
Here the switch is connected to digital pin 4 (D4) and LED is connected to digital pin 8 (D8)
of Arduino Uno. Thus pin D4 should be declared as INPUT and pin D8 as OUTPUT in the
sketch (program). 10KΩ resistor is used as PULL-UP resistor and a 680Ω resistor is used to
limit the current through the LED.
1b.4 Diagram
1b.5 Program
int button=4;
int led=8;
void setup()
{
pinMode(button,INPUT_PULLUP);
pinMode(led,OUTPUT);
}
voidloop()
{
intbuttonState=digitalRead(button);
if(buttonState==HIGH)
{
digitalWrite(led,LOW);
}
P a g e 13 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
else
{
digitalWrite(led,HIGH);
}
delay(50);
}
1b.6 Output
___________________________________________________________________________
___________________________________________________________________________
1b.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
Experiment Date
Date Planed Marks
No Conducted
02.a
Student will able to interface the DHT11 sensor with Arduino /Raspberry Pi and write a
program to print temperature and humidity readings.
2a. Aim
2
P a g e 14 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
To interface the DHT11 sensor with Arduino /Raspberry Pi and write a program to print
temperature and humidity readings.
2a. Diagram
4
P a g e 15 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
2a. Program
5
#include<SimpleDHT.h>
intpinDHT11=4;
SimpleDHT11dht11(pinDHT11);
bytetemperature=0;
bytehumidity=0;
voidsetup()
{
Serial.begin(9600);
}
voidloop()
{
interr=SimpleDHTErrSuccess;
if((err=dht11.read(&temperature,&humidity,NULL))!
=
SimpleDHTErrSuccess)
{
delay(500);
//return;
}
Serial.print("Temperature:");
Serial.print((String)temperature);
Serial.print("C,Humidity:");
Serial. Println((String)humidity);
delay(1000);
}
P a g e 16 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Temperature = ___________0C
Humidity = _________
2a. Output
6
2a. Conclusion
7
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
P a g e 17 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Experiment Date
Date Planed Marks
No Conducted
02.a
Student will able to interface the OLED with Arduino /Raspberry Pi and write a
program to print temperature and humidity readings.
2b.2 Aim
To interface the OLED with Arduino /Raspberry Pi and write a program to print
temperature and humidity readings.
2b.3 Theory
Most of us would be familiar with the 16×2 Dot matrix LCD display that is used in most of the projects
to display some information to the user. However, these LCD displays have a lot of limitations in what
they can do. In this tutorial, we are going to learn about OLED displays and how to use them with
Arduino. There are many types of OLED displays available in the market and there are lots of ways to
get them working.
There are many variants of this module available in the market, having different resolutions,
communication protocols, or pixel colours.
These OLED modules are driven by SSD1306 IC which is a driver IC for 128x64 Dot Matrix OLED
segments. The SSD1306 has its own controller and supports both SPI and I2C communication
protocols. Hence, there are various OLED modules in the market, some that support only SPI
communication, some that support only I2C communication, and some that support both I2C and SPI
communication. (Different number of pins for different modules)
Since the driver IC supports 128x64 resolution, there are some variants that have a lesser resolution like
128x32.
P a g e 18 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Different modules support different colors like blue, yellow, and white. Some modules support multiple
colors as well. You will need to check the specifications of your display module to know which colors
are supported.
We are using a 4-pin I2C supported 128x64 OLED module similar to the one shown in the above
image.
For more information about OLED and how to use it, refer the topic SSD1306 OLED Display in the
sensors and modules section.
VCC: This is the power pin for the module. A supply of 3.3V or 5V can be provided to this pin to
power the display.
SCL and SDA: These are the serial clock and serial data pins for I2C communication.
2b.4 Diagram
P a g e 19 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
The connections are very simple, connect the GND pin to the GND pin of the Arduino and
VCC to the 5V pin. The SCL is connected to the A5 and the SDA is connected to the A4 pin
of the Arduino.
2b.5 Program
#include<SPI.h>
#include<Wire.h>
#include<Adafruit_GFX.h>
#include<Adafruit_SSD1306.h>
#include<SimpleDHT.h>
intpinDHT11=5;
SimpleDHT11dht11(pinDHT11);
#definescreen_width128
#definescreen_height64
#defineOLED_RESET-1
Adafruit_SSD1306display(screen_width,screen_height);
voidsetup()
{
Serial.begin(9600);
Serial. Println("TEMPERATUREANDHUMIDITY");
display.begin(SSD1306_SWITCHCAPVCC,0x3C);
display.clearDisplay();
}
voidloop()
{
bytetemperature=0;
bytehumidity=0;
P a g e 20 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
interr=SimpleDHTErrSuccess;
if((err=dht11.read(&temperature,&humidity,NULL))!=
SimpleDHTErrSuccess)
{
Serial.print("ReadDHT11failed,err="); Serial.println(err);delay(1000);
return;
}
Serial.print((int)temperature);
Serial.print("*C,");
Serial.print((int)humidity);
Serial.println("H");
display.clearDisplay();
display.setTextSize(1); display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print(" TEMP.&HUMIDITY");
display.setCursor(0,25);
display.print("TEMPERATURE:");
display.setCursor(90,25);
display.print((int)temperature);
display.setCursor(0,50);
display.print("HUMIDITY :");
display.setCursor(90,50);
display.print((int)humidity);
display.display();
}
2b.6 Output
Temperature = ___________0C
2b.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
P a g e 21 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Faculty Signature
Experiment Date
Date Planed Marks
No Conducted
03
Student will able to interface motor using relay with Arduino /Raspberry Pi.
3.2 Aim
To interface motor using relay with Arduino and write a program to ‘turn ON’ motor when
push button is pressed.
3.3 Theory
To control a DC motor with an Arduino and a relay, you will need to connect the relay to
the Arduino using a digital output pin. The relay acts as a switch that can be controlled by
the Arduino to turn the motor on and off.
1. Connect the motor to the relay‘s normally open (NO) and common (COM) terminals.
2. Connect the relay‘s control terminal to a digital pin on the Arduino.
3. In the Arduino code, use the digitalWrite() function to set the relay‘s control pin to HIGH or
LOW to turn the motor on and off.
It’s also important to note that a relay is an electromechanical device that is controlled by an electric
current, and typically requires more current than an Arduino can provide. To avoid damaging
P a g e 22 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
the Arduino, you will need to use an external power source to provide power to the relay and the
motor.
If you want to control the speed of the motor you can use a PWM pin and the analogWrite() function.
With this method you can change the duty cycle of the pin, and that will change the speed of the motor.
A relay is an electrically operated switch that can be used to control a 5V DC motor. The relay uses a
small amount of electrical current to control a larger current flowing to the motor. It typically consists
of an electromagnet that is used to open or close the switch, allowing the current to flow to the motor.
The 5V DC motor can be connected to the relay through the normally open or normally closed
contacts. The relay can be controlled by a microcontroller or other control circuit, which can be used to
turn the motor on or off, or to control its speed or direction.
3.4 Diagram
P a g e 23 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
3.5 Program
int button=6;
int motor=5;
void setup()
{
pinMode(button,INPUT_PULLUP);
pinMode(motor,OUTPUT);
}
Void loop()
{
int buttonState=digitalRead(button);
if(buttonState==HIGH)
{
digitalWrite(motor,LOW);
}
else
{
digitalWrite(motor,HIGH);
}
delay(50);
}
3.6 Output
3.7 Conclusion
Faculty Signature
P a g e 24 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Experiment Date
Date Planed Marks
No Conducted
4a
Student will able to interface the soil moisture sensor with Arduino /Raspberry Pi and
write a program to print percentage soil moisture readings.
4a. Aim
2
To interface the soil moisture sensor with Arduino /Raspberry Pi and write a program to print
percentage soil moisture readings.
4a.3 Theory
The Soil Moisture Sensor is a simple breakout for measuring the moisture in the soil and similar
materials. The soil moisture sensor is pretty straightforward to use. The two large, exposed pads
function as probes for the sensor, together acting as a variable resistor. The more water that is in the soil
means the better the conductivity between the pads will be and will result in lower resistance, and a
higher SIG out.
To get the Soil Moisture Sensor functioning all you will need is to connect the VCC and GND pins to
your Arduino-based device (or compatible development board) and you will receive a SIG out which
will depend on the amount of water in the soil.
P a g e 25 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Passive component
Resistive Sensor
Here, the analog output of soil moisture sensor is processed using ADC. The
moisture content in terms of percentage is displayed on the serial monitor. The
output of the soil moisture sensor changes in the range of ADC value from 0 to
1023.This can be represented as moisture value in terms of percentage using
formula given below.
Analog Output = \frac{ADC Value}{1023}
Moisture in percentage = 100 – (Analog output * 100)
P a g e 26 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
For zero moisture, we get maximum value of 10-bit ADC, i.e. 1023. This, in turn,
gives 0% moisture.
4a.4 Diagram
4a.5 Program
const int sensor_pin = A1; /* Soil moisture sensor O/P pin */
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
}
void loop() {
float moisture_percentage;
int sensor_analog;
sensor_analog = analogRead(sensor_pin);
moisture_percentage = ( 100 - ( (sensor_analog/1023.00) * 100 ) );
Serial.print("Moisture Percentage = ");
Serial.print(moisture_percentage);
//Serial.print("%\n\n");
delay(1000);
}
4a. Output
6
4a. Conclusion
7
P a g e 27 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
Experiment Date
Date Planed Marks
No Conducted
4b
Student will able to interface LDR/Photo sensor with Arduino /Raspberry Pi and write a
program interface it.
4b.2 Aim
To interface LDR/Photo sensor with Arduino /Raspberry Pi and write a program interface it
4b.3 Theory
Working of the circuit: Basically when there is darkness the led will glow and when there is sufficient
light led will stop glowing. This a simple circuit for of interface Arduino uno with LDR sensor.
First of all, we need to knw what is LDR sensor and how it works?
LDR (light dependent resistor) also called photoresistors are responsive to light. Photoresistors are used
to indicate the intensity or the presence or the absence of light. When there is darkness the resistance of
photoresistor increases and when there is sufficient light it dramatically decreases.
P a g e 28 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
LDR (light dependent resistor) which has two terminals. Terminal one is the signal pin which should be
connected according to the code. Another terminal is considered as the ground pin which should be
connected to the ground of the system.
Arduino Uno: The Arduino Uno is a microcontroller board based on the ATmega328. It has 20 digital
input/output pins (of which 6 can be used as PWM outputs and 6 can be used as analog inputs), a 16
MHz resonator, a USB connection, a power jack, an in-circuit system programming (ICSP) header, and
a reset button.
In the simplest terms, a light-emitting diode (LED) is a semiconductor device that emits light when an
electric current is passed through it. Light is produced when the particles that carry the current (known
as electrons and holes) combine together within the semiconductor material. Led has two terminals:
positive and negative.
4b.4 Diagram
4b.5 Program
P a g e 29 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
int led=3;
int ldr=6;
void setup()
{
pinMode (led, OUTPUT);
pinMode(ldr,INPUT_PULLUP);
}
void loop() {
int ldrState=digitalRead(ldr);
if(ldrState==HIGH)
{
digitalWrite (led, HIGH);
}
else
{
digitalWrite (led, LOW);
}
delay(50);
}
4b.6 Output
4b.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
P a g e 30 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Experiment Date
Date Planed Marks
No Conducted
5
Student will able to interface ultrasonic sensor with Arduino /Raspberry Pi and write a
program to print distance measured.
5.2 Aim
To interface ultrasonic sensor with Arduino /Raspberry Pi and write a program to print
distance measured.
5.3 Theory
An Ultrasonic Sensor is a device that measures distance to an object using Sound Waves. It works by
sending out a sound wave at ultrasonic frequency and waits for it to bounce back from the object. Then,
the time delay between transmission of sound and receiving of the sound is used to calculate the
distance.
It is done using the formula Distance = (Speed of sound * Time delay) / 2
We divide the distance formula by 2 because the sound waves travel a round trip i.e from the sensor
and back to the sensor which doubles the actual distance.
P a g e 31 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
The HC-SR04 is a typical ultrasonic sensor which is used in many projects such as obstacle detector
and electronic distance measurement tapes. In this Instructable I'll teach you how to interface the HC-
SC04 with an Arduino Uno.
The HC-SR04 is an ultrasonic ranging module. This economical sensor provides 2cm to 400cm of
non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each HC-
SR04 module includes an ultrasonic transmitter, a receiver and a control circuit.
There are Four Pins on the HC-SR04. They are:
Gnd (Ground)
Trig (Trigger)
Echo (Receive)
The key features to be noted are:
Operating Voltage: 5V DC
A timing diagram of the sensor is shown below. The difference in color of the SIG pin’s waveform
signifies which device is controlling the pin. For example, the Uno controls the pin to send a trigger
pulse (blue) and then the sensor controls the pin to send the HIGH pulse (black).
P a g e 32 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Since the received pulse from the sensor is in time, we have to perform some calculations to convert
the time into distance. The speed of sound travels at 1130 feet per second or 340 meters per second.
First, we need to convert the 340m/s into cm/sec by multiplying by 100 which is 34, 000cm/sec.
Then, convert cm/sec into cm/μsec by dividing by 1E6 which is 0.034cm/μsec.
5.4 Diagram
5.5 Program
#include <NewPing.h>
#define TRIGGER_PIN 9 // Arduino pin connected to trigger pin on the ultrasonic sensor.
P a g e 33 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
#define ECHO_PIN 10 // Arduino pin connected to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).
Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and
maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 9600 baud to see the measured distance.
}
void loop() {
delay(500); // Wait for 50ms.
Serial.print("Distance: ");
Serial.print(sonar.ping_cm()); // Display distance in cm and print result (0 = outside set distance
range).
Serial.println("cm");
}
5.6 Output
Distance = _________cm
5.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
6.0 Upload temperature and humidity data to thingspeak cloud
Experiment Date
Date Planed Marks
No Conducted
6
6.3 Theory
ThingSpeak is an open-source Internet of Things (IoT) application and API that allows users to collect
and store sensor data in the cloud and perform analytics on that data. It allows users to create
“channels” to collect data from multiple sensors, and also has built-in support for visualizing and
P a g e 34 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
analyzing the data. ThingSpeak can be used for a variety of applications, such as monitoring
environmental conditions, tracking the location of assets, and controlling devices remotely. It is
available for free and also has paid subscription plans for additional features and support. The device
that sends the data must be configured with the correct channel information, such as the channel ID and
write API key.
Steps to Connect
Step 1: ThingSpeak Setup for Temperature and Humidity Monitoring
a. For creating your channel on Thingspeak, you first need to Sign up on Thingspeak.
b. In case if you already have an account on Thingspeak, just sign in using your id and password.
6.4 Diagram
P a g e 35 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
6.5 Program
Arduino Program
//SendTemperature&HumiditytoESP8266fromArduino.
#include<SimpleDHT.h>
intpinDHT11=5;
SimpleDHT11dht11(pinDHT11);
bytetemperature=0;
bytehumidity=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Interr=SimpleDHTErrSuccess;
if((err=dht11.read(&temperature,&humidity,NULL))!=
SimpleDHTErrSuccess)
{
delay(500);
//return;
}
Serial.println((String)temperature+","+
P a g e 36 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
(String)humidity);
delay(1500);
}
ESP8266Program
#include<ESP8266WiFi.h>
#include<ThingSpeak.h>
const char*ssid="Dmk"; /*Hotspot name*/
const char*password="abcdef"; /*Hotspot passward*/
WiFiClient client;
unsigned long channelId=2384685;
const char*API_Key="88GOBPCQ26D7XJ7W";/*write API key*/
String recData;
int temperature=0;
int humidity=0;
int nwConnectionLED=2;
void setup()
{
Serial.begin(9600);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
pinMode(nwConnectionLED,OUTPUT);
}
void loop()
{
if(WiFi.status()!=WL_CONNECTED)
{
digitalWrite(nwConnectionLED,HIGH);
while(WiFi.status()!=WL_CONNECTED)
{
WiFi.begin(ssid,password);
delay(5000);
}
}
P a g e 37 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
digitalWrite(nwConnectionLED,LOW);
if(Serial.available())
{
recData=Serial.readString();
temperature=recData.substring(0,
recData.indexOf(",")).toInt();
humidity=recData.substring(recData.indexOf(",")+1,
recData.length()).toInt();
}
ThingSpeak.setField(1,temperature);
ThingSpeak.setField(2,humidity);
int response=ThingSpeak.writeFields(channelId,API_Key);
delay(1000);
}
6.6 Output
Temperature = _________0C
Humidity =_________
6.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
7.0 Retrieve temperature and humidity data from thingspeak cloud
Experiment Date
Date Planed Marks
No Conducted
7
7.3 Theory
To read values from Thingspeak we need to upload some data in real time, to do this, first upload
temperature and humidity data to Thingspeak using previous experiment using NodeMCU 8266.
Installing the ThingSpeak Library
1. To send or receive sensor readings to ThingSpeak, we’ll use the ThingSpeak Arduino library.
Go to Sketch > Include Library > Manage Libraries… and search for “ThingSpeak” in the
Library Manager. Install the ThingSpeak library by MathWorks.
OR
2. Upload the Library from drive Go to Sketch > Include Library > Add ZIP Library.…
> open to install Library
7.4 Diagram
P a g e 39 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
7.5 Program
/ESP8266 code to recceive data from ThingSpeak Cloud.
#include<ThingSpeak.h>
#include<ESP8266WiFi.h>
const char* ssid ="Dmk";
const char* password = "dmkumbhar12345";
unsigned long channelNumber = 2384685;
const char* readAPIKey = "YTZYEK2GPJWRGRL1";
const int fieldNumber1 = 1;
const int fieldNumber2 = 2;
WiFiClient client;
void setup()
{
Serial.begin(9600);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
while(WiFi.status() != WL_CONNECTED)
{
P a g e 40 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
delay(500);
Serial.println("Wi-Fi Connecting......");
}
Serial.println("Wi-Fi connected successfully");
}
void loop()
{
long temp = ThingSpeak.readLongField(channelNumber, fieldNumber1, readAPIKey);
int statusCode = 0;
statusCode = ThingSpeak.getLastReadStatus();
if(statusCode == 200)
{
Serial.print("Temperature: ");
Serial.println(temp);
}
else
{
Serial.println("Unable to read channel/No internet connection");
}
delay(100);
long humidity = ThingSpeak.readLongField(channelNumber, fieldNumber2, readAPIKey);
statusCode = ThingSpeak.getLastReadStatus();
if(statusCode == 200)
{
Serial.print("Humidity: ");
Serial.println(humidity);
}
else
{
Serial.println("Unable to read channel/No internet connection");
}
delay(100);
}
P a g e 41 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
7.6 Output
Temperature = _________0C
Humidity =_________
7.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
P a g e 42 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Experiment Date
Date Planed Marks
No Conducted
8
These are popular microcontrollers with WiFi capabilities, allowing you to connect to the
internet and communicate with the Telegram bot.
Telegram Bot:
This is a bot that you create on Telegram. It acts as an intermediary, receiving your commands
and sending them to your microcontroller.
P a g e 43 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Libraries:
Libraries like Telepot (Python) or Telegram Bot Library for Arduino simplify the process of
interacting with Telegram and your microcontroller.
Step to create Telegram Bot
2. Find the Botfather in the result and open it and click ok start.
5. Set the username for your bot (it must me end with bot)
6. You will get the token id, copy this ID and paste in Notepad
8.4 Diagram
8.5 Program
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
P a g e 44 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
#include <UniversalTelegramBot.h>
// Wi-Fi Credentials
const char* ssid = "Smart Bin";
const char* password = "7899262658";
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // LED initially OFF
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500); Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
secured_client.setInsecure(); // For Telegram SSL
}
ledState = false;
bot.sendMessage(chat_id, "LED is OFF", "");
}
else if (text == "/status") {
bot.sendMessage(chat_id, ledState ? "LED is ON" : "LED is OFF", "");
}
else {
bot.sendMessage(chat_id, "Available Commands:\n/on\n/off\n/status", "");
}
}
void loop() {
if (millis() - lastCheck > interval) {
int newMsgs = bot.getUpdates(bot.last_message_received + 1);
while (newMsgs) {
for (int i = 0; i < newMsgs; i++) {
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
handleMessage(text, chat_id);
}
newMsgs = bot.getUpdates(bot.last_message_received + 1);
}
lastCheck = millis();
}
}
8.6 Output
Status
LED ON
LED OFF
8.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
P a g e 46 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Experiment Date
Date Planed Marks
No Conducted
9
9.3 Theory
MQTT stands for Message Queuing Telemetry Transport. MQTT is a simple messaging protocol,
designed for constrained devices with low bandwidth. So, it’s the perfect solution to exchange data
between multiple IoT devices.
Devices publish messages on a specific topic. All devices that are subscribed to that topic receive the
message.
In a publish and subscribe system, a device can publish a
message on a topic, or it can be subscribed to a particular
topic to receive messages
The MQTT broker is responsible for receiving all
messages, filtering the messages, deciding who is interested
in them, and then publishing the message to all subscribed
clients.
The MQTT broker is the central point of communication, and it is in charge of dispatching all messages
between the senders and the rightful receivers. A client is any device that connects to the broker and
can publish or subscribe to topics to access the information. A topic contains the routing information
for the broker. Each client that wants to send messages publishes them to a certain topic, and each
client that wants to receive messages subscribes to a certain topic. The broker delivers all messages
with the matching topic to the appropriate clients.
ThingSpeak™ has an MQTT broker at the URL mqtt3.thingspeak.com and port 1883. The ThingSpeak
broker supports both MQTT publish and MQTT subscribe.
In this Experiment, we will create a setup that allows a NODE MCUESP8266 board to send data to
another MCU ESP 8266, using MQTT (Message Queuing Telemetry Transport). The sender device,
P a g e 47 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
simply publishes a message to a broker service, which then can be subscribed to by a receiver device.
The data we will send consists of readings from a DHT11 sensor, including temperature and humidity
data, from a NODE MCU ESP8266 to another NODE MCU. This experiment utilizes the broker
test.mosquitto.org, an open-source service that is free for anyone to use.
9.4 Diagram
9.5 Program
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
// WiFi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to WiFi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Connecting to MQTT broker...");
if (client.connect("NodeMCUClient")) { // Client ID
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
P a g e 49 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Serial.begin(115200);
dht.begin();
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Read temperature as Celsius
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
9.6 Output
Temperature = _________0C
9.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
P a g e 50 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Experiment Date
Date Planed Marks
No Conducted
10
10.3 Theory
User Datagram Protocol (UDP) is a network communication protocol that operates at the transport
layer of the Internet Protocol (IP) suite. It is a connectionless and lightweight protocol designed for fast
and efficient data transmission, but it does not provide the same level of reliability and error-checking
as Transmission Control Protocol (TCP).
UDP is a lightweight, connectionless, and fast protocol that prioritizes low-latency data transmission
over reliability. It is suitable for applications where occasional packet loss or out-of-order delivery can
be tolerated, and real-time communication is essential. However, for applications that require
guaranteed delivery and error recovery, TCP is a better choice.
10.4 Diagram
P a g e 51 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
10.5 Program
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <DHT.h>
#define DHTPIN D4
#define DHTTYPE DHT11
float temp;
char packet[1024];
const char* ssid = "choukimathwifi";
const char* password ="12345678";
const char* udpServerIP = "192.168.0.245"; // Replace <IP> with the actual IP address of your computer
unsigned int port = 9000;
void loop() {
P a g e 52 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Udp.beginPacket(udpServerIP, port);
UDP_IP = "192.168.0.245"
UDP_PORT = 9000
while True:
received_data, addr = sock.recvfrom(1024)
print(received_data)
10.6 Output
Temperature = _________0C
10.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
P a g e 53 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
Experiment Date
Date Planed Marks
No Conducted
11
A TCP server is a program that listens for incoming connections from clients and responds to their
requests. When a client connects to the server, it sends a request for data. The server then sends back
the requested data to the client. In IoT, TCP servers are used to provide access to data from sensors and
other devices
In the context of the Internet of Things (IoT), a TCP server refers to a network service running on a
device or gateway that listens for incoming TCP connections from other IoT devices or clients
In the context of IoT, TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are
two protocols of the Transport Layer that are used to transmit data between devices over a network.
TCP is a connection-oriented protocol that provides reliable delivery services by keeping track of the
segments being transmitted or received by assigning numbers to every single one of them. It also
implements an error control mechanism for reliable data transfer and takes into account the level of
congestion in the network .
On the other hand, UDP is a connectionless protocol that is used for simple request-response
communication when the size of data is less and hence there is lesser concern about flow and error
control. It is a suitable protocol for multicasting as UDP supports packet switching. Normally used for
real-time applications which cannot tolerate uneven delays between sections of a received message.
In summary, TCP is more reliable but slower than UDP, while UDP is faster but less reliable than TCP
11.5 Program
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTPIN D4
#define DHTTYPE DHT11
float temp;
const char* ssid = "choukimathwifi";
const char* password = "12345678";
const char* tcpServerIP = "192.168.1.6"; // Replace <IP> with the actual IP address of your computer
unsigned int port = 80; //most commonly used protocol port in TCP
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.begin(ssid, password);
P a g e 55 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
void loop() {
client.connect(tcpServerIP, port);
// Read temperature from DHT sensor and convert it to a string
temp = dht.readTemperature();
// Confirm value in serial monitor
Serial.print("Temperature in degree Celsius: ");
Serial.println(temp);
11.6 Output
Temperature = _________0C
11.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
Experiment Date
Date Planed Marks
No Conducted
12
12.2 Aim
Write a program on Arduino to subscribe to MQTT broker for temperature data and print it.
12.3 Theory
The MQTT protocol was first introduced in 1999, as a light-weight publish and subscribe system. It is
P a g e 56 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
particularly useful for devices with low-bandwidth, where we can send commands, sensor values or
messages over the Internet with little effort.
A basic
explanation
on how it
works is
that a node,
for example
and
Arduino
with a Wi-
Fi module, sends a payload to a broker. A broker is a kind of "middle-point" server, that essentially
stores payloads sent to it, in something called topics. A topic, is a definition of what type of data it
contains, it could for example be "humidity" or " temperature". Another node can then subscribe to this
information, from the broker, and voilà, data has been moved from Node A to Node B over the
Internet.
One way to protect the data is for example, by using a token, something that is quite common when
working with various IoT services. For instance, if we are publishing something to a broker, anyone
that has the URL, e.g. randombroker.org/randomtopic can subscribe to it. But if we add a unique
token on both sides, they wouldn't be able to. These tokens could for example be Z6ACuLwr5T,
which is not exactly something easy to guess.
12.4 Diagram
P a g e 57 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
12.5 Program
#include <ESP8266WiFi.h>
#include <ArduinoMqttClient.h>
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
void setup() {
Serial.begin(9600);
while(WiFi.status()!=WL_CONNECTED){
Serial.println("connecting......");
delay(500);
P a g e 58 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
}
Serial.println("Wifi connected successfully");
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
// subscribe to a topic
mqttClient.subscribe(topicT);
mqttClient.subscribe(topicH);
void loop() {
mqttClient.poll();
delay(1000);
}
void onMqttMessage(int messageSize) {
P a g e 59 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in
Control systems Lab Manual / Semester – 4th
Electronics and Communication Engineering Department,
12.6 Output
Temperature = _________0C
Humidity =_________
12.7 Conclusion
___________________________________________________________________________
___________________________________________________________________________
Faculty Signature
P a g e 60 | 60
____________________________________________________________________________________________________
_Nidasoshi-591 236, Tal :Hukkeri, Dist: Belagavi, Karnataka, India.
Phone: +91-8333-278887, Fax: 278886, Web: www.hsit.ac.in, E-mail: principal@hsit.ac.in