Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views62 pages

IOT Mithlesh

This document outlines the practicals conducted by Mithlesh Yadav in the Internet of Things (IoT) course at Government Engineering College, Modasa. It includes various projects using Arduino UNO, NodeMCU, and Raspberry Pi, covering topics like controlling LEDs, measuring temperature and humidity, and using sensors for object detection. Each practical provides a detailed aim, required components, procedures, and conclusions drawn from the experiments.

Uploaded by

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

IOT Mithlesh

This document outlines the practicals conducted by Mithlesh Yadav in the Internet of Things (IoT) course at Government Engineering College, Modasa. It includes various projects using Arduino UNO, NodeMCU, and Raspberry Pi, covering topics like controlling LEDs, measuring temperature and humidity, and using sensors for object detection. Each practical provides a detailed aim, required components, procedures, and conclusions drawn from the experiments.

Uploaded by

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

220160107147 IOT PRACTICALS MITHLESH YADAV

NAME : MITHLESH YADAV

ENROLLMENT NO : 220160107147
SEMESTER : 6
DEPARTMENT : COMPUTER ENGINEERING
DIVISION : B (B3)
SUBJECT : INTERNET OF THINGS (IOT)

6th SEM CE B(B3) Page 1


220160107147 IOT PRACTICALS MITHLESH YADAV

Government Engineering College, Modasa


Department of Computer Engineering
IOT (Laboratory Submission)

Index
Sr. No. Practicals
1. Understanding Arduino UNO, NodeMCU and Raspberry Pi and Arduino IDE.
2. Controlling LED ON/OFF using Arduino UNO.
3. Measuring Temperature and Humidity using Sensor DHT-11 and Arduino UNO.

4. Measuring object presence using IR Sensor and when present, use buzzer for
notification.

5. Measuring object distance using Ultraviolet Sensor and Arduino Uno.


6. Measuring moving object using PIR Sensor and Arduino Uno.
7. Measure temperature using DHT-11 sensor and send it to cloud ThingSpeak using
NodeMCU.

8. Controlling Led ON/OFF by giving command from mobile phone. (Use cloud
ThingSpeak)

9. Controlling Led ON/OFF by giving command using Google Assistant from mobile
phone. (Use cloud Adafruit)

10. Installing Raspbian OS in Raspberry Pi and performing basic practical like LED
on/off.

6th SEM CE B(B3) Page 2


220160107147 IOT PRACTICALS MITHLESH YADAV

Practical – 1
Aim : Understanding Arduino UNO, NodeMCU and Raspberry Pi and
Arduino IDE.

Arduino UNO :

Arduino is an open-source electronics platform based on easy-to-use hardware and


software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a
Twitter message - and turn it into an output - activating a motor, turning on an LED,
publishing something online. You can tell your board what to do by sending a set of
instructions to the microcontroller on the board. To do so you use the Arduino programming
language (based on Wiring), and the Arduino Software (IDE), based on Processing.

Arduino was born at the Ivrea Interaction Design Institute as an easy tool for fast prototyping,
aimed at students without a background in electronics and programming. As soon as it
reached a wider community, the Arduino board started changing to adapt to new needs and
challenges, differentiating its offer from simple 8-bit boards to products for IoT applications,
wearable, 3D printing, and embedded environments. All Arduino boards are completely
open-source, empowering users to build them independently and eventually adapt them to
their particular needs. The software, too, is open-source, and it is growing through the
contributions of users worldwide.

Why Arduino is used ?

Arduino has been used in thousands of different projects and applications. The Arduino
software is easy-to-use for beginners, yet flexible enough for advanced users. It runs on Mac,
Windows, and Linux. Teachers and students use it to build low cost scientific instruments, to
prove chemistry and physics principles, or to get started with programming and robotics.
Designers and architects build interactive prototypes, musicians and artists use it for
installations and to experiment with new musical instruments. Makers, of course, use it to
build many of the projects exhibited at the Maker Faire, for example. Arduino is a key tool to
learn new things. Anyone - children, hobbyists, artists, programmers - can start tinkering just
following the step by step instructions of a kit, or sharing ideas online with other members of
the Arduino community.

6th SEM CE B(B3) Page 3


220160107147 IOT PRACTICALS MITHLESH YADAV

There are many other microcontrollers and microcontroller platforms available for physical
computing. Parallax Basic Stamp, Netmedia's BX-24, Phidgets, MIT's Handyboard, and
many others offer similar functionality. All of these tools take the messy details of
microcontroller programming and wrap it up in an easy-to-use package. Arduino also
simplifies the process of working with microcontrollers, but it offers some advantage for
teachers, students, and interested amateurs over other systems:

➢ Inexpensive - Arduino boards are relatively inexpensive compared to other microcontroller


platforms.
➢ Cross-platform - The Arduino Software (IDE) runs on Windows, Macintosh OSX, and Linux
operating systems. Most microcontroller systems are limited to Windows.
➢ Simple, clear programming environment - The Arduino Software (IDE) is easy-to-use for
beginners, yet flexible enough for advanced users to take advantage of as well. For teachers,
it's conveniently based on the Processing programming environment, so students learning to
program in that environment will be familiar with how the Arduino IDE works.

