#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C LCD(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// #include <WiFi.h>
// #include "ThingSpeak.h"
// const char* WIFI_NAME = "Wokwi-GUEST";
// const char* WIFI_PASSWORD = "";
// const int channelNumber = 2298981; //thingspeak channel ID
// const char* myWriteApiKey = "4NY3JGC455ZV2T3H"; //write API key from ThingSpeak
// const char* server = "api.thingspeak.com";
WiFiClient client;
int value = 0;
// #include <Timer.h>
// Timer timer1();
// Setting variables
#define LED 2
#define LDR 25
#define motion 12
#define swt11 4
#define swt12 5
#define swt21 23
#define manualMode 34
#define ON_OFF 19
void setup() {
Serial.begin(9600);
LCD.init();
LCD.backlight();
pinMode(ON_OFF, INPUT);
pinMode(21, OUTPUT);
pinMode (LDR, INPUT);
pinMode(manualMode, INPUT);
pinMode(LED, OUTPUT);
pinMode(motion, INPUT);
pinMode(swt11, INPUT_PULLUP);
pinMode(swt12, INPUT_PULLUP);
pinMode(swt21, INPUT_PULLUP);
//esp32 module code
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
// Variables to be used in the code
int valueLDR = map(analogRead(LDR), 0, 1023, 0, 255);
int valueMan = map(analogRead(manualMode), 0, 1023, 0, 255);
int onOFF = digitalRead(ON_OFF);
String swt11Bool = "0";
String swt12Bool = "0";
String swt21Bool = "0";
boolean swt11TF = false;
boolean swt12TF = false;
boolean swt21TF = false;
ThingSpeak.setField(2,onOFF);
// If statement for Auto mode with motion detection
// if (digitalRead(swt11) && not(digitalRead(swt21))){
// swt11Bool = "1";
// if (digitalRead(motion)){
// analogWrite(LED, valueLDR);
// ThingSpeak.setField(1, LDR);
// }
// else{analogWrite(LED, LOW);}
// }
// // Else if for the auto using the only the photoresistor
// else if (digitalRead(swt12) && not(digitalRead(swt21))){
// analogWrite(LED, valueLDR);
// ThingSpeak.setField(1, LDR);
// swt12Bool = "1";
// }
// // Else if for the manual mode using potentiometer
// else if (digitalRead(swt21)){
// analogWrite(LED, valueMan);
// ThingSpeak.setField(1, manualMode);
// swt21Bool = "1";
// }
if (digitalRead(swt11) && not(digitalRead(swt21))){
swt11Bool = "1";
}
// Else if for the auto using the only the photoresistor
else if (digitalRead(swt12) && not(digitalRead(swt21))){
swt12Bool = "1";
}
// Else if for the manual mode using potentiometer
else if (digitalRead(swt21)){
swt21Bool = "1";
}
if(swt11TF){
if (digitalRead(motion)){
analogWrite(LED, valueLDR);
ThingSpeak.setField(1, LDR);
}
else{analogWrite(LED, LOW);}
}
else if(swt12TF){
analogWrite(LED, valueLDR);
ThingSpeak.setField(1, LDR);
}
else if(swt21TF){
analogWrite(LED, valueMan);
ThingSpeak.setField(1, manualMode);
}
LCD.setCursor(0, 0);
LCD.print("Auto Motion:" + swt11Bool);
LCD.setCursor(0, 1);
LCD.print("Auto:" + swt12Bool);
LCD.setCursor(8,1);
LCD.print("Man:" + swt21Bool);
// int x = ThingSpeak.writeFields(channelNumber, myWriteApiKey);
// if(x == 200){
// Serial.println("Data pushed successfull");
// }else{
// Serial.println("Push error" + String(x));
// }
delay(5000);
}