0% found this document useful (0 votes)
8 views

Final IOT

The document describes how to control an LED using an Arduino Uno board. It provides the components needed, wiring diagram, code to blink an LED, and conclusions that an LED can be turned on and off with Arduino.

Uploaded by

lipin85350
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)
8 views

Final IOT

The document describes how to control an LED using an Arduino Uno board. It provides the components needed, wiring diagram, code to blink an LED, and conclusions that an LED can be turned on and off with Arduino.

Uploaded by

lipin85350
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/ 55

210180107050 COMP607-IOT

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
1
210180107050 COMP607-IOT

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() {
// LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

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

2
210180107050 COMP607-IOT
Step 3:digitalWrite(LED_BUILTIN, LOW); delay(2000);
// wait for (2 sec = 2000ms)
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.

3
210180107050 COMP607-IOT
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 :

Figure :

4
210180107050 COMP607-IOT

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.

Code :

5
210180107050 COMP607-IOT
#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_api

n);

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.

6
210180107050 COMP607-IOT

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.

7
210180107050 COMP607-IOT

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.

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

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()
{
int value =
digitalRead(irSensor);
Serial.println("");
Serial.print("Sensor Value =
9
210180107050 COMP607-IOT
"); Serial.print(value);
if(value == 0)
{
}
else
{

}digitalWrite(buzzer, HIGH);

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

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 :

10
210180107050 COMP607-IOT

Figure :

11
210180107050 COMP607-IOT
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);

12
210180107050 COMP607-IOT

void loop() {

// Clears the trigger pin


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 :

13
210180107050 COMP607-IOT

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.

Practical – 6
14
210180107050 COMP607-IOT
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.

Figure:

15
210180107050 COMP607-IOT

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

setup()

pinMode(buzzer,

16
210180107050 COMP607-IOT
OUTPUT);

pinMode(sensor,

INPUT); pinMode(led,

OUTPUT);

digitalWrite(buzzer,LO

W);

digitalWrite(sensor,LO

W);

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)==HI

GH);

17
210180107050 COMP607-IOT
}

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

18
210180107050 COMP607-IOT

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 :

19
210180107050 COMP607-IOT

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.

1. Write this program in Arduino IDE.


20
210180107050 COMP607-IOT
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(115

200); delay(10);

dht.begin();

Serial.println("Connecting

to "); Serial.println(ssid);

WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED)

{
delay(500);

21
210180107050 COMP607-IOT
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";

22
210180107050 COMP607-IOT
client.print("POST /update HTTP/1.1\

n"); client.print("Host:

api.thingspeak.com\n");

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

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

client.print("Content-Type: application/x-www-form-

urlencoded\n"); client.print("Content-Length: ");

client.print(postStr.lengt

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

client.print(postStr);

Serial.print("Temperatur

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

23
210180107050 COMP607-IOT

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.

24
210180107050 COMP607-IOT
Temperature :Humidity :

25
210180107050 COMP607-IOT

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.

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

Components required :

 NodeMCU 12E * 1

 LED * 1

 Breadboard * 1

 Jumper wires

Configuration :

Schematic Diagram

Figure :

27
210180107050 COMP607-IOT

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.

 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
28
210180107050 COMP607-IOT
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.

29
210180107050 COMP607-IOT

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

Code :

30
210180107050 COMP607-IOT
#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,OUTP

UT);

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

31
210180107050 COMP607-IOT
server.begin(); // start the server

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.


32
210180107050 COMP607-IOT
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

33
210180107050 COMP607-IOT
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.

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

34
210180107050 COMP607-IOT

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

35
210180107050 COMP607-IOT
 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.

Conclusion :

36
210180107050 COMP607-IOT
From the above practical we demonstrated that using ThingsSpeak cloud through
NodeMCU we can turn on/off the LED using commands from mobile.

Practical – 9
Aim : Controlling Led ON/OFF by giving command using Google
37
210180107050 COMP607-IOT
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 :

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
38
210180107050 COMP607-IOT
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.

39
210180107050 COMP607-IOT

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

40
210180107050 COMP607-IOT

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

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.

41
210180107050 COMP607-IOT

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

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

42
210180107050 COMP607-IOT

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.

 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”.

43
210180107050 COMP607-IOT

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

44
210180107050 COMP607-IOT

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

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.

45
210180107050 COMP607-IOT

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.

46
210180107050 COMP607-IOT

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
"Ashi
sh"
#define WLAN_PASS "12345678"
#define AIO_SERVER
"io.adafruit.c
om" #define AIO_SERVERPORT
1883
#define AIO_USERNAME "DURGESH_SINGH"
#define AIO_KEY
"aio_OQrD206pQvpMfzJhKcPPXBnw79
DL" 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,
47
210180107050 COMP607-IOT
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,

AIO_USERNAME
"/feeds/LED_Control"); void
MQTT_connect();
void setup() {
Serial.begin(1152
00); delay(10);
pinMode(2,OUTP
UT);
// 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

48
210180107050 COMP607-IOT
*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

{
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);
49
210180107050 COMP607-IOT
}
}
Serial.println("MQTT Connected!");
}
OUTPUT :
After uploading of code open your serial monitor and your serial monitor should
look like this:

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.

50
210180107050 COMP607-IOT

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

51
210180107050 COMP607-IOT

Figure :

Procedure :

52
210180107050 COMP607-IOT
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

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

53
210180107050 COMP607-IOT
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

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:

54
210180107050 COMP607-IOT
$ 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.

55

You might also like