The key features are −


➢ Arduino boards are able to read analog or digital input signals from different sensors
and turn it into an output such as activating a motor, turning LED on/off, connect to
the cloud and many other actions.
➢ You can control your board functions by sending a set of instructions to the
microcontroller on the board via Arduino IDE (referred to as uploading software).
➢ Unlike most previous programmable circuit boards, Arduino does not need an extra
piece of hardware (called a programmer) in order to load a new code onto the board.
You can simply use a USB cable.
➢ Additionally, the Arduino IDE uses a simplified version of C++, making it easier to
learn to program.
➢ Finally, Arduino provides a standard form factor that breaks the functions of the
micro-controller into a more accessible package.

NodeMCU :

NodeMCU is an open-source Lua based firmware and development board specially targeted
for IoT based Applications. It includes firmware that runs on the ESP8266 Wi-Fi SoC from
Espressif Systems, and hardware which is based on the ESP-12 module.

6th SEM CE B(B3) Page 4


220160107147 IOT PRACTICALS MITHLESH YADAV

Raspberry Pi :

Arduino is a microcontroller, while Raspberry Pi is a full computer. Microcontrollers don’t


usually run a mainstream operating system, but they’re extremely power-efficient, so they
can be great for projects that can’t stay plugged into the mains. You need to use a separate
computer to set up your Arduino, but you can do everything on a Raspberry Pi itself…
including setting up an Arduino. As we said, the two work really well together in some
projects: for example, you might build a robot where the Raspberry Pi handles intensive
processing tasks and provides you with a friendly environment for developing your code,
while the Arduino handles precise real-time control of the motors.

Raspberry Pi is the name of a series of single-board computers made by the Raspberry Pi


Foundation, a UK charity that aims to educate people in computing and create easier access
to computing education.

The Raspberry Pi launched in 2012, and there have been several iterations and variations
released since then. All over the world, people use the Raspberry Pi to learn programming

6th SEM CE B(B3) Page 5


220160107147 IOT PRACTICALS MITHLESH YADAV

skills, build hardware projects, do home automation, implement Kubernetes


clusters and Edge computing, and even use them in industrial applications.

The Raspberry Pi is a very cheap computer that runs Linux, but it also provides a set of GPIO
(general purpose input/output) pins, allowing you to control electronic components for
physical computing and explore the Internet of Things (IoT).

Arduino IDE :
The Arduino Integrated Development Environment (IDE) is a cross-platform application
(for Windows, macOS, Linux) that is written in functions from C and C++. It is used to write
and upload programs to Arduino compatible boards, but also, with the help of third-party
cores, other vendor development boards.

The source code for the IDE is released under the GNU General Public License, version 2.
The Arduino IDE supports the languages C and C++ using special rules of code
structuring. The Arduino IDE supplies a software library from the Wiring project, which
provides many common input and output procedures. User-written code only requires two
basic functions, for starting the sketch and the main program loop, that are compiled and
linked with a program stub main() into an executable cyclic executive program with the GNU

6th SEM CE B(B3) Page 6


220160107147 IOT PRACTICALS MITHLESH YADAV

toolchain, also included with the IDE distribution.The Arduino IDE employs the
program avrdude to convert the executable code into a text file in hexadecimal encoding that
is loaded into the Arduino board by a loader program in the board's firmware. By default,
avrdude is used as the uploading tool to flash the user code onto official Arduino boards.
Arduino IDE is a derivative of the Processing IDE, however as of version 2.0, the Processing
IDE will be replaced with the Visual Studio Code-based Eclipse Theia IDE framework.
With the rising popularity of Arduino as a software platform, other vendors started to
implement custom open source compilers and tools (cores) that can build and upload sketches
to other microcontrollers that are not supported by Arduino's official line of microcontrollers.

6th SEM CE B(B3) Page 7


220160107147 IOT PRACTICALS MITHLESH YADAV

Practical – 2
Aim : Controlling LED ON/OFF using Arduino UNO.

Components required :

• Arduino Uno board * 1

• USB cable * 1

• Resistor (220Ω) * 1

• LED * 1

• Breadboard * 1

• Jumper wires

Configuration :

Schematic Diagram

6th SEM CE B(B3) Page 8


220160107147 IOT PRACTICALS MITHLESH YADAV

Figure :

Figure

Procedure :

Connect one end to the anode (the long pin) of the LED to the 220ohm resistor one end, the
other end of resistor connected to the Pin 9 in arduino , and the cathode (the short pin) of the
LED to GND.

When the pin 9 outputs high level, the current gets through the current limiting resistor to the
anode of the LED. And since the cathode of the LED is connected to GND, the LED will
light up.

Step 1:

Build the circuit.

Step 2:

Write Code in Arduino IDE

Code :

