Testing Round1 - must figure out balance between PM versus plugged-in mode? also must test bms

This commit is contained in:
2026-03-18 01:47:23 -05:00
parent d2884423a3
commit d2d49c39a4
11 changed files with 101 additions and 40 deletions

View File

@@ -1620,7 +1620,7 @@ CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT=y
CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304
CONFIG_ESP_MAIN_TASK_STACK_SIZE=3584
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0=y
# CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set
CONFIG_ESP_MAIN_TASK_AFFINITY=0x0
@@ -2708,7 +2708,7 @@ CONFIG_ESP32_PHY_MAC_BB_PD=y
CONFIG_ESP_SYSTEM_PM_POWER_DOWN_CPU=y
CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32
CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304
CONFIG_MAIN_TASK_STACK_SIZE=3584
CONFIG_MAIN_TASK_STACK_SIZE=8192
CONFIG_CONSOLE_UART_DEFAULT=y
# CONFIG_CONSOLE_UART_CUSTOM is not set
# CONFIG_CONSOLE_UART_NONE is not set

View File

@@ -1,3 +1,7 @@
#
# Automatically generated file. DO NOT EDIT.
# Espressif IoT Development Framework (ESP-IDF) 5.5.1 Project Configuration
#
CONFIG_SOC_ADC_SUPPORTED=y
CONFIG_SOC_DEDICATED_GPIO_SUPPORTED=y
CONFIG_SOC_UART_SUPPORTED=y
@@ -128,7 +132,6 @@ CONFIG_SOC_GPIO_DEEP_SLEEP_WAKE_VALID_GPIO_MASK=0
CONFIG_SOC_GPIO_DEEP_SLEEP_WAKE_SUPPORTED_PIN_CNT=8
CONFIG_SOC_GPIO_VALID_DIGITAL_IO_PAD_MASK=0x000000007FFFFF00
CONFIG_SOC_GPIO_SUPPORT_FORCE_HOLD=y
CONFIG_SOC_GPIO_SUPPORT_HOLD_IO_IN_DSLP=y
CONFIG_SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP=y
CONFIG_SOC_GPIO_CLOCKOUT_BY_GPIO_MATRIX=y
CONFIG_SOC_CLOCKOUT_HAS_SOURCE_GATE=y
@@ -1525,7 +1528,7 @@ CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE=y
# CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION is not set
CONFIG_ESP_PHY_MAX_WIFI_TX_POWER=20
CONFIG_ESP_PHY_MAX_TX_POWER=20
# CONFIG_ESP_PHY_MAC_BB_PD is not set
CONFIG_ESP_PHY_MAC_BB_PD=y
# CONFIG_ESP_PHY_REDUCE_TX_POWER is not set
# CONFIG_ESP_PHY_ENABLE_CERT_TEST is not set
CONFIG_ESP_PHY_RF_CAL_PARTIAL=y
@@ -1713,6 +1716,7 @@ CONFIG_ESP_WIFI_STA_DISCONNECTED_PM_ENABLE=y
# CONFIG_ESP_WIFI_GCMP_SUPPORT is not set
CONFIG_ESP_WIFI_GMAC_SUPPORT=y
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y
# CONFIG_ESP_WIFI_ENHANCED_LIGHT_SLEEP is not set
# CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT is not set
CONFIG_ESP_WIFI_ESPNOW_MAX_ENCRYPT_NUM=7
CONFIG_ESP_WIFI_MBEDTLS_CRYPTO=y
@@ -2352,13 +2356,6 @@ CONFIG_LIBC_TIME_SYSCALL_USE_RTC_HRT=y
#
# CONFIG_OPENTHREAD_ENABLED is not set
#
# Thread Console
#
CONFIG_OPENTHREAD_CLI=y
CONFIG_OPENTHREAD_CONSOLE_COMMAND_PREFIX="ot"
# end of Thread Console
#
# OpenThread Spinel
#
@@ -2571,6 +2568,10 @@ CONFIG_WIFI_PROV_STA_ALL_CHANNEL_SCAN=y
# CONFIG_WIFI_PROV_STA_FAST_SCAN is not set
# end of Wi-Fi Provisioning Manager
#
# ESP Socket.IO client
#
#
# ESP-NimBLE-CPP configuration
#
@@ -2593,10 +2594,6 @@ CONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT=31
CONFIG_NIMBLE_CPP_IDF=y
# end of ESP-NimBLE-CPP configuration
#
# ESP Socket.IO client
#
#
# ESP WebSocket client
#

