Commit 08987fae authored by simon.nutsey's avatar simon.nutsey

Add new file

parents
#include <SPI.h>
#include <WiFiNINA.h>
#include "ThingSpeak.h"
#include "Wifi_info.h" //Takes information from Wifi info tab
char net[] = Network; //Network Name
char pass[] = Password; //Network Password
int status = WL_IDLE_STATUS;
WiFiClient client;
const unsigned long channel_id = 1223547; //Thing Speak channel Id
const char write_api_key[] = "7JWC5PW2TW4YQ10L"; //Thing speak read and write api keys
const char read_api_key[] = "SSL66E7NF8G3413X";
const int OpenCloseSensor = 13; //set door sensor to pin 13. Gnd to Gnd
int DoorSensor; //Declare Door open/close sensor
int OpenBuzzer = 4; //Declare Open buzzer to pin 4 on board
int RFIDID = 0;
boolean Opendoor = false; //Boolen opendoor set to false
void setup() {
pinMode(2, OUTPUT); //GREEN LED // Green and Red LEDs set to output
pinMode(3, OUTPUT); //RED LED
Serial.begin(9600); //initialize serial to 9600 baud rate
while (!Serial) {
;
}
pinMode(OpenCloseSensor, INPUT_PULLUP); // set arduino pin to input pull-up mode
if (WiFi.status() == WL_NO_MODULE){
Serial.println("NO WIFI MODULE ARE YOU USING THE CORRECT ARDUINO?"); //troubleshooting if user is using the wrong arduino
while (true);
}
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to: "); //attempt to connect to the network taking the ssid and pass from Wifi_info.h
Serial.println(net);
// Connect to WPA/WPA2 network:
status = WiFi.begin(net, pass);
// wait 5 seconds for connection:
delay(5000);
ThingSpeak.begin(client); //begin ThingSpeak client
}
// you're connected now, so print out the data:
Serial.println("CONNECTED TO NETWORK"); //Successful connection to wifi
printNetworkInfo();
}
void loop() {
DoorSensor = digitalRead(OpenCloseSensor); //start read from the door sensor
if (DoorSensor == HIGH) {
Serial.println("The door is open");
digitalWrite(2, HIGH); // if door sensor read is high then door is open. Print "The door is open" to console then write information to thing speak channel
digitalWrite(3, LOW);
Opendoor = true;
tone(OpenBuzzer, 1000, 500);
int open = 1;
ThingSpeak.setField(1, String(open));
ThingSpeak.writeFields(channel_id, write_api_key);
delay(1000);
} else {
Serial.println("The door is closed");
digitalWrite(2, LOW);
digitalWrite(3, HIGH); //If door sensor read is low then door is closed. Print to console "The door is closed" do nothing
Opendoor = false;
delay(1000);
}
}
void printNetworkInfo() {
Serial.print("You are connected to: ");
Serial.println(WiFi.SSID());
Serial.println("~~~~~"); //Print the wifi network user is connected too.
Serial.println("~~~~~");
}
\ No newline at end of file
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