#include "WiFi.h"
#include <LiquidCrystal_I2C.h>
#include <time.h>
/*ID-ing the LCD*/
LiquidCrystal_I2C LCD=LiquidCrystal_I2C(0x27,16,4);
/*Wifi SSID/Pass*/
String password;
String ssid;
/*Setup the NTP connection*/
const char* ntpServer = "pool.ntp.org";
const long gwtOffset_sec = 10800;
const int daylightOffset_sec = 0;
void printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
LCD.setCursor(0,0);
LCD.print(&timeinfo, "%A");
LCD.setCursor(0,1);
LCD.print(&timeinfo, "%B %d %Y");
LCD.setCursor(0,2);
LCD.print(&timeinfo, "%H:%M:%S");
}
void wifiScan(){
if (WiFi.status() == WL_CONNECTED) {
return;
}
LCD.print("scan start");
for (int i = 0 ; i < 6 ; i++ ){
delay(100);
LCD.print(".");
}
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
delay(100);
LCD.clear();
LCD.print("scan done");
Serial.println("scan done");
delay(1000);
LCD.clear();
if (n == 0) {
LCD.print("no networks found");
Serial.println("no networks found");
} else {
Serial.print(n);
LCD.print(n);
LCD.print(" networks found");
Serial.println(" networks found");
delay(1000);
LCD.clear();
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
Serial.println("\nWhat Network would you like to join?");
int i = 0;
int menuChoice;
String respond;
while(respond != "next"){
LCD.print("Select WIFI:");
LCD.setCursor(0,1);
LCD.print(i + 1);
LCD.print(": ");
LCD.print(WiFi.SSID(i));
LCD.setCursor(0,2);
LCD.print(i + 2);
LCD.print(": ");
LCD.print(WiFi.SSID(i + 1));
LCD.setCursor(14,3);
LCD.print("Next >");
while (Serial.available() == 0) {
}
String respond = Serial.readString();
respond.trim();
Serial.println(respond);
if (respond == "next") {
i = i + 2;
if(i >= n){
i = 0;
}
LCD.clear();
}
else {
menuChoice = respond.toInt();
LCD.clear();
break;
}
}
ssid = WiFi.SSID(menuChoice - 1);
Serial.println(ssid);
if(WiFi.encryptionType(menuChoice - 1) == 0){
password = "";
}
else {
LCD.print("Enter Password For");
LCD.setCursor(0,1);
LCD.print(ssid);
LCD.print(":");
Serial.println("Please enter your password: ");
while(Serial.available() == 0){
}
password = Serial.readString();
}
WiFi.begin(ssid,password);
LCD.clear();
LCD.print("Connecting to ");
LCD.setCursor(0,1);
LCD.print(ssid);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
LCD.clear();
LCD.print("Connected!!");
delay(1000);
LCD.clear();
Serial.println("\nConnected to WiFi");
// Wait a bit before scanning again
delay(5000);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
LCD.init();
LCD.backlight();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
wifiScan();
configTime(gwtOffset_sec ,daylightOffset_sec ,ntpServer );
printLocalTime();
delay(100);
Serial.println("Setup done");
}
void loop() {
printLocalTime();
}