IoT Based Smart Street Light using NodeMCU ESP8266 and ThingSpeak

IoT based Smart Street Light using NodeMCU ESP8266 and ThingSpeak

Most of the places have automatic street light which can sense the daytime and nighttime, and automatically turns on  and off according the night and day. Here we are extending this project by adding one more constraint to turn on the light that is Street light will only glow if there is darkness and someone is passing through the street. The main objective of this project is to reduce the power consumption by glowing the Street light only when it is needed. In this project we are demonstrating the prototype of the Smart Street Light with 3 IR sensors, 1 LDR sensor and 3 LEDs - each representing one street light. We will also update the LDR sensor data to the ThingSpeak and control the LEDs (Street lights) over the internet from anywhere in the world.

 

Components Required

  • ESP8266 NodeMCU
  • Micro USB cable
  • LEDs
  • Jumper wires
  • IR sensors
  • LDR sensors

Before moving forward, lets learn about the components used in the project.

 

ESP8266 NodeMCU

ESP8266 NodeMCU is an open source IoT platform. It includes firmware which runs on the low cost Wi-Fi enabled ESP8266 Wi-Fi SoC from Espressif Systems, and hardware which is based on the ESP-12 module. It has GPIO, SPI, I2C, ADC, PWM AND UART pins for communication and controlling other peripherals attached to it. On board NodeMCU has CP2102 IC which provides USB to TTL functionality.

ESP8266 NodeMCU

 

IR Sensor

IR sensor is an electronic device which is used to detect objects by sensing infrared radiations reflected from the objects. It mainly consists of a transmitter IR LED and a receiver photodiode. It detects infrared radiations which have wavelength from 700nm to 1mm. When a specific positive voltage is applied across the transmitter LED it transmits the IR rays. If these rays fall on some object then that object reflects back the IR rays which are received by the receiver photodiode. The receiver diode generates a voltage across its terminals which depend on the intensity of light reflected by the object. Generally IR receiver LED is darker (black) whereas transmitter is transparent in color.

IR Sensor

 

LDR Sensor

LDR stands for Light Dependent Resistor also known as photo-resistor. LDR is sensitive to light and its resistance changes according to the intensity of light falling on it. It is made up of high resistance semiconductor and its resistance increases in darkness and decreases in light. When light incident on the LDR exceeds some threshold, it absorbs the photons and allows electrons to jump into the conduction band. LDR generates a variable resistance which depends on the intensity of light falling on it. It is mainly used in electric circuits like street light, alarm clock, automatic brightness and contrast control etc.

LDR Sensor

 

Thingspeak

ThingSpeak is a cloud based data platform which is used to send and receive the data in real time using HTTP protocol. It is used in Iot applicaiotn to store and monitor the data from anywhere in the world over internet. Here are some Iot projects where ThingSpeak is used as an IoT cloud:

 

Smart Street Light Circuit Diagram and Working

Circuit diagram for this IoT based Smart Street Light is as follows:

Circuit Diagram for IoT based Smart Street Light

 

This circuit mainly consists ESP8266, LDR sensor, IR sensors and LEDs.

Here the LDR sensor is used to detect whether it is daytime or night time. Since LDR sensor generates variable resistance based on the amount of light falling on it, it has to be connected like a potentiometer. One end of the LDR sensor is connected to 5V and other end is connected to fixed resistance which is further connected to ground. NodeMCU has one ADC pin (A0) which is connected to point between fixed 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.

 

IR sensors are used to detect if someone is crossing the street or not. It detects the obstacle or motion in the surrounding. The transmitter will transmit IR rays which will be reflected back if it falls on some object like person, animal, vehicles, etc. The reflected ray will be received by receiver diode and hence will confirm the presence of object and the corresponding LED will be glowed. This method will save significant amount of electricity as the street light will only turns on if there is someone present in the Street. IR sensor has 3 pins, two of which are VCC and ground and one is output pin. 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.

