|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
#include <WiFi.h>
|
|
|
|
#include <FS.h>
|
|
|
|
#include <SPIFFS.h>
|
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
|
|
|
|
#include "wifi-credentials.h"
|
|
|
|
|
|
|
|
AsyncWebServer server(80);
|
|
|
|
|
|
|
|
int16_t batteryVoltage = -1; //in mV
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
|
|
|
if(!SPIFFS.begin(false)){
|
|
|
|
Serial.println("SPIFFS Mount Failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to Wi-Fi
|
|
|
|
WiFi.begin(wifi_ssid, wifi_password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
|
|
{
|
|
|
|
delay(1000);
|
|
|
|
Serial.println("Connecting to Wifi...");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print ESP Local IP Address
|
|
|
|
Serial.print("Wifi connected, ip=");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
|
|
|
|
server.on("/api/status", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
int v = batteryVoltage;
|
|
|
|
char json[128];
|
|
|
|
sprintf(json, "{\"v\":%d,\"c\":1000}", v);
|
|
|
|
request->send(200, "text/json", json);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Special case to send index.html without caching
|
|
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(SPIFFS, "/www/index.html", "text/html"); });
|
|
|
|
server.serveStatic("/index.html", SPIFFS, "/www/index.html");
|
|
|
|
|
|
|
|
// Other static files are cached (index.html knows whether to ignore caching or not for each file)
|
|
|
|
server.serveStatic("/", SPIFFS, "/www/").setCacheControl("max-age=5184000");
|
|
|
|
|
|
|
|
server.begin();
|
|
|
|
Serial.println("HTTP server started");
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
const int potPin = 34;
|
|
|
|
|
|
|
|
delay(1000);
|
|
|
|
int16_t analogV = analogRead(potPin);
|
|
|
|
float v = (float)analogV / 4096.0f * 3.3f;
|
|
|
|
batteryVoltage = (int16_t)(v * 1000.0f + 0.5f);
|
|
|
|
//Serial.println(potValue);
|
|
|
|
}
|