forked from Akcelerometry_drgania_WMT/PI_mikrokontroler
Dodanie Captive Portal z logarytmami
This commit is contained in:
137
src/main.cpp
137
src/main.cpp
@@ -15,7 +15,8 @@
|
||||
#include <Measure.h>
|
||||
#include <Tool.h>
|
||||
#include <Settings.h>
|
||||
#include <Uploader.h> // Dodano moduł wysyłki
|
||||
#include "APIClient.h"
|
||||
#include "UploadManager.h"
|
||||
|
||||
#define WDT_TIMEOUT 60 // Czas watchdoga do restartu
|
||||
#define MAX_ADXL345_SENSORS 4 // Maksymalna ilość podłączanych sensorów
|
||||
@@ -23,7 +24,7 @@
|
||||
SPIClass SPI_ADXL(FSPI); // SPI2 (VSPI)
|
||||
SPIClass SPI_SD(HSPI); // SPI3 (HSPI)
|
||||
|
||||
float x, y, z = 0; // Dane odczytane z akcelerometru
|
||||
float x, y, z = 0; // Dane odczytane z akcelerometru
|
||||
String name;
|
||||
|
||||
bool isRebootRequired = false;
|
||||
@@ -46,14 +47,13 @@ Settings settings(display, rtc); // obsługa przycisków
|
||||
WiFiManager wifi;
|
||||
|
||||
DataCapture capture(adxl, display, rtc, SD, 8192); // NEW !!! MEASURE!!!
|
||||
|
||||
// Inicjalizacja Uploadera
|
||||
Uploader uploader(display);
|
||||
APIClient apiClient;
|
||||
UploadManager uploadManager(apiClient, rtc, capture);
|
||||
|
||||
Thread wifiTestThread = Thread(); // Cykliczny test WiFi
|
||||
Thread offlineThread = Thread(); // Jeśli offline
|
||||
Thread measureThread = Thread(); // Pomiar i zapis na SD
|
||||
Thread uploadThread = Thread(); // Wątek wysyłki SSL
|
||||
Thread uploadThread = Thread(); // Background upload
|
||||
|
||||
//////// PROTOTYPY /////////////////
|
||||
void setup();
|
||||
@@ -63,10 +63,7 @@ void resetBtnClick();
|
||||
void checkWiFi();
|
||||
void showOfflineScreen();
|
||||
void measure();
|
||||
void runUploader();
|
||||
void toogleMode();
|
||||
void settingsDevice();
|
||||
void showError(String err);
|
||||
void setClockRTCBtn();
|
||||
|
||||
/* ******************* SETUP() ************************* */
|
||||
void setup() {
|
||||
@@ -84,8 +81,8 @@ void setup() {
|
||||
ESP_LOGI(TAG_MAIN, "Rejestrator parametrow");
|
||||
ESP_LOGI(TAG_MAIN, "Firmware: %s", VERSION);
|
||||
ESP_LOGI(TAG_MAIN, "----------------------");
|
||||
ESP_LOGI(TAG_MAIN, "ESP32 model: %s Rev %d", ESP.getChipModel(), ESP.getChipRevision());
|
||||
ESP_LOGI(TAG_MAIN, "Chip cores: %d", ESP.getChipCores());
|
||||
ESP_LOGI(TAG_MAIN, "ESP32 model: %s Rev %d", ESP.getChipModel(), ESP.getChipRevision());
|
||||
ESP_LOGI(TAG_MAIN, "Chip cores: %d", ESP.getChipCores());
|
||||
|
||||
// Inicjalizacja Watchdoga na 5 sek, panic_on_trigger = true
|
||||
if (Watchdog::init(25, true)) {
|
||||
@@ -99,6 +96,8 @@ void setup() {
|
||||
|
||||
configManager.begin(); // konfiguracja EEPROM urządzenia
|
||||
|
||||
//configManager.showConfig();
|
||||
|
||||
// Test LCD I2C
|
||||
if (!isI2CDevPresent(0x27)) {
|
||||
ESP_LOGE(TAG_MAIN, "LCD 0x27 wire error!");
|
||||
@@ -158,6 +157,10 @@ void setup() {
|
||||
settings.setTimeRTC();
|
||||
}
|
||||
}
|
||||
|
||||
// Przycisk ustawianai czasu: przytrzymaj OK przy starcie systemu
|
||||
if(settings.isSetClock()) setClockRTCBtn();
|
||||
|
||||
delay(500);
|
||||
|
||||
// Karta SD
|
||||
@@ -165,7 +168,7 @@ void setup() {
|
||||
ESP_LOGI(TAG_MAIN, "SPI2 SD SCK: %d, MISO: %d, MOSI: %d, CS: %d", SD_SCK, SD_MISO, SD_MOSI, SD_CS);
|
||||
SPI_SD.begin(SD_SCK, SD_MISO, SD_MOSI);
|
||||
display.textStatus("SD CARD:");
|
||||
while(!SD.begin(SD_CS, SPI_SD, 4000000)) {
|
||||
while(!SD.begin(SD_CS, SPI_SD, 4000000)) { // 10 MHz na start; w razie czego 4 MHz (bez tego często SD Failed)
|
||||
ESP_LOGE(TAG_MAIN, "SD mount failed");
|
||||
display.print("FAILED");
|
||||
delay(4000);
|
||||
@@ -181,7 +184,8 @@ void setup() {
|
||||
// Inicjalizacja ADXL345
|
||||
ESP_LOGI(TAG_MAIN, "ADXL345 init");
|
||||
display.textStatus("ADXL345: ");
|
||||
if(!adxl.begin(&SPI_ADXL, 5000000, ADXL345FastSPI::RATE_3200HZ, ADXL345FastSPI::RANGE_16G, 1)){
|
||||
//if(!adxl.begin(&SPI_ADXL, 5000000, ADXL345FastSPI::RATE_3200HZ, ADXL345FastSPI::RANGE_16G, 1)){
|
||||
if(!adxl.begin(&SPI_ADXL, 2000000, ADXL345FastSPI::RATE_3200HZ, ADXL345FastSPI::RANGE_16G, 1)){
|
||||
ESP_LOGE(TAG_MAIN, "ADXL345 Error");
|
||||
display.print("FAILED");
|
||||
isAccelExists = false;
|
||||
@@ -208,36 +212,23 @@ void setup() {
|
||||
delay(1000);
|
||||
|
||||
if(config.connect){
|
||||
display.textStatus("WiFi Connect...");
|
||||
wifi.begin();
|
||||
|
||||
int retry_count = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && retry_count < 60) {
|
||||
delay(500);
|
||||
retry_count++;
|
||||
Watchdog::feed();
|
||||
if(retry_count % 10 == 0) display.print(".");
|
||||
}
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
ESP_LOGW(TAG_MAIN, "WiFi Failed. Starting Configuration Portal...");
|
||||
wifi.startConfigPortal(display);
|
||||
} else {
|
||||
ESP_LOGI(TAG_MAIN, "WiFi OK");
|
||||
wifiTestThread.onRun(checkWiFi);
|
||||
wifiTestThread.setInterval(5000);
|
||||
|
||||
uploadThread.onRun(runUploader);
|
||||
uploadThread.setInterval(30000);
|
||||
}
|
||||
wifiTestThread.onRun(checkWiFi);
|
||||
wifiTestThread.setInterval(5000); // Test WiFi co 3 sekundy
|
||||
} else {
|
||||
offlineThread.onRun(showOfflineScreen);
|
||||
offlineThread.setInterval(1000);
|
||||
offlineThread.setInterval(1000); // Jeśli offline, to wyświetl ekran co 1 sek
|
||||
}
|
||||
|
||||
measureThread.onRun(measure);
|
||||
measureThread.setInterval(config.pause);
|
||||
measureThread.setInterval(config.pause); // Test co X sekund
|
||||
|
||||
if(config.connect){
|
||||
uploadThread.onRun([]() { uploadManager.processPendingUploads(); });
|
||||
uploadThread.setInterval(60000); // Co 1 minutę sprawdzaj zaległe
|
||||
}
|
||||
|
||||
// Dodanie taska loop do WDT
|
||||
if (Watchdog::addThisTask()) {
|
||||
ESP_LOGI(TAG_MAIN, "Added main task to Watchdog");
|
||||
}
|
||||
@@ -246,14 +237,23 @@ void setup() {
|
||||
display.clear();
|
||||
}
|
||||
|
||||
void runUploader() {
|
||||
if (!testingNow) {
|
||||
uploader.processQueue(3);
|
||||
}
|
||||
}
|
||||
// Factory reset
|
||||
void setClockRTCBtn(){
|
||||
ESP_LOGI(TAG_MAIN, "SET DATE TIME");
|
||||
while(settings.isSetClock()){
|
||||
display.clear();
|
||||
display.textCenter(0, " PLEASE ");
|
||||
display.textCenter(1, "release key");
|
||||
display.textCenter(3, "TO SET DATE TIME");
|
||||
delay(500);
|
||||
}
|
||||
settings.setTimeRTC();
|
||||
}
|
||||
|
||||
|
||||
// Factory reset
|
||||
void resetBtnClick(){
|
||||
uint8_t counter = 10;
|
||||
uint8_t counter = 10; // ile sekund trzymać przycisk na factory reset
|
||||
ESP_LOGI(TAG_MAIN, "RESET BTN to 10 sec.");
|
||||
while(settings.isBtnReset()){
|
||||
Serial.print(counter); Serial.print(",");
|
||||
@@ -280,6 +280,8 @@ void resetBtnClick(){
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void showError(String err){
|
||||
Watchdog::feed();
|
||||
display.clear();
|
||||
@@ -288,6 +290,7 @@ void showError(String err){
|
||||
delay(3000);
|
||||
}
|
||||
|
||||
|
||||
void checkWiFi(){
|
||||
Watchdog::feed();
|
||||
bool isConnected = WiFi.isConnected();
|
||||
@@ -297,6 +300,7 @@ void checkWiFi(){
|
||||
display.updateNetwork(ip, isConnected);
|
||||
}
|
||||
|
||||
////// Ekran główny tylko gdy brak pomiaru /////////////
|
||||
void showOfflineScreen(){
|
||||
if((!config.connect) && (!testingNow)) {
|
||||
bool isGB;
|
||||
@@ -306,23 +310,26 @@ void showOfflineScreen(){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void reboot() {
|
||||
display.clear();
|
||||
display.textCenter(1, "SYSTEM");
|
||||
display.textCenter(2, "RESTARTING");
|
||||
#if defined(ARDUINO_RASPBERRY_PI_PICO)
|
||||
watchdog_enable(1, 1);
|
||||
while (true);
|
||||
watchdog_enable(1, 1); // 1 ms timeout, restart po upływie
|
||||
while (true); // czekaj na watchdog reset
|
||||
#endif
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
ESP_LOGI(TAG_MAIN, "RESTART");
|
||||
ESP.restart();
|
||||
ESP.restart(); // restart ESP
|
||||
#endif
|
||||
#if defined(ARDUINO_ARCH_STM32)
|
||||
NVIC_SystemReset();
|
||||
NVIC_SystemReset(); // restartuj STM32
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// pomiar z Thread START POMIARU TUTAJ
|
||||
void measure(){
|
||||
Watchdog::feed();
|
||||
testingNow = true;
|
||||
@@ -334,7 +341,7 @@ void measure(){
|
||||
snprintf(btime, sizeof(btime), "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
|
||||
display.initMeasure(config.measure, testingNow, runMeasure, config.pause, config.duration, config.connect, licznik, bdate, btime);
|
||||
ESP_LOGI(TAG_MAIN, "MEASURE RUNNING");
|
||||
|
||||
//delay(1000);
|
||||
capture.captureAuto(config.duration, "/");
|
||||
testingNow = false;
|
||||
Watchdog::feed();
|
||||
@@ -344,14 +351,21 @@ void measure(){
|
||||
ESP_LOGI(TAG_MAIN, "MEASURE FINISH");
|
||||
} else {
|
||||
ESP_LOGI(TAG_MAIN, "MEASURE INTERRUPT");
|
||||
//runMeasure = false;
|
||||
}
|
||||
|
||||
if (config.connect && WiFi.status() == WL_CONNECTED) {
|
||||
ESP_LOGI(TAG_MAIN, "TRIGGER UPLOAD PENDING...");
|
||||
uploadManager.processPendingUploads();
|
||||
}
|
||||
|
||||
runMeasure = false;
|
||||
ESP_LOGI(TAG_MAIN, "DISPLAY Offline");
|
||||
display.displayOffline(testingNow, adxl.connectedSensorsCount(), capture.freeSpaceFloat(), licznik, true);
|
||||
offlineThread.enabled = true;
|
||||
}
|
||||
|
||||
void toogleMode(){
|
||||
void toogleMode(){ // tryb ciągłego, pojedynczego pomiaru
|
||||
config.measure = !config.measure;
|
||||
configManager.saveConfig();
|
||||
display.textStatus("Mode changed");
|
||||
@@ -385,18 +399,30 @@ void loop() {
|
||||
if(settings.isPressed(3)) settingsDevice(); // DOWN
|
||||
if(settings.isPressed(1)) toogleMode(); // UP
|
||||
|
||||
if (settings.readBtnUp() && settings.readBtnDown()) {
|
||||
ESP_LOGI(TAG_MAIN, "Manual AP Mode trigger");
|
||||
wifi.startCaptivePortal();
|
||||
display.clear();
|
||||
display.textCenter(1, "AP MODE");
|
||||
display.textCenter(2, "192.168.4.1");
|
||||
while(settings.readBtnUp() && settings.readBtnDown()) {
|
||||
Watchdog::feed();
|
||||
delay(50);
|
||||
}
|
||||
}
|
||||
|
||||
if(testingNow) {
|
||||
if((runMeasure) && (settings.isPressed(2))){
|
||||
runMeasure = false;
|
||||
display.textStatus("STOPPING. WAIT");
|
||||
}
|
||||
return;
|
||||
return; // jeśli trwa akurat test, nic nie rób
|
||||
}
|
||||
|
||||
if(wifiTestThread.shouldRun() && (config.connect))
|
||||
wifiTestThread.run();
|
||||
|
||||
if(uploadThread.shouldRun() && (config.connect))
|
||||
if(uploadThread.shouldRun() && config.connect)
|
||||
uploadThread.run();
|
||||
|
||||
if(offlineThread.shouldRun() && (!config.connect)){
|
||||
@@ -423,11 +449,12 @@ void loop() {
|
||||
if (isRebootRequired) {
|
||||
ESP_LOGI(TAG_MAIN, "Reboot required");
|
||||
Watchdog::feed();
|
||||
delay(1000);
|
||||
reboot();
|
||||
delay(1000);
|
||||
reboot();
|
||||
}
|
||||
|
||||
wifi.handleClient();
|
||||
Watchdog::feed();
|
||||
delay(20);
|
||||
delay(20); // 100
|
||||
licznik ++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user