/// © MiroZ 2024 #include "App.h" #include #include "Settings.h" #include "errors.h" #include "Ota.h" #include "ProvisionSoftAP.h" #define LED_PIN 26 static const char * TAG = "app"; App::App() { } Led * App::getLed() { return m_led; } CircBuffer * App::getBuffer() { return & m_circ_buffer; } CommandProcessor * App::getCommandProcessor() { return m_commandProcessor; } void App::init() { #pragma message(WH_VERSION) ESP_LOGW(TAG, "Starting the app..."); uint8_t mac[8]; esp_read_mac(mac, ESP_MAC_WIFI_STA); //Lets report all programmed parameters ESP_LOGI(TAG, "mqtt_id: %s, wifi mac: %02x:%02x:%02x:%02x:%02x:%02x well_id: %u, group_id: %u", SETTINGS.mqtt.device_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], SETTINGS.device.tata_id, SETTINGS.device.group_id); m_led = new Led(LED_PIN); m_wifi = new Wifi(); m_commandProcessor = new CommandProcessor(*this); m_ble_service = new BleService(*this); m_ble_service->start(); bool needs_provision = true; #if 0 ESP_LOGI(TAG, "Scanning wifi networks..."); int num_networks = WiFi.scanNetworks(false, false, false, 0); for(int n = 0; n < num_networks; n++) { ESP_LOGI(TAG, "%s, %d", WiFi.SSID(n).c_str(), WiFi.RSSI(n)); } #endif if(SETTINGS.wifi.num > 0) { // try connecting m_led->setColor(0, 255, 0); m_wifi->start(); Wifi::WIFI_STATUS wifi_status = m_wifi->waitForConnection(); if(wifi_status == Wifi::WIFI_STATUS::CONNECTED) { ESP_LOGI(TAG, "Getting local time..."); struct tm timeinfo; int tries = 5; while (tries--) { configTime(0, 0, "pool.ntp.org"); // needed? if(getLocalTime(&timeinfo)) { ESP_LOGI(TAG, "pool.ntp.org ok"); break; } else ESP_LOGE(TAG, "pool.ntp.org Failed"); } needs_provision = false; } else { ESP_LOGI(TAG, "Provisioned networks failed, trying fallback..."); // Don't pause yet - let it try the fallback // The modified startConnecting() will attempt CBX_IoT fallback // Wait a bit more for the fallback attempt delay(5000); wifi_status = m_wifi->status(); if(wifi_status == Wifi::WIFI_STATUS::CONNECTED) { ESP_LOGI(TAG, "Connected via fallback network"); ESP_LOGI(TAG, "Getting local time..."); struct tm timeinfo; int tries = 5; while (tries--) { configTime(0, 0, "pool.ntp.org"); // needed? if(getLocalTime(&timeinfo)) { ESP_LOGI(TAG, "pool.ntp.org ok"); break; } else ESP_LOGE(TAG, "pool.ntp.org Failed"); } needs_provision = false; } else { ESP_LOGI(TAG, "Fallback also failed, entering provisioning mode"); m_wifi->pause(true); } } } else { // No provisioned networks - try fallback directly ESP_LOGI(TAG, "No provisioned networks, trying fallback..."); m_led->setColor(0, 255, 0); m_wifi->start(); Wifi::WIFI_STATUS wifi_status = m_wifi->waitForConnection(); if(wifi_status == Wifi::WIFI_STATUS::CONNECTED) { ESP_LOGI(TAG, "Connected to fallback network"); ESP_LOGI(TAG, "Getting local time..."); struct tm timeinfo; int tries = 5; while (tries--) { configTime(0, 0, "pool.ntp.org"); // needed? if(getLocalTime(&timeinfo)) { ESP_LOGI(TAG, "pool.ntp.org ok"); break; } else ESP_LOGE(TAG, "pool.ntp.org Failed"); } needs_provision = false; } else { ESP_LOGI(TAG, "Fallback failed, entering provisioning mode"); m_wifi->pause(true); } } if(needs_provision) { m_led->setPulse(0, 0, 255); ProvisionSoftAP provision(80); provision.start(); } otaCheck(); // m_led->setPulse(255, 0, 255); m_led->allOff(); m_mqtt_service = new MqttService(*this); m_mqtt_service->start(); m_sensor_service = new SensorService(*this); m_sensor_service->start(); } void App::otaCheck() { Ota ota(*this); ota.start(); } void App::start() { } void App::readSensors() { } void App::reportSensors() { }