[go: up one dir, main page]

0% found this document useful (0 votes)
389 views48 pages

Iot Lab Manual

The document describes an Internet of Things laboratory course. The course involves 13 practical experiments using sensors, actuators, and development boards like Arduino and Raspberry Pi. The experiments include controlling LEDs and other devices, reading sensor data, sending data to the cloud, and designing IoT solutions for applications like air pollution monitoring. The course aims to help students learn to choose sensors and actuators for IoT, use communication protocols, interact with cloud platforms, and design IoT prototypes and solutions.

Uploaded by

Vishnu Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
389 views48 pages

Iot Lab Manual

The document describes an Internet of Things laboratory course. The course involves 13 practical experiments using sensors, actuators, and development boards like Arduino and Raspberry Pi. The experiments include controlling LEDs and other devices, reading sensor data, sending data to the cloud, and designing IoT solutions for applications like air pollution monitoring. The course aims to help students learn to choose sensors and actuators for IoT, use communication protocols, interact with cloud platforms, and design IoT prototypes and solutions.

Uploaded by

Vishnu Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 48

SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY ANANTAPUR


B.Tech (CSE)– IV-I LTPC
0 0 3 1.5
(19A05701P) INTERNET OF THINGS LABORATORY
(Common to CSE & IT)
Practicals:
1. Select any one development board (Eg., Arduino or Raspberry Pi) and control LED using
the board.

2. Using the same board as in (1), read data from a sensor. Experiment with both analog and
digital sensors.

3. Control any two actuators connected to the development board using Bluetooth.

4. Read data from sensor and send it to a requesting client. (using socket communication)
Note: The client and server should be connected to same local area network.

5. Create any cloud platform account, explore IoT services and register a thing on the
platform.

6. Push sensor data to cloud.

7. Control an actuator through cloud.

8. Access the data pushed from sensor to cloud and apply any data analytics or visualization
services.

9. Create a mobile app to control an actuator.

10. Design an IoT based air pollution control system which monitors the air pollution by
measuring carbon monoxide, ammonia, etc and gives alarm or sends message when the
pollution level is more than permitted range.

11. Design an IoT based system which measures the physical and chemical properties of the
water and displays the measured values.

12. Identify a problem in your local area or college which can be solved by integrating the
things you learned and create a prototype to solve it (Mini Project).

13. Design a business model canvas for a digital display.

Course outcomes:
At the end of the course, students will be able to
x Choose the sensors and actuators for an IoT application (L1)
x Select protocols for a specific IoT application (L2)
x Utilize the cloud platform and APIs for IoT application (L3)
x Experiment with embedded boards for creating IoT prototypes (L3)
x Design a solution for a given IoT application (L6)

1
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

SANSKRITHI SCHOOL OF ENGINEERING, PUTTAPARTHI.

EXP-1 Select any one development board (Eg., Arduino or DATE:


Raspberry Pi) and control LED using the board.

AIM: By Selecting any one development board (E.g., Arduino or Raspberry Pi) and
controlling LED using the board.

COMPONENTS REQUIRED:

SL.NO COMPONENTS QUANTITY


1. ARDUINO BOARD 1
2. LED LIGHT 1
3. SWITCH 1
4. CONNECTING WIRES 10
5. PC 1
6. BREADBOARD 1

Arduino UNO

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.

Let's discuss each component in detail.

1. ATmega328 Microcontroller- It is a single chip Microcontroller of the ATmel family.


The processor code inside it is of 8-bit. It combines Memory (SRAM, EEPROM, and
Flash), Analog to Digital Converter, SPI serial ports, I/O lines, registers, timer, external
and internal interrupts, and oscillator.

2. ICSP pin - The In-Circuit Serial Programming pin allows the user to program using the
firmware of the Arduino board.

3. Power LED Indicator- The ON status of LED shows the power is activated. When the
power is OFF, the LED will not light up.

4. Digital I/O pins- The digital pins have the value HIGH or LOW. The pins numbered from
D0 to D13 are digital pins.

2
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

5. TX and RX LED's- The successful flow of data is represented by the lighting of these
LED's.
6. AREF- The Analog Reference (AREF) pin is used to feed a reference voltage to the
Arduino UNO board from the external power supply.

7. Reset button- It is used to add a Reset button to the connection.

8. USB- It allows the board to connect to the computer. It is essential for the programming of
the Arduino UNO board.

9. Crystal Oscillator- The Crystal oscillator has a frequency of 16MHz, which makes the
Arduino UNO a powerful board.

10. Voltage Regulator- The voltage regulator converts the input voltage to 5V.
11. GND- Ground pins. The ground pin acts as a pin with zero voltage.
12. Vin- It is the input voltage.

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

Light Emitting Diode:


LED’s are kind of semi conductors with the light sources in which both P-type(holes) and N-
type(electrons) semiconductors will combine. By giving some certain forward voltage this
combination of holes and electrons occurs, releasing the form of which is light. The wastage
of electricity is very less in LED’s and it directly converts electrical energy into light energy,
Figure shows a view of LED.

3
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Push Buttons:
A push-button is a easy transfer mechanism to govern some issue of a system. while the push
button is open (unpressed) there is no connection between the 2 legs of the push button, so
the pin is hooked up to floor (thru the pull-down resistor) and we study a LOW. whilst the
button is closed (pressed), it makes a connection among its legs, connecting the pin to 5 volts,
so that we study a excessive. Figure shows a view of push button.

Resistors: A resistor is a passive two-terminal device that implements electric resistance as a


circuit. Resistors are used to resist the flow of current and have a stated cost of Resistance.
Many kinds of resistors are used having extraordinary makes use of and construction. The
most common types have a hard and fast value of resistance so are often referred to as fixed
resistors, Figure shows a view of resistors.

Theory :
LED (Light emitting diode) works with very little power. The digital pin 13 is built in the
Arduino board for the LED. LED’s have polarity will work only the legs are oriented in the
proper way. The two legs of the LED, one leg goes for the positive and other leg is for the
ground. The LED has flat edge on one side of the bulb. To protect the LED we should use
resistors in series to protect it from burning.

Circuit Diagram : As shown in Figure resistors are used to safe guard the Light emitting
diodes from the higher voltage and prevent them from burning.

4
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Procedure:
1. Make connections as per circuit diagram.
2. Connect the Arduino to pc and configure Arduino IDE.
3. Copy and verify the code in Arduino IDE.
4. Upload the code to Arduino.
5. Now we can see the L.E.D lights up.

Arduino IDE code :

1 int red = 10;


2 int yellow = 9;
3 int green = 8;
4
5 void setup() {
6 pinMode(red , OUTPUT);
7 pinMode(yellow , OUTPUT);
8 pinMode(green , OUTPUT);
9}
10
11 void loop(){
12 digitalWrite(green , HIGH);
13 digitalWrite(yellow , LOW);
14 digitalWrite(red , LOW);
15 delay(5000);
16 digitalWrite (green , LOW);
17 digitalWrite(yellow , HIGH);
18 digitalWrite(red , LOW);
19 delay(5000);
20 digitalWrite(green , LOW);
21 digitalWrite(yellow , LOW);

5
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

22 digitalWrite(red , HIGH);
23 delay(5000);
24 }
Precautions:
1.Connections should be made properly.
2.Avoid loose connections.
3.Verify the code before uploading.

Result: Traffic lights with LED’s are achieved with time difference.
SANSKRITHI SCHOOL OF ENGINEERING, PUTTAPARTHI.

EXP-2 Using the Arduino board, read data from a sensor. DATE:
Experiment with both analog and digital sensors.

