240 lines
6.7 KiB
C++

/* ===================================================================================
* THIS SECTION IS FOR METHODES AND FUNCTIONS
* =================================================================================*/
void setupWiFi()
{
// Turn On LCD
lcd_light_on();
// Printing Device Mac Address
Serial.println();
Serial.println("-----------------------------------------------------------");
Serial.println(" TAKEONE WIFI MANAGER STARTING ");
Serial.println("-----------------------------------------------------------");
//Serial.println("TAKEONE: MAC Address:");
//Serial.println("TAKEONE: " + devMac);
// Starting File System
Serial.println("TAKEONE: Mounting File System");
read_config();
// The extra parameters to be configured (can be either global or just in the setup)
WiFiManagerParameter custom_devID("devID", "Club UserID", devID.c_str(), 100);
WiFiManagerParameter custom_devPASS("devPASS", "Club Password", devPASS.c_str(), 100);
//WiFiManager Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//add all your parameters here
//wifiManager.addParameter(&custom_server);
wifiManager.addParameter(&custom_devID);
wifiManager.addParameter(&custom_devPASS);
// Display Booting Up Message
lcd_clear();
lcd_line1("TAKEONE WiFi");
lcd_line2("RFID < READY >");
delay(2500);
// Reset Settings
if(button.pressed())
{
reconfig();
}
// Display On OLED
lcd_clear();
lcd_line1("CONNECTING TO");
lcd_line2("WiFi ROUTER");
delay(2500);
// Trying To Connect To Network Or Reconfig
if (!wifiManager.autoConnect(devName.c_str())) {
lcd_clear();
lcd_line1("CONNECTION TO");
lcd_line2("ROUTER FAILD!");
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
lcd_clear();
lcd_line1("< REBOOTING >");
delay(3000);
ESP.restart();
}
// Copying The SSID & Password Of Successfully Connected Network
WiFi_SSID = wifiManager.getSSID();
WiFi_PASS = wifiManager.getPassword();
lcd_clear();
lcd_line1("CONNECTION");
lcd_line2("< SUCCESSFUL >");
delay(2500);
lcd_clear();
lcd_line1("IP ADDRESS");
lcd_line2(WiFi.localIP().toString());
delay(2500);
//read updated parameters
devID = custom_devID.getValue();
devPASS = custom_devPASS.getValue();
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonDocument json(4000);
// Assignning Parameters To JSON
json["devID"] = devID;
json["devPASS"] = devPASS;
// Opening The File For Reading
File configFile = SPIFFS.open("/config.json", "w");
// Checking If File Opened
if (!configFile) {
Serial.println("failed to open config file for writing");
}
// Print JSON Content On Monitor
String memory = "";
serializeJson(json, memory);
// Print To Serial Monitor
Serial.println(memory);
// Write To File
configFile.print(memory);
// Closing Config File
configFile.close();
// Clearing The Buffer Memory
json.clear();
//end save
}
// Display Data
Serial.println("TAKEONE: divID : " + devID);
Serial.println("TAKEONE: divPASS : " + devPASS);
Serial.println("TAKEONE: divMAC : " + devMac);
// End Of WiFi Manager
Serial.println("-----------------------------------------------------------");
// Clearing LCD
lcd_clear();
// Turnning Off LCD Light
lcd_light_off();
}
/* ===================================================================================
* THIS SECTION IS FOR METHODES AND FUNCTIONS
* =================================================================================*/
void saveConfigCallback ()
{
Serial.println("Should Save Config File");
shouldSaveConfig = true;
}
/* ===================================================================================
* THIS SECTION IS FOR METHODES AND FUNCTIONS
* =================================================================================*/
void reconfig()
{
lcd_clear();
lcd_light_on();
lcd_line1("Reset WiFi");
lcd_line2("Settings");
// Make Sure That Spiffs Started
if(!strtFS) {
delay(1000);
wipeFS;
}
WiFiManager wifiManager;
delay(1500);
wifiManager.resetSettings();
lcd_clear();
lcd_line1("Formating SPIFFS");
delay(1500);
// LCD Display
lcd_clear();
lcd_line1(lcd_center("< REBOOTING >"));
delay(1500);
lcd_light_off();
ESP.restart();
}
/* ===================================================================================
* THIS SECTION IS FOR METHODES AND FUNCTIONS
* =================================================================================*/
bool checkWiFi()
{
if(WiFi.status() == WL_CONNECTED)
{
return true;
}
else
{
return false;
}
}
/* ===================================================================================
* Reading Config File
* =================================================================================*/
void read_config()
{
// After Suceessfull Do The Following
if(strtFS)
{
Serial.println(" -> Mounted File System");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println(" -> Reading Config File");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
//Serial.println(" -> Opened Config File");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
// Display The Read Data
String mydata = String(buf.get());
mydata.trim();
//Serial.println(" -> My Stored Data : " + mydata);
// JSON Document
DynamicJsonDocument json(2000);
DeserializationError error = deserializeJson(json, mydata);
if (!error) {
devID = json["devID"].as<String>();
devPASS = json["devPASS"].as<String>();
} else {
Serial.println(" -> failed to load json config");
}
// Clearing JSON Memory
json.clear();
}
}
} else {
Serial.println(" -> failed to mount FS");
}
}