Compare commits
16 Commits
66a21928a4
...
rev2
| Author | SHA1 | Date | |
|---|---|---|---|
| 96341cfd42 | |||
| c432f47dff | |||
| 123f75dd89 | |||
| 462c5f3db7 | |||
| 4f02a8f3d0 | |||
| 8196ae4601 | |||
| 7697cb4ae9 | |||
| dccdca3ad8 | |||
| d2c3fc0b46 | |||
| 1d9f0f119a | |||
| 037fc3391c | |||
| aeac15aa2c | |||
| 6ee4086716 | |||
| 013356c41e | |||
| c5b882d693 | |||
| d951d1d2be |
@@ -1,24 +1,26 @@
|
||||
name: Test compiling project
|
||||
name: Compilie project and upload binaries
|
||||
|
||||
on: [push]
|
||||
on:
|
||||
push:
|
||||
branches: ["main"]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:latest
|
||||
image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:2.1
|
||||
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v2
|
||||
- name: Run PlatformIO build for SPIFFS (same in both platforms, so only one build is necessary)
|
||||
run: which pio && id && pio run --target buildfs -e ESP32_INA233
|
||||
- name: Run PlatformIO Tests
|
||||
run: pio test -e native
|
||||
- name: Run PlatformIO build for INA233
|
||||
run: pio run -e ESP32_INA233
|
||||
- name: Run PlatformIO build for INA226
|
||||
run: pio run -e ESP32_INA226
|
||||
- name: Run PlatformIO build for SPIFFS (same in both platforms, so only one build is necessary)
|
||||
run: pio run --target buildfs -e ESP32_INA233
|
||||
- name: Upload firmware binary for INA233
|
||||
run: |
|
||||
VERSION=$(sed -n 's/#define current_software_version Version{[[:space:]]*\([0-9]\+\),[[:space:]]*\([0-9]\+\),[[:space:]]*\([0-9]\+\)}/\1.\2.\3/p' src/global_data/defines.h)
|
||||
@@ -27,7 +29,7 @@ jobs:
|
||||
curl -X PUT \
|
||||
-H "Content-Type: application/octet-stream" \
|
||||
--data-binary @.pio/build/ESP32_INA233/firmware.bin \
|
||||
https://iot.tobiasmaier.me/firmware/waterlevel/INA233/${VERSION}
|
||||
https://iot.tobiasmaier.me/firmware/waterlevel/INA233REV2/${VERSION}
|
||||
|
||||
- name: Upload firmware binary for INA226
|
||||
run: |
|
||||
@@ -37,7 +39,7 @@ jobs:
|
||||
curl -X PUT \
|
||||
-H "Content-Type: application/octet-stream" \
|
||||
--data-binary @.pio/build/ESP32_INA226/firmware.bin \
|
||||
https://iot.tobiasmaier.me/firmware/waterlevel/INA226/${VERSION}
|
||||
https://iot.tobiasmaier.me/firmware/waterlevel/INA226REV2/${VERSION}
|
||||
|
||||
- name: Upload SPIFFS binary
|
||||
run: |
|
||||
@@ -46,5 +48,5 @@ jobs:
|
||||
|
||||
curl -X PUT \
|
||||
-H "Content-Type: application/octet-stream" \
|
||||
--data-binary @.pio/build/ESP32_INA233/spiffs.bin \
|
||||
--data-binary @.pio/build/ESP32_INA233/littlefs.bin \
|
||||
https://iot.tobiasmaier.me/filesystem/waterlevel/generic/${VERSION}
|
||||
|
||||
24
.gitea/workflows/test_build.yaml
Normal file
24
.gitea/workflows/test_build.yaml
Normal file
@@ -0,0 +1,24 @@
|
||||
name: Test project compilation
|
||||
|
||||
on:
|
||||
push:
|
||||
branches-ignore: ["main"]
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: gitea.tobiasmaier.me/tobimai/devcontainer-pio:2.1
|
||||
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v2
|
||||
- name: Run PlatformIO build for SPIFFS (same in both platforms, so only one build is necessary)
|
||||
run: which pio && id && pio run --target buildfs -e ESP32_INA233
|
||||
- name: Run PlatformIO Tests
|
||||
run: pio test -e native
|
||||
- name: Run PlatformIO build for INA233
|
||||
run: pio run -e ESP32_INA233
|
||||
- name: Run PlatformIO build for INA226
|
||||
run: pio run -e ESP32_INA226
|
||||
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -21,6 +21,7 @@
|
||||
"span": "cpp",
|
||||
"text_encoding": "cpp",
|
||||
"thread": "cpp",
|
||||
"*.inc": "cpp"
|
||||
"*.inc": "cpp",
|
||||
"cstddef": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ function fetchTelemetryData() {
|
||||
.then(data => {
|
||||
document.getElementById('uptime').textContent = data.uptime_seconds ? (roundToTwo(data.uptime_seconds) + ' s') : 'XXX';
|
||||
document.getElementById('heap').textContent = data.heap_percent ? (roundToTwo(data.heap_percent) + ' %%') : 'XXX';
|
||||
document.getElementById('temperature').textContent = data.heap_percent ? (roundToTwo(data.temperature) + ' °C') : 'XXX';
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("There was an error fetching data from the API", error);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="UTF-8">
|
||||
|
||||
<head>
|
||||
<link rel="stylesheet" href="/chota.css">
|
||||
@@ -19,9 +20,6 @@
|
||||
<a href="/export">Data export</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="nav-right">
|
||||
<a class="button outline">Button</a>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="row">
|
||||
@@ -121,6 +119,10 @@
|
||||
<td>Heap usage: </td>
|
||||
<td id="heap">XXX</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>CPU Temperature: </td>
|
||||
<td id="temperature">XXX</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
@@ -1 +1 @@
|
||||
8
|
||||
9
|
||||
@@ -10,25 +10,25 @@
|
||||
#define REGISTER_READ_IIN 0x89
|
||||
|
||||
enum AveragingMode {
|
||||
averages_1 = B000,
|
||||
averages_4 = B001,
|
||||
averages_16 = B010,
|
||||
averages_64 = B011,
|
||||
averages_128 = B100,
|
||||
averages_256 = B101,
|
||||
averages_512 = B110,
|
||||
averages_1024 = B111
|
||||
averages_1 = 0b000,
|
||||
averages_4 = 0b001,
|
||||
averages_16 = 0b010,
|
||||
averages_64 = 0b011,
|
||||
averages_128 = 0b100,
|
||||
averages_256 = 0b101,
|
||||
averages_512 = 0b110,
|
||||
averages_1024 = 0b111
|
||||
};
|
||||
|
||||
enum ConversionTime {
|
||||
conversion_time_140uS = B000,
|
||||
conversion_time_204uS = B001,
|
||||
conversion_time_332us = B010,
|
||||
conversion_time_588uS = B011,
|
||||
conversion_time_1100uS = B100,
|
||||
conversion_time_2116uS = B101,
|
||||
conversion_time_4156uS = B110,
|
||||
conversion_time_8244uS = B111
|
||||
conversion_time_140uS = 0b000,
|
||||
conversion_time_204uS = 0b001,
|
||||
conversion_time_332us = 0b010,
|
||||
conversion_time_588uS = 0b011,
|
||||
conversion_time_1100uS = 0b100,
|
||||
conversion_time_2116uS = 0b101,
|
||||
conversion_time_4156uS = 0b110,
|
||||
conversion_time_8244uS = 0b111
|
||||
};
|
||||
|
||||
class INA233{
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "Arduino.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <vector>
|
||||
#include <Elog.h>
|
||||
#include <HTTPUpdate.h>
|
||||
|
||||
@@ -11,42 +11,40 @@
|
||||
[platformio]
|
||||
default_envs = ESP32_INA233, ESP32_INA226
|
||||
|
||||
[env:ESP32_INA233]
|
||||
[env:esp32_base]
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.32/platform-espressif32.zip
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
; monitor_filters = esp32_exception_decoder
|
||||
lib_deps =
|
||||
esp32async/AsyncTCP @ ~3.4.9
|
||||
esp32async/ESPAsyncWebServer @ ~3.8.1
|
||||
robtillaart/INA226@ ~0.6.4
|
||||
bblanchon/ArduinoJson@^6.21.3
|
||||
x385832/Elog@~2.0.10
|
||||
INA233
|
||||
fetchOTA
|
||||
monitor_filters = esp32_exception_decoder
|
||||
board_build.partitions = min_spiffs.csv
|
||||
lib_deps =
|
||||
esp32async/AsyncTCP @ ~3.4.9
|
||||
esp32async/ESPAsyncWebServer @ ~3.8.1
|
||||
robtillaart/INA226@ ~0.6.4
|
||||
bblanchon/ArduinoJson@^6.21.3
|
||||
x385832/Elog@~2.0.10
|
||||
fetchOTA
|
||||
build_flags = -fexceptions
|
||||
build_src_flags = -Wall -Wextra
|
||||
|
||||
|
||||
[env:ESP32_INA233]
|
||||
extends = env:esp32_base
|
||||
lib_deps =
|
||||
${env:esp32_base.lib_deps}
|
||||
INA233
|
||||
upload_protocol = espota
|
||||
upload_port = 192.168.5.205
|
||||
build_flags = -Wall -Wextra -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE -DELOG_SYSLOG_ENABLE -fexceptions
|
||||
|
||||
[env:ESP32_INA226]
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.32/platform-espressif32.zip
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
; monitor_filters = esp32_exception_decoder
|
||||
extends = env:esp32_base
|
||||
lib_deps =
|
||||
esp32async/AsyncTCP @ ~3.4.9
|
||||
esp32async/ESPAsyncWebServer @ ~3.8.1
|
||||
${env:esp32_base.lib_deps}
|
||||
robtillaart/INA226@ ~0.6.4
|
||||
bblanchon/ArduinoJson@^6.21.3
|
||||
x385832/Elog@~2.0.10
|
||||
fetchOTA
|
||||
board_build.partitions = min_spiffs.csv
|
||||
upload_protocol = espota
|
||||
upload_port = 192.168.6.45
|
||||
build_flags = -Wall -Wextra -DUSE_INA226 -DLOGGING_SPIFFS_DISABLE -DLOGGING_SD_DISABLE -DELOG_SYSLOG_ENABLE -fexceptions
|
||||
build_flags = ${env:esp32_base.build_flags} -DUSE_INA226
|
||||
|
||||
[env:native]
|
||||
platform = native
|
||||
@@ -54,3 +52,4 @@ build_flags = -DUNIT_TEST -Ilib/fetchOTA/
|
||||
lib_deps =
|
||||
fetchOTA
|
||||
arduino-libraries/ArduinoHttpClient@^0.6.1
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ extern WaterData water_data;
|
||||
|
||||
void display_task(void* parameter)
|
||||
{
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting display tasks");
|
||||
while (true) {
|
||||
if (!is_error(active_errors)) {
|
||||
// We have no error, refresh status display and wait half a second
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#define water_level_min_key "water_level_min"
|
||||
#define water_level_max_key "water_level_max"
|
||||
#define water_volume_key "water_volume"
|
||||
#define current_software_version Version{1, 3, 0}
|
||||
#define REQUIRED_SPIFFS_VERSION Version{8, 0, 0}
|
||||
#define current_software_version Version{2, 2, 0}
|
||||
#define REQUIRED_SPIFFS_VERSION Version{9, 0, 0}
|
||||
|
||||
#define RESISTOR_VALUE 4
|
||||
@@ -29,6 +29,7 @@ struct NetworkData {
|
||||
struct DeviceTelemetry {
|
||||
float heap_used_percent;
|
||||
int uptime_seconds;
|
||||
float temperature;
|
||||
};
|
||||
|
||||
struct ActiveErrors {
|
||||
|
||||
118
src/main.cpp
118
src/main.cpp
@@ -39,48 +39,41 @@ extern NetworkData ethernet_data;
|
||||
extern SensorData shunt_data;
|
||||
extern ActiveErrors active_errors;
|
||||
|
||||
extern "C" int rom_phy_get_vdd33();
|
||||
|
||||
Version current_spiffs_version;
|
||||
|
||||
AsyncWebServer server(80);
|
||||
// AsyncWebSocket webSocket("/webSocket");
|
||||
AsyncWebSocket webSocket("/webSocket");
|
||||
#define FORMAT_LITTLEFS_IF_FAILED true
|
||||
|
||||
void setup()
|
||||
{
|
||||
Logger.registerSerial(MYLOG, ELOG_LEVEL_DEBUG, "Serial");
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Init LEDs");
|
||||
led_setup();
|
||||
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Init Starting prefs and Serial output");
|
||||
prefs.begin("waterlevel", false);
|
||||
Serial.begin(115200);
|
||||
|
||||
|
||||
delay(500);
|
||||
LOG(ELOG_LEVEL_DEBUG, "Init Sensor");
|
||||
init_sensor();
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Beginning SPIFFS");
|
||||
//SPIFFS.begin(FORMAT_LITTLEFS_IF_FAILED);
|
||||
LOG(ELOG_LEVEL_DEBUG, "Beginning LittleFS");
|
||||
LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED);
|
||||
|
||||
// Read the current SPIFFS version from the versions file on the spiffs
|
||||
LOG(ELOG_LEVEL_DEBUG, "Reading LittleFS version");
|
||||
File file = LittleFS.open("/version", FILE_READ);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open version file for reading");
|
||||
LOG( ELOG_LEVEL_ERROR, "Failed opening LittleFS version file");
|
||||
} else {
|
||||
String version = file.readStringUntil('\n');
|
||||
LOG(ELOG_LEVEL_DEBUG, "Version: %s", version);
|
||||
current_spiffs_version = parseVersion(version.c_str());
|
||||
LOG(ELOG_LEVEL_DEBUG, "Current SPIFFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch);
|
||||
LOG(ELOG_LEVEL_DEBUG, "Current LittleFS Version: %d.%d.%d", current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch);
|
||||
}
|
||||
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "SPIFFS initialized");
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Begin INA");
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "LittleFS initialized");
|
||||
|
||||
/////////////////////////////// ROUTES ///////////////////////////////
|
||||
LOG(ELOG_LEVEL_DEBUG, "Route Setup");
|
||||
@@ -98,8 +91,6 @@ void setup()
|
||||
|
||||
// API stuff - internal
|
||||
server.on("/update_wifi_credentials", HTTP_POST, [](AsyncWebServerRequest* request) {
|
||||
int params = request->params();
|
||||
|
||||
// For settings SSID
|
||||
if (request->hasParam(ssid_key, true) && request->hasParam(wifi_password_key, true)) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Updating SSID config");
|
||||
@@ -134,12 +125,6 @@ void setup()
|
||||
float level_max_float = level_max_str.toFloat();
|
||||
float liters_float = liters_str.toFloat();
|
||||
|
||||
const char* paramCStr = range_str.c_str();
|
||||
char* endPtr;
|
||||
|
||||
// Convert the C string to a float using strtod
|
||||
float value = strtod(paramCStr, &endPtr);
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "range_float:%D:", range_float);
|
||||
|
||||
prefs.putFloat(level_sensor_range_key, range_float);
|
||||
@@ -166,10 +151,8 @@ void setup()
|
||||
});
|
||||
|
||||
setup_api_endpoints();
|
||||
// webSocket.onEvent(onWsEvent);
|
||||
// server.addHandler(&webSocket);
|
||||
|
||||
|
||||
webSocket.onEvent(onWsEvent);
|
||||
server.addHandler(&webSocket);
|
||||
|
||||
server.on("/chota.css", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/chota.css", "text/css", false); });
|
||||
server.on("/gauge.js", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(LittleFS, "/gauge.js", "application/javascript", false); });
|
||||
@@ -178,14 +161,14 @@ void setup()
|
||||
LOG(ELOG_LEVEL_DEBUG, "OTA Setup");
|
||||
ArduinoOTA
|
||||
.onStart([]() {
|
||||
String type;
|
||||
if (ArduinoOTA.getCommand() == U_FLASH)
|
||||
type = "sketch";
|
||||
else // U_SPIFFS
|
||||
type = "filesystem";
|
||||
String type;
|
||||
if (ArduinoOTA.getCommand() == U_FLASH)
|
||||
type = "sketch";
|
||||
else // U_SPIFFS
|
||||
type = "filesystem";
|
||||
|
||||
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
||||
LOG(ELOG_LEVEL_DEBUG, "Start updating %s", type); })
|
||||
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
||||
LOG(ELOG_LEVEL_DEBUG, "Start updating %s", type); })
|
||||
.onEnd([]() { LOG(ELOG_LEVEL_DEBUG, "\nEnd"); })
|
||||
.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); })
|
||||
.onError([](ota_error_t error) {
|
||||
@@ -196,67 +179,22 @@ void setup()
|
||||
else if (error == OTA_RECEIVE_ERROR) LOG(ELOG_LEVEL_DEBUG, "Receive Failed");
|
||||
else if (error == OTA_END_ERROR) LOG(ELOG_LEVEL_DEBUG, "End Failed"); });
|
||||
|
||||
digitalWrite(LED_RED, 0);
|
||||
|
||||
// Starting bootup sequence
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting main tasks");
|
||||
xTaskCreate(ethernet_task, "EthernetTask", 4096, NULL, 1, NULL);
|
||||
xTaskCreate(wifi_task, "WiFiTask", 10000, NULL, 1, NULL);
|
||||
|
||||
esp_netif_t* eth = esp_netif_get_handle_from_ifkey("ETH_DEF");
|
||||
esp_netif_t* wifi = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
|
||||
|
||||
if (ETH.linkUp()){
|
||||
LOG(ELOG_LEVEL_DEBUG, "Ethernet connected, starting update checker");
|
||||
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Ethernet not connected, starting update checker and WiFi Task");
|
||||
// esp_netif_set_default_netif(esp_netif_get_handle_from_ifkey("ETH_DEF"));
|
||||
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Getting time now");
|
||||
// Configure time to UTC
|
||||
configTime(0, 0, "pool.ntp.org");
|
||||
|
||||
// Wait until a valid time is obtained (time > 8 hours in seconds)
|
||||
time_t now = time(NULL);
|
||||
struct tm timeinfo;
|
||||
int retry = 0;
|
||||
while (now < 8 * 3600 && retry < 10) {
|
||||
if(!getLocalTime(&timeinfo)){
|
||||
Serial.println("Failed to obtain time");
|
||||
}
|
||||
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
|
||||
retry++;
|
||||
}
|
||||
|
||||
|
||||
getLocalTime(&timeinfo);
|
||||
|
||||
// Sanity check: Ensure the year is at least 2020.
|
||||
int currentYear = timeinfo.tm_year + 1900; // tm_year is years since 1900
|
||||
if (currentYear < 2020) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Time not set properly: ");
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Time is valid: %s", asctime(&timeinfo));
|
||||
}
|
||||
|
||||
// Setup syslog
|
||||
LOG(ELOG_LEVEL_ERROR, "Here is an error message, error code: %d", 17);
|
||||
|
||||
|
||||
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting webserver");
|
||||
server.begin();
|
||||
ArduinoOTA.begin();
|
||||
|
||||
xTaskCreate(read_sensor_task, "ReadSensorTask", 1024 * 4, NULL, 1, NULL);
|
||||
xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 2048, NULL, 1, NULL);
|
||||
xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL);
|
||||
xTaskCreate(get_time_task, "GetTimeTask", 1024 * 4, NULL, 1, NULL);
|
||||
|
||||
delay(5000);
|
||||
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting webserver");
|
||||
server.begin();
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting OTA handler");
|
||||
ArduinoOTA.begin();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ StaticJsonDocument<128> build_telemetry_json(DeviceTelemetry data) {
|
||||
StaticJsonDocument<128> doc;
|
||||
doc["uptime_seconds"] = data.uptime_seconds;
|
||||
doc["heap_percent"] = data.heap_used_percent;
|
||||
doc["temperature"] = data.temperature;
|
||||
return doc;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,15 +51,15 @@ void wifi_task(void* parameter)
|
||||
if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5) {
|
||||
wifi_data.link = false;
|
||||
if (failed_connection_attempts > 5) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Failed to connect to currently saved SSID, starting SoftAP");
|
||||
LOG(ELOG_LEVEL_WARNING, "Failed to connect to currently saved SSID, starting SoftAP");
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "No SSID saved, starting SoftAP");
|
||||
LOG(ELOG_LEVEL_WARNING, "No SSID saved, starting SoftAP");
|
||||
}
|
||||
|
||||
String ap_ssid = get_hostname(Wireless);
|
||||
WiFi.softAP(ap_ssid.c_str(), "");
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "[WIFI_TASK] Waiting for SSID now...");
|
||||
LOG(ELOG_LEVEL_DEBUG, "Waiting for SSID now...");
|
||||
|
||||
String old_ssid = prefs.getString(ssid_key, "xxx");
|
||||
while (prefs.getString(ssid_key, "") == "" || prefs.getString(ssid_key, "") == old_ssid) {
|
||||
@@ -74,16 +74,16 @@ void wifi_task(void* parameter)
|
||||
wifi_data.link = true;
|
||||
wifi_data.network_name = WiFi.SSID();
|
||||
wifi_data.ip_address = WiFi.localIP().toString();
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "RSSI: %F, IP Address, %s, SSID: %s", float(WiFi.RSSI()), WiFi.localIP().toString(), prefs.getString(ssid_key, "NOSSID"));
|
||||
Serial.println(WiFi.localIP());
|
||||
delay(5000);
|
||||
LOG(ELOG_LEVEL_DEBUG, "WIFI connected; RSSI: %F, IP Address, %s, SSID: %s", float(WiFi.RSSI()), WiFi.localIP().toString(), prefs.getString(ssid_key, "NOSSID"));
|
||||
delay(1000 * 60);
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Connecting to %s using password %s", prefs.getString(ssid_key, ""), prefs.getString(wifi_password_key, ""));
|
||||
String ssid = prefs.getString(ssid_key, "");
|
||||
String password = prefs.getString(wifi_password_key, "");
|
||||
LOG(ELOG_LEVEL_DEBUG, "Connecting to %s...", ssid);
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
WiFi.begin(prefs.getString(ssid_key, "").c_str(), prefs.getString(wifi_password_key, "").c_str());
|
||||
WiFi.begin(ssid, password);
|
||||
failed_connection_attempts++;
|
||||
LOG(ELOG_LEVEL_WARNING, "Failed to connect, retrying...");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
@@ -92,7 +92,7 @@ void wifi_task(void* parameter)
|
||||
|
||||
void ethernet_task(void* parameter)
|
||||
{
|
||||
LOG(ELOG_LEVEL_DEBUG, "Connecting Ethernet");
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting Ethernet Task");
|
||||
ETH.begin();
|
||||
ETH.setHostname(get_hostname(Ethernet));
|
||||
while (true) {
|
||||
@@ -100,6 +100,12 @@ void ethernet_task(void* parameter)
|
||||
ethernet_data.rssi = ETH.linkSpeed();
|
||||
ethernet_data.ip_address = ETH.localIP().toString();
|
||||
LOG(ELOG_LEVEL_DEBUG, "Ethernet RSSI: %F, IP Address, %s, LINK: %s", float(ethernet_data.rssi), ETH.localIP().toString(), String(ethernet_data.link));
|
||||
|
||||
if (ETH.linkUp() && !ETH.isDefault() && ETH.localIP().toString() != "0.0.0.0") {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Ethernet is up, setting to default");
|
||||
ETH.setDefault();
|
||||
}
|
||||
|
||||
delay(60 * 1000);
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,6 @@ void setup_api_endpoints(){
|
||||
};
|
||||
|
||||
xTaskCreate(run_ota_update_task, "RunOTAUpdate", 1024 * 8, (void *)&args, 1, NULL);
|
||||
request->send(SPIFFS, "/update_progress.html", "text/html", false, processor);
|
||||
request->send(LittleFS, "/update_progress.html", "text/html", false, processor);
|
||||
});
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <LittleFS.h>
|
||||
|
||||
void setup_api_endpoints();
|
||||
@@ -45,6 +45,7 @@ void init_sensor(){
|
||||
|
||||
void read_sensor_task(void* parameter)
|
||||
{
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting read sensor tasks");
|
||||
while (true) {
|
||||
// Get Values from sensor
|
||||
#ifndef USE_INA226
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
#include "telemetry.h"
|
||||
#include <Arduino.h>
|
||||
#include "../global_data/global_data.h"
|
||||
#include "tools/log.h"
|
||||
|
||||
extern "C" uint8_t temprature_sens_read();
|
||||
|
||||
extern DeviceTelemetry telemetry;
|
||||
|
||||
void collect_internal_telemetry_task(void* parameter)
|
||||
{
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting internal telemetry tasks");
|
||||
while (true) {
|
||||
float heap_usage = (float(ESP.getFreeHeap()) / float(ESP.getHeapSize())) * 100;
|
||||
uint64_t uptime_seconds = millis() / 1000;
|
||||
|
||||
telemetry.heap_used_percent = heap_usage;
|
||||
telemetry.uptime_seconds = uptime_seconds;
|
||||
telemetry.temperature = (temprature_sens_read()-32) / 1.8;
|
||||
delay(60000);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#define LOG(LEVEL, FMT, ...) \
|
||||
do { \
|
||||
Logger.log(0, LEVEL, "[Core: %i][Task: %s][Fn: %s]" FMT, \
|
||||
Logger.log(0, LEVEL, "[Core: %i][Task: %s][Fn: %s] " FMT, \
|
||||
xPortGetCoreID(), \
|
||||
pcTaskGetName(xTaskGetCurrentTaskHandle()), \
|
||||
__FUNCTION__, \
|
||||
|
||||
@@ -6,10 +6,18 @@
|
||||
#include <fetchOTA.h>
|
||||
#include <AsyncWebSocket.h>
|
||||
#include "log.h"
|
||||
#include "esp_sntp.h"
|
||||
#include <HTTPClient.h>
|
||||
|
||||
#ifdef USE_INA226
|
||||
#define BOARD_VARIANT "INA226REV2"
|
||||
#else
|
||||
#define BOARD_VARIANT "INA233REV2"
|
||||
#endif
|
||||
|
||||
extern Preferences prefs;
|
||||
extern OTAStatus ota_status;
|
||||
// extern AsyncWebSocket webSocket;
|
||||
extern AsyncWebSocket webSocket;
|
||||
|
||||
extern Version current_spiffs_version;
|
||||
|
||||
@@ -43,7 +51,7 @@ void update_started() {
|
||||
void update_finished() {
|
||||
LOG(ELOG_LEVEL_DEBUG, "OTA Update finished");
|
||||
ota_status.update_progress = -1;
|
||||
// webSocket.textAll(String(-1).c_str());
|
||||
webSocket.textAll(String(-1).c_str());
|
||||
}
|
||||
|
||||
void update_progress(int cur, int total) {
|
||||
@@ -52,7 +60,7 @@ void update_progress(int cur, int total) {
|
||||
ota_status.update_progress = int(float(cur)/float(total)*100);
|
||||
LOG(ELOG_LEVEL_DEBUG, "OTA Update progress: %d/%d, %i", cur, total, ota_status.update_progress);
|
||||
}
|
||||
// webSocket.textAll(String(ota_status.update_progress).c_str());
|
||||
webSocket.textAll(String(ota_status.update_progress).c_str());
|
||||
LOG(ELOG_LEVEL_DEBUG, "OTA Update progress: %d/%d", cur, total);
|
||||
}
|
||||
|
||||
@@ -62,63 +70,87 @@ void update_error(int err) {
|
||||
}
|
||||
|
||||
void check_update_task(void* parameter) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting check Update Task");
|
||||
ota_status.current_version = current_software_version;
|
||||
ota_status.update_progress = -1;
|
||||
#ifdef USE_INA226
|
||||
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA226");
|
||||
#else
|
||||
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA233");
|
||||
#endif
|
||||
OTA spiffs_fs("https://iot.tobiasmaier.me/filesystem/waterlevel", REQUIRED_SPIFFS_VERSION, "generic");
|
||||
|
||||
|
||||
// Init SPIFFS update and check for update
|
||||
// If there is a SPIFSS update it will be ran automatically, as SPIFFS is necessary for the firmware to run
|
||||
Firmware latest_spiff_version = spiffs_fs.getLatestVersionOnServer();
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch);
|
||||
|
||||
if (latest_spiff_version.valid) {
|
||||
if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION)) {
|
||||
// If Required SPIFFS version is newer than current version, update
|
||||
LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version, running update now");
|
||||
run_ota_spiffs_update(latest_spiff_version.url, update_started, update_finished, update_progress, update_error);
|
||||
// Reboot just to be safe
|
||||
ESP.restart();
|
||||
} else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_spiff_version.version)) {
|
||||
// If Server has new SPIFFS version but it's not required
|
||||
LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_spiff_version.version.major, latest_spiff_version.version.minor, latest_spiff_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch);
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "No new SPIFFS version available");
|
||||
}
|
||||
}
|
||||
|
||||
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, BOARD_VARIANT);
|
||||
OTA littlefs_ota("https://iot.tobiasmaier.me/filesystem/waterlevel", REQUIRED_SPIFFS_VERSION, "generic");
|
||||
|
||||
while (true) {
|
||||
Firmware fw = ota.getLatestVersionOnServer();
|
||||
if (fw.valid) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "New firmware available: %d.%d.%d, current version: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch);
|
||||
ota_status.latest_version = fw.version;
|
||||
ota_status.update_url = fw.url;
|
||||
if (isVersionNewer(current_software_version, fw.version)) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Remote version is newer than current version");
|
||||
ota_status.update_available = true;
|
||||
} else {
|
||||
ota_status.update_available = false;
|
||||
}
|
||||
// Check if internet connection exists before running update, the web client seems to fail otherwise
|
||||
|
||||
if (check_for_internet_connection()) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Ping sucessful, starting update checks");
|
||||
check_and_update_littleFS(littlefs_ota);
|
||||
check_and_update_firmware(ota);
|
||||
} else {
|
||||
if (fw.version.major != 0 && fw.version.minor != 0 && fw.version.patch != 0) {
|
||||
ota_status.latest_version = fw.version;
|
||||
ota_status.update_available = false;
|
||||
}
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "No new firmware available");
|
||||
LOG(ELOG_LEVEL_WARNING, "Server did not respond to ping, waiting...");
|
||||
}
|
||||
delay(1000 * 60 * 1);
|
||||
}
|
||||
}
|
||||
|
||||
void check_and_update_littleFS(OTA littlefs) {
|
||||
Firmware latest_fs_version = littlefs.getLatestVersionOnServer();
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Required SPIFFS Version: %d.%d.%d; Current SPIFFS version: %d.%d.%d; Server SPIFFS Version: %d.%d.%d", REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch, current_spiffs_version.major, current_spiffs_version.minor, current_spiffs_version.patch, latest_fs_version.version.major, latest_fs_version.version.minor, latest_fs_version.version.patch);
|
||||
|
||||
if (latest_fs_version.valid) {
|
||||
|
||||
// If we need new version and the server has a new version
|
||||
if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION) && isVersionNewer(current_spiffs_version, latest_fs_version.version)) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version, running update now");
|
||||
run_ota_spiffs_update(latest_fs_version.url, update_started, update_finished, update_progress, update_error);
|
||||
ESP.restart();
|
||||
|
||||
// If we do not need a new version but one is available on the server
|
||||
} else if (isVersionNewer(REQUIRED_SPIFFS_VERSION, latest_fs_version.version)) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "New SPIFFS version available: %d.%d.%d, current version: %d.%d.%d but not necessary to update", latest_fs_version.version.major, latest_fs_version.version.minor, latest_fs_version.version.patch, REQUIRED_SPIFFS_VERSION.major, REQUIRED_SPIFFS_VERSION.minor, REQUIRED_SPIFFS_VERSION.patch);
|
||||
|
||||
// If we need a new version but server has no new version
|
||||
} else if (isVersionNewer(current_spiffs_version, REQUIRED_SPIFFS_VERSION)) {
|
||||
LOG(ELOG_LEVEL_ERROR, "New LittleFS Version is needed, but not found on server");
|
||||
|
||||
// Catch case for the rest
|
||||
} else {
|
||||
LOG(ELOG_LEVEL_DEBUG, "No new SPIFFS version available");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void check_and_update_firmware(OTA ota){
|
||||
Firmware fw = ota.getLatestVersionOnServer();
|
||||
if (fw.valid) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Firmware available on server: %d.%d.%d, current version: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch);
|
||||
ota_status.latest_version = fw.version;
|
||||
ota_status.update_url = fw.url;
|
||||
if (isVersionNewer(current_software_version, fw.version)) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Remote version is newer than current version");
|
||||
ota_status.update_available = true;
|
||||
} else {
|
||||
ota_status.update_available = false;
|
||||
}
|
||||
} else {
|
||||
if (fw.version.major != 0 && fw.version.minor != 0 && fw.version.patch != 0) {
|
||||
ota_status.latest_version = fw.version;
|
||||
ota_status.update_available = false;
|
||||
}
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "No new firmware available");
|
||||
}
|
||||
}
|
||||
|
||||
bool check_for_internet_connection() {
|
||||
HTTPClient http;
|
||||
http.begin("https://iot.tobiasmaier.me");
|
||||
int code = http.sendRequest("HEAD");
|
||||
if (code > 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void run_ota_update_task(void* parameter) {
|
||||
TaskArgs_t *args = (TaskArgs_t *) parameter;
|
||||
@@ -127,14 +159,47 @@ void run_ota_update_task(void* parameter) {
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
// void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
|
||||
// void *arg, uint8_t *data, size_t len) {
|
||||
// if (type == WS_EVT_CONNECT) {
|
||||
// Serial.println("WebSocket client connected");
|
||||
// } else if (type == WS_EVT_DISCONNECT) {
|
||||
// Serial.println("WebSocket client disconnected");
|
||||
// } else if (type == WS_EVT_DATA) {
|
||||
// // Optionally process data received from the client
|
||||
// }
|
||||
// }
|
||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
|
||||
void *arg, uint8_t *data, size_t len) {
|
||||
if (type == WS_EVT_CONNECT) {
|
||||
Serial.println("WebSocket client connected");
|
||||
} else if (type == WS_EVT_DISCONNECT) {
|
||||
Serial.println("WebSocket client disconnected");
|
||||
} else if (type == WS_EVT_DATA) {
|
||||
// Optionally process data received from the client
|
||||
}
|
||||
}
|
||||
|
||||
void get_time_task(void* parameter) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting GetTimeTask");
|
||||
LOG(ELOG_LEVEL_DEBUG, "Trying to get time from Internet");
|
||||
|
||||
sntp_set_time_sync_notification_cb(onTimeSync);
|
||||
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
|
||||
sntp_set_sync_interval(1000 * 60 * 60); //re-sync every hour
|
||||
configTzTime("UTC0", "pool.ntp.org", "1.europe.pool.ntp.org", " time.Windows.com");
|
||||
waitForTime();
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
// Time stuff
|
||||
|
||||
// Handler to handle successful time sync
|
||||
void onTimeSync(struct timeval* tv) {
|
||||
struct tm timeinfo;
|
||||
getLocalTime(&timeinfo);
|
||||
LOG(ELOG_LEVEL_DEBUG, "Time synchronized. New Time: %s", asctime(&timeinfo));
|
||||
}
|
||||
|
||||
// wait for succesful time sync
|
||||
bool waitForTime() {
|
||||
time_t now;
|
||||
do {
|
||||
time(&now);
|
||||
if (now > 8 * 3600) break;
|
||||
delay(500);
|
||||
LOG(ELOG_LEVEL_WARNING, "No valid time.");
|
||||
} while (true);
|
||||
LOG(ELOG_LEVEL_DEBUG, "Found valid time.");
|
||||
return true;
|
||||
}
|
||||
@@ -10,7 +10,14 @@ bool is_error(ActiveErrors active_errors);
|
||||
String processor(const String& var);
|
||||
void check_update_task(void* parameter);
|
||||
void run_ota_update_task(void* parameter);
|
||||
// void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,void *arg, uint8_t *data, size_t len);
|
||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,void *arg, uint8_t *data, size_t len);
|
||||
void get_time_task(void* parameter);
|
||||
|
||||
void onTimeSync(struct timeval* tv);
|
||||
bool waitForTime();
|
||||
void check_and_update_firmware(OTA fw);
|
||||
void check_and_update_littleFS(OTA littlefs);
|
||||
bool check_for_internet_connection();
|
||||
|
||||
typedef struct {
|
||||
OTAStatus ota_status;
|
||||
|
||||
Reference in New Issue
Block a user