Circuit Hardware for IoT based Smart Street Light

 

Installing ThingSpeak library in Arduino IDE

Open Arduino IDE and Go to Sketch -> Include Library -> Manage Libraries.

Installing ThingSpeak library in Arduino IDE

 

Now search for ThingSpeak in library manager and install ThingSpeak library by MathWorks.

Install ThingSpeak Library for Smart Street Light System

 

Uploading Smart Street Light data on ThingSpeak

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

 

 

Now Click on New Channel and then give some name to your channel and then fill the fields 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 filling the details scroll down and click on “Save Channel”.

Uploading Smart Street Light data on ThingSpeak

 

Your channel will be created and now you will be able to see field 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.

 

Controlling the Street Light from anywhere using ThingSpeak

Now we have to control the LEDs over the internet using ThingSpeak. Click on Sharing and select the “Share channel view with everyone” radio button.

Controlling the Street Light from anywhere using ThingSpeak

 

Now go to API keys and copy the URL given in “Update a Channel Feed”. We have to edit this URL to change the status of LED.

Update Channel Feed on ThingSpeak for Smart Street Light System

 

Fields for our LEDs are 5, 6 and 7. Now edit the Url as shown below:

https://api.thingspeak.com/update?api_key=FZTOUARV558GRZ8J&field5=1&field6=1&field7=1

Here we are setting field 5, field 6 and field 7 as 1 to turn on the LEDs. Copy this URL and paste it in a new tab. It will turn on the LEDs with some delay time. You can observe the change in field charts.

Controlling Street Light from anywhere using ThingSpeak

 

Code Explanation

Complete code with a demonstration Video is given at the end of this tutorial. Code can be uploaded in NodeMCU by using Arduino IDE. Here we are explaining few important part of the code.

First include all the required libraries.

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

 

Replace SSID and password given in code with you Wi-Fi SSID and password.

const char* ssid = "CircuitLoop";
const char* password = "circuitdigest101";

 

Copy channel number, read and write API keys from ThingSpeak as shown above.

unsigned long myChannelNumber = 795820;
const char * myWriteAPIKey = "FZTOUARV558GRZ8J";
const char * myReadAPIKey = "T52GT3QQOQBVPG4V";

 

Define variable for GPIO pins of leds and IR sensors, ADC channel

WiFiClient client;
int led_1;
int led_2;
int led_3;

int ir1 = D0;
int led1 = D5;

int ir2 = D1;
int led2 = D6;

int ir3 = D2;
int led3 = D7;

int ldr = A0;
int val =0;

 

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

 

Initialization of Wi-Fi and ThingSpeak

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

 

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);
  val = analogRead(ldr);

 

Now check the value of LDR sensor for low light. Here I have set value as 700 means if the analog value of LDR is lower than 700 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 700 then it will be considered as daytime and LEDs will not glow even if IR sensor detects someone passing the street.

  if(val<700)
  {
    if(s1==0)
    {
      digitalWrite(led1,LOW);
    }
    else
    {
      digitalWrite(led1,HIGH);
    }
    if(s2==0)
    {
      digitalWrite(led2,LOW);
    }
    else
    {
      digitalWrite(led2,HIGH);
    }
    if(s3==0)
    {
      digitalWrite(led3,LOW);
    }
    else
    {
      digitalWrite(led3,HIGH);
    }
  }
  else
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }

 

Finally upload the data on the ThingSpeak cloud by using function ThingSpeak.writeField(). It take channel number, field number, data (you want to upload in respective field) 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,led1, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 6,led2, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 7,led3, myWriteAPIKey);

 

Here is the code for changing the state of LEDs using ThingSpeak. We have already shown above the procedure to change the state of the LEDs. Led_1, led_2, led_3 stores the last state of led from the ThingSpeak using the function ThingSpeak.readIntField which takes channel number, respective field number and read API key. If the state of some led is “1” then we turn on the respective led and if the state of some led is “0” we turn off the respective led.

