Skip to content

Commit

Permalink
Config server changes, rtc fixes, increase wifi password config length
Browse files Browse the repository at this point in the history
  • Loading branch information
Claypuppet committed Sep 22, 2024
1 parent c1b0695 commit f35e41c
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 58 deletions.
8 changes: 2 additions & 6 deletions bgeigiezen_firmware/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,14 @@ bool Controller::write_sd_config() {
return SDInterface::i().write_safezen_file_from_settings(_settings);
}

bool Controller::gps_cold_start() {
_gnss.coldStart();
return true; // how to check? no idea
TeenyUbloxConnect& Controller::get_gnss() {
return _gnss;
}

void Controller::reset_settings() {
_settings.reset_defaults();
}

void Controller::set_fixed_testing(bool enable) {

}
const LocalStorage& Controller::get_settings() const {
return _settings;
}
9 changes: 2 additions & 7 deletions bgeigiezen_firmware/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,9 @@ class Controller : public Aggregator, public Worker<DeviceState> {
bool write_sd_config();

/**
* Writes device settings onto SD
*/
bool gps_cold_start();

/**
* Set fixed mode testing
* get GNSS instance
*/
void set_fixed_testing(bool enable);
TeenyUbloxConnect& get_gnss();

/**
* Reset memory settings and restart system
Expand Down
20 changes: 10 additions & 10 deletions bgeigiezen_firmware/screens/config_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void ConfigModeScreen::render_page_main(const worker_map_t& workers, const handl

// Temp print out device settings on screen
const auto& config = *workers.worker<LocalStorage>(k_worker_local_storage);
M5.Lcd.setCursor(0, 30, &fonts::Font2);
M5.Lcd.setCursor(0, 40, &fonts::Font2);

if (_main_page_info_section == e_config_section_device) {
M5.Lcd.printf("Device settings\n\n");
Expand Down Expand Up @@ -142,13 +142,13 @@ void ConfigModeScreen::render_page_ap(const worker_map_t& workers, const handler

const auto settings = workers.worker<LocalStorage>(k_worker_local_storage);

M5.Lcd.setCursor(0, 78, &fonts::Font0);
M5.Lcd.setCursor(0, 70, &fonts::Font2);
M5.Lcd.setTextColor(LCD_COLOR_DEFAULT, LCD_COLOR_BACKGROUND);
M5.Lcd.printf("Config through Access Point, connect to\n");
M5.Lcd.printf("SSID: %s\n", WiFiWrapper_i.get_hostname());
M5.Lcd.printf("Password: %s\n", settings->get_ap_password());
M5.Lcd.printf("SSID: %s\n", WiFiWrapper_i.get_hostname());
M5.Lcd.printf("Password: %s\n", settings->get_ap_password());

M5.Lcd.printf("IP: %s \n", WiFi.softAPIP().toString().c_str());
M5.Lcd.printf("IP: %s \n", WiFi.softAPIP().toString().c_str());
}

void ConfigModeScreen::render_page_wifi(const worker_map_t& workers, const handler_map_t& handlers) {
Expand All @@ -157,21 +157,21 @@ void ConfigModeScreen::render_page_wifi(const worker_map_t& workers, const handl

const auto settings = workers.worker<LocalStorage>(k_worker_local_storage);

M5.Lcd.setCursor(0, 78, &fonts::Font0);
M5.Lcd.setCursor(0, 70, &fonts::Font2);
M5.Lcd.setTextColor(LCD_COLOR_DEFAULT, LCD_COLOR_BACKGROUND);
M5.Lcd.printf("Config through local network, connect to\n");
M5.Lcd.printf("Network: %s\n", settings->get_wifi_ssid());
M5.Lcd.printf("Connected: %s\n", WiFiWrapper_i.wifi_connected() ? "Yes " : "Not yet...");
M5.Lcd.printf("Network: %s\n", settings->get_wifi_ssid());
M5.Lcd.printf("Connected: %s\n", WiFiWrapper_i.wifi_connected() ? "Yes " : "Not yet...");

M5.Lcd.printf("IP: %s \n", WiFiWrapper_i.wifi_connected() ? WiFi.localIP().toString().c_str() : "Waiting for connection... ");
M5.Lcd.printf("IP: %s \n", WiFiWrapper_i.wifi_connected() ? WiFi.localIP().toString().c_str() : "Waiting for connection... ");
}

void ConfigModeScreen::render_reset_device(const worker_map_t& workers, const handler_map_t& handlers) {
drawButton1("Options");
drawButton2("RESET");
drawButton3("Menu");

M5.Lcd.setCursor(0, 78, &fonts::Font0);
M5.Lcd.setCursor(0, 70, &fonts::Font2);
M5.Lcd.setTextColor(LCD_COLOR_DEFAULT, LCD_COLOR_BACKGROUND);
M5.Lcd.printf("Press RESET to confirm clearing all local storage\n");
}
Expand Down
8 changes: 4 additions & 4 deletions bgeigiezen_firmware/screens/satellite_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void SatelliteViewScreen::render(const worker_map_t& workers, const handler_map_
case 4: label = 'S'; break;
case 6: label = 'W'; break;
}
M5.Lcd.drawChar(xCoord + mapCenterX - 5, yCoord + mapCenterY - 7, label, LCD_COLOR_DEFAULT, LCD_COLOR_BACKGROUND, 2);
M5.Lcd.drawChar(xCoord + mapCenterX - 5, yCoord + mapCenterY - 7, label, LCD_COLOR_BACKGROUND, LCD_COLOR_DEFAULT, 2);
}

if (navsat->get_data().available) {
Expand All @@ -141,10 +141,10 @@ void SatelliteViewScreen::render(const worker_map_t& workers, const handler_map_
const auto& navsat_info = navsat->get_data().navsat_info;

// draw the positions of the sats
for(int16_t i = navsat_info.numSvsEphValid -1; i >= 0; i--) {
for(int16_t i = navsat_info.numSvsEphValid -1; i >= 0; --i) {

// Sat position
numSats ++;
++numSats;
satAzimuth = navsat_info.svSortList[i].azim;
satElevation = navsat_info.svSortList[i].elev;
if(satElevation < 0) satElevation = 0;
Expand Down Expand Up @@ -211,7 +211,7 @@ void SatelliteViewScreen::enter_screen(Controller& controller) {
// controller.set_worker_active(k_worker_navsat_collector, true);
switch (_current_page) {
case e_satellite_page_cold_start:
controller.gps_cold_start();
controller.get_gnss().coldStart();
set_status_message(F(" GPS cold start, this can take a while..."));
_current_page = e_satellite_page_main; // Back on main page
open_menu(false); // Re-enter menu
Expand Down
8 changes: 3 additions & 5 deletions bgeigiezen_firmware/utils/http_pages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const char* base_page_format_begin =
"<li class='pure-menu-item'><a href='/device' class='pure-menu-link'><i class='fa fa-tablet'></i>Device</a></li>"
"<li class='pure-menu-item'><a href='/connection' class='pure-menu-link'><i class='fa fa-wifi'></i>Connection</a></li>"
"<li class='pure-menu-item'><a href='/location' class='pure-menu-link'><i class='fa fa-map-marker-alt'></i>Location</a></li>"
"<li class='pure-menu-item'><a href='/update' class='pure-menu-link'><i class='fa fa-download'></i>Update firmware</a></li>"
"<li class='pure-menu-item'><a target='_blank' href='https://github.com/Safecast/bGeigieZen/' class='pure-menu-link'><i class='fab fa-github'></i>Github</a></li>"
"</ul>"
"</div>"
Expand All @@ -79,10 +78,9 @@ const char* HttpPages::get_home_page(const LocalStorage& settings) {
"<p class='pure-form-message'>bGeigieZen version %s</p>"
"<p>"
"<ul>"
"<li><a href='/device'>Configure your device</a></li>"
"<li><a href='/connection'>Configure your connection to the API</a></li>"
"<li><a href='/location'>Configure your location for fixed mode</a></li>"
"<li><a href='/update'>Update the firmware</a></li>"
"<li><a href='/device'>Configure device settings</a></li>"
"<li><a href='/connection'>Configure Network connections</a></li>"
"<li><a href='/location'>Configure location settings for fixed mode</a></li>"
"</ul>"
"More information about configurations in the <a href='https://github.com/Safecast/bGeigieZen/wiki/User-manual#available-settings' target='_blank'>User manual</a>. "
"Or view your device on <a href='%s' target='_blank'>Grafana</a>."
Expand Down
2 changes: 0 additions & 2 deletions bgeigiezen_firmware/workers/battery_indicator.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "battery_indicator.h"

#include <M5Unified.hpp>

BatteryIndicator::BatteryIndicator() : Worker<BatteryStatus>(1000) {
}

Expand Down
6 changes: 3 additions & 3 deletions bgeigiezen_firmware/workers/battery_indicator.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#ifndef BGEIGIEZEN_BATTERYINDICATOR_H_
#define BGEIGIEZEN_BATTERYINDICATOR_H_

#include <M5Unified.hpp>
#include <Worker.hpp>
#include <stdint.h>

/**
* Relevant battery status
*/
struct BatteryStatus {
uint8_t percentage;
bool isCharging;
int32_t percentage;
m5::Power_Class::is_charging_t isCharging;
};

/**
Expand Down
38 changes: 26 additions & 12 deletions bgeigiezen_firmware/workers/configuration_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ T clamp(T2 val, T min, T max) {
ConfigWebServer::ConfigWebServer(LocalStorage& config)
: Worker<ServerStatus>(k_server_status_offline, 0),
_server(SERVER_WIFI_PORT),
_config(config) {
_config(config),
_handled_client(false) {
add_urls();
}

Expand All @@ -27,20 +28,21 @@ bool ConfigWebServer::activate(bool) {
// Start config server
_server.begin(SERVER_WIFI_PORT);
HttpPages::internet_access = WiFiWrapper_i.wifi_connected();
data = HttpPages::internet_access ? k_server_status_running_wifi : k_server_status_running_access_point;
return true;
}
return false;
}

void ConfigWebServer::deactivate() {
data = k_server_status_offline;
_server.close();
_server.stop();
}

int8_t ConfigWebServer::produce_data() {
_server.handleClient();
if(data == k_server_status_offline) {
data = HttpPages::internet_access ? k_server_status_running_wifi : k_server_status_running_access_point;
if (_handled_client) {
_handled_client = false;
return Worker::e_worker_data_read;
}
return Worker::e_worker_idle;
Expand All @@ -49,60 +51,70 @@ int8_t ConfigWebServer::produce_data() {
void ConfigWebServer::add_urls() {
// Home
_server.on("/", HTTP_GET, [this]() {
M5_LOGD("Handle get home");
_server.sendHeader("Connection", "close");
_server.send(200, "text/html", HttpPages::get_home_page(_config));
_server.send_P(200, "text/html", HttpPages::get_home_page(_config));
_handled_client = true;
});

// Configure Device
_server.on("/device", HTTP_GET, [this]() {
M5_LOGD("Handle get device settings");
_server.sendHeader("Connection", "close");
_server.send(200, "text/html", HttpPages::get_config_device_page(
_server.send_P(200, "text/html", HttpPages::get_config_device_page(
_server.hasArg("success"),
_config
));
_handled_client = true;
});

// Configure Connection
_server.on("/connection", HTTP_GET, [this]() {
M5_LOGD("Handle get connection settings");
_server.sendHeader("Connection", "close");
_server.send(200, "text/html", HttpPages::get_config_connection_page(
_server.send_P(200, "text/html", HttpPages::get_config_connection_page(
_server.hasArg("success"),
_config
));
_handled_client = true;
});

// Configure Location
_server.on("/location", HTTP_GET, [this]() {

M5_LOGD("Handle get location settings");
_server.sendHeader("Connection", "close");
_server.send(200, "text/html", HttpPages::get_config_location_page(
_server.send_P(200, "text/html", HttpPages::get_config_location_page(
_server.hasArg("success"),
_config
));

_server.sendHeader("Connection", "close");
_handled_client = true;
});

// Save config
_server.on("/save", HTTP_POST, [this]() {
handle_save();
_handled_client = true;
});

_server.on("/reboot", [this]() { // Reboot
_server.send(200, "text/plain", "OK");
M5_LOGD("Handle reboot request");
_server.send_P(200, "text/plain", "OK");
_server.client().flush();
ESP.restart();
_handled_client = true;
});

// css get
_server.on("/pure.css", HTTP_GET, [this]() {
_server.sendHeader("Content-Encoding", "gzip");
_server.send_P(200, "text/css", reinterpret_cast<const char*>(HttpPages::pure_css), PURE_CSS_SIZE);
_handled_client = true;
});

// css get
_server.on("/favicon.ico", HTTP_GET, [this]() {
_server.send_P(200, "image/x-icon", reinterpret_cast<const char*>(HttpPages::favicon), FAVICON_SIZE);
_handled_client = true;
});
}

Expand All @@ -111,6 +123,8 @@ void ConfigWebServer::handle_save() {
// _config.set...(_server.arg(...).c_str(), ...);
// }

M5_LOGD("Handle post request");

if(_server.hasArg(FORM_NAME_ALERT_THRESHOLD)) {
_config.set_alert_threshold(clamp<uint16_t>(_server.arg(FORM_NAME_ALERT_THRESHOLD).toInt(), 0, 34464), false);
}
Expand Down
1 change: 1 addition & 0 deletions bgeigiezen_firmware/workers/configuration_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ConfigWebServer : public Worker<ServerStatus> {

WebServer _server;
LocalStorage& _config;
bool _handled_client;
};

#endif //BGEIGIEZEN_SERVER_H_
2 changes: 1 addition & 1 deletion bgeigiezen_firmware/workers/local_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void LocalStorage::set_wifi_ssid(const char* wifi_ssid, bool force) {
}

void LocalStorage::set_wifi_password(const char* wifi_password, bool force) {
if(force || (wifi_password != nullptr && strlen(wifi_password) < CONFIG_VAL_MAX)) {
if(force || (wifi_password != nullptr && strlen(wifi_password) < CONFIG_LONG_VAL_MAX)) {
if(_memory.begin(memory_name)) {
strcpy(_wifi_password, wifi_password);
_memory.putString(key_wifi_password, _wifi_password);
Expand Down
3 changes: 2 additions & 1 deletion bgeigiezen_firmware/workers/local_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Handler.hpp>

#define CONFIG_VAL_MAX 32
#define CONFIG_LONG_VAL_MAX 64

/**
* Configurations for the ESP32, stored in the flash memory
Expand Down Expand Up @@ -96,7 +97,7 @@ class LocalStorage : public ProcessWorker<bool> {

// Connection settings
char _wifi_ssid[CONFIG_VAL_MAX];
char _wifi_password[CONFIG_VAL_MAX];
char _wifi_password[CONFIG_LONG_VAL_MAX];
char _api_key[CONFIG_VAL_MAX];

// Location settings
Expand Down
18 changes: 11 additions & 7 deletions bgeigiezen_firmware/workers/rtc_connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "identifiers.h"


DateTimeProvider::DateTimeProvider() : ProcessWorker<RtcData>({.valid=false}, 1000), _sys_time{}, _last_sys_set(0) {
DateTimeProvider::DateTimeProvider() : ProcessWorker<RtcData>({.valid=false, .gps_confirmed=false}, 1000), _sys_time{}, _last_sys_set(0) {
}

/**
Expand Down Expand Up @@ -67,19 +67,27 @@ void DateTimeProvider::gps_to_system(const GnssData& gps_data) {
sys_time.tm_hour = gps_data.hour;
sys_time.tm_min = gps_data.minute;
sys_time.tm_sec = gps_data.second;
save_to_system(sys_time);

data.gps_confirmed = true;

M5_LOGD("Set system time from GPS: %04d-%02d-%02d %02d:%02d:%02d",
gps_data.year, gps_data.month, gps_data.day,
gps_data.hour, gps_data.minute, gps_data.second);

save_to_system(sys_time);
}
}

void DateTimeProvider::rtc_to_system() {
if (M5.Rtc.isEnabled()) {
m5::rtc_datetime_t rtc_datetime;
M5.Rtc.getDateTime(&rtc_datetime);
if (rtc_datetime.date.year < 2024 || rtc_datetime.date.year > 2035) {

M5_LOGD("Load from RTC: %04d-%02d-%02d %02d:%02d:%02d",
rtc_datetime.date.year, rtc_datetime.date.month, rtc_datetime.date.date,
rtc_datetime.time.hours, rtc_datetime.time.minutes, rtc_datetime.time.seconds);

if (M5.Rtc.getVoltLow() || rtc_datetime.date.year < 2024) {
// RTC returned invalid data
M5_LOGD("RTC invalid year, reset sys time");
return invalidate_sys_time();
Expand All @@ -93,10 +101,6 @@ void DateTimeProvider::rtc_to_system() {
sys_time.tm_min = static_cast<uint8_t>(rtc_datetime.time.minutes);
sys_time.tm_sec = static_cast<uint8_t>(rtc_datetime.time.seconds);

M5_LOGD("Set system time from RTC: %04d-%02d-%02d %02d:%02d:%02d",
rtc_datetime.date.year, rtc_datetime.date.month, rtc_datetime.date.date,
rtc_datetime.time.hours, rtc_datetime.time.minutes, rtc_datetime.time.seconds);

save_to_system(sys_time);
}
}
Expand Down
1 change: 1 addition & 0 deletions bgeigiezen_firmware/workers/rtc_connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct RtcData {
uint8_t minute;
uint8_t second;
bool valid;
bool gps_confirmed;
};

/**
Expand Down

0 comments on commit f35e41c

Please sign in to comment.