Smart Street Light Using NodeMCU and IOT ThingSpea

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

instructables

Smart Street Light Using NodeMCU and IOT ThingSpeak

by mohdfarhan

There have been a limited number of streets in smart societies, and over the last few decades, street lamps and
management regulation have been relatively simple. However, as the world has become a more prosperous community
and as urbanization has increased, the number of smart societies in modern cities has increased rapidly. As a result,
regulating and maintaining street lights for smart societies and cities became a problem. It is also considered to be the
rst generation of the original street light control, which is ine cient and wastes manpower. At the moment, street
lamps control much of the urban environment only through manual control, via a control switch installed in each street
lamp. A signi cant amount of electrical energy is lost. So we have created a solution for that problem which is an
automatic street light only using NodeMCU, LDR sensor and IR sensor. In this project, LDR sensor is function to glow only
when it in darkness and dark when only in brightness while for IR sensor is used to detect objects by sensing infrared
radiations re ected from the objects which means if there is an object passing by, the light will turn on and if there is no
object passing by, the light will turn o . This detection can be monitored through ThingSpeak and IoT platform on your
computer.
Supplies:

ESP8266 NodeMCU - 1
Micro USB cable - 1
LEDs -3
Jumper wires - Depends on connection
IR sensors - 3
LDR sensors -1

Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 1


Step 1: How the Project Works

As showed on that video above, this step-by-step manufacturing of the project producing an output that will be save
and consume less energy which is the light will glow up only if there are any detection of an object. In fact, WiFi is used to
provide wireless communication, as ThingSpeak that we use in this project is to send the data in a graph gure toward
admin.

https://youtu.be/7_lOt-bkzqA

https://youtu.be/FbbTKxITW6E

Step 2: Components Setup

The connections are pretty basic.


To connect the sensor, there are 3 leads. One end of the LDR sensor is connected to VIN and other end is connected to
Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 2
xed resistance which is further connected to ground. NodeMCU has one ADC pin (A0) which is connected to point
between xed resistance and one end of the LDR sensor as shown in the circuit diagram. Since the LDR sensor gives
variable resistance therefore variable voltage will be generated at A0 according to the amount of light falling on LDR.
While, IR sensor has 3 pins, two of which are VCC and ground and one is output pin. The VCC end is connected to VIN pin,
while ground end connected to GND pin output pin is connected to D0, D1 and D2 pin for three IR sensor respectively.
The output of IR sensor gets high if detects presence of some object. This pin is connected to GPIO pin of NodeMCU so
whenever the IR sensor detects someone passing through the street it triggers the Street light. In our case one LED will
be turned on. Furthurmore, the led has 3 pins, this 3 led connected to D7, D6 and D5 pins respectively.
Detail Instruction:
Bright or Dark Detection
Connect the LDR sensor to pins on Nodemcu ESP8266.
1. Connect a jumper wire from any one of the end to NodeMCU VIN pin
2. Connect a jumper wire from another end to NodeMCU A0 pin
2. Connect a resistor to the NodeMCU GND
Detection of an object
Connect the IR sensor to pins on Nodemcu ESP8266.
1. Connect a jumper wire from VCC to NodeMCU VIN pin
2. Connect a jumper wire from GND to NodeMCU GND pin
3. Connect a jumper wire from Output to one of the pin which is D0, D1 and D2 for IR sensors respectively
Object presence
Connect the LED to pins on NodeMCU ESP8266.
1. Connect a jumper wire from anode end to one of the pin which is D7, D6 and D5 for leds respectively
2. Connect another end of leds to negative line same with the GND line

Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 3


Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 4
Step 3: Installing ThingSpeak Library in Arduino IDE

Step 1: Open Arduino IDE and Go to Sketch -> Include Library -> Manage Libraries
Step2: Now search for ThingSpeak in library manager and install ThingSpeak library by MathWorks

Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 5


Step 4: Uploading Smart Street Light Data on ThingSpeak

Step 1: Now we will upload the LDR sensor, IR sensors and leds data on the ThingSpeak. Go to https://thingspeak.com/
and start by signing up if you don’t have account on it

Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 6


