Sunteți pe pagina 1din 10

Automatic Street Light with ThingSpeak Cloud

https://www.factoryforward.com/automatic-
street-light-thingspeak-cloud/
By Sharath IoT, NodeMCU IoT, NodeMCU, ThingSpeak 2 Comments

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:

1 #include <ESP8266WiFi.h>;
2 #include <WiFiClient.h>;
3 #include <ThingSpeak.h>;
4 const char* ssid = "Your Wi-Fi"; //Your Network SSID
5 const char* password = "Your Password"; //Your Network Password
6 int val;
7 int LDRpin = A0; //LDR Pin Connected at A0 Pin
8 int IRpin = D2;
9 int tresholdLDR = 300; //LDR Threshold Value
10 int LEDpin = D5;
11 WiFiClient client;
12 unsigned long myChannelNumber = 123456; //Your Channel Number (Without Brackets)
13 const char * myWriteAPIKey = "XXXXXXXXXXXXX"; //Your Write API Key
14 void setup()
15 {
16 Serial.begin(9600);
17 pinMode(LDRpin, INPUT);
18 pinMode(IRpin,INPUT);
19 pinMode(LEDpin,OUTPUT);
20 delay(10);
21 // Connect to WiFi network
22 WiFi.begin(ssid, password);
23 ThingSpeak.begin(client);
24
25 }
26 void loop(){
27
28 int LDRval = analogRead(LDRpin); //Read Analog values from LDR
29 int IRval = digitalRead(IRpin); //Read IR is detected or not (Digital)
30
31 //Detects LDR and and Turn ON LED
32 while(LDRval<tresholdLDR){
33 digitalWrite(LEDpin,HIGH);
34 Serial.println("Dark Light"); //Print on Serial Monitor
35 delay(100);
36 //yield(); //Uncomment this if your NodeMCU Resets (FFFF AAAA wdt reset)
37 ThingSpeak.writeField(myChannelNumber, 1, LDRval, myWriteAPIKey); //Update in ThingSpeak
38 LDRval = analogRead(LDRpin);
39 delay(100);
40 }
41
42 //detects IR and update to ThingSpeak
43 while(IRval == LOW){ //LOW Signal when motion detected on IR Sensor
44 digitalWrite(LEDpin,HIGH);
45 //yield(); //Uncomment this if your NodeMCU Resets (FFFF AAAA wdt reset)
46 ThingSpeak.writeField(myChannelNumber, 2, IRval, myWriteAPIKey); //Update in ThingSpeak
47 delay(100);
48 Serial.println("IR Detected"); //Print on Serial Monitor
49 IRval = digitalRead(IRpin); //Read IR is detected or not
50 }
51 digitalWrite(LEDpin,LOW);
52 Serial.println("Idle"); //Print on Serial Monitor
53 ThingSpeak.writeField(myChannelNumber, 2, IRval, myWriteAPIKey); //Update Field1 in ThingSpeak
54 ThingSpeak.writeField(myChannelNumber, 1, LDRval, myWriteAPIKey); //Update Field2 in ThingSpeak
55 }

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.
Share this post

S-ar putea să vă placă și