View File

@@ -10,6 +10,7 @@
#include "bmHTTP.hpp"
#include "setup.hpp"
#include "esp_mac.h"
#include "esp_netif.h"
std::atomic<bool> flag_scan_requested{false};
@@ -324,17 +325,42 @@ bool tokenCheck() {
notifyAuthStatus(false);
return false;
}
vTaskDelay(pdMS_TO_TICKS(500));
// --- DIAGNOSTIC: snapshot network state before HTTP ---
{
extern esp_netif_t* _diag_netif __attribute__((weak));
// Use esp_netif_get_handle_from_ifkey to get the STA netif without changing headers
esp_netif_t* sta_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
if (sta_netif) {
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(sta_netif, &ip_info);
printf("[DIAG tokenCheck] IP: " IPSTR "\n", IP2STR(&ip_info.ip));
esp_netif_dns_info_t dns;
if (esp_netif_get_dns_info(sta_netif, ESP_NETIF_DNS_MAIN, &dns) == ESP_OK)
printf("[DIAG tokenCheck] DNS: " IPSTR "\n", IP2STR(&dns.ip.u_addr.ip4));
else
printf("[DIAG tokenCheck] DNS: NOT SET\n");
} else {
printf("[DIAG tokenCheck] Could not get STA netif handle\n");
}
printf("[DIAG tokenCheck] WiFi::isConnected() = %d\n", (int)WiFi::isConnected());
}
// HTTP request to verify device with token
std::string tmpTOKEN;
{
std::lock_guard<std::mutex> lock(dataMutex);
tmpTOKEN = TOKEN;
}
cJSON *responseRoot;
bool success = httpGET("verify_device", tmpTOKEN, responseRoot);
if (!success) return false;
if (!success) {
notifyAuthStatus(false);
return false;
}
success = false;
if (responseRoot != NULL) {

View File

@@ -3,6 +3,8 @@
#include "cJSON.h" // Native replacement for ArduinoJson
#include "BLE.hpp"
#include "esp_wifi_he.h"
#include "esp_netif.h"
#include "lwip/dns.h"
TaskHandle_t WiFi::awaitConnectHandle = NULL;
EventGroupHandle_t WiFi::s_wifi_event_group = NULL;
@@ -51,6 +53,7 @@ void WiFi::event_handler(void* arg, esp_event_base_t event_base,
case WIFI_REASON_ASSOC_LEAVE: // Reason 8 - Manual disconnect
printf("Manual disconnect, not retrying\n");
esp_netif_dhcpc_start(netif);
break;
case WIFI_REASON_ASSOC_FAIL: // Reason 203 (Can be AP busy/rate limiting)
@@ -79,7 +82,6 @@ void WiFi::event_handler(void* arg, esp_event_base_t event_base,
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
printf("Got IP: " IPSTR "\n", IP2STR(&event->ip_info.ip));
esp_netif_dhcpc_stop(netif);
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
if (awaitConnectHandle != NULL) {
xTaskNotify(awaitConnectHandle, true, eSetValueWithOverwrite);
@@ -202,7 +204,20 @@ bool WiFi::awaitConnected() {
}
if (isConnected()) {
esp_wifi_set_ps(WIFI_PS_MIN_MODEM);
// --- DIAGNOSTIC: snapshot network state before DHCP stop ---
// Save IP and DNS before stopping DHCP — dhcpc_stop clears both
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(netif, &ip_info);
esp_netif_dns_info_t dns_main, dns_backup;
esp_netif_get_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_main);
esp_netif_get_dns_info(netif, ESP_NETIF_DNS_BACKUP, &dns_backup);
esp_netif_dhcpc_stop(netif);
// Re-apply IP and DNS as static configuration
esp_netif_set_ip_info(netif, &ip_info);
esp_netif_set_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_main);
esp_netif_set_dns_info(netif, ESP_NETIF_DNS_BACKUP, &dns_backup);
uint8_t protocol_bitmap = 0;
esp_err_t err = esp_wifi_get_protocol(WIFI_IF_STA, &protocol_bitmap);
@@ -309,9 +324,18 @@ bool WiFi::attemptDHCPrenewal() {
pdMS_TO_TICKS(4000));
if (bits & WIFI_CONNECTED_BIT) {
printf("renewal success");
// Stop the client again to save power.
printf("renewal success\n");
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(netif, &ip_info);
esp_netif_dns_info_t dns_main, dns_backup;
esp_netif_get_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_main);
esp_netif_get_dns_info(netif, ESP_NETIF_DNS_BACKUP, &dns_backup);
esp_netif_dhcpc_stop(netif);
esp_netif_set_ip_info(netif, &ip_info);
esp_netif_set_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns_main);
esp_netif_set_dns_info(netif, ESP_NETIF_DNS_BACKUP, &dns_backup);
return true;
}
printf("DHCP Renewal failed. Reconnecting Wi-Fi...\n");

View File

@@ -112,7 +112,7 @@ bool Calibration::completeCalib(Encoder& topEnc) {
int32_t Calibration::convertToTicks(uint8_t appPos) {
// appPos between 0 and 10, convert to target encoder ticks.
return (((int32_t)appPos * (UpTicks - DownTicks)) / 10) + DownTicks;
return (((int32_t)appPos * (UpTicks - DownTicks)) / 10) + DownTicks + ((UpTicks - DownTicks) / 20);
}
uint8_t Calibration::convertToAppPos(int32_t ticks) {
@@ -136,6 +136,11 @@ bool calibrate() {
// Notification received within timeout
if (status) {
printf("Connected successfully, awaiting destroy command\n");
// Socket is now authenticated. Tell the server this device is not
// calibrated so it resets DB state and notifies the app to show the
// pre-calibration screen. The device stays connected, so when the user
// taps Calibrate the subsequent calib_start event will arrive here.
emitCalibStatus(false, 1);
xTaskNotifyWait(0, ULONG_MAX, &status, portMAX_DELAY);
calibTaskHandle = NULL;
if (status == 2) { // calibration complete

View File

@@ -51,8 +51,8 @@ void IRAM_ATTR Encoder::isr_handler(void* arg)
if (calibListen) servoCalibListen();
if (encoder->feedWDog) {
esp_timer_stop(encoder->watchdog_handle);
esp_timer_start_once(encoder->watchdog_handle, 500000);
debugLEDTgl();
esp_timer_start_once(encoder->watchdog_handle, 2000000);
// debugLEDTgl();
}
if (encoder->wandListen) servoWandListen();
if (encoder->serverListen) servoServerListen();
@@ -63,8 +63,8 @@ void IRAM_ATTR Encoder::isr_handler(void* arg)
if (calibListen) servoCalibListen();
if (encoder->feedWDog) {
esp_timer_stop(encoder->watchdog_handle);
esp_timer_start_once(encoder->watchdog_handle, 500000);
debugLEDTgl();
esp_timer_start_once(encoder->watchdog_handle, 2000000);
// debugLEDTgl();
}
if (encoder->wandListen) servoWandListen();
if (encoder->serverListen) servoServerListen();
@@ -112,7 +112,7 @@ void Encoder::setupWatchdog() {
ESP_ERROR_CHECK(esp_timer_create(&enc_watchdog_args, &watchdog_handle));
}
ESP_ERROR_CHECK(esp_timer_start_once(watchdog_handle, 500000));
ESP_ERROR_CHECK(esp_timer_start_once(watchdog_handle, 2000000));
feedWDog = true;
}

View File

@@ -38,8 +38,9 @@ void mainApp() {
bottomEnc->init();
servoInit();
max17048_init();
printf("beforeSetup\n");
setupAndCalibrate();
printf("afterSetup\n");
xTaskCreate(wakeTimer, "wakeTimer", 2048, NULL, 5, &wakeTaskHandle);
@@ -57,6 +58,6 @@ void pm_init() {
extern "C" void app_main() {
// pm_init();
// mainApp();
encoder_test();
mainApp();
// servo_test();
}

View File

@@ -201,7 +201,7 @@ void mainEventLoop() {
postBatteryAlert(alertType, established_soc);
printf("CRITICAL BATTERY EVENT (%s, SOC=%d%%). Entering deep sleep.\n",
battAlertTypeStr(alertType), established_soc);
esp_deep_sleep_start();
// esp_deep_sleep_start();
} else if (received_event_type == EVENT_BATTERY_WARNING) {
postBatteryAlert((batt_alert_type_t)bms_pending_alert, established_soc);

View File

@@ -51,7 +51,7 @@ void servoInit() {
topEnc->count = servoReadPos();
if (Calibration::getCalibrated()) initMainLoop();
debugLEDSwitch(1);
// debugLEDSwitch(1);
}
void servoOn(uint8_t dir, uint8_t manOrServer) {
@@ -232,20 +232,20 @@ void servoWandListen() {
// otherwise, run servo in whichever direction necessary and
// ensure servo-listener is active.
if (topCount >= (MAX(upBound, downBound) - 1)
&& effDiff > 1) { // TODO: see whether these margins need to be removed.
&& effDiff > 2) { // TODO: see whether these margins need to be removed.
servoOff();
topEnc->wandListen.store(false, std::memory_order_release);
}
else if (topCount <= (MIN(upBound, downBound) + 1)
&& effDiff < -1) {
&& effDiff < -2) {
servoOff();
topEnc->wandListen.store(false, std::memory_order_release);
}
else if (effDiff > 1) {
else if (effDiff > 2) {
topEnc->wandListen.store(true, std::memory_order_release);
servoOn(CCW, manual);
}
else if (effDiff < -1) {
else if (effDiff < -2) {
topEnc->wandListen.store(true, std::memory_order_release);
servoOn(CW, manual);
}
@@ -268,8 +268,12 @@ void runToAppPos(uint8_t appPos) {
if (runningManual || !Calibration::getCalibrated()) return;
servoOff();
if (Calibration::convertToAppPos(topEnc->getCount()) == appPos) {
printf("Already at pos: %d, not running\n", appPos);
return;
}
target = Calibration::convertToTicks(appPos); // calculate target encoder position
printf("runToAppPos Called, running to %d from %d", target.load(), topEnc->getCount());
printf("runToAppPos Called, running to %d from %d\n", target.load(), topEnc->getCount());
// allow servo position to settle
vTaskDelay(pdMS_TO_TICKS(500));

View File

@@ -20,6 +20,9 @@ void initialSetup() {
}
void setupAndCalibrate() {
gpio_reset_pin(debugLED);
gpio_set_direction(debugLED, GPIO_MODE_OUTPUT);
gpio_set_level(debugLED, 1); // Start with LED off
while (1) {
setupLoop();
if (awaitCalibration) {
@@ -27,6 +30,7 @@ void setupAndCalibrate() {
}
else break;
}
gpio_set_level(debugLED, 0); // Start with LED off
}
void setupLoop() {

View File

@@ -292,8 +292,8 @@ void initSocketIO() {
config.websocket_config.headers = authHeader.c_str();
if (secureSrv) {
config.websocket_config.transport = WEBSOCKET_TRANSPORT_OVER_SSL;
config.websocket_config.crt_bundle_attach = esp_crt_bundle_attach;
config.websocket_config.transport = WEBSOCKET_TRANSPORT_OVER_SSL;
config.websocket_config.crt_bundle_attach = esp_crt_bundle_attach;
}
io_client = esp_socketio_client_init(&config);