R.M.K.
ENGINEERING COLLEGE
RSM NAGAR, KAVARAIPETTAI – 601 206
(An Autonomous Institution)
Department of Electronics and Electronics Engineering
Laboratory Manual
20EE512 - EMBEDDED SYSTEMS AND IOT LABORATORY
Regulation - 2020
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
OBJECTIVE:
To impart practical knowledge of embedded systems and Internet of Things to develop
real-time IoT based projects.
LIST OF
EXPERIMENTS
1. Study of Arduino UNO and Arduino IDE software
2. Interfacing LED/buzzer with Arduino/Raspberry Pi
3. Interfacing push button/ digital sensor (IR/LDR) Arduino/Raspberry Pi
4. Interfacing DHT11 sensor, OLED with Arduino/Raspberry Pi
5. Interfacing motor with Arduino/Raspberry Pi
6. Interfacing Bluetooth with Arduino/Raspberry Pi
7. Uploading/Retrieving temperature and humidity data to ThingSpeak cloud through Arduino
UNO sketch .
8. Perform basic SQL queries installing MySQL database on Raspberry Pi.
9. Program Arduino/Raspberry Pi to subscribe and publish data to MQTT broker.
10. Program to create TCP server on Arduino/Raspberry Pi and respond with TCP client.
11. Program to create UDP server on Arduino/Raspberry Pi and respond with UDP client.
12. GPS Module interfacing with Raspberry Pi
13. RFID Module interfacing with Raspberry Pi
14. IOT Based Home Automation using Raspberry Pi
Total:30 periods
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #1 – Familiarization with Arduino and Perform Necessary Software Installation
Aim:
To study Arduino UNO hardware and Arduino IDE software.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Arduino UNO 1
2 Computer with Arduino IDE 1
3 USB Cable 1
Theory:
Internet of Things (IoT):
The Internet of Things - (IoT) describes physical objects (Or groups of such objects) that are embedded with
sensors, processing ability, software, and other technologies, and that connect and exchange data with other devices
and systems over the Internet or other communications networks.
The field has evolved due to the convergence of multiple technologies, including ubiquitous computing, commodity
sensors, increasingly powerful embedded systems, and machine learning. Traditional fields of embedded systems,
wireless sensor networks, control systems, automation (Including home and building automation), independently and
collectively enable the Internet of things. In the consumer market, IoT technology is most synonymous with products
pertaining to the concept of the "Smart Home", including devices and appliances (Such as lighting fixtures,
thermostats, home security systems and cameras, and other home appliances) that support one or more common
ecosystems, and can be controlled via devices associated with that ecosystem, such as smartphones and smart
speakers. The IoT can also be used in healthcare systems.
Microcontroller:
Basically, any product or device that interacts with its user has a microcontroller buried inside. Microcontrollers
are "Special Purpose Computers”, that differs from the PC on:
1. Microcontrollers are "Embedded" inside some other device (Often a consumer product) so that they can
control the features or actions of the product.
2. Microcontrollers are dedicated to one task and run one specific program. The program is stored in ROM
(Read Only Memory) and generally does not change.
3. Microcontrollers are often low-power devices. A desktop computer is almost always plugged into a wall
socket and might consume 50 watts of electricity. A battery-operated microcontroller might consume 50
milliwatts.
Arduino UNO:
An Arduino board consists of an Atmel 8-bit AVR microcontroller with complementary components that facilitate
programming and incorporation into other circuits. It's an open-source physical computing platform based on a
simple microcontroller board, and a development environment for writing software for the board.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
General Pin Functions:
LED: There is a built-in LED driven by digital pin 13. When the pin is high value, the LED is ON, when the pin is
low, it is OFF.
VIN: The input voltage to the Arduino board when it is using an external power source (As opposed to 5 volts from
the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying
voltage via the power jack, access it through this pin.
5V: This pin outputs a regulated 5V from the regulator on the board. The board can be supplied with power either
from the DC power jack (7 - 20V), the USB connector (5V), or the VIN pin of the board (7 - 20V). Supplying voltage
via the 5V or 3.3V pins bypasses the regulator, and can damage the board.
3V3: A 3.3V supply generated by the on-board regulator. Maximum current draw is 50 mA.
GND: Ground pins.
Reset: Typically used to add a reset button to shields that block the one on the board.
Special Pin Functions:
Each of the 14 digital pins and 6 analog pins on the Uno can be used as an input or output, under software
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
control (Using pinMode(), digitalWrite(), and digitalRead() functions). They operate at 5 volts. Each pin can
provide or receive 20 mA as the recommended operating condition and has an internal pull-up resistor
(Disconnected by default) of 20-50K ohm. A maximum of 40mA must not be exceeded on any I/O pin to avoid
permanent damage to the microcontroller. The Uno has 6 analog inputs, labeled A0 through A5; each provides 10
bits of resolution (That is, 1024 different values). By default, they measure from ground to 5 volts, though it is
possible to change the upper end of the range using the AREF pin and the analogReference() function.
In addition, some pins have specialized functions:
Serial / UART: Pins 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins are
connectedto the corresponding pins of the ATmega8U2 USB-to-TTL serial chip.
External interrupts: Pins 2 and 3. These pins can be configured to trigger an interrupt on a low value, a rising or
falling edge, or a change in value.
PWM (Pulse Width Modulation): Pins 3, 5, 6, 9, 10, and 11. Can provide 8-bit PWM output with the analogWrite()
function.
SPI (Serial Peripheral Interface): Pins 10 (SS), 11 (MOSI), 12 (MISO), and 13 (SCK). These pins
support SPI communication using the SPI library.
I²C (Inter IC Communication): Pin SDA (A4) and pin SCL (A5). Support communication using the Wire library.
AREF (Analog Reference): Reference voltage for the analog inputs.
Arduino IDE Software:
The Arduino Integrated Development Environment (IDE) is a cross-platform application (for Windows,
macOS, Linux) that is written in functions from C and C++.[3] It is used to write and upload programs to
Arduinocompatible boards, but also, with the help of third-party cores, other vendor development boards.
Procedure to Install Arduino IDE software:
1. Visit: [Link]
2. Download & install the Arduino environment (IDE) for Windows, Mac, or Linux.
3. Extract the ZIP file. (The extracted folder will contain both the Arduino program itself and also the drivers
that allow the Arduino to be connected to your computer by a USB cable).
4. Connect the board to your computer via the UBS cable.
5. The power light on the LED will light up and you may get a “Found New Hardware” message from Windows.
6. Ignore this message and cancel any attempts that Windows makes to try and install drivers automatically for
you.
7. Open Device Manager
8. Under the section “Other Devices” you should see an icon for “Unknown Device”, write click on it and press
update driver software.
9. Select the option: “Search Automatically for Drivers”.
10. You should be done by successfully installing the Arduino driver.
11. Launch the Arduino IDE.
12. Select your board (Tools -> Board ->UNO)
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Result:
Thus, the Arduino UNO Hardware and Arduino IDE software were studied.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise # 2a – Interfacing LED With Arduino UNO
Aim:
To interface LED with Arduino UNO.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Arduino UNO 1
2 Computer with Arduino IDE 1
3 USB Cable 1
4 LED 1
5 330Ω Resistor 1
6 Breadboard 1
7 Jumper Wires As Required
Theory:
Light Emitting Diode (LED) is a widely used standard source of light in electrical equipment. It has a
widerange of applications ranging from your mobile phone to large advertising billboards.
Here, an LED is connected to one of Arduino’s digital pins via 330Ω resistor. Whenever the respective pin is set
HIGH, current flows via LED and hence it glows.
Circuit Diagram:
Code:
#define LED 13
void setup()
{
pinMode(LED, OUTPUT);
}
void loop()
{
digitalWrite(LED, HIGH);
delay(1000); digitalWrite(LED,
LOW); delay(1000);
}
Result:
Hence, a LED is interfaced with Arduino UNO and is made to blink successfully.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #2b – Interfacing Buzzer with Arduino UNO
Aim:
To interface a buzzer with Arduino UNO and write program to turn ON the buzzer at different frequencies.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Arduino UNO 1
2 Computer with Arduino IDE 1
3 USB Cable 1
4 Buzzer 1
5 Breadboard 1
6 Jumper Wires As Required
Theory:
A piezo electric buzzer is a simple device which can generate beeps and tones. Working principle of the
device is piezoelectric effect. The main component of this device is a piezo crystal, a special material that change
shape when a voltage applied to it.
Piezo buzzers are used for making beeps alarms and tones. They can be used in alarm systems, for keypad feedback,
or some games. Light weight, simple construction and low price make it usable in various applications like car/truck
reversing indicator, computers, call bells etc.
Simply change the frequency of the voltage sent to the piezo and it will start generating sounds by changing shape
very quickly.
Circuit Diagram:
Code:
#define BUZZER 5
#define FREQUENCY 200
void setup()
{
pinMode(BUZZER, OUTPUT);
}
void loop()
{
tone(BUZZER,FREQUENCY);
delay(500);
noTone(BUZZER);
delay(500);
}
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Result:
Hence, buzzer is successfully interfaced with Arduino UNO and is turned ON with different frequencies.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #3a – Interfacing digital sensor (IR/LDR) Arduino
Aim:
To interface a LDR with Arduino UNO and write program to turn ON the LED based on LDR value.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Arduino UNO 1
2 Computer with Arduino IDE 1
3 USB Cable 1
4 LDR 1
5 LED 1
6 Resistor 10 Kohm
7 Breadboard 1
8 Jumper Wires As Required
Theory:
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.
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.
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
Circuit Diagram:
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Code:
// Interfacing Arduino uno with LDR
sensor const int ledPin = 5; // digital pin 5
const int ldrPin = A0; // analog pin 0
void setup() { // The setup() function will only run once, after each powerup or reset of the Arduino board.
[Link](9600);
pinMode(ledPin, OUTPUT); // Here LED is determined as an output or an
indicator. pinMode(ldrPin, INPUT); // Here LDR sensor is determined as input.
}
void loop() { // Void loop is ran again and again and contains main code.
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <= 200) {digitalWrite(ledPin, HIGH); // If LDR senses darkness led pin high that means led will
glow. [Link]("Darkness over here,turn on the LED :");
[Link](ldrStatus);
} else {
digitalWrite(ledPin, LOW); // If LDR senses light led pin low that means led will stop
glowing. [Link]("There is sufficient light , turn off the LED : ");
[Link](ldrStatus);
}
}
Result:
Hence, LDR is successfully interfaced with Arduino UNO and LED is turned ON based on LDR value.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #4a – Interfacing DHT11 with Arduino and Print Temperature and Humidity
Aim:
To interface DHT11 temperature sensor with Arduino UNO and write a program to print temperature and
humidity.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Arduino UNO 1
2 Computer with Arduino IDE 1
3 USB Cable 1
4 DHT11 Temperature sensor 1
5 Breadboard 1
6 Jumper Wires As Required
Theory:
DHT11 is a low-cost digital sensor for sensing temperature and humidity. This sensor can be easily
interfaced with any micro-controller such as Arduino, Raspberry Pi etc. to measure humidity and temperature
instantaneously. DHT11 humidity and temperature sensor is available as a sensor and as a module. The difference
between this sensor and module is the pull-up resistor and a power-on LED.
DHT11 is a relative humidity sensor. To measure the surrounding air this sensor uses a thermistor and for measuring
temperature this sensor uses a Negative Temperature coefficient thermistor, which causes a decrease in its resistance
value with increase in temperature. To get larger resistance value even for the smallest change in temperature, this
sensor is usually made up of semiconductor ceramics or polymers.
The temperature range of DHT11 is from 0 to 50 degree Celsius with a 2-degree accuracy. Humidity range of this
sensor is from 20 to 80% with 5% accuracy. The sampling rate of this sensor is 1Hz. That is, it gives one reading
for every second. DHT11 is small in size with operating voltage from 3 to 5 volts. The maximum current used
while measuring is 2.5mA.
Circuit Diagram:
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Code:
Procedure:
1. Make connections as per the circuit diagram.
2. Open the Arduino IDE in your computer and write the above sketch.
3. Compile the sketch and upload it to Arduino UNO.
4. Once uploaded, open the Serial Monitor.
5. The temperature and humidity will be printed there.
Output:
Result:
Thus, the DHT11 temperature and humidity sensor is interfaced with Arduino UNO and the temperature and
humidity data is printed successfully.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #4b – Interfacing OLED Display with Arduino and Print Temperature and Humidity
Aim:
To interface OLED with Arduino and print temperature and humidity readings on it.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Arduino UNO 1
2 Computer with Arduino IDE 1
3 USB Cable 1
4 DHT11 Temperature and Humidity Sensor 1
5 OLED Display 1
6 Breadboard 1
7 Jumper Wires As Required
Theory:
OLED stands for Organic Light Emitting Diode. The OLED displays are very small and have high resolution.
These displays have no back light and they make their own light. That’s why these are very low power devices.
When the voltage is applied to the OLED. The current flows from the cathode to anode through the organic layers of
the OLED. The cathode gives the electrons to the emissive layer of organic molecules and the anode removes
electrons from the conductive layer of organic molecules.
At the boundary between the conductive and emissive layer, electron holes are created. These holes are filled by the
electrons and the OLED emits light. The colour of the OLED depends upon the organic molecules used.
DHT11 is a relative humidity sensor. To measure the surrounding air this sensor uses a thermistor and for measuring
temperature this sensor uses a Negative Temperature coefficient thermistor, which causes a decrease in its resistance
value with increase in temperature. To get larger resistance value even for the smallest change in temperature, this
sensor is usually made up of semiconductor ceramics or polymers.
Circuit Diagram:
Code:
#include <Wire.h> #include
<Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
#define SCREEN_WIDTH_PX 128
#define SCREEN_HEIGHT_PX 64
#define DHT_PIN 2
Adafruit_SSD1306 oled(SCREEN_WIDTH_PX, SCREEN_HEIGHT_PX, &Wire, -1);DHT
dht(DHT_PIN, DHT11);
float temperature, humidity;void
setup()
{
// Start OLED.
[Link](SSD1306_SWITCHCAPVCC, 0x3C);
// Clear display.
[Link]();
// Set text size.
[Link](1.75);
// Set text colour.
[Link](WHITE);
// Start DHT11.
[Link]();
}
void loop()
{
humidity = [Link](); temperature =
[Link]();
[Link]();
// Print humidity.
[Link](10, 10);
[Link]("Humidity: ");
[Link](20, 10);
[Link](humidity);
// Print temperature.
[Link](10, 20);
[Link]("Temperature: ");
[Link](23, 30);
[Link](temperature);
// Print it.
[Link]();
delay(1000);
}
Procedure:
1. Make connections as per the circuit diagram.
2. Open the Arduino IDE in your computer and write the above sketch.
3. Compile the sketch and upload it to Arduino UNO.
4. Arduino UNO will now print the temperature and humidity data fetched from DHT11 on OLED display.
Result:
Thus, the OLED display is interfaced with Arduino and the temperature and humidity data fetched from
DHT11 is printed on OLED display.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #5 – Interfacing Motor with Arduino Via Relay and Control It with PushButton
Aim:
To interface motor with Arduino via relay module and turn it ON or OFF with a push button.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Arduino UNO 1
2 Computer with Arduino IDE 1
3 USB Cable 1
4 Relay Module 1
5 Push Button 1
6 Breadboard 1
7 Jumper Wires As Required
Theory:
The pushbutton is a component that connects two points in a circuit when you press it. When the pushbutton
is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to 5 volts
(through the pull-up resistor) and we read a HIGH. When the button is closed (pressed), it makes a connection
between its two legs, connecting the pin to ground, so that we read a LOW. (The pin is still connected to 5 volts, but
the resistor in-between them means that the pin is "closer" to ground.)
A relay is an electrically operated switch. It consists of a set of input terminals for a single or multiple control signals,
and a set of operating contact terminals. The switch may have any number of contacts in multiple contact forms, such
as make contacts, break contacts, or combinations thereof.
Circuit Diagram:
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Code:
#define PUSH_BUTTON 8 // Use push button with pull-down resistor.#define RELAY
7
bool motorState = false; void
setup()
{
pinMode(RELAY, OUTPUT);
pinMode(PUSH_BUTTON, INPUT);
}
void loop()
{
if(digitalRead(PUSH_BUTTON) == HIGH)
{
if(motorState == false) // If already OFF, turn it ON.
{
digitalWrite(RELAY, HIGH);
motorState = true;
}
else
{
digitalWrite(RELAY, LOW); // Else, turn it [Link] =
false;
}
delay(100); // Delay to avoid debounce of push button.
}
}
Procedure:
1. Make connections as per the circuit diagram.
2. Open the Arduino IDE in your computer and write the above sketch.
3. Compile the sketch and upload it to Arduino UNO.
4. Once uploaded, press the button. The relay turns ON and hence, the motor runs.
5. If the button is pressed again, the relay turns OFF and hence, the motor stops running.
Result:
Thus, motor is successfully interfaced with Arduino via relay module and is turned ON and OFF with push
button.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #6 – Interface Bluetooth Module HC-05 with Arduino and Send Temperature and
humidity Data
Aim:
To interface Bluetooth module HC-05 with Arduino and send temperature and humidity data.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Arduino UNO 1
2 Computer with Arduino IDE 1
3 USB Cable 1
4 HC-05 Bluetooth Module 1
5 Smartphone with Bluetooth Terminal HC-05 Application 1
6 Smartphone 1
7 Breadboard 1
8 Jumper Wires As Required
Theory:
HC05 module is a Bluetooth module using serial communication, mostly used in electronics projects. HC-05
is a Bluetooth SPP (Serial Port Protocol) module designed for wireless communication. It can also be operated as a
master or slave configuration.
Circuit Diagram:
Code:
#include <SoftwareSerial.h>
#include "DHT.h"
#define DHT_PIN 2
SoftwareSerial bluetooth(2, 3); // Rx and Tx respectively. DHT dht(DHT_PIN,
DHT11);
float temperature, humidity;void
setup()
{
pinMode(LED, OUTPUT);
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
[Link](9600);
[Link](9600);
[Link]("Ready to connect. Defualt password: 1234 or 0000.");
}
void loop()
{
if([Link]())
{
temperature = [Link](); humidity =
[Link]();
if(isnan(temperature) || isnan(humidity))
{
[Link]("ERROR: Unable to read temperature and humidity data.");
}
else
{
[Link](temperature); [Link](humidity);
}
delay(1000);
}
}
Procedure:
1. Make connections as per the circuit diagram.
2. Open the Arduino IDE in your computer and write the above sketch.
3. Compile the sketch and upload it to Arduino UNO.
4. Connect to HC-05 via your smartphone and send data to it with application named “Bluetooth Terminal HC-
05.” Click here to download the app.
5. Once downloaded, setup the application to receive data.
6. Now, the Arduino UNO will fetch temperature and humidity data from DHT11 and send it to smartphone via
Bluetooth.
Result:
Hence, Bluetooth module HC-05 is interfaced successfully with Arduino UNO and data is sent to smartphone
via Bluetooth.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #7a – Uploading Temperature and Humidity to ThingSpeak Cloud
Aim:
To write a program to send temperature and humidity data to ThingSpeak cloud.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 ESP8266 1
2 DHT11 Temperature Sensor 1
3 Computer with Arduino IDE and Internet Connection 1
4 USB Cable 1
5 Breadboard 1
6 Jumper Wires As Required
Theory:
ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live
data streams in the cloud. You can send data to ThingSpeak from your devices, create instant visualization of live
data, and send alerts.
Here, we use ESP8266 and DHT11 temperature and humidity sensor. The ESP8266 connects to a wireless access
point after which it starts fetching temperature and humidity data and logging it to ThingSpeak cloud.
Circuit Diagram:
Code:
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
#include "DHT.h"
#define _SSID "SSID" #define
PASSWORD "PASSWORD"
#define CHANNEL_KEY 0000
#define API_KEY "API_KEY"
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
#define DHT_PIN 12
WiFiClient client;
DHT dht(DHT_PIN, DHT11);
int statusCode;
float temperature, humidity;
void setup()
{
[Link](client);
delay(5000); // Wait for the user to open Serial [Link](9600);
[Link](9600);
}
void loop()
{
// Incase WiFi gets disconnected.
if([Link]() != WL_CONNECTED)
{
[Link]("Connecting to: ");
[Link](_SSID); while([Link]() !=
WL_CONNECTED)
{
[Link](_SSID, PASSWORD);
[Link](".");
delay(5000);
}
[Link]("\nConnected successfully.\n");
}
// Read temperature and humidity from [Link]
= [Link](); humidity = [Link]();
if(isnan(temperature) || isnan(humidity))
{
[Link]("ERROR: Unable to fetch data from DHT11.");
}
else
{
[Link]("Humidity: ");
[Link](humidity);
[Link]("%\tTemperature: ");
[Link](temperature);
[Link]("°C");
// Send data.
[Link](1, temperature);
[Link](2, humidity);
// writeField(channelKey, fieldNumber, data, writeApiKey); statusCode =
[Link](CHANNEL_KEY, API_KEY); if(statusCode == 200)
{
[Link]("Channel update successful.");
}
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
else
{
[Link]("ERROR: ");
[Link](statusCode);
}
}
delay(10000);
}
Output:
Result:
Thus, the temperature and humidity data are logged to ThingSpeak server and can be viewed with the help
of any devices provided the device has active internet connection.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Excersie #7b – Retrieving Temperature and Humidity from ThingSpeak Cloud
Aim:
To write a program to retrieve temperature and humidity data to ThingSpeak cloud.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 ESP8266 1
2 Computer with Arduino IDE and Internet Connection 1
3 USB Cable 1
7 Breadboard 1
8 Jumper Wires As Required
Theory:
ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live
data streams in the cloud. You can send data to ThingSpeak from your devices, create instant visualization of live
data, and send alerts.
Here, we use ESP8266 and DHT11 temperature and humidity sensor. The ESP8266 connects to a wireless access
point after which it starts fetching temperature and humidity data and logs it to ThingSpeak cloud.
Code:
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
#define _SSID "SSID" #define
PASSWORD "PASSWORD"
#define CHANNEL_KEY 0000
#define API_KEY "API_KEY"
WiFiClient client; int
statusCode;
float temperature, humidity;
void setup()
{
[Link](client);
delay(5000); // Wait for the user to open Serial [Link](9600);
}
void loop()
{
// Incase WiFi gets disconnected.
if([Link]() != WL_CONNECTED)
{
[Link]("Connecting to: ");
[Link](SSID); while([Link]() !=
WL_CONNECTED)
{
[Link](SSID, PASSWORD);
[Link](".");
delay(5000);
}
[Link]("\nConnected successfully.\n");
}
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
// Retreive data.
temperature = [Link](CHANNEL_KEY, 1, API_KEY); humidity
= [Link](CHANNEL_KEY, 2, API_KEY); if(isnan(temperature)
|| isnan(humidity))
{
[Link]("ERROR: Unable to fetch data from ThingSpeak.");
}
else
{
statusCode = [Link]();
if(statusCode == 200)
{
[Link]("Humidity: ");
[Link](humidity);
[Link]("%\tTemperature: ");
[Link](temperature);
[Link]("°C");
}
else
{
[Link]("ERROR: ");
[Link](statusCode);
}
}
delay(10000);
}
Output:
Result:
Thus, data is retrieved from ThingSpeak Cloud and printed on the Serial Monitor.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Excersie #8 – Perform basic SQL queries installing MySQL database on Raspberry Pi
Aim:
To perform basic SQL queries by installing MySQL database on Raspberry Pi.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Raspberry Pi 1
2 Ethernet cable 1
3 USB cable 1
Theory:
MySQL is one of the world’s most popular relational database system and is a common inclusion in most LAMP (
Linux, Apache, MYSQL, and PHP) stacks. It is one of the pieces of technology that helps drive the modern web. A
database such as MYSQL is often a key component of dynamic websites and is one of the best ways of storing data for
web applications.
[Link]
Perfrom the above listed steps
Result:
The basic SQL queries by installing MySQL database on Raspberry Pi is performed.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #9a – Publishing Data to MQTT Service
Aim:
To publish temperature and humidity data from DHT11 to MQTT server using ESP8266.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 ESP8266 1
2 DHT11 1
3 Computer with Arduino IDE and Internet Connection 1
4 USB Cable 1
5 Breadboard 1
6 Jumper Wires As Required
Theory:
Adafruit IO is a cloud service, that just means we run it for you and you don't have to manage it. You can
connect to it over the Internet. You can connect to it over the Internet. It's meant primarily for storing and then
retrieving data.
Here, we use ESP8266 and DHT11 temperature and humidity sensor. The ESP8266 connects to a wireless access
point after which it starts fetching temperature and humidity data and logs it to Adafruit IO via MQTT protocol.
Code:
#include <ESP8266WiFi.h> #include
"AdafruitIO_WiFi.h"#include
<DHT.h>
#define DHT_PIN 12
#define _SSID "SSID" // WiFi SSID here.
#define PASSWORD "PASSWORD" // WiFi password here.
#define IO_USERNAME "IO_USERNAME" // Adafruit username here. #define
IO_KEY "IO_KEY" // Adafruit key here.
DHT dht(DHT_PIN, DHT11);
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, _SSID, PASSWORD);
AdafruitIO_Feed *temperatureFeed = [Link]("temperature"); AdafruitIO_Feed
*humidityFeed = [Link]("humidity");
float temperature, humidity;void
setup()
{
[Link](9600);
[Link]();
[Link]();
[Link]();
}
void loop()
{
// Fetching values from sensor. humidity =
[Link](); temperature =
[Link]();
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
if(isnan(temperature) || isnan(humidity))
{
[Link]("ERROR: Unable to fetch temperature and humidity data.");
}
else
{
[Link]("Temperature: ");
[Link](temperature);
[Link]("*C\tHumidity: ");
[Link](humidity);
temperatureFeed->save(temperature); // Sending values to Adafruit server. delay(2000);
humidityFeed->save(humidity);
}
delay(2000);
}
Output:
Result:
Hence, temperature and humidity data from DHT11 is logged to a MQTT broker (Adafruit IO).
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #9b – Subscribing to MQTT Service
Aim:
To subscribe to a MQTT service to fetch useful data by programming ESP8266.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 ESP8266 1
3 Computer with Arduino IDE and Internet Connection 1
4 USB Cable 1
Code:
#include <ESP8266WiFi.h> #include
"AdafruitIO_WiFi.h"
#define _SSID "SSID" // WiFi SSID here.
#define PASSWORD "PASSWORD" // WiFi password here.
#define IO_USERNAME "IO_USERNAME" // Adafruit username here. #define
IO_KEY "IO_KEY" // Adafruit key here.
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, _SSID, PASSWORD);
AdafruitIO_Feed *ledFeed = [Link]("led");
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
[Link](9600);
[Link]();
ledFeed->onMessage(printTemperature);
}
void loop()
{
[Link]();
}
void printTemperature(AdafruitIO_Data *ledStatus)
{
[Link]("LED Status: ");
[Link](ledStatus->value());
digitalWrite(LED_BUILTIN, ledStatus->value()[0] - '0');[Link]();
}
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Output:
Result:
Thus, ESP8266 is subscribed to MQTT service and the on-board LED is controlled successfully.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #10 – Creating TCP Server with ESP8266 and Sending Temperature Data on Request
Aim:
To create a TCP server with ESP8266 and send temperature data from DHT11 whenever data is requested.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 ESP8266 1
2 DHT11 Temperature Sensor 1
3 Computer with Arduino IDE and Internet Connection 1
4 USB Cable 1
5 Breadboard 1
6 Jumper Wires As Required
Code:
#include <ESP8266WiFi.h> #include
<ESP8266WebServer.h>#include
"DHT.h"
#define _SSID "SSID" #define
PASSWORD "PASSWORD"
#define DHT_PIN 4
ESP8266WebServer server(80); // 80 is the port for HTTP communication. DHT
dht(DHT_PIN, DHT11);
float temperature;void
setup()
{
[Link](9600);
[Link]();
delay(5000); [Link]("Connecting to:
");[Link](_SSID);
[Link](WIFI_STA);
[Link](_SSID, PASSWORD);
while([Link]() != WL_CONNECTED)
{
[Link]('.');
delay(500);
}
[Link]();
[Link]("\nEnter \"[Link] [Link]([Link]());
[Link]("\" in your web browser.");
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
[Link]("/", sendTemperature);
[Link](sendError);
[Link]();
}
void loop()
{
[Link]();
}
void sendTemperature()
{
temperature = [Link]();
if(isnan(temperature))
{
[Link]("ERROR: Unable to fetch temperature data."); [Link](200, "text/plain", "ERROR:
Unable to fetch temperature data.");
}
else
{
[Link](200, "text/plain", "Temperature: " + String(temperature));
}
}
void sendError()
{
[Link](404, "text/plain", "ERROR: Invalid URL.");
}
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Output:
Result:
Hence, a TCP server is created and temperature data is sent to it via TCP.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #11 – Creating UDP Server with ESP8266 and Sending Humidity Data onRequest
Aim:
To create a UDP server with ESP8266 and send humidity data from DHT11 whenever data is requested.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 ESP8266 1
2 Computer with Arduino IDE and Internet Connection 1
3 USB Cable 1
7 Breadboard 1
8 Jumper Wires As Required
Code:
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include "DHT.h"
#define _SSID "SSID" #define
PASSWORD "PASSWORD"
#define UDP_PORT 0000
#define DHT_PIN 2
WiFiUDP udp;
DHT dht(DHT_PIN, DHT11);
int humidity;
char sendPacket[3];
void setup()
{
[Link](9600);
[Link](_SSID, PASSWORD);
[Link]();
delay(5000); [Link]("Connecting to:
");[Link](_SSID);
while ([Link]() != WL_CONNECTED)
{
delay(100);
[Link](".");
}
[Link]();
[Link]("Connected to IP address: ");
[Link]([Link]());
// Start listening to udp port. [Link](UDP_PORT);
[Link]("Listening on UDP port: ");
[Link](UDP_PORT);
void loop()
{
// Check for packets.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
if([Link]())
{
// Send humidity data.
[Link]("\nHumidity data requested. Sending humidity data...");humidity =
[Link]();
if(isnan(humidity))
{
[Link]("ERROR: Unable to get humidity data.");
}
else
{
[Link]("Humidity: ");
[Link](humidity);
// Convert int to char *. sendPacket[0] =
humidity / 10 + '0';sendPacket[1] = humidity %
10 + '0';sendPacket[2] = '\0';
// Send return packet. [Link]([Link](),
[Link]());[Link](sendPacket);
[Link]();
}
}
}
Output:
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Result:
Hence, a UDP server is started in ESP8266 and humidity data is sent whenever a request is made.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #12 – GPS Module interfacing with Raspberry Pi
[Link]
Aim:
To interface Global Positioning System (GPS) module with Raspberry Pi.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Raspberry Pi 1
2 Computer and Internet Connection 1
3 GPS module 1
4 Breadboard 1
5 Jumper Wires As Required
Interfacing Diagram:
Code(Using C):
/*
GPS Interfacing with Raspberry Pi using C (WiringPi Library)
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
int main ()
{
int serial_port;
char dat,buff[100],GGA_code[3];
unsigned char IsitGGAstring=0;
unsigned char GGA_index=0;
unsigned char is_GGA_received_completely = 0;
if ((serial_port = serialOpen ("/dev/ttyS0", 9600)) < 0) /* open serial port */
{
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
if (wiringPiSetup () == -1/* initializes wiringPi setup */
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
while(1){
if(serialDataAvail (serial_port) ) /* check for any data available on serial port */
{
dat = serialGetchar(serial_port); /* receive character serially */
if(dat == '$'){
IsitGGAstring = 0;
GGA_index = 0;
}
else if(IsitGGAstring ==1){
buff[GGA_index++] = dat;
if(dat=='\r')
is_GGA_received_completely = 1;
}
else if(GGA_code[0]=='G' && GGA_code[1]=='G' && GGA_code[2]=='A'){
IsitGGAstring = 1;
GGA_code[0]= 0;
GGA_code[0]= 0;
GGA_code[0]= 0;
}
else{
GGA_code[0] = GGA_code[1];
GGA_code[1] = GGA_code[2];
GGA_code[2] = dat;
}
}
if(is_GGA_received_completely==1){
printf("GGA: %s",buff);
is_GGA_received_completely = 0;
}
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
}
return 0;
}
Output:
Result:
Hence a GPS module is interfaced with Raspberry Pi and tested.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #13 – RFID Module interfacing with Raspberry Pi
[Link]
Aim:
To interface RFID module with Raspberry Pi
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Raspberry Pi 1
2 EM-18 RFID Reader Module 1
3 RS232 – to – USB Adapter (as my RFID Reader has only 1
RS232 Output)
4 Few RFID Cards or RFID Tags 1
5 Power Supply for RFID Reader (my RFID Reader has a 1
5V Regulator)
6 5V Supply for Raspberry Pi and RS232-USB Adapter 1
7 680Ω Resistor (1/4 Watt) 1
8 1.5KΩ Resistor (1/4 Watt) 1
9 Connecting Wires As required
Interfacing Diagram:
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Code:
import
time
import serial
data = [Link](
port='/dev/ttyS0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=[Link]
)
#timeout=1 # must use when using [Link]()
#)
print " "
try:
while 1:
#x=[Link]()#print the whole data at once
#x=[Link]()#print single data at once
print "Place the card"
x=[Link](12)#print upto 10 data at once and the
#remaining on the second line
if x=="13004A29E191":
print "Card No - ",x
print "Welcome Bala"
print " "
elif x=="13006F8C7282":
print "Card No - ",x
print "Welcome Teja"
print " "
else:
print "Wrong Card....."
print " "
#print x
except KeyboardInterrupt:
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
[Link]()
Output:
Result:
Hence RFID module is interfaced with raspberry Pi.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Exercise #14 – IOT Based Home Automation using Raspberry Pi
[Link]
Aim:
To build IOT Based Home Automation using Raspberry Pi
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Raspberry Pi 1
2 5v Relays 1
3 Transistors 1
4 Diodes 1
5 Ressitors 1
6 Connection Blocks 1
7 LEDs to test. 1
8 Jumper Wires As required
Circuit Diagram:
Result:
Home Automation is done using IoT
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
CONTENT BEYOND THE SYLLABUS
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
7-Segment Display Interfacing with Arduino UNO
Aim:
To interface Seven Segment Display with Arduino UNO.
Apparatus Required:
Sign Number Name of the Equipment Quantity
1 Arduino UNO 1
2 Computer with Arduino IDE 1
3 USB Cable 1
4 Seven Segment Display 1
5 330Ω Resistor 1
6 Breadboard 1
7 Jumper Wires As Required
Theory:
Segments – 7 Segment Display
This seven segment display has a total of 8 LEDs per digit as shown in above image, seven LED’s for each segment
and one for the decimal point. As mentioned above, we are using two types of seven segment displays in this tutorial -
1) the single digit 7 segment display 2) the 4 digit 7 segment display and they both will be interfaced with arduino in
two different ways.
Interfacing single digit seven segment display to Arduino
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
We are going to interface a seven segment display with single digit/character to arduino uno. Final result would be
displaying 0-9 on the display. Lets identify the pinout and circuit diagram of the display.
Single Digit Seven Segment Display – Pin Out Diagram
As you can see there are 10 pins in total. You may notice two pins named com, as shown in the circuit diagram all the
anodes (+ pins) of the LEDs are connected to these two pins. We call these 2 pins as common anodes and such
displays are called Common Anode 7 segment displays. There are some seven segment displays which have common
cathodes instead of common anodes. The only difference for common cathode displays is all the cathodes (- pins) are
connected together and they are known as Common Cathode 7 segment displays. Apart from these 2 com pins, there
are 8 other pins named A,B,C,D,E,F,G and DP. As you can see in the figure, these pins are cathodes (- pins) of the led
segments of common anode display (in the case of common cathode display these pins will be anodes)
To turn on each segment ( LED), we have to supply appropriate voltage to it. Lets just say we want to show number
“3” on the display. To display ‘3’ properly, we need to turn ON A,B,C,D & G segment LED’s (refer the figures ). since
anodes are common, we supply positive voltage to common anode pins and connect GND (ground) to the segment
pins we want to turn ON. If you wire like this on a breadboard. the 7 segment display will show ‘3’ on its display. But
hard wiring is not practical, as we need to change the wiring all the time we want to change digit/character to be
displayed. We use micro controllers like 8051 or AVR or even Arduino to solve this problem. We can manipulate
the 7 segment wiring (the process of turning ON andOFF led segments) using software inside the micro controller.
In our tutorial of interfacing with arduino, we achieve this by writing specific lines of arduino code to turn led
segments ON and OFF
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
Circuit Diagram:
Code:
int disp_pin[7]; /* array for a-g pins of 7-Segment display */
void define_segment_pins(int a, int b, int c, int d, int e, int f, int g) /*
Assigns 7-segment display pins to board */
{
disp_pin[0] = a;
disp_pin[1] = b;
disp_pin[2] = c;
disp_pin[3] = d;
disp_pin[4] = e;
disp_pin[5] = f;
disp_pin[6] = g;
}
void display_number(int num) /* Function for displaying number (0-9) */
{
switch(num)
{
case 0:
digitalWrite(disp_pin[0], LOW); /* Drive disp_pin[0] to LOW */ digitalWrite(disp_pin[1], LOW); /* Driving
LOW turns on LED segment for common
anode display */ digitalWrite(disp_pin[2],
LOW); digitalWrite(disp_pin[3], LOW);
digitalWrite(disp_pin[4], LOW);
digitalWrite(disp_pin[5], LOW);
digitalWrite(disp_pin[6], HIGH);break;
case 1:
digitalWrite(disp_pin[0], HIGH); /* Drive disp_pin[7] to HIGH */
digitalWrite(disp_pin[1], LOW);
digitalWrite(disp_pin[2], LOW);
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
digitalWrite(disp_pin[3], HIGH); /* Driving HIGH turns off LED segment for common
anode display */
digitalWrite(disp_pin[4], HIGH);
digitalWrite(disp_pin[5], HIGH);
digitalWrite(disp_pin[6], HIGH);
break; case
2:
digitalWrite(disp_pin[0], LOW);
digitalWrite(disp_pin[1], LOW);
digitalWrite(disp_pin[2], HIGH);
digitalWrite(disp_pin[3], LOW);
digitalWrite(disp_pin[4], LOW);
digitalWrite(disp_pin[5], HIGH);
digitalWrite(disp_pin[6], LOW); break;
case 3: digitalWrite(disp_pin[0], LOW);
digitalWrite(disp_pin[1], LOW);
digitalWrite(disp_pin[2], LOW);
digitalWrite(disp_pin[3], LOW);
digitalWrite(disp_pin[4], HIGH);
digitalWrite(disp_pin[5], HIGH);
digitalWrite(disp_pin[6], LOW); break;
case 4: digitalWrite(disp_pin[0], HIGH);
digitalWrite(disp_pin[1], LOW);
digitalWrite(disp_pin[2], LOW);
digitalWrite(disp_pin[3], HIGH);
digitalWrite(disp_pin[4], HIGH);
digitalWrite(disp_pin[5], LOW);
digitalWrite(disp_pin[6], LOW); break;
case 5: digitalWrite(disp_pin[0], LOW);
digitalWrite(disp_pin[1], HIGH);
digitalWrite(disp_pin[2], LOW);
digitalWrite(disp_pin[3], LOW);
digitalWrite(disp_pin[4], HIGH);
digitalWrite(disp_pin[5], LOW);
digitalWrite(disp_pin[6], LOW); break;
case 6: digitalWrite(disp_pin[0], LOW);
digitalWrite(disp_pin[1], HIGH);
digitalWrite(disp_pin[2], LOW);
digitalWrite(disp_pin[3], LOW);
digitalWrite(disp_pin[4], LOW);
digitalWrite(disp_pin[5], LOW);
digitalWrite(disp_pin[6], LOW); break;
case 7: digitalWrite(disp_pin[0], LOW);
digitalWrite(disp_pin[1], LOW);
digitalWrite(disp_pin[2], LOW);
digitalWrite(disp_pin[3], HIGH);
digitalWrite(disp_pin[4], HIGH);
digitalWrite(disp_pin[5], HIGH);
digitalWrite(disp_pin[6], HIGH);break;
case 8: digitalWrite(disp_pin[0], LOW);
digitalWrite(disp_pin[1], LOW);
digitalWrite(disp_pin[2], LOW);
digitalWrite(disp_pin[3], LOW);
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER
digitalWrite(disp_pin[4], LOW);
digitalWrite(disp_pin[5], LOW);
digitalWrite(disp_pin[6], LOW);
break; case
9:
digitalWrite(disp_pin[0], LOW);
digitalWrite(disp_pin[1], LOW);
digitalWrite(disp_pin[2], LOW);
digitalWrite(disp_pin[3], LOW);
digitalWrite(disp_pin[4], HIGH);
digitalWrite(disp_pin[5], LOW);
digitalWrite(disp_pin[6], LOW); break;
default: digitalWrite(disp_pin[0], HIGH);
digitalWrite(disp_pin[1], LOW);
digitalWrite(disp_pin[2], LOW);
digitalWrite(disp_pin[3], LOW);
digitalWrite(disp_pin[4], LOW);
digitalWrite(disp_pin[5], HIGH);
digitalWrite(disp_pin[6], LOW); break;
}
}
void setup() { pinMode(6,
OUTPUT); pinMode(7,
OUTPUT); pinMode(8,
OUTPUT); pinMode(9,
OUTPUT); pinMode(10,
OUTPUT);pinMode(11,
OUTPUT);pinMode(12,
OUTPUT);
define_segment_pins(12,11,10,9,8,7,6); /* a-g segment pins to Arduino */
}
void loop() {int i;
for(i = 9; i>=0; i--)
{
display_number(i);
delay(1000);
}
for(i = 0; i<=9; i++)
{
display_number(i);
delay(1000);
}
}
Result:
Hence, seven segment display is interfaced with Arduino UNO successfully.
RMKEC/EEE/LM/20EE512 /2022-23/ODD SEMESTER