AIM: Using the Arduino board, read data from a sensor. Experiment with both analog and
digital sensors. To write a program to measure the distance using ultrasonic sensor and make
LED blink using Arduino.

COMPONENTS REQUIRED:

SL.NO COMPONENTS QUANTITY


1. ARDUINO BOARD 1
2. LED LIGHT 1
3. ULTRA SONIC SENSOR 1
4. CONNECTING WIRES 10
5. PC 1
6. BREADBOARD 1

Arduino UNO

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.

Let's discuss each component in detail.

1. ATmega328 Microcontroller- It is a single chip Microcontroller of the ATmel family.


The processor code inside it is of 8-bit. It combines Memory (SRAM, EEPROM, and

6
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Flash), Analog to Digital Converter, SPI serial ports, I/O lines, registers, timer, external
and internal interrupts, and oscillator.

2. ICSP pin - The In-Circuit Serial Programming pin allows the user to program using the
firmware of the Arduino board.

3. Power LED Indicator- The ON status of LED shows the power is activated. When the
power is OFF, the LED will not light up.
4. Digital I/O pins- The digital pins have the value HIGH or LOW. The pins numbered from
D0 to D13 are digital pins.
5. TX and RX LED's- The successful flow of data is represented by the lighting of these
LED's.
6. AREF- The Analog Reference (AREF) pin is used to feed a reference voltage to the
Arduino UNO board from the external power supply.

7. Reset button- It is used to add a Reset button to the connection.

8. USB- It allows the board to connect to the computer. It is essential for the programming of
the Arduino UNO board.

9. Crystal Oscillator- The Crystal oscillator has a frequency of 16MHz, which makes the
Arduino UNO a powerful board.

10. Voltage Regulator- The voltage regulator converts the input voltage to 5V.
11. GND- Ground pins. The ground pin acts as a pin with zero voltage.
12. Vin- It is the input voltage.

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

ALGORITHM:

STEP 1: Start the process.


STEP 2: Start ->Arduino IDE -1.8.8
STEP 3: Then enter the coding in Arduino Software.
STEP 4: Compile the coding in Arduino Software.

7
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

STEP 5: In Arduino board, connect VCC to power supply 5V and connect to ground as in
PIN gnd and connect trig to trigpio =9, connect echo to echopin=10 using jumper wires.
STEP 6: Connect the Arduino board with USB cable to the system.
STEP 7: Select tools -> select board ->Arduino Nano -> select
processor -> AT Mega 328 p and the select port.
STEP 8: Upload the coding in Arduino board and now for the LED
to blink.
STEP 9: Then, the output will be displayed in the serial monitor.
STEP 10: Stop the process.

Circuit Diagram:

Theory:
The Ultrasonic sensor emits out the very high frequency sound pulse and checks the time
taken to reflect the sound back. It has two openings in front one for transmitting and other for
receiving the sound waves. The sound travels with the speed of 341 meters per second in air.
The time difference between the sending and receiving will help to measure the of the object
The mathematical equation: Distance = Time *(speed of sound/2) Ultrasonic sensors will
measure the following without any contact with the medium:
1. Distance
2. Level
3. Position
4. Presence
5. Diameter

CODING:
const int trigPin = 9;
const int techoPin = 10;
long duration;
int distance;
void setup()

8
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop()
{
digitalWrite(trigPin, LOW);// Clears the trigPin
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10
micro seconds
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // distance= (Time x Speed of
Sound in Air (340 m/s))/2
Serial.println(distance);
delay(1000);
}
Arduino IDE code to display in Web server:
#include <SPI.h>
#include <WiFiNINA.h>
char ssid[] = "username"; //Enter your wifi username here
char pass[] = "password"; //Enter your wifi password here
int keyIndex = 0;
int status = WL_IDLE_STATUS;
const int trigPin = 3;
const int echoPin = 2;
long duration;
int distance;
WiFiServer server(80);
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");

9
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
Serial.print(" cm");
WiFiClient client = server.available();
client.println("<html>");
client.println("<table style='width:100%',>");
client.println("<tr>");
client.println("<th>");
client.println("Distance: ");
client.println(distance);
client.println(" cm");
client.println("</th>");
client.println("</tr></table>");
client.println("</html>");

10
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

delay(5000);
}
RESULT: Thus the output for measuring the distance using ultrasonic sensor and LED
blink using Arduino has successfully executed.

SANSKRITHI SCHOOL OF ENGINEERING, PUTTAPARTHI.

EXP-3 Control any two actuators connected to the development DATE:


board using Bluetooth

AIM: Control any actuators connected to the development board using Bluetooth. To control
the speed of Stepper Motor using Arduino

COMPONENTS REQUIRED:

SL.NO COMPONENTS QUANTITY


1. ARDUINO BOARD 1
2. STEPPER MOTOR 1
3. 10K OHM POTENTIOMETER 1
4. CONNECTING WIRES 10
5. PC 1
6. BREADBOARD 1
7. ARDUINO SOFTWARE 1
8. U2004 DARLINGTON ARRAY (IN CASE OF 1
UNIPOLAR STEPPER MOTOR)
9. SN754410NE H-BRIDGE (IN CASE OF BIPOLAR 1
STEPPER MOTOR)

Arduino UNO

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.

11
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Let's discuss each component in detail.

1. ATmega328 Microcontroller- It is a single chip Microcontroller of the ATmel family.


The processor code inside it is of 8-bit. It combines Memory (SRAM, EEPROM, and
Flash), Analog to Digital Converter, SPI serial ports, I/O lines, registers, timer, external
and internal interrupts, and oscillator.

2. ICSP pin - The In-Circuit Serial Programming pin allows the user to program using the
firmware of the Arduino board.

3. Power LED Indicator- The ON status of LED shows the power is activated. When the
power is OFF, the LED will not light up.

4. Digital I/O pins- The digital pins have the value HIGH or LOW. The pins numbered from
D0 to D13 are digital pins.

5. TX and RX LED's- The successful flow of data is represented by the lighting of these
LED's.

6. AREF- The Analog Reference (AREF) pin is used to feed a reference voltage to the
Arduino UNO board from the external power supply.

7. Reset button- It is used to add a Reset button to the connection.

8. USB- It allows the board to connect to the computer. It is essential for the programming of
the Arduino UNO board.

9. Crystal Oscillator- The Crystal oscillator has a frequency of 16MHz, which makes the
Arduino UNO a powerful board.

10. Voltage Regulator- The voltage regulator converts the input voltage to 5V.
11. GND- Ground pins. The ground pin acts as a pin with zero voltage.
12. Vin- It is the input voltage.

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

12
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

THEORY:
Stepper motors, due to their unique design, can be controlled to a high degree of accuracy
without any feedback mechanisms. The shaft of a stepper, mounted with a series of magnets,
is controlled by a series of electromagnetic coils that are charged positively and negatively in
a specific sequence, precisely moving it forward or backward in small "steps". There are two
types of steppers, Unipolars and Bipolars, and it is very important to know which type you
are working with. For each of the motors, there is a different circuit.

13
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Program:
#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
void setup() {
}
void loop() {
int sensorReading = analogRead(A0);
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
myStepper.step(stepsPerRevolution / 100);
}
}
Result: The speed of the Uni and Bipolar Stepper motor is controlled using Arduino
EXP-4 Read data from sensor and send it to a requesting client. (using DATE:
socket communication) Note: The client and server should be
connected to same local area network.

AIM: To measure the room temperature of a place through Arduino using LM35 and
send the data using same local area network using wifi.
COMPONENTS REQUIRED:

SL.NO COMPONENTS QUANTITY


1. ARDUINO BOARD 1
2. TEMPERATURE MEASUREMENT SENSOR 1
3. RESISTORS 1
4. CONNECTING WIRES 10
5. PC 1
6. BREADBOARD 1
7. ARDUINO SOFTWARE 1

Theory : LM-35 is an absolute temperature sensor which can measure the temperature of the
surroundings within 100 to 500 feet. LM-35 output voltage is proportional to the
Celsius/Centigrade temperature which increments the output by 1 on every 10-mV change in
temperature. LM-35 can measure from -50 to 150 degree Celsius. • Arduino analog pins work
normally on +5 V. • Resolution of analog pin starts from 0 to 1023. • Maximum voltage of
LM-35 is 1.5 V. • Formula for converting the voltage into system input number = (V/5)
*1023 The measured temperature can be viewed in the web-server with the help of code.

14
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Circuit Diagram : As shown in Figure resistor is connected in between the temperature


sensor and ground to avoid the sensor from burning.

Arduino IDE code


1 const int sensor=A0;
2 float tempc;
3 float tempf;
4 float vout;
5
6 void setup() {
7 pinMode(sensor ,INPUT);
8 Serial.begin(9600);

15
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

9}
10
11 void loop() {
12 vout=analogRead(sensor);
13 vout=(vout /1024.0) *5.0;
14 tempc = (vout - 0.5)*100;
15 tempf=(tempc*1.8)+32;
16 Serial.print("in DegreeC=") ;
17 Serial.print("\t") ;
18 Serial.print(tempc);
19 Serial.print("C ") ;
20 Serial.print("in Fahrenheit=") ;
21 Serial.print("\t") ;
22 Serial.print(tempf);
23 Serial.println("F ") ;
24 delay(5000);
25 }

WIFI code for measuring temperature


1 #include <SPI.h>
2 #include <WiFiNINA.h>
3
4
5 char ssid[] = "username"; // your network SSID (name)
6 char pass[] = "password"; // your network password
7 int keyIndex = 0; // your network key Index number (
needed only for WEP)
8
9 int status = WL_IDLE_STATUS;
10
11 const int sensor=A0;
12 float tempc;
13 float tempf;
14 float vout;
15
16 WiFiServer server(80);
17
18 void setup() {
19 pinMode(sensor ,INPUT);
20 Serial.begin(9600);
21
22 // attempt to connect to Wifi network:
23 while ( status != WL_CONNECTED) {
24 Serial.print("Attempting to connect to SSID: ") ;

16
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

25 Serial.println (ssid);
26 status = WiFi.begin(ssid , pass);
27
28 // wait 10 seconds for connection:
29 delay(10000);
30 }
31 server.begin();
32 // you’re connected now , so print out the status:
33 Serial.print("SSID: ") ;
34 Serial.println (WiFi.SSID());
35 IPAddress ip = WiFi.localIP();
36 Serial.print("IP Address: ") ;
37 Serial.println(ip);
38 }
39
40
41 void loop() {
42 vout=analogRead(sensor); //Reading the value from sensor
43 vout=(vout /1024.0) *5.0;

44 tempc = (vout - 0.5)*100;


45 tempf=(tempc*1.8)+32; // Converting to Fahrenheit
46 Serial.print("in DegreeC=") ;
47 Serial.print("\t") ;
48 Serial.print(tempc);
49 Serial.print("C ") ;
50 Serial.print("in Fahrenheit=") ;
51 Serial.print("\t") ;
52 Serial.print(tempf);
53 Serial.println("F ") ;
54 // listen for incoming clients
55 WiFiClient client = server.available();
56 client.println("<html >");
57 client.println("<table style=’width:100%’,>") ;
58 client.println("<tr >") ;
59 client.println("<th >") ;
60 client.println("Temperature in Celsius: ") ;
61 client.println(tempc);
62 client.println("C ") ;

17
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

63 client.println(" </th >") ;


64 client.println("Temperature in Fahrenheit: ") ;
65 client.println(tempf);
66 client.println("F ") ;
67 client.println(" </th >") ;
68 client.println(" </tr ></table >") ;
69 client.println("</html >") ;
70 delay(5000);
71 }

Results : We can find out the temperature of the environment in both Fahrenheit and
Celsius with LM-35 and update it to web server.

SANSKRITHI SCHOOL OF ENGINEERING, PUTTAPARTHI.

EXP-5 Push sensor data to cloud by selecting any one development DATE:
board (eg., arduino or raspberry pi)

AIM: Push sensor data to cloud by selecting any one development board (eg., arduino
or raspberry pi).

COMPONENTS REQUIRED:

SL.NO COMPONENTS QUANTITY


1. ARDUINO BOARD 1
2. ESP8266 MICROCHIP WITH WI-FI 1
CONNECTIVITY
3. NODEMCU FIRMWARE FOR THE INTERNET 1
OF THINGS (IOT)
4. CONNECTING WIRES 10
5. PC 1
6. BREADBOARD 1

Arduino UNO

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.

18
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

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.

Let's discuss each component in detail.

1. ATmega328 Microcontroller- It is a single chip Microcontroller of the ATmel family.


The processor code inside it is of 8-bit. It combines Memory (SRAM, EEPROM, and
Flash), Analog to Digital Converter, SPI serial ports, I/O lines, registers, timer, external
and internal interrupts, and oscillator.

2. ICSP pin - The In-Circuit Serial Programming pin allows the user to program using the
firmware of the Arduino board.

3. Power LED Indicator- The ON status of LED shows the power is activated. When the
power is OFF, the LED will not light up.

4. Digital I/O pins- The digital pins have the value HIGH or LOW. The pins numbered from
D0 to D13 are digital pins.

5. TX and RX LED's- The successful flow of data is represented by the lighting of these
LED's.
6. AREF- The Analog Reference (AREF) pin is used to feed a reference voltage to the
Arduino UNO board from the external power supply.

7. Reset button- It is used to add a Reset button to the connection.

8. USB- It allows the board to connect to the computer. It is essential for the programming of
the Arduino UNO board.

9. Crystal Oscillator- The Crystal oscillator has a frequency of 16MHz, which makes the
Arduino UNO a powerful board.

10. Voltage Regulator- The voltage regulator converts the input voltage to 5V.
11. GND- Ground pins. The ground pin acts as a pin with zero voltage.
12. Vin- It is the input voltage.

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

19
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Arduino