led_1 = ThingSpeak.readIntField(myChannelNumber, 5, myReadAPIKey);
led_2 = ThingSpeak.readIntField(myChannelNumber, 6, myReadAPIKey);
 led_3 = ThingSpeak.readIntField(myChannelNumber, 7, myReadAPIKey);
  if(led_1==1)
  {
    digitalWrite(led1,HIGH);
  }
  else
  {
    digitalWrite(led1,LOW);
  }

  if(led_2==1)
  {
    digitalWrite(led2,HIGH);
  }
  else
  {
    digitalWrite(led2,LOW);
  }

  if(led_3==1)
  {
    digitalWrite(led3,HIGH);
  }

  else
  {
    digitalWrite(led3,LOW);
  }

 

This is how a Smart Street Light works, it only glows if it is a night time and someone is passing through the street. And it can be also controlled manually from anywhere in the world using ThingSpeak IoT cloud

Check the complete code with a Demonstration Video below.

Code

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

const char* ssid = "CircuitLoop";
const char* password = "circuitdigest101";

WiFiClient client;

unsigned long myChannelNumber = 795820;
const char * myWriteAPIKey = "FZTOUARV558GRZ8J";
const char * myReadAPIKey = "T52GT3QQOQBVPG4V";

int led_1;
int led_2;
int led_3;

int ir1 = D0;
int led1 = D5;

int ir2 = D1;
int led2 = D6;

int ir3 = D2;
int led3 = D7;

int ldr = A0;
int val =0;

void setup() {
  Serial.begin(9600);
  delay(10);
  pinMode(ir1,INPUT);
  pinMode(led1,OUTPUT);

  pinMode(ir2,INPUT);
  pinMode(led2,OUTPUT);

  pinMode(ir3,INPUT);
  pinMode(led3,OUTPUT);

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

void loop() {
  int s1 = digitalRead(ir1);
  int s2 = digitalRead(ir2);
  int s3 = digitalRead(ir3);
  s3 = not(s3);

  val = analogRead(ldr);

  Serial.print(s1);
  Serial.print(":");
  Serial.print(s2);
  Serial.print(":");
  Serial.print(s3);
  Serial.print("  ");
  Serial.println(val);
  if(val<800)
  {
    if(s1==0)
    {
      digitalWrite(led1,LOW);
    }
    else
    {
      digitalWrite(led1,HIGH);
    }
    if(s2==0)
    {
      digitalWrite(led2,LOW);
    }
    else
    {
      digitalWrite(led2,HIGH);
    }

    if(s3==0)
    {
      digitalWrite(led3,LOW);
    }
    else
    {
      digitalWrite(led3,HIGH);
    }
  }
  else
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }

  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,led1, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 6,led2, myWriteAPIKey);
  ThingSpeak.writeField(myChannelNumber, 7,led3, myWriteAPIKey);

  led_1 = ThingSpeak.readIntField(myChannelNumber, 5, myReadAPIKey);
  led_2 = ThingSpeak.readIntField(myChannelNumber, 6, myReadAPIKey);
  led_3 = ThingSpeak.readIntField(myChannelNumber, 7, myReadAPIKey);

  if(led_1==1)
  {
    digitalWrite(led1,HIGH);
  }
  else
  {
    digitalWrite(led1,LOW);
  }

  if(led_2==1)
  {
    digitalWrite(led2,HIGH);
  }
  else
  {
    digitalWrite(led2,LOW);
  }

  if(led_3==1)
  {
    digitalWrite(led3,HIGH);
  }
  else
  {
    digitalWrite(led3,LOW);
  }
}
 

Video

30 Comments

I cannot control leds on ThingsPeak with "update" command and I also see sensor circuit delay when connecting WIFI. Is there a way to fix it?

