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:
- NodeMCU
- Breadboard
- LED
- IR Sensor
- LDR
- Jumper Wires
- 220 Ohm and 10K Ohm resistors – 1 each
Circuit Diagram:
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.
Step 3: Now if you switch to public view you can see your new field.
Step 4: Now copy the Channel ID and API Key. These two we need to update in the code.
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:
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.
Step 2: Choose your favorite widget.
Step 3: Set the configuration values. Here I set for IR Sensor Field.
Step 4: Save and test. When IR Detected, this Light will glow in Green Color.
Comments (5)
please make the blog on patient health monitoring system.
When I get time. I will make it.
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.
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.
It’s not working on my end. Any help?