#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <RTClib.h>
#include <DHT.h>
#include <Arduino.h>
#include <ArduinoJson.h>
#include <math.h>
#include <time.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define DATABASE_URL "https://iot-202402-eb-solution-default-rtdb.firebaseio.com/.json"
#define DHTPIN 25
#define DHTTYPE DHT22
HTTPClient client;
DHT dht(DHTPIN, DHTTYPE);
char timeStringBuffer[20];
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 rtc;
String data;
const int relay1 = 34;
const int relay2 = 35;
const int relay3 = 32;
const int relay4 = 33;
const char daysOfTheWeek[7][12] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
void presentation() {
Serial.println(F("EASY IRRIOT"));
Serial.println("\tWelcome, EASY IRRIOT is the next upgrade for your farm that allows you to improve the irrigation system with ease!");
Serial.println(F("\tDeveloped by Andres Leiva"));
}
void setup(){
Serial.begin(115200);
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(5, 1);
lcd.print("WELCOME TO");
lcd.setCursor(8, 2);
lcd.print("EASY IRRIOT!");
delay(4000);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
presentation();
delay(500);
rtc.begin();
lcd.clear();
// Basic WiFi configuration
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
delay(250);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Stablish connection
client.begin(DATABASE_URL);
int httpResponseCode = client.GET();
// Time Client Configuration
configTime(-9000, -9000, "1.south-america.pool.ntp.org");
}
void loop () {
DateTime now = rtc.now();
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
lcd.setCursor(3,0);
lcd.print("Time:");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
String operationMode = "ScheduleMode";
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (now.second() > 1 && now.second() < 15 ) {
lcd.setCursor(0,1);
lcd.print("Relay1:ON ");
Serial.println("relay1 is on");
digitalWrite(relay1, HIGH);
operationMode = "ScheduleMode";
} else {
lcd.setCursor(0,1);
lcd.print("Relay1:OFF");
digitalWrite(relay1,LOW);
}
if (now.second() > 20 && now.second() < 30) {
lcd.setCursor(10,1);
lcd.print("Relay2:ON ");
Serial.println("relay2 is on");
digitalWrite(relay2, HIGH);
} else {
lcd.setCursor(10,1);
lcd.print("Relay2:OFF");
digitalWrite(relay2, LOW);
}
if (now.second() > 35 && now.second() < 45) {
lcd.setCursor(0,2);
lcd.print("Relay3:ON ");
Serial.println("relay3 is on");
digitalWrite(relay3, HIGH);
} else {
lcd.setCursor(0,2);
lcd.print("Relay3:OFF");
digitalWrite(relay3, LOW);
}
if(now.second() > 50 && now.second() < 59) {
lcd.setCursor(10,2);
lcd.print("Relay4:ON ");
Serial.println("relay4 is on");
digitalWrite(relay4, HIGH);
} else {
lcd.setCursor(10,2);
lcd.print("Relay4:OFF");
digitalWrite(relay4, LOW);
}
// Error handling from sensors
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("An error has occurred when reading data from the DHT sensor"));
return;
}
lcd.setCursor(0,3);
lcd.print(F("°C: "));
lcd.print(temperature);
lcd.setCursor(10,3);
lcd.print(F("Hum: "));
lcd.print(humidity);
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Time Error");
return;
}
strftime(timeStringBuffer, sizeof(timeStringBuffer), "%d/%m/%Y %H:%M", &timeinfo);
Serial.println(String(timeStringBuffer));
// Creating JSON as payload from PATCH request
StaticJsonDocument<200> jsonDoc;
JsonObject irriotData = jsonDoc.createNestedObject("irriotData");
irriotData["deviceId"] = "EIR-01";
irriotData["createdAt"] = String(timeStringBuffer);
irriotData["currentHumidity"] = humidity;
irriotData["currentTemperature"] = temperature;
irriotData["operationMode"] = operationMode;
String jsonData;
serializeJson(jsonDoc, jsonData);
// Send information to api endpoint
client.begin(DATABASE_URL);
client.addHeader("Content-Type", "application/json");
int httpResponseCode = client.PATCH(jsonData);
if (httpResponseCode > 0) {
String response = client.getString();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.print("Response from server: ");
Serial.println(response);
} else {
Serial.print("Error on sending PATCH: ");
Serial.println(httpResponseCode);
Serial.print("Error details: ");
Serial.println(client.errorToString(httpResponseCode).c_str());
}
client.end();
}