Arduino (https://www.arduino.cc) is an OSH and electronics platform for real-time


applications.
It is available in many variants, and can be selected depending upon the type of application
for which it is required. There are different boards available that can be programmed for
specific applications.
Arduino boards are available for:

 Beginners

 Enhanced features

 Internet of Things (IoT)

 Education

 Wearable devices
Arduino UNO is one of the widely used microcontroller boards for beginners. This board can
be easily programmed using the Arduino IDE, available
at https://www.arduino.cc/en/Main/Software. The Arduino IDE software is available for
multiple platforms including Windows, Mac OSX and Linux. C and C++ are the base
programming languages used to program and customise the behaviour of the Arduino
microcontroller board. The Arduino IDE can be downloaded without any cost, along with the
live updates of the software.

Preparing Arduino for Internet of Things (IoT) applications

Arduino can be connected with live platforms of Internet of Things (IoT) by using Wi-Fi or
similar network based chips. ESP8266 is a very popular device for communication with the
Internet that can be easily interfaced with Arduino microcontroller boards. The ESP8266 Wi-
Fi chip is very low cost and can be directly connected with the Arduino board to establish
communication with the real cloud.

20
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

NodeMCU, an open source IoT based hardware platform with inbuilt Wi-Fi: To
communicate and transfer data to the cloud in real-time, there is another chip called
NodeMCU (http://www.nodemcu.com) that can directly communicate with live servers.
NodeMCU integrates the firmware with inbuilt Wi-Fi connectivity so that direct interaction
with the network can be done without physical jumper wires. It is an Arduino-like board that
is used for interfacing with the cloud in real-time so that the live signals or data can be logged
or stored for predictive analysis.

Storing sensor data in IoT platforms


There are a number of IoT platforms that can be used for the storage of real-time streaming
sensor data from Arduino and similar boards. Once the circuit is created using Arduino, the
signals can be transmitted and stored directly onto IoT platforms. These open source IoT
platforms can be used for storage, processing, prediction and visualisation of the data
recorded from the sensors on the Arduino board.
A few prominent IoT platforms that can be used for device management and the visualisation
of data are:

 ThingSpeak: thingspeak.com

 Carriots: carriots.com

 KAA:  kaaproject.org

 Things Board: thingsboard.io

21
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

 MainFlux: mainflux.com

 Thinger: thinger.io

 DeviceHive: devicehive.com

Transferring data to ThingSpeak


ThingSpeak (https://www.thingspeak.com) is a widely used open IoT platform for the
collection, storage and visualisation of real-time sensor data fetched from Arduino, Raspberry
Pi, BeagleBone Black and similar boards. Live streaming data on the cloud can be preserved
using channels in ThingSpeak.
To work with ThingSpeak, a free account can be created. After successfully signing up on
ThingSpeak, a channel is created on the dashboard and a ‘Write Key’ is generated, which is
to be used in the Arduino code.
The key features of ThingSpeak include the following:
1. Configuration of devices for sending data to a cloud based environment.
2. Aggregation of sensor data from different devices and motherboards connected with
sensors.
3. Visualisation of real-time sensor data including historical records.
4. Pre-processing and evaluation of sensor data for predictive analysis and knowledge
discovery.
The source code that can be customised and executed on Arduino IDE so that real-time data
can be fetched on board and then transmitted to ThingSpeak is given below. For simplicity,
the execution of the code is done for NodeMCU hardware, which has inbuilt Wi-Fi for
sending the data to the IoT platform.

#include <ESP8266WiFi.h>
 
const char* mynetwork = “<Wi-Fi Network Name>”;
const char* mypassword = “<Wi-Fi Network Mypassword>”;
const char* thingspeakhost = “api.thingspeak.com”;
char tsaddress[] = “api.thingspeak.com”;
String ThingSpeakAPI = “<ThingSpeak Write Key>”;
const int UpdateInterval = 30 * 1000;
long ConnectionTime = 0;
boolean connectedlast = false;
int fcounter = 0;
float value = 100;
WiFiClient client;
void setup()
{
Serial.begin(9600);
startEthernet();
}
void loop()
{
String RecordedAnalogInput0 = String(value++, DEC);
if (client.available() 
{

22
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

char c = client.read();
}
if (!client.connected() && connectedlast)
{
client.stop();
}
if(!client.connected() && (millis() - ConnectionTime > UpdateInterval))
{
updateThingSpeak(“field1=”+RecordedAnalogInput0);
}
if (fcounter > 3) {startEthernet();}
connectedlast = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect(tsaddress, 80))
{
client.print(“POST /update HTTP/1.1\n”);
client.print(“Thingspeakhost: api.thingspeak.com\n”);
client.print(“Connection: close\n”);
client.print(“X-THINGSPEAKAPIKEY: “+ThingSpeakAPI+”\n”);
client.print(“Content-Type: application/x-www-form-urlencoded\n”);
client.print(“Content-Length: “);
client.print(tsData.length());
client.print(“\n\n”);
client.print(tsData);
ConnectionTime = millis();
if (client.connected())
{
Serial.println(“Connecting to ThingSpeak...”);
Serial.println();
fcounter = 0; 
}
else

fcounter++;
Serial.println(“Connection to ThingSpeak failed (“+String(fcounter, DEC)+”)”);
Serial.println();


else
{
fcounter++;
Serial.println(“Connection to ThingSpeak Failed (“+String(fcounter, DEC)+”)”);
Serial.println(); 
ConnectionTime = millis(); 


void startEthernet() 

fcounter = 0; 

23
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

client.stop(); 
Serial.println(“Connecting”);
WiFi.begin(mynetwork, mypassword); 
while (WiFi.status() != WL_CONNECTED) {
delay(500);  
}

Transferring data to Carriots


Carriots (https://www.carriots.com) by Altair is another open IoT platform for logging and
processing of sensor data on the cloud based environment. The use cases of Carriots include
implementation in smart retail, smart cities, smart energy, smart oil, smart agriculture, smart
buildings, smart banking and others.

The Carriots platform can be accessed after free registration at carriots.com. After logging
into Carriots, a secured control panel is provided that has options for creating and mapping
devices and streams. There are options to integrate the streaming with e-mail and SMS for
live notifications.

The following is the source code for the Arduino IDE which transmits data to Carriots:

#include “ESP8266WiFi.h”
const char* WiFiNetworkName = “<WiFi Network Name>”;
const char* NetworkPassword = “<WiFi NetworkPassword>”;
const char* ServerName = “api.carriots.com”;
const String CARRIOTSAPI = “<API Key of Carriots>”;
const String DEVICE = “defaultDevice@kumargaurav.kumargaurav”;
WiFiClient client;
int readval = 0;
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println();
Serial.print(“Connecting to Network”); 
Serial.println(WiFiNetworkName); 
WiFi.begin(WiFiNetworkName, NetworkPassword);
while (WiFi.status()!= WL_CONNECTED)  
delay(1000); 

Serial.println(“Connected”); 
}
void sendStream() 

if (client.connect(ServerName, 80)) {
Serial.println(F(“connected”)); 
String json = “{\”protocol\”:\”v2\”,\”device\”:\”” + DEVICE +
“\”,\”at\”:\”now\”,\”data\”:{\”RecordedData\”:\”” + val + “\”}}”;
client.println(“POST /streams HTTP/1.1”); 

24
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

client.println(“Host: api.carriots.com”);
client.println(“Accept: application/json”); 
client.println(“User-Agent: Arduino-Carriots”); 
client.println(“Content-Type: application/json”); 
client.print(“carriots.CarriotsAPI: “); 
client.println(CARRIOTSAPI); 
client.print(“Content-Length: “); 
int thisLength = json.length(); 
client.println(thisLength); 
client.println(“Connection: close”); 
client.println(); 
client.println(json); 

else { Serial.println(F(“Not Connected”)); } 
}
 
void loop() {
readval = analogRead(A0);
Serial.println(val);
Serial.println(F(“Transmit Data”));
sendStream();
delay(1000);
}

Results : Hence we had developed a program to Push sensor data to cloud by selecting
any one development board (eg., arduino or raspberry pi).

SANSKRITHI SCHOOL OF ENGINEERING, PUTTAPARTHI.

EXP-6 Developed a program to Control an actuator through cloud DATE:


using any one development board (eg., arduino or
raspberry pi).

AIM: Developed a program to Control an actuator through cloud using any one
development board (eg., arduino or raspberry pi).

COMPONENTS REQUIRED:

SL.NO COMPONENTS QUANTITY


1. ARDUINO BOARD 1

25
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

2. ESP8266 MICROCHIP WITH WI-FI 1


CONNECTIVITY
3. NODEMCU FIRMWARE FOR THE INTERNET 1
OF THINGS (IOT)
4. CONNECTING WIRES 10
5. PC 1
6. BREADBOARD 1
Arduino UNO

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.

Let's discuss each component in detail.

1. ATmega328 Microcontroller- It is a single chip Microcontroller of the ATmel family.


The processor code inside it is of 8-bit. It combines Memory (SRAM, EEPROM, and
Flash), Analog to Digital Converter, SPI serial ports, I/O lines, registers, timer, external
and internal interrupts, and oscillator.
2. ICSP pin - The In-Circuit Serial Programming pin allows the user to program using the
firmware of the Arduino board.
3. Power LED Indicator- The ON status of LED shows the power is activated. When the
power is OFF, the LED will not light up.
4. Digital I/O pins- The digital pins have the value HIGH or LOW. The pins numbered from
D0 to D13 are digital pins.
5. TX and RX LED's- The successful flow of data is represented by the lighting of these
LED's.
6. AREF- The Analog Reference (AREF) pin is used to feed a reference voltage to the
Arduino UNO board from the external power supply.

7. Reset button- It is used to add a Reset button to the connection.


8. USB- It allows the board to connect to the computer. It is essential for the programming of
the Arduino UNO board.
9. Crystal Oscillator- The Crystal oscillator has a frequency of 16MHz, which makes the
Arduino UNO a powerful board.
10. Voltage Regulator- The voltage regulator converts the input voltage to 5V.
11. GND- Ground pins. The ground pin acts as a pin with zero voltage.
12. Vin- It is the input voltage.
13. 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.

26
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Arduino
Arduino (https://www.arduino.cc) is an OSH and electronics platform for real-time
applications.
It is available in many variants, and can be selected depending upon the type of application
for which it is required. There are different boards available that can be programmed for
specific applications.
Arduino boards are available for:

 Beginners

 Enhanced features

 Internet of Things (IoT)

 Education

 Wearable devices
Arduino UNO is one of the widely used microcontroller boards for beginners. This board can
be easily programmed using the Arduino IDE, available
at https://www.arduino.cc/en/Main/Software. The Arduino IDE software is available for
multiple platforms including Windows, Mac OSX and Linux. C and C++ are the base
programming languages used to program and customise the behaviour of the Arduino
microcontroller board. The Arduino IDE can be downloaded without any cost, along with the
live updates of the software.

Preparing Arduino for Internet of Things (IoT) applications

Arduino can be connected with live platforms of Internet of Things (IoT) by using Wi-Fi or
similar network based chips. ESP8266 is a very popular device for communication with the
Internet that can be easily interfaced with Arduino microcontroller boards. The ESP8266 Wi-
Fi chip is very low cost and can be directly connected with the Arduino board to establish
communication with the real cloud.

27
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

NodeMCU, an open source IoT based hardware platform with inbuilt Wi-Fi: To
communicate and transfer data to the cloud in real-time, there is another chip called
NodeMCU (http://www.nodemcu.com) that can directly communicate with live servers.
NodeMCU integrates the firmware with inbuilt Wi-Fi connectivity so that direct interaction
with the network can be done without physical jumper wires. It is an Arduino-like board that
is used for interfacing with the cloud in real-time so that the live signals or data can be logged
or stored for predictive analysis.

Storing sensor data in IoT platforms


There are a number of IoT platforms that can be used for the storage of real-time streaming
sensor data from Arduino and similar boards. Once the circuit is created using Arduino, the
signals can be transmitted and stored directly onto IoT platforms. These open source IoT
platforms can be used for storage, processing, prediction and visualisation of the data
recorded from the sensors on the Arduino board.
A few prominent IoT platforms that can be used for device management and the visualisation
of data are:
 ThingSpeak: thingspeak.com
 Carriots: carriots.com
 KAA:  kaaproject.org
 Things Board: thingsboard.io
 MainFlux: mainflux.com
 Thinger: thinger.io
 DeviceHive: devicehive.com

Transferring data to ThingSpeak

28
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

ThingSpeak (https://www.thingspeak.com) is a widely used open IoT platform for the


collection, storage and visualisation of real-time sensor data fetched from Arduino, Raspberry
Pi, BeagleBone Black and similar boards. Live streaming data on the cloud can be preserved
using channels in ThingSpeak.
To work with ThingSpeak, a free account can be created. After successfully signing up on
ThingSpeak, a channel is created on the dashboard and a ‘Write Key’ is generated, which is
to be used in the Arduino code.
The key features of ThingSpeak include the following:
1. Configuration of devices for sending data to a cloud based environment.
2. Aggregation of sensor data from different devices and motherboards connected with
sensors.
3. Visualisation of real-time sensor data including historical records.
4. Pre-processing and evaluation of sensor data for predictive analysis and knowledge
discovery.
The source code that can be customised and executed on Arduino IDE so that real-time data
can be fetched on board and then transmitted to ThingSpeak is given below. For simplicity,
the
execution of the code is done for NodeMCU hardware, which has inbuilt Wi-Fi for sending
the data to the IoT platform.
Step 1: Wiring

Step 2: Code 1 - Manual Control


/*
Manual Linear Actuator Control using an Arduino and two pushbuttons
This demo shows how to do basic manual control of a large linear
actuator using an Arduino and two buttons. The first button extends
the actuator and the second retracts the actuator.
The circuit:
* RobotGeek Pushbutton - Digital Pin 2
* RobotGeek Pushbutton - Digital Pin 4
* RobotGeek Relay - Digital Pin 7
* RobotGeek Relay - Digital Pin 8
Products Used in this demo:
- http://www.robotgeek.com/linear-actuators
- http://www.robotgeek.com/robotgeek-geekduino-sensor-kit
- http://www.robotgeek.com/robotGeek-pushbutton
- http://www.robotgeek.com/robotgeek-relay
*/
// constants won't change. They're used here to set pin numbers:

29
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

const int button1Pin = 2; // the number of the pushbutton1 pin


const int button2Pin = 4; // the number of the pushbutton2 pin
const int relay1Pin = 7; // the number of the Realy1 pin
const int relay2Pin = 8; // the number of the Relay2 pin
// variables will change:
int button1State = 0; // variable for reading the pushbutton
status
int button2State = 0; // variable for reading the pushbutton
status
const int sensorPin = 0; // select the input pin for the
potentiometer
int sensorValue = 0; // variable to store the value coming from the
sensor
void setup() {

//start serial connection


Serial.begin(9600);

// initialize the pushbutton pin as an input:


pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
// initialize the relay pin as an output:
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
}
void loop(){

// read the value from the sensor:


sensorValue = analogRead(sensorPin);
//print out the value of the pushbutton
Serial.println(sensorValue);

// read the state of the pushbutton values:


button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// check if the pushbutton1 is pressed.
// if it is, the buttonState is HIGH:
// we also ensure tha the other button is not pushed to avoid conflict
if (button1State == HIGH && button2State == LOW) {
// turn relay1 on:
digitalWrite(relay1Pin, HIGH);
}
// When we let go of the button, turn off the relay
else if (digitalRead(relay1Pin) == HIGH) {
// turn relay1 off:
digitalWrite(relay1Pin, LOW);
}

// repeat the same procedure for the second pushbutton


if (button1State == LOW && button2State == HIGH) {
// turn relay2 on:
digitalWrite(relay2Pin, HIGH);
}
// When we let go of the button, turn off the relay
else if (digitalRead(relay2Pin) == HIGH) {
// turn relay2 off:
digitalWrite(relay2Pin, LOW);
}
}

30
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Step 3: Code 2 - Preset Position using Positional Feedback


/*
Linear Actuator Control using preset position
This demo shows how to do basic control of a large linear
actuator using an Arduino and two buttons. Each button is hard
coded with a preset position. Pressing a button will move
the actuator to that position.
The circuit:
* RobotGeek Pushbutton - Digital Pin 1
* RobotGeek Pushbutton - Digital Pin 2
* RobotGeek Relay - Digital Pin 4
* RobotGeek Relay - Digital Pin 7
Products Used in this demo:
- http://www.robotgeek.com/linear-actuators
- http://www.robotgeek.com/robotgeek-geekduino-sensor-kit
- http://www.robotgeek.com/robotGeek-pushbutton
- http://www.robotgeek.com/robotgeek-relay
*/
// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2; // the number of the pushbutton1 pin
const int button2Pin = 4; // the number of the pushbutton2 pin
const int relay1Pin = 7; // the number of the Realy1 pin
const int relay2Pin = 8; // the number of the Relay2 pin
const int sensorPin = 0; // select the input pin for the
potentiometer
// variables will change:
int button1State = 0; // variable for reading the pushbutton
status
int button2State = 0; // variable for reading the pushbutton
status
int sensorValue = 0; // variable to store the value coming from the
sensor
int goalPosition = 350;
int CurrentPosition = 0;
boolean Extending = false;
boolean Retracting = false;
void setup() {
//start serial connection
Serial.begin(9600);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
// initialize the relay pin as an output:
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);

//preset the relays to LOW


digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);

}
void loop(){

// read the value from the sensor:


CurrentPosition = analogRead(sensorPin);

// print the results to the serial monitor:


Serial.print("Current = " );
Serial.print(CurrentPosition);
Serial.print("\t Goal = ");
Serial.println(goalPosition);

31
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

// read the state of the pushbutton values:


button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State == HIGH) {
// set new goal position
goalPosition = 300;

if (goalPosition > CurrentPosition) {


Retracting = false;
Extending = true;
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
Serial.println("Extending");
}
else if (goalPosition < CurrentPosition) {
Retracting = true;
Extending = false;
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH);
Serial.println("Retracting");
}
}
if (button2State == HIGH) {
// set new goal position
goalPosition = 500;

if (goalPosition > CurrentPosition) {


Retracting = false;
Extending = true;
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
Serial.println("Extending");
}
else if (goalPosition < CurrentPosition) {
Retracting = true;
Extending = false;
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH);
Serial.println("Retracting");
}
}
if (Extending = true && CurrentPosition > goalPosition) {
//we have reached our goal, shut the relay off
digitalWrite(relay1Pin, LOW);
boolean Extending = false;
Serial.println("IDLE");
}

if (Retracting = true && CurrentPosition < goalPosition){


//we have reached our goal, shut the relay off
digitalWrite(relay2Pin, LOW);
boolean Retracting = false;
Serial.println("IDLE");
}

}
Results : Hence we had developed a program to Control an actuator through cloud
using any one development board (eg., arduino or raspberry pi).
SANSKRITHI SCHOOL OF ENGINEERING, PUTTAPARTHI.

32
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

EXP-7 Design an IoT based air pollution control system which DATE:
monitors the air pollution by measuring carbon monoxide,
ammonia, etc and gives alarm or sends message when the
pollution level is more than permitted range.

AIM: Design an IoT based air pollution control system which monitors the air pollution
by measuring carbon monoxide, ammonia, etc and gives alarm or sends message when
the pollution level is more than permitted range.

COMPONENTS REQUIRED:

SL.NO COMPONENTS QUANTITY


1. ARDUINO BOARD 1
2. ESP8266 MICROCHIP WITH WI-FI 1
CONNECTIVITY
3. NODEMCU FIRMWARE FOR THE INTERNET 1
OF THINGS (IOT)
4. CONNECTING WIRES 10
5. PC 1
6. BREADBOARD 1
Arduino UNO

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.

Let's discuss each component in detail.

1. ATmega328 Microcontroller- It is a single chip Microcontroller of the ATmel family.


The processor code inside it is of 8-bit. It combines Memory (SRAM, EEPROM, and
Flash), Analog to Digital Converter, SPI serial ports, I/O lines, registers, timer, external
and internal interrupts, and oscillator.

2. ICSP pin - The In-Circuit Serial Programming pin allows the user to program using the
firmware of the Arduino board.

3. Power LED Indicator- The ON status of LED shows the power is activated. When the
power is OFF, the LED will not light up.

33
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

4. Digital I/O pins- The digital pins have the value HIGH or LOW. The pins numbered from
D0 to D13 are digital pins.

5. TX and RX LED's- The successful flow of data is represented by the lighting of these
LED's.
6. AREF- The Analog Reference (AREF) pin is used to feed a reference voltage to the
Arduino UNO board from the external power supply.

7. Reset button- It is used to add a Reset button to the connection.

8. USB- It allows the board to connect to the computer. It is essential for the programming of
the Arduino UNO board.

9. Crystal Oscillator- The Crystal oscillator has a frequency of 16MHz, which makes the
Arduino UNO a powerful board.

10. Voltage Regulator- The voltage regulator converts the input voltage to 5V.
11. GND- Ground pins. The ground pin acts as a pin with zero voltage.
12. Vin- It is the input voltage.

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

Arduino
Arduino (https://www.arduino.cc) is an OSH and electronics platform for real-time
applications.
It is available in many variants, and can be selected depending upon the type of application
for which it is required. There are different boards available that can be programmed for
specific applications.
Arduino boards are available for:

 Beginners

 Enhanced features

 Internet of Things (IoT)

34
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

 Education

 Wearable devices
Arduino UNO is one of the widely used microcontroller boards for beginners. This board can
be easily programmed using the Arduino IDE, available
at https://www.arduino.cc/en/Main/Software. The Arduino IDE software is available for
multiple platforms including Windows, Mac OSX and Linux. C and C++ are the base
programming languages used to program and customise the behaviour of the Arduino
microcontroller board. The Arduino IDE can be downloaded without any cost, along with the
live updates of the software.

Preparing Arduino for Internet of Things (IoT) applications

Arduino can be connected with live platforms of Internet of Things (IoT) by using Wi-Fi or
similar network based chips. ESP8266 is a very popular device for communication with the
Internet that can be easily interfaced with Arduino microcontroller boards. The ESP8266 Wi-
Fi chip is very low cost and can be directly connected with the Arduino board to establish
communication with the real cloud.

NodeMCU, an open source IoT based hardware platform with inbuilt Wi-Fi: To
communicate and transfer data to the cloud in real-time, there is another chip called
NodeMCU (http://www.nodemcu.com) that can directly communicate with live servers.
NodeMCU integrates the firmware with inbuilt Wi-Fi connectivity so that direct interaction
with the network can be done without physical jumper wires. It is an Arduino-like board that
is used for interfacing with the cloud in real-time so that the live signals or data can be logged
or stored for predictive analysis.

35
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Storing sensor data in IoT platforms


There are a number of IoT platforms that can be used for the storage of real-time streaming
sensor data from Arduino and similar boards. Once the circuit is created using Arduino, the
signals can be transmitted and stored directly onto IoT platforms. These open source IoT
platforms can be used for storage, processing, prediction and visualisation of the data
recorded from the sensors on the Arduino board.
A few prominent IoT platforms that can be used for device management and the visualisation
of data are:
 ThingSpeak: thingspeak.com
 Carriots: carriots.com
 KAA:  kaaproject.org
 Things Board: thingsboard.io
 MainFlux: mainflux.com
 Thinger: thinger.io
 DeviceHive: devicehive.com

Transferring data to ThingSpeak


ThingSpeak (https://www.thingspeak.com) is a widely used open IoT platform for the
collection, storage and visualisation of real-time sensor data fetched from Arduino, Raspberry
Pi, BeagleBone Black and similar boards. Live streaming data on the cloud can be preserved
using channels in ThingSpeak.
To work with ThingSpeak, a free account can be created. After successfully signing up on
ThingSpeak, a channel is created on the dashboard and a ‘Write Key’ is generated, which is
to be used in the Arduino code.
The key features of ThingSpeak include the following:
1. Configuration of devices for sending data to a cloud based environment.
2. Aggregation of sensor data from different devices and motherboards connected with
sensors.
3. Visualisation of real-time sensor data including historical records.
4. Pre-processing and evaluation of sensor data for predictive analysis and knowledge
discovery.
The source code that can be customised and executed on Arduino IDE so that real-time data
can be fetched on board and then transmitted to ThingSpeak is given below. For simplicity,
the
execution of the code is done for NodeMCU hardware, which has inbuilt Wi-Fi for sending
the data to the IoT platform.

36
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

In this project we are going to make an IoT Based Air Pollution Monitoring System in
which we will monitor the Air Quality over a webserver using internet and will trigger a
alarm when the air quality goes down beyond a certain level, means when there are sufficient
amount of harmful gases are present in the air like CO2, smoke, alcohol, benzene and NH3. It
will show the air quality in PPM on the LCD and as well as on webpage so that we can
monitor it very easily.
Previously we have built the LPG detector using MQ6 sensor, Smoke detector using MQ2
sensor, and Air Quality Analyser but this time we have used MQ135 sensor as the air quality
sensor which is the best choice for monitoring Air Quality as it can detects most harmful
gases and can measure their amount accurately. In this IOT project, you can monitor the
pollution level from anywhere using your computer or mobile. We can install this system
anywhere and can also trigger some device when pollution goes beyond some level, like we
can switch on the Exhaust fan or can send alert SMS/mail to the user.

Required Components:

 MQ135 Gas sensor

 Arduino Uno

 Wi-Fi module ESP8266

 16X2 LCD

 Breadboard

 10K potentiometer

 1K ohm resistors

 220 ohm resistor

 Buzzer
You can buy all the above components from here.

Circuit Diagram and Explanation:


First of all we will connect the ESP8266 with the Arduino. ESP8266 runs on 3.3V and if
you will give it 5V from the Arduino then it won’t work properly and it may get damage.
Connect the VCC and the CH_PD to the 3.3V pin of Arduino. The RX pin of ESP8266 works
on 3.3V and it will not communicate with the Arduino when we will connect it directly to the
Arduino. So, we will have to make a voltage divider for it which will convert the 5V into
3.3V. This can be done by connecting three resistors in series like we did in the circuit.
Connect the TX pin of the ESP8266 to the pin 10 of the Arduino and the RX pin of the
esp8266 to the pin 9 of Arduino through the resistors.
ESP8266 Wi-Fi module gives your projects access to Wi-Fi or internet. It is a very cheap
device and make your projects very powerful. It can communicate with any microcontroller
and it is the most leading devices in the IOT platform. Learn more about using ESP8266 with
Arduino here.

37
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Then we will connect the MQ135 sensor with the Arduino. Connect the VCC and the
ground pin of the sensor to the 5V and ground of the Arduino and the Analog pin of sensor to
the A0 of the Arduino.
Connect a buzzer to the pin 8 of the Arduino which will start to beep when the condition
becomes true.
In last, we will connect LCD with the Arduino. The connections of the LCD are as follows

 Connect pin 1 (VEE) to the ground.

 Connect pin 2 (VDD or VCC) to the 5V.

 Connect pin 3 (V0) to the middle pin of the 10K potentiometer and connect the other
two ends of the potentiometer to the VCC and the GND. The potentiometer is used to
control the screen contrast of the LCD. Potentiometer of values other than 10K will
work too.

 Connect pin 4 (RS) to the pin 12 of the Arduino.

 Connect pin 5 (Read/Write) to the ground of Arduino. This pin is not often used so we
will connect it to the ground.

 Connect pin 6 (E) to the pin 11 of the Arduino. The RS and E pin are the control pins
which are used to send data and characters.

 The following four pins are data pins which are used to communicate with the
Arduino.
Connect pin 11 (D4) to pin 5 of Arduino.
Connect pin 12 (D5) to pin 4 of Arduino.
Connect pin 13 (D6) to pin 3 of Arduino.
Connect pin 14 (D7) to pin 2 of Arduino.

 Connect pin 15 to the VCC through the 220 ohm resistor. The resistor will be used to
set the back light brightness. Larger values will make the back light much more
darker.

 Connect pin 16 to the Ground.

38
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Working Explanation:
The MQ135 sensor can sense NH3, NOx, alcohol, Benzene, smoke, CO2 and some other
gases, so it is perfect gas sensor for our Air Quality Monitoring Project. When we will
connect it to Arduino then it will sense the gases, and we will get the Pollution level in PPM
(parts per million). MQ135 gas sensor gives the output in form of voltage levels and we need
to convert it into PPM. So for converting the output in PPM, here we have used a library for
MQ135 sensor, it is explained in detail in “Code Explanation” section below.
Sensor was giving us value of 90 when there was no gas near it and the safe level of air
quality is 350 PPM and it should not exceed 1000 PPM. When it exceeds the limit of 1000
PPM, then it starts cause Headaches, sleepiness and stagnant, stale, stuffy air and if exceeds
beyond 2000 PPM then it can cause increased heart rate and many other diseases.
When the value will be less than 1000 PPM, then the LCD and webpage will display “Fresh
Air”.  Whenever the value will increase 1000 PPM, then the buzzer will start beeping and the
LCD and webpage will display “Poor Air, Open Windows”. If it will increase 2000 then the
buzzer will keep beeping and the LCD and webpage will display “Danger! Move to fresh
Air”.

Code Explanation:
Before beginning the coding for this project, we need to first Calibrate the MQ135 Gas
sensor. There are lots of calculations involved in converting the output of sensor into PPM
value, we have done this calculation before in our previous Smoke Detector project. But here
we are using the Library for MQ135, you can download and install this MQ135 library from
here: https://github.com/GeorgK/MQ135.
Using this library you can directly get the PPM values, by just using the below two lines:
MQ135 gasSensor = MQ135(A0);

39
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

float air_quality = gasSensor.getPPM();


But before that we need to calibrate the MQ135 sensor, for calibrating the sensor upload the
below given code and let it run for 12 to 24 hours and then get the RZERO value.
#include "MQ135.h"
void setup (){
Serial.begin (9600);
}
void loop() {
MQ135 gasSensor = MQ135(A0); // Attach sensor to pin A0
float rzero = gasSensor.getRZero();
Serial.println (rzero);
delay(1000);
}
After getting the RZERO value. Put the RZERO value in the library file you downloaded
"MQ135.h": #define RZERO 494.63
Now we can begin the actual code for our Air quality monitoring project.
In the code, first of all we have defined the libraries and the variables for the Gas sensor and
the LCD. By using the Software Serial Library, we can make any digital pin as TX and RX
pin. In this code, we have made Pin 9 as the RX pin and the pin 10 as the TX pin for the
ESP8266. Then we have included the library for the LCD and have defined the pins for the
same. We have also defined two more variables: one for the sensor analog pin and other for
storing air_quality value.
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(9,10);
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11, 5, 4, 3, 2);
const int sensorPin= 0;
int air_quality;
Then we will declare the pin 8 as the output pin where we have connected the buzzer.
lcd.begin(16,2) command will start the LCD to receive data and then we will set the cursor to
first line and will print the ‘circuitdigest’. Then we will set the cursor on the second line and
will print ‘Sensor Warming’.
pinMode(8, OUTPUT);
lcd.begin(16,2);
lcd.setCursor (0,0);
lcd.print ("circuitdigest ");
lcd.setCursor (0,1);
lcd.print ("Sensor Warming ");
delay(1000);
Then we will set the baud rate for the serial communication. Different ESP’s have different
baud rates so write it according to your ESP’s baud rate. Then we will send the commands to
set the ESP to communicate with the Arduino and show the IP address on the serial monitor.
Serial.begin(115200);
esp8266.begin(115200);

40
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

sendData("AT+RST\r\n",2000,DEBUG);
sendData("AT+CWMODE=2\r\n",1000,DEBUG);
sendData("AT+CIFSR\r\n",1000,DEBUG);
sendData("AT+CIPMUair_quality=1\r\n",1000,DEBUG);
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG);
pinMode(sensorPin, INPUT);
lcd.clear();
For printing the output on the webpage in web browser, we will have to use HTML
programming. So, we have created a string named webpage and stored the output in it. We
are subtracting 48 from the output because the read() function returns the ASCII decimal
value and the first decimal number which is 0 starts at 48.
if(esp8266.available())
{
if(esp8266.find("+IPD,"))
{
delay(1000);
int connectionId = esp8266.read()-48;
String webpage = "<h1>IOT Air Pollution Monitoring System</h1>";
webpage += "<p><h2>";
webpage+= " Air Quality is ";
webpage+= air_quality;
webpage+=" PPM";
webpage += "<p>";
The following code will call a function named sendData and will send the data & message
strings to the webpage to show.
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);

cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
The following code will print the data on the LCD. We have applied various conditions for
checking air quality, and LCD will print the messages according to conditions and buzzer
will also beep if the pollution goes beyond 1000 PPM.
lcd.setCursor (0, 0);
lcd.print ("Air Quality is ");
lcd.print (air_quality);
lcd.print (" PPM ");
lcd.setCursor (0,1);
if (air_quality<=1000)
{
lcd.print("Fresh Air");
digitalWrite(8, LOW);

41
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

Finally the below function will send and show the data on the webpage. The data we stored in
string named ‘webpage’ will be saved in string named ‘command’. The ESP will then read
the character one by one from the ‘command’ and will print it on the webpage.
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}

Testing and Output of the Project:


Before uploading the code, make sure that you are connected to the Wi-Fi of your ESP8266
device. After uploading, open the serial monitor and it will show the IP address like shown
below.

Type this IP address in your browser, it will show you the output as shown below. You will
have to refresh the page again if you want to see the current Air Quality Value in PPM.

42
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

We have setup a local server to demonstrate its working, you can check the Video below. But
to monitor the air quality from anywhere in the world, you need to forward the port 80
(used for HTTP or internet) to your local or private IP address (192.168*) of you device.
After port forwarding all the incoming connections will be forwarded to this local address
and you can open above shown webpage by just entering the public IP address of your
internet from anywhere. You can forward the port by logging into your router (192.168.1.1)
and find the option to setup the port forwarding.
Code
#include "MQ135.h"

#include <SoftwareSerial.h>

#define DEBUG true

SoftwareSerial esp8266(9,10); // This makes pin 9 of Arduino as RX pin and pin 10 of


Arduino as the TX pin

const int sensorPin= 0;

int air_quality;

#include <LiquidCrystal.h> 

LiquidCrystal lcd(12,11, 5, 4, 3, 2);

void setup() {

pinMode(8, OUTPUT);

lcd.begin(16,2);

43
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

lcd.setCursor (0,0);

lcd.print ("circuitdigest ");

lcd.setCursor (0,1);

lcd.print ("Sensor Warming ");

delay(1000);

Serial.begin(115200);

esp8266.begin(115200); // your esp's baud rate might be different

  sendData("AT+RST\r\n",2000,DEBUG); // reset module

  sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point

  sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address

  sendData("AT+CIPMUair_quality=1\r\n",1000,DEBUG); // configure for multiple


connections

  sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80

pinMode(sensorPin, INPUT);        //Gas sensor will be an input to the arduino

lcd.clear();

void loop() {

MQ135 gasSensor = MQ135(A0);

float air_quality = gasSensor.getPPM();

if(esp8266.available()) // check if the esp is sending a message 

 {

    if(esp8266.find("+IPD,"))

  {

     delay(1000);

     int connectionId = esp8266.read()-48; /* We are subtracting 48 from the output because


the read() function returns the ASCII decimal value and the first decimal number which is 0

44
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

starts at 48*/ 

     String webpage = "<h1>IOT Air Pollution Monitoring System</h1>";

       webpage += "<p><h2>";   

       webpage+= " Air Quality is ";

       webpage+= air_quality;

       webpage+=" PPM";

       webpage += "<p>";

     if (air_quality<=1000)

  webpage+= "Fresh Air";

else if(air_quality<=2000 && air_quality>=1000)

  webpage+= "Poor Air";

else if (air_quality>=2000 )

webpage+= "Danger! Move to Fresh Air";

webpage += "</h2></p></body>"; 

     String cipSend = "AT+CIPSEND=";

     cipSend += connectionId;

     cipSend += ",";

     cipSend +=webpage.length();

     cipSend +="\r\n";

45
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

   

     sendData(cipSend,1000,DEBUG);

     sendData(webpage,1000,DEBUG);

   

     cipSend = "AT+CIPSEND=";

     cipSend += connectionId;

     cipSend += ",";

     cipSend +=webpage.length();

     cipSend +="\r\n";

   

     String closeCommand = "AT+CIPCLOSE="; 

     closeCommand+=connectionId; // append connection id

     closeCommand+="\r\n";

   

     sendData(closeCommand,3000,DEBUG);

  }

 }

lcd.setCursor (0, 0);

lcd.print ("Air Quality is ");

lcd.print (air_quality);

lcd.print (" PPM ");

lcd.setCursor (0,1);

if (air_quality<=1000)

lcd.print("Fresh Air");

46
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

digitalWrite(8, LOW);

else if( air_quality>=1000 && air_quality<=2000 )

lcd.print("Poor Air, Open Windows");

digitalWrite(8, HIGH );

else if (air_quality>=2000 )

lcd.print("Danger! Move to Fresh Air");

digitalWrite(8, HIGH);   // turn the LED on

lcd.scrollDisplayLeft();

delay(1000);

String sendData(String command, const int timeout, boolean debug)

    String response = ""; 

    esp8266.print(command); // send the read character to the esp8266

    long int time = millis();

    while( (time+timeout) > millis())

  {

      while(esp8266.available())

   {

        // The esp has data so display its output to the serial window 

47
SANSKRITHI SCHOOL OF ENGINEERING INTERNET OF THINGS LABORATORY (19A05701P)

        char c = esp8266.read(); // read the next character.

        response+=c;

   } 

  }

    if(debug)

  {

      Serial.print(response);

  }

    return response;

Result: Thus we have developed an IoT based air pollution control system which
monitors the air pollution by measuring carbon monoxide, ammonia, etc and gives
alarm or sends message when the pollution level is more than permitted range.

48

You might also like