#include "Arduino.h"
#include "WebSerialLite.h"
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ESPmDNS.h>
#include "esp_wifi.h"
#include "esp_system.h"
#include "LittleFS.h"
#include "ArduinoJson.h"
void Home();
extern void loadSettings();
extern void updateSettings();
void webpageSetup();
void webPrint(String);
extern bool isWifiConnected;
extern StaticJsonDocument<1000> settings;
#define http_username "Admin"
#define http_password "Admin"
String webpage;
String Version = "1.0.0";
const char *ssid = "VA_SUB_02";
const char *password = "controlytics";
bool isWifiConnected = false;
AsyncWebServer server(80);
String HTML_Footer();
String HTML_Header();
bool StartMDNSservice(const char *Name);
void updateData(AsyncWebServerRequest *request, int ARGs);
StaticJsonDocument<1000> settings;
bool StartupErrors = false;
const char *ServerName = "cai_ss";
void loadSettings() {
Serial.println("getting settings from spiffs");
LittleFS.begin();
File spiffsFile = LittleFS.open("/settings.txt", FILE_READ);
String spiffsString = spiffsFile.readString();
deserializeJson(settings, spiffsString);
spiffsFile.close();
serializeJson(settings, Serial);
}
void updateSettings() {
Serial.println("Saving settings to Spiffs");
File spiffsFile = LittleFS.open("/settings.txt", FILE_WRITE);
serializeJson(settings, spiffsFile);
spiffsFile.close();
}
void Home() {
delay(100);
isWifiConnected = true;
String searchKey = ""; // Machine Name
String searchValue = "";
JsonObject checkObject = settings.as<JsonObject>(); //
webpage = "";
webpage = HTML_Header();
webpage += "<h1></br>CAN TX</br></h1>";
webpage += HTML_Footer();
}
void updateData(AsyncWebServerRequest *request, int ARGs) {
JsonObject checkObject = settings.as<JsonObject>();
webpage = "";
webpage = HTML_Header();
webpage += "</br>";
webpage += "<h1>Updated Form</h1>";
Serial.println("After Updated Value Json Serial:");
serializeJson(settings, Serial);
for (JsonPair kv : checkObject) {
String key = kv.key().c_str();
if (request->hasArg(key.c_str())) { // Fixing the issue with hasArg() function
checkObject[key] = request->arg(key.c_str());
}
}
Serial.println("Updated SPIFFs");
serializeJson(settings, Serial);
updateSettings();
webpage += "</br></br><a href='/'> Updated </a></br></br>";
webpage += HTML_Footer();
}
bool StartMDNSservice(const char *Name) {
esp_err_t err = mdns_init(); // Initialise mDNS service
if (err) {
printf("MDNS Init failed: %d\n", err); // Return if error detected
return false;
}
mdns_hostname_set(Name); // Set hostname
return true;
}
String HTML_Header() {
String page;
page = "<!DOCTYPE html>";
page += "<html lang = 'en'>";
page += "<head>";
page += "<title>CAN</title>";
page += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>"; // For mobile device
page += "<meta charset='UTF-8'>"; // Needed for special characters
page += "<style>";
page += "body {width:75em;margin-left:auto;margin-right:auto;font-family:Arial,Helvetica,sans-serif;font-size:16px;color:#0099cc;background-color:white;text-align:center;}";
page += "footer {padding:0.08em;background-color:#0099cc;font-size:1.1em;color:white;}";
page += "table {font-family:arial,sans-serif;width:70%;border-collapse:collapse;}";
page += "table.center {margin-left:auto;margin-right:auto;}";
page += "td, th {text-align:left;padding:8px;border:1px solid #dddddd;}";
page += "tr:nth-child(even) {background-color:white;}";
page += "h4 {color:slateblue;font:0.8em;text-align:left;font-style:oblique;text-align:center;}";
page += ".center {margin-left:100px;margin-right:100px;}";
page += "@media screen and (max-width: 480px),only screen and (-webkit-min-device-pixel-ratio:2),screen and (-webkit-min-device-pixel-ratio:1.5) {";
page += "body {width :100%}";
page += "}";
page += ".topnav {width: auto;margin: auto;height:auto}";
page += ".topnav {overflow: hidden;background-color:#0099cc;width:100%; align: center;}";
page += ".topnav a {float:left;color:white;text-align:center;padding:0.6em 0.6em;text-decoration:none;font-size:1.3em;margin-left: 10px;margin-right: 10px;}";
page += ".topnav a:hover {background-color:deepskyblue;color:white;}";
page += ".topnav a.active {background-color:lightblue;color:#0099cc;}";
page += ".notfound {padding:0.8em;text-align:center;font-size:1.5em;}";
page += ".left {text-align:left;}";
page += ".medium {font-size:1.4em;padding:0;margin:0}";
page += ".ps {font-size:0.7em;padding:0;margin:0}";
page += ".sp {background-color:silver;white-space:nowrap;width:2%;}";
page += "</style>";
page += "</head>";
page += "<body>";
page += "<div class = 'topnav'>";
page += "<a href='/'>Home</a>";
page += "<a href='/webserial'>View Live Readings</a>";
page += "</div>";
return page;
}
String HTML_Footer() {
String page;
page += "<br><br><footer>";
page += "<p class='medium'>CONTROLYTICS AI PRIVATE LIMITED</p>";
page += "<p class='ps'><i>Copyright © 2024 Version " + Version + "</i></p>";
page += "</footer>";
page += "</body>";
page += "</html>";
return page;
}
void webpageSetup() {
WiFi.disconnect(true);
delay(100);
WiFi.mode(WIFI_MODE_APSTA);
delay(100);
esp_wifi_set_ps(WIFI_PS_NONE);
delay(100);
WiFi.softAP(ssid, password); //(SSID, Password)
delay(500);
if (!StartMDNSservice(ServerName)) {
Serial.println("Error starting mDNS Service...");
StartupErrors = true;
}
// Home Page Handler
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println("Home Page...");
if (!request->authenticate(http_username, http_password)) {
return request->requestAuthentication();
}
Home(); // Build webpage ready for display
request->send(200, "text/html", webpage);
});
// Update Data Handler
server.on("/updateData", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println("Displaying Admin Access...");
updateData(request, request->args());
request->send(200, "text/html", webpage);
});
server.begin(); // Start the server
WebSerial.begin(&server);
if (!StartupErrors) {
Serial.println("System started successfully...");
} else {
Serial.println("There were problems starting all services...");
}
}
void webPrint(String temp) {
if (isWifiConnected) {
WebSerial.print(F(temp.c_str()));
}
}
void setup() {
Serial.begin(115200);
webpageSetup();
}
void loop() {
String distance_str = "distance: " " cm\n";
webPrint(distance_str);
}