// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int x=125;
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
struct tm timeinfo;
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec =10800; // 60*60* (+3 GMT) =10800 Jordan TimeZone
const int daylightOffset_sec = 1; //no Daylight saving in Jordan Currently
int switchPin = 14; //the switch connect to pin 12
int switchState = 0; // variable forreading the pushbutton status
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void printLocalTime() {
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
/*LCD.setCursor(8, 0);
LCD.println(&timeinfo, "%H:%M:%S");
LCD.setCursor(0, 1);
LCD.println(&timeinfo, "%d/%m/%Y %Z");*/
}
void setup() {
Serial.begin(115200);
pinMode(switchPin, INPUT_PULLDOWN); //initialize thebuttonPin as input
LCD.init();
LCD.backlight();
LCD.clear();
LCD.setCursor(0, 0);
/* LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
*/
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
LCD.clear();
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print(WiFi.SSID());
delay(500);
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
void loop() {
/* printLocalTime();
//LCD.clear();
LCD.setCursor(0, 0);
LCD.println(x);
LCD.setCursor(0, 1);
LCD.println("Drn");
LCD.setCursor(4, 0);
LCD.println("125");
LCD.setCursor(4, 1);
LCD.println("Fll");
LCD.setCursor(8 ,0);
LCD.println(&timeinfo, "%r");
LCD.setCursor(8, 1);
LCD.println(&timeinfo, "%p");
LCD.setCursor(11, 1);
LCD.println(&timeinfo, "%a");*/
//7,0 7,1
//8,0 8,1
/*
LCD.setCursor(8, 0);
LCD.println(&timeinfo, "%H:%M:%S");
LCD.setCursor(0, 1);
LCD.println(&timeinfo, "%d/%m/%Y %Z");
}*/
printLocalTime();
char sec[4];
//int =timeinfo
strftime(sec, sizeof(sec), "%S", &timeinfo);
int secs=atoi(sec);
Serial.println(secs);
char w[]="1%";
char f[6]="15";
switchState = digitalRead(switchPin);
Serial.printf("switchState=%d\n",switchState);
if (switchState == HIGH ) //if it is,the state is HIGH
{
//f=f+"%";
strcat(f,"%");
if (secs>=0 && secs<10 || secs>=30 &&secs<40){
if (secs==00 || secs==20){
LCD.clear();
}
//LEFT
LCD.setCursor(0, 0);
LCD.println(f);
LCD.setCursor(0, 1);
LCD.println("DRN");
//Right
LCD.setCursor(4, 0);
LCD.println(w);
LCD.setCursor(4, 1);
LCD.println("FLN");
//Time
LCD.setCursor(8 ,0);
LCD.println(&timeinfo, "%r");
LCD.setCursor(8, 1);
LCD.println(&timeinfo, "%p");
LCD.setCursor(11, 1);
LCD.println(&timeinfo, "%a");
}
// for(int i =0;i<100;i++){
//delay (500);
//}
else if (secs>=11 &&secs<20 || secs>=40 &&secs<50){
if( secs==11 || secs==40){
LCD.clear();
}
LCD.setCursor(4, 0);
LCD.println("Local IP");
LCD.setCursor(2,1);
//LCD.println(WiFi.localIP());
LCD.println("192.168.1.220");
}
else if (secs>=20 &&secs<30 || secs>=50 &&secs<60){
if(secs==20 || secs==50){
LCD.clear();
}
LCD.setCursor(2, 0);
LCD.println("MAC Address");
LCD.setCursor(0,1);
LCD.println(WiFi.BSSIDstr());
}
}
else {
LCD.clear();
//LEFT
LCD.setCursor(0, 0);
LCD.println(f);
LCD.setCursor(0, 1);
LCD.println("DRN");
//Right
LCD.setCursor(4, 0);
LCD.println(w);
LCD.setCursor(4, 1);
LCD.println("FLN");
//Time
LCD.setCursor(8 ,0);
LCD.println(&timeinfo, "%r");
LCD.setCursor(8, 1);
LCD.println(&timeinfo, "%p");
LCD.setCursor(11, 1);
LCD.println(&timeinfo, "%a");
}
/*Faster Cycle
if (secs>=0 && secs<10 || secs>=20 &&secs<30 || secs>=40 &&secs<50 ){
if (secs==00 || secs==20 ||secs==40){
LCD.clear();
}
//LEFT
LCD.noAutoscroll();
LCD.setCursor(0, 0);
LCD.println(f);
LCD.setCursor(0, 1);
LCD.println("Drn");
//Right
LCD.setCursor(4, 0);
LCD.println(w);
LCD.setCursor(4, 1);
LCD.println("Fll");
//Time
LCD.setCursor(8 ,0);
LCD.println(&timeinfo, "%r");
LCD.setCursor(8, 1);
LCD.println(&timeinfo, "%p");
LCD.setCursor(11, 1);
LCD.println(&timeinfo, "%a");
}
// for(int i =0;i<100;i++){
//delay (500);
else if (secs>=11 &&secs<15 || secs>=30 &&secs<35 || secs>=50 &&secs<55){
if( secs==11 || secs==40 ||secs==50){
LCD.clear();
}
LCD.noAutoscroll();
LCD.setCursor(0, 0);
LCD.println(WiFi.BSSIDstr());
LCD.setCursor(2,1);
//LCD.println(WiFi.localIP());
LCD.println("192.168.1.230");
}
else if (secs>=15 &&secs<20 || secs>=35 &&secs<40 ||secs>=55 &&secs<60){
if(secs==15 || secs==35 || secs==55){
LCD.clear();
}
LCD.setCursor(2, 0);
//LCD.println("MAC Address");
LCD.println(WiFi.SSID());
// LCD.scrollDisplayLeft();
//LCD.setCursor(0,1);
//LCD.println(WiFi.BSSIDstr());
}
*/
delay(1000);
}