#include <WiFi.h>
#include "time.h"
#include "TimeLib.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHTPIN 23//33
#define DHTTYPE DHT22
#define TDSPIN 34 //TDS PIN
#define ABPIN 12
#define SMPIN 14
#define FMPIN 27
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 25200 //UTC+7
const char *ssid = "Abdullah"; // NTP
const char *password = "bismillaah"; // NTP
byte myHour, myMin, mySec;
int tdsValue;
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
lcd.setCursor(15, 2);
lcd.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
lcd.setCursor(0, 0);
lcd.println("Conn Err");
return;
}
lcd.setCursor(0, 0);
lcd.print(&timeinfo, "%d/%m/%Y %H:%M:%S");
getLocalTime(&timeinfo);
myHour = timeinfo.tm_hour;
myMin = timeinfo.tm_min;
mySec = timeinfo.tm_sec;
Serial.println(String(myHour) + ":" + String(myMin) + ":" + String(mySec));
}
void dht22() {
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
t = 0;
h = 0;
}
byte customDegreeSymbol[8] = {
B00000,
B00110,
B00110,
B00000,
B00000,
B00000,
B00000,
B00000
};
lcd.createChar(0, customDegreeSymbol); // Simpan di address 0
lcd.setCursor(0, 1);
lcd.print("T:" + String(t,1));
lcd.setCursor(6, 1);
// lcd.print((char)223);
lcd.write(byte(0)); // Menampilkan karakter khusus derajat Celsius
lcd.setCursor(7, 1);
lcd.print("C");
lcd.setCursor(0, 2);
lcd.print("H:" + String(h,1) + "%");
}
void tds() {
tdsValue = analogRead(TDSPIN);
lcd.setCursor(0, 3);
lcd.print("TDS:" + String(tdsValue) + "ppm ");
}
void ABPump() {
Serial.println("Spray Mode");
digitalWrite(ABPIN, HIGH);
digitalWrite(SMPIN, LOW);
digitalWrite(FMPIN, LOW);
lcd.setCursor(12, 1);
lcd.print("AB: > ON");
lcd.setCursor(12, 2);
lcd.print("SM: ");
lcd.setCursor(12, 3);
lcd.print("FM: ");
delay5s();
}
void stirMode() {
Serial.println("Stir Mode");
digitalWrite(ABPIN, LOW);
digitalWrite(SMPIN, HIGH);
digitalWrite(FMPIN, LOW);
lcd.setCursor(12, 1);
lcd.print("AB: ");
lcd.setCursor(12, 2);
lcd.print("SM: > ON");
lcd.setCursor(12, 3);
lcd.print("FM: ");
delay10s();
}
void flushMode() {
Serial.println("Flush Mode");
digitalWrite(ABPIN, LOW);
digitalWrite(SMPIN, LOW);
digitalWrite(FMPIN, HIGH);
lcd.setCursor(12, 1);
lcd.print("AB: ");
lcd.setCursor(12, 2);
lcd.print("SM: ");
lcd.setCursor(12, 3);
lcd.print("FM: > ON");
delay10s();
}
void setup() {
Serial.begin(115200);
pinMode(TDSPIN, INPUT);
pinMode(ABPIN, OUTPUT);
pinMode(SMPIN, OUTPUT);
pinMode(FMPIN, OUTPUT);
dht.begin();
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 1);
lcd.print("Connecting to ");
lcd.setCursor(0, 2);
lcd.print("WiFi ");
// WiFi.begin(ssid, password);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
lcd.clear();
}
void loop() {
dht22();
tds();
printLocalTime();
delay1s();
if ((myHour == 17 && myMin == 35 && mySec < 10) || (myHour == 17 && myMin == 37 && mySec < 10)){
if (tdsValue < 800) {
for (int x = 0; x < 100; x++) {
ABPump();
stirMode();
tds();
if (tdsValue > 800) {
flushMode();
break;
}
}
} else {
flushMode();
return;
}
}
actuatorOff();
}
void actuatorOff(){
digitalWrite(FMPIN, LOW);
lcd.setCursor(12, 3);
lcd.print("FM: ");
digitalWrite(SMPIN, LOW);
lcd.setCursor(12, 2);
lcd.print("SM: ");
digitalWrite(ABPIN, LOW);
lcd.setCursor(12, 1);
lcd.print("AB: ");
}
void delay1s() {
for (int x = 0; x < 10; x++) {
delay(100);
if (x == 9) {
break;
}
}
}
void delay5s() {
for (int x = 0; x < 50; x++) {
delay(100);
if (x == 49) {
break;
}
}
}
void delay10s() {
for (int x = 0; x < 100; x++) {
delay(100);
if (x == 99) {
break;
}
}
}
void delay20s() {
for (int x = 0; x < 200; x++) {
delay(100);
if (x == 199) {
break;
}
}
}