Automatic Street Light with ThingSpeak Cloud

Back to Blog
Automatic Street Light with ThingSpeak Cloud - Automatic Street Light with ThingSpeak Cloud - Automatic Street Light with ThingSpeak Cloud - Automatic Street Light with ThingSpeak Cloud

Automatic Street Light with ThingSpeak Cloud

In our previous blog, we have seen how to upload sensor data to ThingSpeak. Now we will see how to use this to update the street light data into ThingSpeak cloud. In this project, we will use the LDR to detect the day/night environment and turns on the LED during night time as well as update the data to ThingSpeak. Additionally, we have an IR Sensor to detect any motion and turn on the LED and update that data separately in the ThingSpeak.

Idea & Credits: Kamna Jha

Components Required:

Circuit Diagram:

 

Automatic Street Light Thingspeak_bb

Thingspeak – Adding New Channel:

If you have missed the channel creation process, refer to this previous tutorial. There we have created an account, installed the thingspeak library and set up a single channel example.

We are just adding another channel for the IR Sensor here.

Note 1: Thingspeak free account supports up to 4 Channels only. You need to upgrade the plan for more channels and message limits.

Note 2: Thingspeak provides only a 15-second interval for updating data to thingspeak. Summing up the network delay slows down the code. If you consider real-world scenario Day/Night won’t change instantly. But for IR motion detection it matters.

Step 1: Login to thingspeak and navigate to My Channels on the dashboard.

Step 2: Now click on the ‘channel settings’. Add a field for IR Sensor.

New FieldStep 3: Now if you switch to public view you can see your new field.

2.Field2 CreatedStep 4: Now copy the Channel ID and API Key. These two we need to update in the code.

3.channel-id-api-key

Code:

#include <ESP8266WiFi.h>;
#include <WiFiClient.h>;
#include <ThingSpeak.h>;
const char* ssid = "Your Wi-Fi"; //Your Network SSID
const char* password = "Your Password"; //Your Network Password
int val;
int LDRpin = A0; //LDR Pin Connected at A0 Pin
int IRpin = D2;
int tresholdLDR = 300; //LDR Threshold Value
int LEDpin = D5;
WiFiClient client;
unsigned long myChannelNumber = 123456; //Your Channel Number (Without Brackets)
const char * myWriteAPIKey = "XXXXXXXXXXXXX"; //Your Write API Key
void setup()
{
 Serial.begin(9600);
 pinMode(LDRpin, INPUT);
 pinMode(IRpin,INPUT);
 pinMode(LEDpin,OUTPUT);
 delay(10);
 // Connect to WiFi network
 WiFi.begin(ssid, password);
 ThingSpeak.begin(client);

}
void loop(){
 
int LDRval = analogRead(LDRpin); //Read Analog values from LDR
int IRval = digitalRead(IRpin); //Read IR is detected or not (Digital)

 //Detects LDR and and Turn ON LED
 while(LDRval<tresholdLDR){
 digitalWrite(LEDpin,HIGH);
 Serial.println("Dark Light"); //Print on Serial Monitor
 delay(100);
 //yield(); //Uncomment this if your NodeMCU Resets (FFFF AAAA wdt reset)
 ThingSpeak.writeField(myChannelNumber, 1, LDRval, myWriteAPIKey); //Update in ThingSpeak
 LDRval = analogRead(LDRpin);
 delay(100);
 }
 
 //detects IR and update to ThingSpeak
 while(IRval == LOW){ //LOW Signal when motion detected on IR Sensor
 digitalWrite(LEDpin,HIGH);
 //yield(); //Uncomment this if your NodeMCU Resets (FFFF AAAA wdt reset)
 ThingSpeak.writeField(myChannelNumber, 2, IRval, myWriteAPIKey); //Update in ThingSpeak
 delay(100);
 Serial.println("IR Detected"); //Print on Serial Monitor
 IRval = digitalRead(IRpin); //Read IR is detected or not
 }
 digitalWrite(LEDpin,LOW);
 Serial.println("Idle"); //Print on Serial Monitor
 ThingSpeak.writeField(myChannelNumber, 2, IRval, myWriteAPIKey); //Update Field1 in ThingSpeak
 ThingSpeak.writeField(myChannelNumber, 1, LDRval, myWriteAPIKey); //Update Field2 in ThingSpeak
}

Output:

4.Result

Optional Feature:

ThingSpeak also has a widget feature that displays a GUI type of visualization according to the field data. Let’s see how to add it.

Step 1: Click the widget button on the dashboard.

5.Widget-Feature

 

Step 2: Choose your favorite widget.

5b-choose widgetv2 -  -  -

Step 3: Set the configuration values. Here I set for IR Sensor Field.

6.Conditions

Step 4: Save and test. When IR Detected, this Light will glow in Green Color.

7-Lamp-output

Share this post

Comments (5)

  • Om Kumar Yadav

    please make the blog on patient health monitoring system.

    May 30, 2019 at 4:26 PM
    • Sharath

      When I get time. I will make it.

      June 2, 2019 at 7:41 PM
      • Sri

        Can thingspeak be used to communicate PPG sensor data from Max30100 and temperature data from ds18b20 as of now as we are currently making the wired prototype using ESP8266 alone? Searching for various methodologies in sending sensor data from each of these sensors is being single handedly forwarded via esp8266 and not both the data together. Can you provided a code for the issue or exactly what changes needs to be made in the LDR code mentioned by you along with exactly which sections of these sensor codes can be merged together in one code so that the output can be found in thingspeak?
        Kindly provide your inputs at the earliest possible. Hoping for a positive response as i am running short on deadline time during this pandemic.

        July 6, 2020 at 2:07 PM
        • Sharath

          There is a library for MAX30100 for getting values from the sensor. All values can be stored in a variable (here LDRval, IRval), You need to replace these variables with the values got from MAX30100 Sensor. Combining two codes together.

          July 8, 2020 at 2:16 PM
  • welp

    It’s not working on my end. Any help?

    June 1, 2021 at 6:20 AM

Leave a Reply

Back to Blog