· 6 years ago · Feb 13, 2020, 01:36 PM
1// Cheap Home Security Motion Detector
2
3#include "ThingSpeak.h" // Include ThingSpeak Library
4#include <ESP8266WiFi.h> // Include ESP8266wifi Library
5
6char ssid[] = "Abhimat PC"; // Enter your WiFi SSID (router name)
7char pass[] = "aabhimatg"; // Enter your WiFi router password
8unsigned long myChannelNumber = 989515; // Enter your Thingspeak Channel Number
9const char * myWriteAPIKey = "MF7XXGK5H20DLDP4"; // Enter your ThingSpeak API Key
10
11int ledPin = D5; // Choose the pin for the LED (Pin D5, GPIO 14)
12int inputPin = D6; // Choose the input pin for motion sensor (Pin D6, GPIO 12)
13int motionState = 0; // We start, assuming no motion detected
14int val = 0; // Variable for reading the pin status
15
16unsigned long currentMillis = millis();
17unsigned long previousMillis = 0;
18unsigned long resetMillis = 0;
19const long interval = 90000; // Time delay interval (90 seconds) to prevent false triggers when setting up device
20
21int status = WL_IDLE_STATUS;
22WiFiClient client;
23
24void setup()
25{
26 WiFi.begin(ssid, pass); // Start WiFi connection
27 ThingSpeak.begin(client); // Start ThingSpeak connection
28 Serial.begin(115200); // Serial Baud Rate
29 pinMode(inputPin, INPUT); // Set the Motion Sensor pin as an Input
30
31 Serial.println();
32 Serial.print("Connecting to ");
33 Serial.print(ssid);
34 while (WiFi.status() != WL_CONNECTED)
35 {
36 delay(500);
37 Serial.print(".");
38 }
39 Serial.println();
40 Serial.println("WiFi connected");
41 Serial.print("IP address: ");
42 Serial.println(WiFi.localIP());
43 Serial.println("Motion Sensor Alarm");
44
45 ThingSpeak.writeField(myChannelNumber, 1, val, myWriteAPIKey); // Update ThingSpeak channel the PIR sensor value '0'
46
47}
48
49void loop()
50{
51//********************* Read motion sensor and upload to ThingSpeak *****************************
52
53currentMillis = millis(); // Set the currentMillis equal to the millis function
54
55val = digitalRead(inputPin); // Read motion sensor
56
57if (val == HIGH) // Check if the input pin is HIGH
58 {
59 if ((motionState == 0) && (currentMillis >= interval))
60 { // Turn LED ON
61 Serial.println("Motion detected!"); // Motion Detected
62 motionState = 1; // We only want to update on the change, not state
63 ThingSpeak.writeField(myChannelNumber, 1, val, myWriteAPIKey); // Update ThingSpeak channel the PIR sensor value '0'
64 Serial.println("Data Sent to ThingSpeak!");
65 delay(500);
66 }
67 } else {
68 if (motionState == 1)
69 { // Motion stopped
70 Serial.println("Motion ended!");
71 motionState = 0; // We only want to print on the output change, not state
72 ThingSpeak.writeField(myChannelNumber, 1, val, myWriteAPIKey); // Update ThingSpeak channel the PIR sensor value '0'
73 Serial.println("Data Sent to ThingSpeak!");
74 delay(500);
75
76 }
77 }
78
79 if (currentMillis - previousMillis >= 10000) // Update Serial Monitor every ten seconds (10000 milliseonds)
80 { // as to how long the program has been running
81 Serial.print("Program running for ");
82 Serial.print((currentMillis/1000)/60);
83 Serial.println(" Minutes");
84 previousMillis = currentMillis;
85 yield(); // Yield to prevent the WDT from crashing the NodeMCU
86 }
87}