#include <HTTPClient.h>
#include "time.h"
#include "DHT.h"
#include "Arduino.h"
#include <LiquidCrystal_I2C.h>
#include <FS.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int WIFI_CONNECT_TIMEOUT = 10000; // 10 seconds
int wifioffline;
// Check if 5 minutes have passed since the last time the light was turned on
static unsigned long lastOnTime = 0;
String GAS_ID = "AKfycbxZAqqvw3e_P6_yTAwZsgcHrLvxlUI6OF8bVozeEI8P1H_GcNVQWcX7VhaQXehBkgWtvg";
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte BUTTON_SET = 15;
const byte BUTTON_UP = 2;
const byte BUTTON_CANCEL = 4;
byte black[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte upload[] = {
0b00100,
0b01110,
0b11111,
0b00100,
0b00100,
0b00100,
0b11111,
0b11111
};
byte line[] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
byte battery[] = {
0b00000,
0b01110,
0b11111,
0b11011,
0b10001,
0b11011,
0b11111,
0b00000
};
byte celsius[] = {
0b00000,
0b10000,
0b00111,
0b01000,
0b01000,
0b01000,
0b00111,
0b00000
};
byte humidity[] = {
0b00000,
0b00100,
0b01110,
0b11011,
0b11101,
0b11011,
0b01110,
0b00000
};
byte fan[] = {
0b11011,
0b11011,
0b00100,
0b11011,
0b11011,
0b00100,
0b00100,
0b11111
};
byte pump[] = {
0b11111,
0b11101,
0b11101,
0b11101,
0b11101,
0b11001,
0b11111,
0b01110
};
byte wifion[] = {
B00000,
B01110,
B10001,
B00000,
B01110,
B10001,
B00000,
B00100
};
byte wifioff[] = {
B00000,
B10001,
B01010,
B00100,
B01010,
B10001,
B00000,
B00100
};
#define DHTPIN1 12
#define DHTPIN2 13
#define DHTTYPE DHT22
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
float h1, t1, h2, t2, havg, favg;
int havgin, favgin;
const char * ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 7 * 3600;
const int daylightOffset_sec = 0;
int Seccoun,Mincoun, hourcoun;
int EOD = 0;
int SGG = 0;
int S1 = 14;
int S2 = 27;
int S3 = 26;
int S4 = 25;
int pumpStartTime = 0;
int fanStartTime = 0;
float wat;
#define ANALOG_IN_PIN 33 // Define analog input
float adc_voltage = 0.0; // Floats for ADC voltage &Input voltage
float in_voltage = 0.0; // Floats for ADC voltage &Input voltage
float R1 = 30000.0; // Floats for resistor values in divider (in ohms)
float R2 = 7500.0; // Floats for resistor values in divider (in ohms)
float ref_voltage = 3.3; // Float for Reference Voltage
int adc_value = 0; // Integer for ADC value
void setup() {
Serial.begin(115200);
dht1.begin();
dht2.begin();
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(S4, OUTPUT);
// Attempt to connect to WiFi network
WiFi.begin(ssid, password);
unsigned long wifiConnectStart = millis();
while (WiFi.status() != WL_CONNECTED && millis() - wifiConnectStart < WIFI_CONNECT_TIMEOUT) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi");
wifioffline = 1;
// Initialize the NTP client and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
time_t now = time(nullptr);
while (now <= 0) {
Serial.println("Waiting for time synchronization...");
delay(1000);
now = time(nullptr);
}
// Set the system time
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
time_t epoch = mktime(&timeinfo);
struct timeval tv;
tv.tv_sec = epoch;
tv.tv_usec = 0;
settimeofday(&tv, nullptr);
} else {
Serial.println("Failed to connect to WiFi");
wifioffline = 0;
// Skip WiFi connection and set the system time using NTP server directly
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
time_t now = time(nullptr);
if (now > 0) {
struct tm timeinfo;
localtime_r(&now, &timeinfo);
char timeStr[20];
strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo);
Serial.print("System time set to ");
Serial.println(timeStr);
} else {
Serial.println("Failed to set system time");
}
}
pinMode(BUTTON_SET, INPUT_PULLUP);
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_CANCEL, INPUT_PULLUP);
lcd.begin(0x27, 16, 2);
lcd.init();
lcd.backlight();
lcd.createChar(0, celsius);
lcd.createChar(1, humidity);
lcd.createChar(2, line);
lcd.createChar(3, battery);
lcd.createChar(4, fan);
lcd.createChar(5, upload);
lcd.createChar(6, pump);
lcd.createChar(7, black);
lcd.createChar(8, wifion);
lcd.createChar(9, wifioff);
lcd.clear();
}
void loop() {
havgin = havg;
favgin = favg;
static boolean Display = true;
static boolean ButtonSet = false;
if (Display == true) {
Display = false;
lcd.setCursor(0, 0);
lcd.print(favgin);
lcd.setCursor(2, 0);
lcd.write(0);
lcd.setCursor(3, 0);
lcd.write(2);
lcd.setCursor(4, 0);
lcd.write(1);
lcd.setCursor(5, 0);
lcd.print(havgin);
lcd.setCursor(7, 0);
lcd.print("%");
lcd.setCursor(8, 0);
lcd.write(2);
}
if (digitalRead(BUTTON_SET) == 0) {
delay(50);
if (digitalRead(BUTTON_SET) == 0) {
if (ButtonSet == false) {
ButtonSet = true;
lcd.clear();
SelectMenu();
Display = true;
}
}
} else {
ButtonSet = false;
}
static unsigned long lastTime = 0;
const unsigned long interval = 1000; // update every 1 second
unsigned long currentTime = millis();
if (currentTime - lastTime >= interval) {
lastTime = currentTime;
if (WiFi.status() == WL_CONNECTED) {
wifioffline = 1;
lcd.setCursor(11, 0);
lcd.write(8);
}
else{
wifioffline = 0;
lcd.setCursor(11, 0);
lcd.write(9);
}
// Get the current time
time_t now = time(nullptr);
// Print the time to the serial monitor
if (now > 0) {
struct tm timeinfo;
localtime_r(&now, &timeinfo);
hourcoun = timeinfo.tm_hour; // added code to extract hour
Mincoun = timeinfo.tm_min; // added code to extract minute
Seccoun = timeinfo.tm_sec; // added code to extract second
char timeStr[20];
strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); // 24-hour format
Serial.println(timeStr);
} else {
Serial.println("Failed to obtain time");
}
if (hourcoun == 23 && EOD < 1) {
EOD++;
SEOD();
}
if (hourcoun != 23 && EOD != 0) {
EOD = 0;
}
if (Mincoun % 1 == 0 && Seccoun == 0 && lastOnTime != Mincoun) {
Mincoun = Mincoun;
SGG++;
SSGG();
}
if (SGG != 0) {
SGG = 0;
}
h1 = dht1.readHumidity();
t1 = dht1.readTemperature();
h2 = dht2.readHumidity();
t2 = dht2.readTemperature();
havg = (h1 + h2) / 2;
favg = (t1 + t2) / 2;
lcd.setCursor(0, 0);
lcd.print(favgin);
lcd.setCursor(2, 0);
lcd.write(0);
lcd.setCursor(3, 0);
lcd.write(2);
lcd.setCursor(4, 0);
lcd.write(1);
lcd.setCursor(5, 0);
lcd.print(havgin);
lcd.setCursor(7, 0);
lcd.print("%");
lcd.setCursor(8, 0);
lcd.write(2);
if (millis() - pumpStartTime >= 5000) {
digitalWrite(S1, LOW); // turn off S1
pumpStartTime = 0; // reset the start time
}
if (pumpStartTime == 0) {
lcd.setCursor(13, 0);
lcd.write(2);
} else {
lcd.setCursor(13, 0);
lcd.write(6);
}
if (millis() - fanStartTime >= 5000) {
digitalWrite(21, LOW); // turn off S1
fanStartTime = 0; // reset the start time
}
if (fanStartTime == 0) {
lcd.setCursor(14, 0);
lcd.write(2);
} else {
lcd.setCursor(14, 0);
lcd.write(4);
}
// Read the Analog Input
adc_value = analogRead(ANALOG_IN_PIN);
// Determine voltage at ADC input
adc_voltage = (adc_value * ref_voltage) / 4096.0;
// Calculate voltage at divider input
in_voltage = adc_voltage / (R2 / (R1 + R2));
// Print results to Serial Monitor to 2 decimal places
//Serial.println(in_voltage, 2);
if(in_voltage != 0){
lcd.setCursor(12, 0);
lcd.write(3);
}
else{
lcd.setCursor(12, 0);
lcd.write(2);
}
}
}
void SelectMenu(void) {
boolean Display = true;
boolean Exit = false;
boolean ButtonSet = true;
boolean ButtonUp = false;
boolean ButtonDown = false;
boolean ButtonCancel = false;
byte Menu = 0;
const char MenuText[4][17] = {
" Temperatures",
" Humidity",
" Water Pump",
" Open Fan"
};
while (Exit == false) {
if (Display == true) {
Display = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Menu");
lcd.setCursor(0, 1);
lcd.print(MenuText[Menu]);
}
if (digitalRead(BUTTON_SET) == 0) {
delay(50);
if (digitalRead(BUTTON_SET) == 0) {
if (ButtonSet == false) {
ButtonSet = true;
switch (Menu) {
case 0:
//--------------------------------------Manu 1
lcd.setCursor(0, 1);
lcd.print(t1, 2);
lcd.setCursor(5, 1);
lcd.write(0);
lcd.setCursor(7, 1);
lcd.print(t2, 2);
lcd.setCursor(12, 1);
lcd.write(0);
break;
case 1:
//--------------------------------------Manu 2
lcd.setCursor(0, 1);
lcd.print(h1, 2);
lcd.setCursor(5, 1);
lcd.write(1);
lcd.setCursor(7, 1);
lcd.print(h2, 2);
lcd.setCursor(12, 1);
lcd.write(1);
break;
case 2:
//--------------------------------------Manu 3
pumpStartTime = 1;
lcd.clear();
Exit = true;
pumpopen();
break;
case 3:
//--------------------------------------Manu 4
fanStartTime = 1;
lcd.clear();
Exit = true;
fanopen();
break;
}
}
}
}
else {
ButtonSet = false; {
if (digitalRead(BUTTON_UP) == 0) {
delay(50);
if (digitalRead(BUTTON_UP) == 0) {
if (ButtonUp == false) {
ButtonUp = true;
if (Menu < 3) {
Menu++;
} else {
Menu = 0;
}
Display = true;
}
}
} else {
ButtonUp = false;
}
}
}
if (digitalRead(BUTTON_CANCEL) == 0) {
delay(50);
if (digitalRead(BUTTON_CANCEL) == 0) {
if (ButtonCancel == false) {
ButtonCancel = true;
lcd.clear();
Exit = true;
}
}
} else {
ButtonCancel = false;
}
}
}
void pumpopen(void) {
if (pumpStartTime == 1) {
digitalWrite(S1, HIGH); // turn on S1
pumpStartTime = millis(); // record the start time
}
}
void fanopen(void) {
if (fanStartTime == 1) {
digitalWrite(S2, HIGH); // turn on S1
fanStartTime = millis(); // record the start time
}
}
void SEOD(void) {
if (wifioffline == 1) {
Serial.println("END OF DAY");
HTTPClient http;
http.begin("https://script.google.com/macros/s/AKfycbxZAqqvw3e_P6_yTAwZsgcHrLvxlUI6OF8bVozeEI8P1H_GcNVQWcX7VhaQXehBkgWtvg/exec?end");
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
lcd.setCursor(15, 0);
lcd.write(2);
Serial.print("OK");
} else {
Serial.print("Error");
lcd.setCursor(15, 0);
lcd.write(2);
}
http.end();
}
else{
Serial.print("U R Offline");
}
}
void SSGG(void) {
if (wifioffline == 1) {
lcd.setCursor(15, 0);
lcd.write(5);
String url = "https://script.google.com/macros/s/" + GAS_ID + "/exec?temp1=" + t1 + "&temp2=" + t2 + "&tempavg=" + favg + "&humi1=" + h1 + "&humi2=" + h2 + "&humiavg=" + havg + "&water=" + "-" + "&co=" + "-" + "&Volt=" + in_voltage; // variables
HTTPClient http;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
lcd.setCursor(15, 0);
lcd.write(2);
Serial.print("OK");
} else {
Serial.print("Error");
lcd.setCursor(15, 0);
lcd.write(2);
}
http.end();
}
else{
Serial.print("U R Offline");
}
}