Commit 05140a2f authored by Ollie Rhodes's avatar Ollie Rhodes

Arduino Code

parent 0b3e5d1b
#include <WiFiNINA.h>
#include "ThingSpeak.h"
char ssid[] = "SKYACDWV_2GEXT"; //network SSID
char pass[] = "tCNGqxviKrBK"; //private network password
unsigned long channelID = 1295506;
const char * apiKey = "VL6RQ2RMEKEEXW0K";
WiFiClient client;
int ledPin = 12;
int lightPin = A1;
bool doorOpen = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
//pinMode(lightPin, INPUT);
ThingSpeak.begin(client); //Initialize ThingSpeak
}
void loop() {
// put your main code here, to run repeatedly:
if (WiFi.status() != WL_CONNECTED) { //for connecting or reconnecting to wifi
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
float lightsensor = analogRead(lightPin); //reads light level
Serial.println(lightsensor); //prints light level
if (lightsensor > 400) { //if in light - door open
if (!doorOpen) { //preventing repeating reports
Serial.println("Door Opened");
int x = ThingSpeak.writeField(channelID, 1, String("Door Opened"), apiKey); //write to channel 1
if (x == 200) {
Serial.println("Door Reported");
}
else {
Serial.println("Error code " + String(x));
}
}
doorOpen = true;
for (int i = 0; i < 25; i++) { //flash and wait if door open
digitalWrite(12, LOW);
delay(100);
digitalWrite(12, HIGH);
delay(100);
}
} else {
digitalWrite(12, HIGH); //just wait if door closed
doorOpen = false;
delay(5000);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment