better formatting
All checks were successful
Test compiling project / test (push) Successful in 1m42s
All checks were successful
Test compiling project / test (push) Successful in 1m42s
This commit is contained in:
191
src/main.cpp
191
src/main.cpp
@@ -1,13 +1,13 @@
|
||||
#include <Arduino.h>
|
||||
#include "SPIFFS.h"
|
||||
#include <WiFi.h>
|
||||
#include <Arduino.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <ETH.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include "AsyncJson.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <ArduinoLog.h>
|
||||
#include "AsyncJson.h"
|
||||
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
@@ -42,16 +42,14 @@ bool current_low = false;
|
||||
|
||||
uint8_t failed_connection_attempts = 0;
|
||||
|
||||
struct SensorData
|
||||
{
|
||||
struct SensorData {
|
||||
int percentage;
|
||||
float voltage;
|
||||
float current;
|
||||
float water_height;
|
||||
};
|
||||
|
||||
struct NetworkData
|
||||
{
|
||||
struct NetworkData {
|
||||
String ip_address;
|
||||
bool link;
|
||||
float rssi;
|
||||
@@ -61,7 +59,7 @@ struct NetworkData
|
||||
NetworkData wifi_data;
|
||||
NetworkData ethernet_data;
|
||||
|
||||
SensorData current_data = SensorData{-1, -1, -1, -1};
|
||||
SensorData current_data = SensorData { -1, -1, -1, -1 };
|
||||
|
||||
int64_t mac_address = ESP.getEfuseMac();
|
||||
|
||||
@@ -76,46 +74,32 @@ void display_percentage(int percentage)
|
||||
{
|
||||
digitalWrite(LED_RED, 0);
|
||||
|
||||
if (percentage > 20)
|
||||
{
|
||||
if (percentage > 1) {
|
||||
digitalWrite(LED_1, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
digitalWrite(LED_1, 0);
|
||||
}
|
||||
if (percentage > 40)
|
||||
{
|
||||
if (percentage > 20) {
|
||||
digitalWrite(LED_2, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
digitalWrite(LED_2, 0);
|
||||
}
|
||||
if (percentage > 60)
|
||||
{
|
||||
if (percentage > 40) {
|
||||
digitalWrite(LED_3, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
digitalWrite(LED_3, 0);
|
||||
}
|
||||
if (percentage > 80)
|
||||
{
|
||||
if (percentage > 60) {
|
||||
digitalWrite(LED_4, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
digitalWrite(LED_4, 0);
|
||||
}
|
||||
if (percentage > 95)
|
||||
{
|
||||
if (percentage > 80) {
|
||||
digitalWrite(LED_5, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
digitalWrite(LED_5, 0);
|
||||
}
|
||||
delay(3000);
|
||||
}
|
||||
|
||||
void display_error_code(byte err_code)
|
||||
@@ -133,12 +117,12 @@ bool is_error()
|
||||
return voltage_high || voltage_low || current_high || current_low;
|
||||
}
|
||||
|
||||
void printSuffix(Print *_logOutput, int logLevel)
|
||||
void printSuffix(Print* _logOutput, int logLevel)
|
||||
{
|
||||
_logOutput->print(CR);
|
||||
}
|
||||
|
||||
void print_prefix(Print *_logOutput, int logLevel)
|
||||
void print_prefix(Print* _logOutput, int logLevel)
|
||||
{
|
||||
_logOutput->print("WATERMETER - C");
|
||||
_logOutput->print(xPortGetCoreID());
|
||||
@@ -147,57 +131,40 @@ void print_prefix(Print *_logOutput, int logLevel)
|
||||
_logOutput->print(" - ");
|
||||
}
|
||||
|
||||
void display_task(void *parameter)
|
||||
void display_task(void* parameter)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (!is_error())
|
||||
{
|
||||
while (true) {
|
||||
if (!is_error()) {
|
||||
// We have no error, refresh status display and wait half a second
|
||||
display_percentage(current_data.percentage);
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Log.verbose("Error detected");
|
||||
// We have an error, display error code for 3 seconds and then water level for 3 seconds
|
||||
if (voltage_low)
|
||||
{
|
||||
if (voltage_low) {
|
||||
display_error_code(1);
|
||||
}
|
||||
else if (voltage_high)
|
||||
{
|
||||
} else if (voltage_high) {
|
||||
display_error_code(2);
|
||||
}
|
||||
else if (current_low)
|
||||
{
|
||||
} else if (current_low) {
|
||||
display_error_code(3);
|
||||
}
|
||||
else if (current_high)
|
||||
{
|
||||
} else if (current_high) {
|
||||
display_error_code(4);
|
||||
}
|
||||
delay(3000);
|
||||
display_percentage(current_data.percentage);
|
||||
delay(3000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_task(void *parameter)
|
||||
void wifi_task(void* parameter)
|
||||
{
|
||||
Log.verbose("Starting WiFi Task");
|
||||
while (true)
|
||||
{
|
||||
if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5)
|
||||
{
|
||||
while (true) {
|
||||
if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5) {
|
||||
wifi_data.link = false;
|
||||
if (failed_connection_attempts > 5)
|
||||
{
|
||||
if (failed_connection_attempts > 5) {
|
||||
Log.verbose("Failed to connecto to currently saved SSID, starting SoftAP");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Log.verbose("No SSID saved, starting SoftAP");
|
||||
}
|
||||
|
||||
@@ -207,17 +174,13 @@ void wifi_task(void *parameter)
|
||||
Log.verbose("[WIFI_TASK] Waiting for SSID now...");
|
||||
|
||||
String old_ssid = prefs.getString(ssid_key, "xxx");
|
||||
while (prefs.getString(ssid_key, "") == "" || prefs.getString(ssid_key, "") == old_ssid)
|
||||
{
|
||||
while (prefs.getString(ssid_key, "") == "" || prefs.getString(ssid_key, "") == old_ssid) {
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
failed_connection_attempts = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (WiFi.isConnected() && WiFi.SSID() == prefs.getString(ssid_key, ""))
|
||||
{
|
||||
} else {
|
||||
if (WiFi.isConnected() && WiFi.SSID() == prefs.getString(ssid_key, "")) {
|
||||
failed_connection_attempts = 0;
|
||||
wifi_data.rssi = WiFi.RSSI();
|
||||
wifi_data.link = true;
|
||||
@@ -226,9 +189,7 @@ void wifi_task(void *parameter)
|
||||
|
||||
Log.verbose("RSSI: %F, IP Address, %p, SSID: %s", float(WiFi.RSSI()), WiFi.localIP(), prefs.getString(ssid_key, "NOSSID"));
|
||||
delay(5000);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Log.verbose("Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, ""));
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, ""));
|
||||
@@ -239,13 +200,12 @@ void wifi_task(void *parameter)
|
||||
}
|
||||
}
|
||||
|
||||
void ethernet_task(void *parameter)
|
||||
void ethernet_task(void* parameter)
|
||||
{
|
||||
Log.verbose("Connecting Ethernet");
|
||||
ETH.begin(0, 17, 23, 18);
|
||||
ETH.setHostname("watermeter");
|
||||
while (true)
|
||||
{
|
||||
while (true) {
|
||||
ethernet_data.link = ETH.linkUp();
|
||||
ethernet_data.rssi = ETH.linkSpeed();
|
||||
ethernet_data.ip_address = ETH.localIP().toString();
|
||||
@@ -253,11 +213,10 @@ void ethernet_task(void *parameter)
|
||||
}
|
||||
}
|
||||
|
||||
void collect_internal_telemetry_task(void *parameter)
|
||||
void collect_internal_telemetry_task(void* parameter)
|
||||
{
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (true) {
|
||||
float heap_usage = (float(ESP.getFreeHeap()) / float(ESP.getHeapSize())) * 100;
|
||||
uint64_t uptime_seconds = millis() / 1000;
|
||||
|
||||
@@ -268,10 +227,9 @@ void collect_internal_telemetry_task(void *parameter)
|
||||
}
|
||||
}
|
||||
|
||||
void read_sensor_task(void *parameter)
|
||||
void read_sensor_task(void* parameter)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
while (true) {
|
||||
float bus_voltage = ina_sensor.getBusVoltage();
|
||||
float shunt_voltage = ina_sensor.getShuntVoltage_mV() - zero_value;
|
||||
float shunt_current = shunt_voltage / 4;
|
||||
@@ -298,7 +256,7 @@ void read_sensor_task(void *parameter)
|
||||
Log.verbose("Shunt voltage: %F", shunt_voltage);
|
||||
Log.verbose("Value percentage: %F", percentage);
|
||||
|
||||
current_data = SensorData{percentage, bus_voltage, shunt_current, shunt_current_over_zero};
|
||||
current_data = SensorData { percentage, bus_voltage, shunt_current, shunt_current_over_zero };
|
||||
|
||||
delay(20000);
|
||||
}
|
||||
@@ -340,45 +298,31 @@ void setup()
|
||||
|
||||
/////////////////////////////// ROUTES ///////////////////////////////
|
||||
Log.verbose("Route Setup");
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(SPIFFS, "/status.html", "text/html", false); });
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/status.html", "text/html", false); });
|
||||
|
||||
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(SPIFFS, "/settings.html", "text/html", false); });
|
||||
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/settings.html", "text/html", false); });
|
||||
|
||||
server.on("/export", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(SPIFFS, "/data_export.html", "text/html", false); });
|
||||
server.on("/export", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/data_export.html", "text/html", false); });
|
||||
|
||||
server.on("/logic.js", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(SPIFFS, "/logic.js", "application/javascript", false); });
|
||||
server.on("/logic.js", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/logic.js", "application/javascript", false); });
|
||||
|
||||
server.on("/update_wifi_credentials", HTTP_POST, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
server.on("/update_wifi_credentials", HTTP_POST, [](AsyncWebServerRequest* request) {
|
||||
int params = request->params();
|
||||
|
||||
if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true))
|
||||
{
|
||||
if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true)) {
|
||||
Log.verbose("Updating SSID config");
|
||||
AsyncWebParameter *ssid_param = request->getParam(ssid_key, true);
|
||||
AsyncWebParameter *password_param = request->getParam(wifi_password_key, true);
|
||||
AsyncWebParameter* ssid_param = request->getParam(ssid_key, true);
|
||||
AsyncWebParameter* password_param = request->getParam(wifi_password_key, true);
|
||||
prefs.putString(ssid_key, ssid_param->value().c_str());
|
||||
prefs.putString(wifi_password_key, password_param->value().c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < params; i++)
|
||||
{
|
||||
AsyncWebParameter *p = request->getParam(i);
|
||||
if (p->isFile())
|
||||
{ // p->isPost() is also true
|
||||
} else {
|
||||
for (int i = 0; i < params; i++) {
|
||||
AsyncWebParameter* p = request->getParam(i);
|
||||
if (p->isFile()) { // p->isPost() is also true
|
||||
Log.verbose("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
||||
}
|
||||
else if (p->isPost())
|
||||
{
|
||||
} else if (p->isPost()) {
|
||||
Log.verbose("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Log.verbose("GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
||||
}
|
||||
}
|
||||
@@ -387,8 +331,7 @@ void setup()
|
||||
request->send(SPIFFS, "/settings.html", "text/html", false); // TODO add proper return templating
|
||||
});
|
||||
|
||||
server.on("/sensor_data", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
server.on("/sensor_data", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
StaticJsonDocument<128> doc;
|
||||
doc["percentage"] = current_data.percentage;
|
||||
doc["voltage"] = current_data.voltage;
|
||||
@@ -399,8 +342,7 @@ void setup()
|
||||
serializeJson(doc, output);
|
||||
request->send(200, "application/json", output); });
|
||||
|
||||
server.on("/network_info", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
server.on("/network_info", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
StaticJsonDocument<256> doc;
|
||||
doc["wifi"]["ip"] = wifi_data.ip_address;
|
||||
doc["wifi"]["rssi"] = wifi_data.rssi;
|
||||
@@ -416,8 +358,7 @@ void setup()
|
||||
serializeJson(doc, output);
|
||||
request->send(200, "application/json", output); });
|
||||
|
||||
server.on("/chota.css", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(SPIFFS, "/chota.css", "text/css", false); });
|
||||
server.on("/chota.css", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(SPIFFS, "/chota.css", "text/css", false); });
|
||||
|
||||
display_error_code(23);
|
||||
|
||||
@@ -425,8 +366,7 @@ void setup()
|
||||
|
||||
Log.verbose("OTA Setup");
|
||||
ArduinoOTA
|
||||
.onStart([]()
|
||||
{
|
||||
.onStart([]() {
|
||||
String type;
|
||||
if (ArduinoOTA.getCommand() == U_FLASH)
|
||||
type = "sketch";
|
||||
@@ -435,12 +375,9 @@ void setup()
|
||||
|
||||
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
||||
Log.verbose("Start updating %s", type); })
|
||||
.onEnd([]()
|
||||
{ Log.verbose("\nEnd"); })
|
||||
.onProgress([](unsigned int progress, unsigned int total)
|
||||
{ Serial.printf("Progress: %u%%\r", (progress / (total / 100))); })
|
||||
.onError([](ota_error_t error)
|
||||
{
|
||||
.onEnd([]() { Log.verbose("\nEnd"); })
|
||||
.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); })
|
||||
.onError([](ota_error_t error) {
|
||||
Serial.printf("Error[%u]: ", error);
|
||||
if (error == OTA_AUTH_ERROR) Log.verbose("Auth Failed");
|
||||
else if (error == OTA_BEGIN_ERROR) Log.verbose("Begin Failed");
|
||||
|
||||
Reference in New Issue
Block a user