#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 28800, 60000);
WiFiClient client;
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const int led_red = 17;
const int led_yellow = 16;
const int led_green = 4;
int flag_red_led=0;
int flag_yellow_led=0;
int flag_green_led=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
//serial monitor enable
//lcd test
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("lab 3 v1");
lcd.setCursor(0,1);
lcd.print("By Nasrul_2023");
delay(110);
lcd.clear();
//connecting to ssid notification
WiFi.disconnect();
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
//check wifi status
while (WiFi.status() != WL_CONNECTED) {
delay(550);
Serial.print(".");
}
Serial.println("");
Serial.print(" connected to wifi...");
Serial.println(ssid);
Serial.println();
//PIN LED AS output
pinMode(led_red, OUTPUT); // initialize digital ledPin as an output.
pinMode(led_green, OUTPUT); // initialize digital ledPin as an output.
pinMode(led_yellow, OUTPUT); // initialize digital ledPin as an output.
timeClient.begin();
}
void loop() {
//read sensor and display on lcd time with tem& humi
//enable ntp
timeClient.update();
Serial.println(timeClient.getFormattedTime());
//delay(1000);
lcd.setCursor(0,0);
lcd.print(" ");
lcd.print(timeClient.getFormattedTime());
lcd.setCursor(0,1);
lcd.print("L1(");
lcd.print(flag_red_led);
lcd.print(")");
lcd.print("L2(");
lcd.print(flag_yellow_led);
lcd.print(")");
lcd.print("L3(");
lcd.print(flag_green_led);
lcd.print(")");
int masa1= 20;
int masa2=40;
int masa3=55;
int masa_now = timeClient.getSeconds();
//led red timing on
//masa set
if(masa_now<=masa1){
digitalWrite(led_red,HIGH); //
digitalWrite(led_yellow,LOW); //
digitalWrite(led_green,LOW); //
flag_red_led=1;
flag_yellow_led=0;
flag_green_led=0;
}
if(masa_now>masa1 && masa_now<=masa2){
digitalWrite(led_red,LOW); //
digitalWrite(led_yellow,HIGH); //
digitalWrite(led_green,LOW); //
flag_red_led=0;
flag_yellow_led=1;
flag_green_led=0;
}
if(masa_now>masa2 && masa_now<=masa3){
digitalWrite(led_red,LOW); //
digitalWrite(led_yellow,LOW); //
digitalWrite(led_green,HIGH); //
flag_red_led=0;
flag_yellow_led=0;
flag_green_led=1;
}
if(masa_now>masa3){
digitalWrite(led_red,LOW); //
digitalWrite(led_yellow,LOW); //
digitalWrite(led_green,LOW); //
flag_red_led=0;
flag_yellow_led=0;
flag_green_led=0;
}
}