I and my friends have already been reviewing the good guidelines located on your site and then unexpectedly developed a terrible suspicion I had not thanked the website owner for those secrets. The boys are actually for that reason joyful to see all of them and already have truly been making the most of them. Appreciation for actually being so considerate as well as for settling on this kind of magnificent themes most people are really eager to learn about. My very own sincere apologies for not expressing gratitude to you sooner.

Thank you so much for giving everyone an extremely brilliant opportunity to read articles and blog posts from this web site. It is often so awesome and stuffed with fun for me personally and my office mates to search your blog on the least three times in one week to learn the fresh guides you will have. And lastly, I'm also always pleased for the dazzling guidelines served by you. Some 3 points in this posting are rather the finest I've had.

I needed to draft you this little bit of note just to give many thanks the moment again relating to the lovely views you've documented on this website. This has been certainly wonderfully generous with you to deliver easily all many of us would have offered for sale for an ebook to earn some cash for themselves, even more so considering the fact that you might well have tried it in case you wanted. These secrets as well served like a good way to know that other people have a similar eagerness like my own to understand more and more with respect to this condition. I think there are lots of more pleasurable instances ahead for folks who scan your blog.

I precisely needed to thank you very much once again. I'm not certain what I could possibly have undertaken in the absence of the type of techniques documented by you about such a theme. It actually was the frightening problem in my opinion, however , understanding the very skilled way you handled it made me to jump with gladness. I am happier for this support and then sincerely hope you are aware of a powerful job that you are carrying out teaching most people all through your web page. Probably you've never got to know all of us.

I precisely wished to thank you so much once more. I do not know the things I would have undertaken in the absence of these tips shared by you directly on such a field. It had become the depressing difficulty in my circumstances, nevertheless looking at this skilled manner you handled that took me to weep with happiness. I'm grateful for your assistance as well as hope you really know what a powerful job you are always providing educating the others via your web site. I am sure you haven't met any of us.

My spouse and i have been quite happy Emmanuel managed to finish off his studies with the ideas he obtained while using the blog. It's not at all simplistic just to always be giving for free guidance which others have been making money from. And we all understand we have you to thank for this. The specific illustrations you've made, the simple blog menu, the relationships your site help instill - it's many terrific, and it's helping our son and us know that the theme is satisfying, and that's quite vital. Thanks for all!

Thanks for all of your labor on this site. Gloria delights in doing investigation and it is simple to grasp why. My partner and i know all about the dynamic form you provide invaluable secrets via your web blog and even welcome contribution from other individuals on that theme while my princess is without question starting to learn a whole lot. Take pleasure in the remaining portion of the new year. You are always conducting a good job.

I and my pals were reading the excellent points located on your site while all of the sudden came up with a horrible suspicion I never thanked the website owner for those tips. These guys happened to be certainly warmed to see them and have definitely been using those things. Thank you for genuinely very thoughtful and for having this sort of fantastic resources millions of individuals are really desirous to understand about. Our sincere regret for not expressing gratitude to earlier.

I precisely needed to say thanks again. I am not sure what I might have followed in the absence of the actual advice shown by you concerning that situation. It actually was a real troublesome setting in my circumstances, but coming across the expert mode you dealt with it took me to weep with delight. Now i'm happier for this help and in addition trust you recognize what a great job you happen to be carrying out educating others thru your websites. I know that you haven't encountered all of us.

I am only commenting to make you know what a notable encounter my friend's child went through reading your site. She even learned so many pieces, which included what it is like to possess a very effective coaching mindset to get others smoothly fully understand chosen complicated subject matter. You really exceeded our own expected results. I appreciate you for offering these invaluable, healthy, educational and in addition unique tips on that topic to Evelyn.

I enjoy you because of your entire work on this site. Debby delights in engaging in internet research and it is simple to grasp why. All of us hear all concerning the powerful manner you offer worthwhile tricks by means of your web blog and as well inspire contribution from people on the area of interest while my princess is really becoming educated a lot. Have fun with the remaining portion of the year. Your conducting a dazzling job.