void setup() {

6th SEM CE B(B3) Page 9


220160107147 IOT PRACTICALS MITHLESH YADAV

// LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);

void loop() {

digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
// wait for (1 sec = 1000ms)

digitalWrite(LED_BUILTIN, LOW);
delay(2000);
// wait for (2 sec = 2000ms)
}
Step 3:

Upload the sketch to the Arduino Uno board

Click the Upload icon to upload the code to the control board.

If "Done uploading" appears at the bottom of the window, it means the sketch has been
successfully uploaded.

You should now see the LED blinking.

Conclusion :
From the above practical we demonstrated that a led can be turn off/on using arduino uno.

6th SEM CE B(B3) Page 10


220160107147 IOT PRACTICALS MITHLESH YADAV

Practical – 3
Aim : Measuring Temperature and Humidity using Sensor DHT-11 and
Arduino UNO.

Components required :

• Arduino Uno board * 1

• Arduino USB 2.0 cable * 1

• DHT-11 Temperature and Humidity Sensor (3 pins)* 1

• Breadboard * 1

• Jumper wires

Configuration :

6th SEM CE B(B3) Page 11


220160107147 IOT PRACTICALS MITHLESH YADAV

Figure :

Procedure :

Step 1: Place the sensor


The first thing you are going to do is place the sensor on the breadboard. If you place the
breadboard in the orientation the way it was made, the blue part of the sensor should be
facing you.

Step 2: Connect everything


The pins on the sensor are S, for signal, the one in the middle is voltage, and the minus
sign is ground. The signal pin goes to header A0 on the Arduino. The middle pin goes
to 5V, and the minus sign goes to GND. Our yellow cable is our Analog cable, our red
cable is power, and our black one is ground.

6th SEM CE B(B3) Page 12


220160107147 IOT PRACTICALS MITHLESH YADAV

Code :

#include <dht.h>

#define dht_11_PIN7

dht DHT;

void setup(){

Serial.begin(9600);

delay(500);//Delay to let system boot

Serial.println("DHT11 Humidity & temperature Sensor\n\n");

delay(1000);//Wait before accessing Sensor

void loop(){

DHT.read11(dht_apin);

Serial.print("Current humidity = ");

Serial.print(DHT.humidity);

Serial.print("% ");

Serial.print("temperature = ");

Serial.print(DHT.temperature);

Serial.println("C ");

delay(5000);

Step 3: Adding the DHT library

Now write code in IDE. In the Arduino IDE, go to Sketch >> Include Library >> Add
ZIP file.

6th SEM CE B(B3) Page 13


220160107147 IOT PRACTICALS MITHLESH YADAV

When you click the 'Add .ZIP library', you should get a file window that pops up. Add
the DHT_Library.zip.

NOW upload the code. When it is finished, go to the top right of the Arduino IDE
window and click the little magnifying glass button. That will open the serial monitor,
and the data of the sensor should be displaying itself and updating every 5 seconds.

6th SEM CE B(B3) Page 14


220160107147 IOT PRACTICALS MITHLESH YADAV

OUTPUT :

Conclusion :
From the above practical we demonstrated that arduino uno can be used to measure
temperature and humidity with sensor DHT-11 at an interval of 5 seconds.

6th SEM CE B(B3) Page 15


220160107147 IOT PRACTICALS MITHLESH YADAV

Practical – 4
Aim : Measuring object presence using IR Sensor and when present, use
buzzer for notification.

COMPONENTS:

1. Arduino UNO
2. Jumper Cables
3. Buzzer
4. Bread board
5. IR Sensor

Circuit Diagram – Connections :

PROCEDURE:
1. Build the circuit according to the above circuit diagram.
2. Then, open the Arduino IDE and write the following code:

int irSensor = 12;


int buzzer = 7;
void setup()
{
Serial.begin(9600);
pinMode(irSensor, INPUT);
pinMode(buzzer, OUTPUT);
}
void loop()
{

6th SEM CE B(B3) Page 16


220160107147 IOT PRACTICALS MITHLESH YADAV

int value = digitalRead(irSensor);


Serial.println("");
Serial.print("Sensor Value = ");
Serial.print(value);
if(value == 0)
{
digitalWrite(buzzer, HIGH);
}
else
{
digitalWrite(buzzer, LOW);
}
delay(50);
}

OBSERVATION :

We observed that when an object is placed in front of the IR sensor, buzzer


starts making tone.

CONCLUSION:
From the above practical we can conclude that using IR sensor with arduino
UNO, presence of an object can be detected upto a particular distance from the
sensor

6th SEM CE B(B3) Page 17


220160107147 IOT PRACTICALS MITHLESH YADAV

Practical – 5
Aim : Measuring object distance using Ultraviolet Sensor and Arduino
Uno.

Components required :

• Arduino Uno board * 1

• HC-SR04 Ultrasonic Sensor * 1

• Jump Wires

• BreadBoard * 1

Configuration :

6th SEM CE B(B3) Page 18


220160107147 IOT PRACTICALS MITHLESH YADAV

Figure :

6th SEM CE B(B3) Page 19


220160107147 IOT PRACTICALS MITHLESH YADAV

Procedure :
First, place the distance sensor on one end of the breadboard so that none of the pins are
connected as shown

➢ Connect the pin labeled "trigger" to pin 10 on the Arduino

➢ Connect the pin labeled "echo" to pin 9 on the Arduino

➢ Connect the Ultrasonic Sensor's 5V pin to the 5V pin on the Arduino

➢ Lastly, complete the circuit by connecting the Ultrasonic Sensor's ground pin to the Arduino's

ground pin.

The ultrasonic sensor is a cheap sensor that can measure 2cm to 400cm of non-contact
measurement functionality with a ranging accuracy that can reach up to 3mm. It uses sonar
waves for echolocation, like bats, to be able to measure distances. This project is an
introduction to the use of an ultrasonic sensor, since it is often not straightforward nor
intuitive to wire and code.

Code :

const int trigger = 9;

const int echo = 10;

// defining variables long duration; int distance;

void setup() {

// Sets the trigger pin as an OUTPUT

pinMode(trigger, OUTPUT);

// Sets the echo pin as an INPUT

pinMode(echo, INPUT); /

/ Begins the serial communication

Serial.begin(9600);

void loop() {

// Clears the trigger pin

6th SEM CE B(B3) Page 20


220160107147 IOT PRACTICALS MITHLESH YADAV

digitalWrite(trigger, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigger, HIGH);

delayMicroseconds(10);

digitalWrite(trigger, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echo, HIGH);

// Calculating the distance

distance= duration*0.034/2;

// Prints the distance on the Serial Monitor

Serial.print("Distance: ");

Serial.println(distance);

OUTPUT :

6th SEM CE B(B3) Page 21


220160107147 IOT PRACTICALS MITHLESH YADAV

Conclusion :
From the above practical we demonstrated that using ultraviolet sensor with arduino uno the
distance to an object can be measured in range of 2cm to 400cm.

6th SEM CE B(B3) Page 22


220160107147 IOT PRACTICALS MITHLESH YADAV

Practical – 6
Aim : Measuring moving object using PIR Sensor and Arduino Uno.

Components required :

• Arduino Uno board * 1

• PIR Sensor * 1

• 5V Buzzer * 1

• BreadBoard * 1

• Connecting Wires

Configuration :

The design of the PIR Motion Sensor using Arduino is very simple. The PIR Sensor Module
has three pins: VCC, Digital Out and GND. Connect VCC and GND to +5V and GND
respectively. Then connect the Digital Out Pin of the PIR sensor to the digital I/O pin 8 of
Arduino.

As we need to indicate the detection of motion by the sensor, connect a buzzer to Pin 11 of
the Arduino.

6th SEM CE B(B3) Page 23


220160107147 IOT PRACTICALS MITHLESH YADAV

Figure :

Procedure :

The working of this project is very simple. When the system is powered on, the Arduino
waits for the PIR Sensor to be calibrated. The calibration period is set to 10 seconds and
during this time, there should be no movements in front of the PIR Sensor.

After the calibration, the PIR Sensor will be ready to detect any movement in front of it. If
the PIR Sensor detects any movements, its Digital Out pin, which is connected to Pin 8 of
Arduino will become HIGH.

Arduino will detect this HIGH Signal and activates the buzzer.

Code :

int buzzer = 11;

int sensor = 8;

int led = 13;

void setup()

6th SEM CE B(B3) Page 24


220160107147 IOT PRACTICALS MITHLESH YADAV

pinMode(buzzer, OUTPUT);

pinMode(sensor, INPUT);

pinMode(led, OUTPUT);

digitalWrite(buzzer,LOW);

digitalWrite(sensor,LOW);

digitalWrite(led,LOW);

while(millis()<13000)

digitalWrite(led,HIGH);

delay(50);

digitalWrite(led,LOW);

delay(50);

digitalWrite(led,HIGH);

void loop()

if(digitalRead(sensor)==HIGH)

digitalWrite(buzzer,HIGH);

delay(3000);

digitalWrite(buzzer,LOW);

while(digitalRead(sensor)==HIGH);

6th SEM CE B(B3) Page 25


220160107147 IOT PRACTICALS MITHLESH YADAV

Conclusion :
From the above practical we demonstrated that using PIR sensor with arduino the motion of
an object can be detected.

6th SEM CE B(B3) Page 26


220160107147 IOT PRACTICALS MITHLESH YADAV

Practical – 7
Aim : Measure temperature using DHT-11 sensor and send it to cloud
ThingSpeak using NodeMCU.

Components required :

• NodeMCU * 1

• DHT-11 Temperature and Humidity Sensor (3 pins)* 1

• Breadboard * 1

• Jumper wires

Configuration :

6th SEM CE B(B3) Page 27


220160107147 IOT PRACTICALS MITHLESH YADAV

Figure :

Procedure :

Step 1: Place the sensor


The first thing you are going to do is place the sensor on the breadboard. If you place the
breadboard in the orientation the way it was made, the blue part of the sensor should be
facing you.

Step 2: Getting API Key


1. Go to https://thingspeak.com/ and create an account if you do not have one. Login to
your account.

2. Create a new channel by clicking on the button. Enter the basic details of the channel. Then
Scroll down and save the channel.

3. Then go to API keys copy and paste this key to a separate notepad file. You will need it
later while programming.

The program for Humidity & Temperature Monitoring using DHT11 & NodeMCU on

ThingSpeak is given below.

6th SEM CE B(B3) Page 28


220160107147 IOT PRACTICALS MITHLESH YADAV

1. Write this program in Arduino IDE.


2. Download the DHT11/DHT22 library from GitHub and add it to your library manager.
3. Select the NodeMCU ESP-12E board from the board manager.
4. Paste API Key from thingspeak which you created earlier on a programming section line.
5. Edit the program to change the wifi SSID and password with your own.
6. Compile the code and Upload it to NodeMCU board

Code :
#include <DHT.h> // Including library for dht

#include <ESP8266WiFi.h>

String apiKey = "H38TEGNC0XKW43BB"; // Enter your Write API key from

ThingSpeak

const char *ssid = "how2electronics"; // replace with your wifi ssid and wpa2 key

const char *pass = "alhabibi";

const char* server = "api.thingspeak.com";

#define DHTPIN 0 //pin where the dht11 is connected

DHT dht(DHTPIN, DHT11);

WiFiClient client;

void setup()

{
Serial.begin(115200);

delay(10);

dht.begin();

Serial.println("Connecting to ");

Serial.println(ssid);

WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED)

{
delay(500);

6th SEM CE B(B3) Page 29


220160107147 IOT PRACTICALS MITHLESH YADAV

Serial.print(".");

Serial.println("");

Serial.println("WiFi connected");

void loop()

float h = dht.readHumidity();

float t = dht.readTemperature();

if (isnan(h) || isnan(t))

Serial.println("Failed to read from DHT sensor!");

return;

if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com

String postStr = apiKey;

postStr +="&field1=";

postStr += String(t);

postStr +="&field2=";

postStr += String(h);

postStr += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");

client.print("Host: api.thingspeak.com\n");

client.print("Connection: close\n");

6th SEM CE B(B3) Page 30


220160107147 IOT PRACTICALS MITHLESH YADAV

client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");

client.print("Content-Type: application/x-www-form-urlencoded\n");

client.print("Content-Length: ");

client.print(postStr.length());

client.print("\n\n");

client.print(postStr);

Serial.print("Temperature: ");

Serial.print(t);

Serial.print(" degrees Celcius, Humidity: ");

Serial.print(h);

Serial.println("%. Send to Thingspeak.");

client.stop();

Serial.println("Waiting...");

// thingspeak needs minimum 15 sec delay between updates

delay(1000);

6th SEM CE B(B3) Page 31


220160107147 IOT PRACTICALS MITHLESH YADAV

OUTPUT :
On every successful data upload, success message is displayed on serial monitor

Output of this practical is seen on Thingspeak and serial monitor. Open channel at
Thingspeak and output will be shown as mentioned ahead.

6th SEM CE B(B3) Page 32


220160107147 IOT PRACTICALS MITHLESH YADAV

Temperature :

6th SEM CE B(B3) Page 33


220160107147 IOT PRACTICALS MITHLESH YADAV

Humidity :

Conclusion :
From the above practical we demonstrated that using NodeMCU, the data (temperature and
humidity measured by DHT-11) can be send to the ThingsSpeak cloud.

6th SEM CE B(B3) Page 34


220160107147 IOT PRACTICALS MITHLESH YADAV

Practical – 8
Aim : Controlling Led ON/OFF by giving command from mobile phone.
(Use cloud ThingSpeak)

Components required :

• NodeMCU 12E * 1

• LED * 1

• Breadboard * 1

• Jumper wires

Configuration :

Schematic Diagram

Figure :

6th SEM CE B(B3) Page 35


220160107147 IOT PRACTICALS MITHLESH YADAV

Procedure :

Step1: Setting ThingsSpeak

➢ First login to the Thingspeak server. https://thingspeak.com/login


➢ If you are new user then create the new account.
➢ after login you show this type of webpage, i have already created two project . but if
you are new user then click on the New Channel.

6th SEM CE B(B3) Page 36


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ After click the new channel and first write the name of the new channel .
➢ if you want give the description then write the description. and select the Field 1 and
don't forget the click on checkbox, after click on the check box scroll the page and
click on the save button.

➢ I give the new channel name is LED then select the Field 1 and scroll the page and
click on the save channel button.

➢ Now show your field 1 as a charts.

6th SEM CE B(B3) Page 37


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ Here filed is empty because I do not upload any data on the channel. Now our goal to blink
the led via webserver.

Firmware :

➢ Now write the code in Arduino IDE. open the Arduino IDE and select the NodeMCU
12E board

After installing click on the NodeMCU 1.0 borad.

6th SEM CE B(B3) Page 38


220160107147 IOT PRACTICALS MITHLESH YADAV

Code :
#include<ThingSpeak.h>

#include<ESP8266WiFi.h>

#include<ESP8266WebServer.h>

unsigned long channel_num=1070593;

const char* ssid="shravan";

const char* password="8905552134";

int led;

unsigned int value;

WiFiClient client; // make the client of the WiFi which connect to the ThingSpeak webServer

ESP8266WebServer server(80); // make the naother server

pinMode(D1,OUTPUT);

digitalWrite(D1,0);

Serial.begin(115200);

WiFi.begin(ssid,password); // connect to the wifi STA connection

while(WiFi.status()!=WL_CONNECTED)

delay(500);

Serial.print(".");

Serial.println(WiFi.localIP()); // print the wifi local ip

server.on("/",handleonconnect); // in urt type the "/" then call the handle on connect function

ThingSpeak.begin(client); // connect the client to the thingSpeak server

server.begin(); // start the server

6th SEM CE B(B3) Page 39


220160107147 IOT PRACTICALS MITHLESH YADAV

server.handleClient(); // it realy handle the Client

led=ThingSpeak.readFloatField(channel_num,1); // rad the last data of the field 1

if(led==1)

digitalWrite(D1,1);

else if(led==0)

digitalWrite(D1,0);

server.send(200,"text/html",SendHTML());

➢ Show the Thingspeak server where we create the our new channel. and copy the
channel id and pate to the our Arduino IDE.

➢ in this page Channel ID: 1070593, it’s written in code.

6th SEM CE B(B3) Page 40


220160107147 IOT PRACTICALS MITHLESH YADAV

HTML :
String SendHTML(void){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0,
user-scalable=no\">\n";
ptr +="<title>LED Control</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-
align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color:
#444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color:
white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto
35px;cursor: pointer;border-radius: 4px;}\n";
ptr +=".button-on {background-color: #1abc9c;}\n";
ptr +=".button-on:active {background-color: #16a085;}\n";
ptr +=".button-off {background-color: #34495e;}\n";
ptr +=".button-off:active {background-color: #2c3e50;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>ESP8266 with ThingSpeak Server</h1>\n";
ptr +="<h3>Using Station(STA) Mode</h3>\n";
ptr +="<h4>Control status For D1</h4>\n";
ptr +="<a class=\"button button-on\"
href=\"https://api.thingspeak.com/update?api_key=MOHD33LYGVXTG5UF&field1=1\">O
N</a>\n";
ptr +="<a class=\"button button-off\"
href=\"https://api.thingspeak.com/update?api_key=MOHD33LYGVXTG5UF&field1=0\">O
FF</a>\n";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;

➢ That is my HTML code if you are an embedded engineer then do not learn the HTML and
CSS, but knowledge abut the how it work we press the any url.
➢ https://api.thingspeak.com/update?api_key=MOHD33LYGVXTG5UF&field1=0 thi
s url is copy from the thingserver here api_key is different in your case last one
&field1=0 mean we we press this url then send the 0 on your thingspeak server to the
field1 and &field1=1 mean when we press this url then send the 1 on your thingspeak
server to the field1. but we don't press the url using HTML code we only clink on the ON
button to turn on the led and OFF button for turn off the led.

6th SEM CE B(B3) Page 41


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ Now go to the thingspeak server click on the API KEYS and copy the Write API
KEY and paste the HTML code code which highlighted on the upper HTML code pic.

➢ so for that click on sharing button and click the Share channel view with everyone.

6th SEM CE B(B3) Page 42


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ Finally upload the code on the NodeMCU. show the serial port wifi is connect or not if
connect then give the local ip where we print on serial.

➢ Finally this IP copy on the Chrome browser. then show the this type.

6th SEM CE B(B3) Page 43


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ Now click on the ON button LED is ON .


➢ Now click on the OFF button LED is OFF.

OUTPUT :

➢ So finally LED is ON
➢ Now show the status of the thingSpeak server.

6th SEM CE B(B3) Page 44


220160107147 IOT PRACTICALS MITHLESH YADAV

Conclusion :
From the above practical we demonstrated that using ThingsSpeak cloud through NodeMCU
we can turn on/off the LED using commands from mobile.

6th SEM CE B(B3) Page 45


220160107147 IOT PRACTICALS MITHLESH YADAV

Practical – 9
Aim : Controlling Led ON/OFF by giving command using Google Assistant
from mobile phone. (Use cloud Adafruit)

Components :

• Breadboard * 1

• ESP32 Module * 1

• USB Cable * 1

• LED

• Resistor and Jumper wires

• Google Assistance Enabled device

• Account on AdaFruit IO

• Account on IFTTT

• Google Account – Same which are using Google Assistance

Circuit Diagram – Connections :

6th SEM CE B(B3) Page 46


220160107147 IOT PRACTICALS MITHLESH YADAV

Procedure :

Step 1 : Setting up Adafruit IO Account for IOT controlled LED


Adafruit IO is an IOT platform built around the MQTT Protocol. MQTT is a
lightweight messaging protocol that provides resource-constrained network
clients with a simple way to distribute telemetry information. The protocol
which uses a publish/subscribe communication pattern, is used for machine to
machine (M2M) communication and plays an important role in the Internet of
Things (IoT).
For this project you have to follow following steps to getting started with
Adafruit IO:
➢ Visit https://io.adafruit.com and create an account.

➢ After creating Account you will be taken to your home screen. Click
on “Feeds” from the menu.

6th SEM CE B(B3) Page 47


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ Now click on New Feed and then create a New feed. Then it will ask you to
name your feed I am giving it LED_Control, you can give according to you
and then create and your feed is created.

➢ Now go to “Dashboards” from the menu. Click on New Dashboard and it


will create a new dashboard, give it name as you want; I am
giving “LEDSwitch” and then click on Create and your dashboard will now
created.

6th SEM CE B(B3) Page 48


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ Now open your new dashboard by simply clicking on it and you should be taken
to a mostly blank page. Clicking on blue + button will let you add new UI
components to the dashboard.

6th SEM CE B(B3) Page 49


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ For this practical I just need a button, so select first option, it will ask you to
select the feed, so select the one you just made and keep the defaults for the rest
of the settings.

➢ After selecting your dashboard window will look like this:

➢ During programming you will required your unique AIO key so for this
click on key button at right hand corner of your window on My Key option.

6th SEM CE B(B3) Page 50


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ After clicking on key button your Active key for this project is generated,
don’t share this key with anyone this must be confidential.

Step 2 : Connecting to Google Assistant through IFTTT

In this step, we will connect our Google Assistant to the Adafruit IO MQTT
Broker to allow us to control the lights with voice commands. To do this I
am using IFTTT (If This Then That) platform.

To perform this you need to follow the following procedure:

➢ Go to www.IFTTT.com website and create a new account if you are not


having already. You can sign up using your Google account also.
➢ After creating an account click on your username at right hand corner of
the window and click on “New Applet” button.

6th SEM CE B(B3) Page 51


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ After clicking on New applet you will find a window which ask you ‘If this then
that’. The term IF THIS THEN THAT means if something happens on the
“This” then we have do something on “that”.

➢ Click on + blue button and search for “Google Assistant”, and then select “Say
a simple phrase” from the menu of specific triggers. This will ask you some
details, fill according to you and create trigger.

6th SEM CE B(B3) Page 52


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ Now you have to give Action so click on + button of “That”, and search
for Adafruit and click on “Send data to Adafruit IO”
➢ Now it will ask you to select the Feed name so select the feed that you made
earlier for this project and in Data to save we will send ON for this applet and
click on Create action.

6th SEM CE B(B3) Page 53


220160107147 IOT PRACTICALS MITHLESH YADAV

➢ Once you have created this applet, you have to create another applet for turning
the LED “OFF”. You have to follow the same steps to create another applet.
➢ After creating both the applets go to “My Applets” and you can see both the
applets here.

Programming ESP32 for Google Assistant Controlled LED


Complete program is given at the end of this project. ESP32 is programmed
using Arduino IDE. Connecting ESP32 with Adafruit IO is very easy using
Arduino IDE, you have to include Adafruit MQTT Client Library in your IDE,
for this open your Arduino IDE and go to Sketch--> include library-->Manage
library and search for “adafruit mqtt” then a library associated with this will
be shown to you; you just have to install it.

6th SEM CE B(B3) Page 54


220160107147 IOT PRACTICALS MITHLESH YADAV

After installing this library We are ready to use Adafruit IO with the ESP32.

Code :
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WLAN_SSID "Ashish"
#define WLAN_PASS "12345678"
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "DURGESH_SINGH"
#define AIO_KEY "aio_OQrD206pQvpMfzJhKcPPXBnw79DL"
int output=2;
WiFiClient client; // Create an ESP8266 WiFiClient class to connect to the MQTT server.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT,
AIO_USERNAME, AIO_KEY); // Setup the MQTT client class by passing in the WiFi
client and MQTT server and login details.
Adafruit_MQTT_Subscribe LED_Control = Adafruit_MQTT_Subscribe(&mqtt,

6th SEM CE B(B3) Page 55


220160107147 IOT PRACTICALS MITHLESH YADAV

AIO_USERNAME "/feeds/LED_Control");
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
pinMode(2,OUTPUT);
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
mqtt.subscribe(&LED_Control);
}
uint32_t x=0;
void loop() {
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &LED_Control) {
Serial.print(F("Got: "));
Serial.println((char *)LED_Control.lastread);
if (!strcmp((char*) LED_Control.lastread, "ON"))
{
digitalWrite(2, HIGH);
}
else

6th SEM CE B(B3) Page 56


220160107147 IOT PRACTICALS MITHLESH YADAV

{
digitalWrite(2, LOW);
}
}
}
}
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
OUTPUT :
After uploading of code open your serial monitor and your serial monitor should look
like this:

6th SEM CE B(B3) Page 57


220160107147 IOT PRACTICALS MITHLESH YADAV

Now open Google assistant in your Android and give voice command like “Turn LED
on” or “Turn LED off” and it will respond you like you defined earlier and you will
observe change of LED state also.

Conclusion :

From the above practical we demonstrated that using ESP32 Module with
arduino uno we can control led on/off by giving command from google assistant
and using cloud Adafruit.

6th SEM CE B(B3) Page 58


220160107147 IOT PRACTICALS MITHLESH YADAV

Practical – 10
Aim : Installing Raspbian OS in Raspberry Pi and performing basic
practical like LED on/off.

Components required :

• Rasperry Pi 3 Setup

• Resistor Pack

• Red LED * 1

• Breadboard * 1

• Jumper wires

Configuration :

6th SEM CE B(B3) Page 59


220160107147 IOT PRACTICALS MITHLESH YADAV

Figure :

Procedure :

The first step in this project is to design a simple LED circuit. Then we will make the LED
circuit controllable from the Raspberry Pi by connecting the circuit to the general purpose
input/output (GPIO) pins on the Raspberry Pi.

A simple LED circuit consists of a LED and resistor. The resistor is used to limit the current
that is being drawn and is called a current limiting resistor. Without the resistor the LED
would run at too high of a voltage, resulting in too much current being drawn which in turn
would instantly burn the LED, and likely also the GPIO port on the Raspberry Pi.

To calculate the resistor value we need to examine the specifications of the LED. Specifically
we need to find the forward voltage (VF) and the forward current (IF). A regular red LED has
a forward voltage (VF) of 1.7V and forward current of 20mA (IF). Additionally we need to
know the output voltage of the Raspberry Pi which is 3.3V.

We can then calculate the resistor size needed to limit the current to the LED’s maximum
forward current (IF) using ohm’s law like this:

RΩ=VI=3.3–VFIF=3.3–1.720mA=80Ω
Unfortunately 80 ohm is not a standard size of a resistor. To solve this we can either combine
multiple resistors, or round up to a standard size. In this case we would round up to 100 ohm.

With the value calculated for the current limiting resistor we can now hook the LED and
resistor up to GPIO pin 8 on the Raspberry Pi. The resistor and LED needs to be in series like
the diagram below. To find the right resistor use the resistor color code – for a 100 ohm

6th SEM CE B(B3) Page 60


220160107147 IOT PRACTICALS MITHLESH YADAV

resistor it needs to be brown-black-brown. You can use your multimeter to double check the
resistor value.

When hooking up the circuit note the polarity of the LED. You will notice that the LED has a
long and short lead. The long lead is the positive side also called the anode, the short lead is
the negative side called the cathode. The long should be connected to the resistor and the
short lead should be connected to ground via the blue jumper wire and pin 6 on the Raspberry
Pi as shown on the diagram.

With the circuit created we need to write the Python script to blink the LED. Before we start
writing the software we first need to install the Raspberry Pi GPIO Python module. This is a
library that allows us to access the GPIO port directly from Python.

To install the Python library open a terminal and execute the following

$ sudo apt-get install python-rpi.gpio python3-rpi.gpio

With the library installed now open your favorite Python IDE (I recommend Thonny Python
IDE more information about using it here).

Our script needs to do the following:

• Initialize the GPIO ports


• Turn the LED on and off in 1 second intervals

To initialize the GPIO ports on the Raspberry Pi we need to first import the Python library,
the initialize the library and setup pin 8 as an output pin.

Code :
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library

from time import sleep # Import the sleep function from the time module

GPIO.setwarnings(False) # Ignore warning for now

GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set


initial value to low (off)

while True: # Run forever

GPIO.output(8, GPIO.HIGH) # Turn on

sleep(1) # Sleep for 1 second

GPIO.output(8, GPIO.LOW) # Turn off

sleep(1) # Sleep for 1 second

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library

6th SEM CE B(B3) Page 61


220160107147 IOT PRACTICALS MITHLESH YADAV

from time import sleep # Import the sleep function from the time module

GPIO.setwarnings(False) # Ignore warning for now

GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial
value to low (off)

while True: # Run forever

GPIO.output(8, GPIO.HIGH) # Turn on

sleep(1) # Sleep for 1 second

GPIO.output(8, GPIO.LOW) # Turn off

sleep(1) # Sleep for 1 second

With our program finished, save it as blinking_led.py and run it either inside your IDE or in
the console with:

$ python blinking_led.py

OUTPUT :

Conclusion :
From the above practical we demonstrated that how to install Raspbian OS in Raspberry Pi
and perform basic operation like LED on/off.

6th SEM CE B(B3) Page 62

You might also like