intsensor=2;// Establishes the PIR motion sensor to use Pin 2
intled=13;// Establishes the LED light to use Pin 13
intmotion=0;// Variable to be used as a status code: 0 indicates no motion, 1 indicates motion
constcharssid[]="SKYHHQZG";// WiFi set-up variables (change if relocating)
constcharpassword[]="puAwnu3f33e7";
constunsignedlongchannel_id=2010206;// Establishes the correct credentials for the ThingSpeak Channel
constcharwrite_api_key[]="VE3UTMCYSQAIY2AQ";
WiFiClientclient;
voidsetup(){
pinMode(led,OUTPUT);// Sets up the testing LED as an output device
pinMode(sensor,INPUT);// Sets up the PIR sensor as an input device
Serial.begin(9600);// Configures the baud rate
WiFi.begin(ssid,password);// Establishes the Arduino's connection to the WiFi network.
Serial.println("Connected to WiFi network.");
Serial.println();
ThingSpeak.begin(client);
Serial.println("Sensor initialising. You have 30 seconds to leave the area without triggering the alert. Once triggered, the sensor will wait 1 minute to re-test for motion.");
delay(30000);// Waits thirty seconds to allow a user to leave the room they need monitored and not affect the sensor
Serial.println("Sensor ready and watching.");// Assuming the user has left the area, the sensor can now wait for motion.
}
voidloop(){
if(digitalRead(sensor)){// If the PIR sensor detects a motion (voltage is sent high when motion is detected, creating the signal)
motion=1;// Changes the variable to reflect that there is now motion detected (value of 1)
Serial.print("Status ");
Serial.print(motion);
Serial.println(": Motion detected. Uploading motion data to ThingSpeak.");// Indicates the detection of a motion to the serial monitor
digitalWrite(led,HIGH);// Turns the LED on (testing purposes)
ThingSpeak.setField(1,String(motion));
ThingSpeak.writeFields(channel_id,write_api_key);// Writes the motion status to ThingSpeak
delay(60000);// Sets a delay of 1 minute before the next motion can be identified
}
else{// If no motion is detected
motion=0;// Returns the value to its default state so that it can be re-triggered
Serial.print("Status ");
Serial.print(motion);
Serial.println(": No motion");// Indicates no detection of motion to the serial monitor
digitalWrite(led,LOW);// The LED is turned off
ThingSpeak.setField(1,String(motion));
ThingSpeak.writeFields(channel_id,write_api_key);// Uploads the 0 motion status to ThingSpeak to return it to its default state
delay(5000);// Sets a delay of 5 seconds before the next motion can be identified
}
//motion = 0; // Returns the value to its default state so that it can be re-triggered
//ThingSpeak.setField(1, String(motion));
//ThingSpeak.writeFields(channel_id, write_api_key); // Uploads the 0 motion status to ThingSpeak to return it to its default state
//delay(3000); // Sets a delay before the sensor can be re-triggered