I really wanted to post a simple remark to express gratitude to you for all of the remarkable tips and tricks you are sharing on this site. My rather long internet research has finally been paid with extremely good content to go over with my co-workers. I would suppose that we site visitors actually are extremely blessed to live in a superb network with very many special professionals with very beneficial suggestions. I feel truly blessed to have encountered your entire weblog and look forward to really more exciting minutes reading here. Thank you once more for a lot of things.

A lot of thanks for each of your work on this web site. Ellie really likes conducting internet research and it's really simple to grasp why. My partner and i hear all regarding the compelling form you render both interesting and useful techniques on the website and as well strongly encourage response from other people on the concept so my daughter is actually discovering a lot. Have fun with the remaining portion of the year. You are always performing a glorious job.

Thank you a lot for providing individuals with remarkably splendid chance to read critical reviews from this web site. It can be very pleasing plus packed with a great time for me and my office colleagues to visit your site at a minimum three times weekly to find out the new stuff you will have. And indeed, I am just at all times motivated with the special secrets you serve. Certain 3 ideas in this posting are completely the finest we have all had.

Thank you for every one of your hard work on this web page. My mum takes pleasure in setting aside time for internet research and it is obvious why. Many of us learn all concerning the powerful medium you give efficient guidance through the website and as well as inspire contribution from some other people on this matter so our favorite daughter is in fact becoming educated so much. Take advantage of the remaining portion of the new year. You have been carrying out a superb job.

I want to voice my appreciation for your kindness for those individuals that require help with that field. Your very own commitment to passing the message all around has been unbelievably effective and have always empowered some individuals just like me to get to their objectives. Your useful useful information implies so much a person like me and still more to my office colleagues. Warm regards; from each one of us.

I definitely wanted to jot down a simple message in order to appreciate you for some of the fantastic suggestions you are sharing at this site. My extended internet research has at the end of the day been recognized with incredibly good tips to exchange with my neighbours. I would point out that many of us site visitors are rather lucky to be in a fantastic community with very many awesome professionals with insightful suggestions. I feel quite privileged to have seen your webpages and look forward to some more amazing moments reading here. Thanks a lot again for all the details.

I have to point out my gratitude for your kindness supporting visitors who absolutely need help with that subject matter. Your personal commitment to getting the message all around had become exceedingly beneficial and have continually made folks much like me to arrive at their targets. Your personal important recommendations indicates a lot a person like me and somewhat more to my peers. Thank you; from everyone of us.

A lot of thanks for each of your effort on this web page. My mum loves making time for investigation and it's really obvious why. We hear all relating to the dynamic mode you render very helpful tricks by means of this website and even inspire response from some other people on the concept and my daughter is really learning a great deal. Take advantage of the rest of the year. You are performing a superb job.

The following time I learn a weblog, I hope that it doesnt disappoint me as a lot as this one. I imply, I do know it was my option to learn, however I truly thought youd have something attention-grabbing to say. All I hear is a bunch of whining about one thing that you might fix when you werent too busy on the lookout for attention.

There are actually loads of particulars like that to take into consideration. That may be a nice point to bring up. I offer the ideas above as basic inspiration but clearly there are questions like the one you convey up where the most important factor shall be working in trustworthy good faith. I don?t know if best practices have emerged around things like that, but I'm sure that your job is clearly recognized as a good game. Both boys and girls really feel the impact of only a second抯 pleasure, for the rest of their lives.

I want to show appreciation to this writer just for bailing me out of this particular circumstance. As a result of scouting throughout the world wide web and meeting views which are not productive, I figured my entire life was done. Living without the approaches to the problems you have resolved as a result of your guide is a critical case, and the kind that might have adversely damaged my career if I had not noticed your web page. Your main knowledge and kindness in taking care of all areas was excellent. I'm not sure what I would have done if I hadn't encountered such a subject like this. It's possible to at this moment look forward to my future. Thank you so much for the high quality and effective guide. I won't hesitate to endorse your web site to anyone who would need assistance about this subject.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.