//-----Include the ThingSpeak Library, Download here : https://github.com/mathworks/thingspeak-arduino
#include "ThingSpeak.h"
#include <WiFi.h>
#define ON_Board_LED 2 //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router
#define LIGHT 12 //--> Defining LIGHT/Relay Port
#define tState_PIN 23
//-----SSID and Password of your WiFi router/hotspot
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//-----Channel ID on ThingSpeak
unsigned long channel = 1380767;
const char * myWriteAPIKey = "QVJJM9LGGBVRW3EW";
//-----Declaration field1, I use field1 for light / relay, if you use many fields, add field1, field2, and so on.
unsigned int field1 = 1;
unsigned int field2 = 2;
WiFiClient client;
int tState;
int nState;
//-----SETUP
void setup() {
Serial.begin(115200);
delay(100);
pinMode(ON_Board_LED, OUTPUT); //--> On Board LED port Direction output
pinMode(LIGHT, OUTPUT); //--> LIGHT/Relay port Direction output
pinMode(tState_PIN, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board. Led On Board use active low.
digitalWrite(LIGHT, LOW); //--> Turn off LIGHT. LIGHT/Relay use active high, some relay modules may be different.
//-----Wait for connection
WiFi.begin(ssid, password);
Serial.println("");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
//-----Make the On Board Flashing LED on the process of connecting to the wifi router.
digitalWrite(ON_Board_LED, LOW);
delay(250);
digitalWrite(ON_Board_LED, HIGH);
delay(250);
}
digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Netmask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
//------ThingSpeak.begin(client)
ThingSpeak.begin(client);
}
void loop() {
int statusCode = 0;
int last_light_state = ThingSpeak.readFloatField(channel, field1); //--> get the latest data from the fields on ThingSpeak
statusCode = ThingSpeak.getLastReadStatus(); //--> Check the status of the read operation to see if it was successful
if(statusCode == 200){
if(last_light_state == 1){
digitalWrite(LIGHT, HIGH);
Serial.println("LIGHT is On");
}
else if(last_light_state == 0){
digitalWrite(LIGHT, LOW);
Serial.println("LIGHT is Off");
}
Serial.print("The latest data from Field1 on ThingSpeak is : ");
Serial.println(last_light_state);
}
else {
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
tState = digitalRead(tState_PIN); // read state
if (tState == HIGH) {
Serial.println("The door is open");
nState = 0;
//digitalWrite(LED_PIN, HIGH); // turn on LED
} else {
Serial.println("The door is closed");
nState = 1;
//digitalWrite(LED_PIN, LOW); // turn off LED
}
int x = ThingSpeak.writeField(channel, field2, nState, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(15000);
}