Testing Round1 - must figure out balance between PM versus plugged-in mode? also must test bms
This commit is contained in:
32
src/BLE.cpp
32
src/BLE.cpp
@@ -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) {
|
||||
|
||||
32
src/WiFi.cpp
32
src/WiFi.cpp
@@ -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");
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user