Step 2: Now Click on New Channel and then give some name to your channel and then ll the elds as shown below.
Field 1 is for LDR sensor data, Field 2 to Field 4 are for IR sensors, Field 5 to Field 7 are for LEDS. After lling the details
scroll down and click on “Save Channel”.
Step 3: Your channel will be created and now you will be able to see eld charts. Now Click on API Keys and copy
channel id, read and write API keys and paste them in the Arduino code given at the end of the tutorial. Now upload
the Arduino code to the NodeMCU. On successfully uploading, test your project by putting objects in front of IR sensors.
You will be able to see the change in the ThingSpeak charts for every change in values of LDR, ir sensors and LEDs.

Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 7


Step 5: Coding Explanation and Setup

Step 1: First include all the required libraries.


#include <ESP8266WiFi.h>;
#include <WiFiClient.h>;
#include <ThingSpeak.h>;

Step 2: Replace SSID and password given in code with you Wi-Fi SSID and password
const char* ssid = "UniSZA-WiFi";
const char* password = "unisza2016";

Step 3: Copy channel number, read and write API keys from ThingSpeak as shown below
unsigned long myChannelNumber = 1778641;
const char * myWriteAPIKey = "CBWL1EPYK078WV49";
const char * myReadAPIKey = "HS82229EGKQ9IO2O";

Step 4: De ne variable for GPIO pins of leds and IR sensors, ADC channel
int ir1 = D0;
int led1 = D5;

Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 8


int ir2 = D1;
int led2 = D6;
int ir3 = D2;
int led3 = D7;
int ldr = A0;
int val =0;

Step 5: Set the pinMode for pins of led and IR sensor on the NodeMCU
pinMode(ir1,INPUT);
pinMode(led1,OUTPUT);
pinMode(ir2,INPUT);
pinMode(led2,OUTPUT);
pinMode(ir3,INPUT);
pinMode(led3,OUTPUT);

Step 6: Initialization of Wi-Fi and ThingSpeak


WiFi.begin(ssid, password);
ThingSpeak.begin(client);

Step 7: Now we take digital value of the IR sensors and analog value of LDR sensor and store them in variables.
int s1 = digitalRead(ir1);
int s2 = digitalRead(ir2);
int s3 = digitalRead(ir3);
int l1 = digitalRead(led1);
int l2 = digitalRead(led2);
int l3 = digitalRead(led3);
val = analogRead(ldr);

Step 8: Initialize back the declaration of IR, LDR sensor reading and led to display at the serial monitor
Serial.print(s1);
Serial.print(":");
Serial.print(s2);
Serial.print(":");
Serial.print(s3);
Serial.print(":");
Serial.print(l1);
Serial.print(":");

Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 9


Serial.print(l2);
Serial.print(":");
Serial.print(l3);
Serial.print(" ");
Serial.println(val);
Step 9: Now check the value of LDR sensor for low light. Here we have set value as 200 means if the analog value of LDR
is lower than 200 then it will be night time or low light and hence it will turn on the led if IR sensors detect some obstacle
or motion. If the analog value of the LDR sensor is more than 200 then it will be considered as daytime and LEDs will not
glow even if IR sensor detects someone passing the street.
if(val<=200)
{
if(s1==0)
{
digitalWrite(led1,HIGH);
}
else
{
digitalWrite(led1,LOW);
}
if(s2==0)
{
digitalWrite(led2,HIGH);
}
else
{
digitalWrite(led2,LOW);
}
if(s3==0)
{
digitalWrite(led3,HIGH);
}
else
{
digitalWrite(led3,LOW);
}
}
else
{

Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 10


digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
}

Step 10: Finally upload the data on the ThingSpeak cloud by using function ThingSpeak.writeField(). It take channel
number, eld number, data (you want to upload in respective eld) and write API key. Here we are uploading LDR sensor
data, IR sensors data and LEDs data to the ThingSpeak cloud.
ThingSpeak.writeField(myChannelNumber, 1,val, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 2,s1, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 3,s2, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 4,s3, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 5,digitalRead(led1), myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 6,digitalRead(led2), myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 7,digitalRead(led3), myWriteAPIKey);

Step 6: How to Visualize the Graph Output in ThingSpeak

Step 1: Log On to your account


Step 2: Go to My Channel then click private view. All the data that had been received from NodeMCU will display as
graph gure

Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 11


Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 12
Thanks for sharing :)

Smart Street Light Using NodeMCU and IOT ThingSpeak: Page 13

You might also like