#include "nx/software/SystemSettings.h" #include "nx/software/SystemSettingsApi.h" #include "nx/software/AppSettings.h" #include "nx/software/AppSettingsApi.h" #include "freertos/FreeRTOS.h" #include "esp_system.h" #include "nx/firmware/Storage.h" #include "nx/firmware/Wifi.h" #include "nx/firmware/MqTriggerHttpServer.h" #include "nx/firmware/MqttClient.h" #include "nx/firmware/SntpClient.h" #include "HttpHandlers.h" #include "esp_log.h" #include #define TAG "MAIN" #define SNTP_RETRIES 5 static const char* DEVICE_NAME_PREFIX = "mqtrigger-"; extern const char ca_crt_start[] asm("_binary_ca_crt_start"); static void startWifi(void); static void onWifiConnected(void); static void onAppSettingsUpdate(void); static void onMqttMessage(const char* msg); static SystemSettings* systemSettings = NULL; static AppSettings* appSettings = NULL; static WifiSettings wifiSettings; static MqttSettings mqttSettings; static const MqTriggerHttpCallbacks httpCallbacks = { .getRoot = rootHandler, .getJs = jsHandler, .getCss = cssHandler, .getSysSetForm = sysFormHandler, .getAppSetForm = appFormHandler, .getSysSet = nxApiGetSystemSettings, .postSysSet = nxApiUpdateSystemSettings, .getAppSet = nxApiGetAppSettings, .postAppSet = nxApiUpdateAppSettings // .postCmd = nxApiHandleLedCmd }; void app_main(void) { nxInitStorage(); nxInitSystemSettings(nxStorageWrite, nxStorageRead); nxInitAppSettings(nxStorageWrite, nxStorageRead, onAppSettingsUpdate); systemSettings = nxGetSystemSettings(); appSettings = nxGetAppSettings(); nxSetWifiConnectedCallback(onWifiConnected); startWifi(); } // ----- static void startWifi(void) { strcpy(systemSettings->wifiSsid, "cintra"); strcpy(systemSettings->wifiPassword, "fffefdfcfb"); systemSettings->useStaticAddr = true; systemSettings->ip4addr[3] = 91; wifiSettings = (struct WifiSettings){ .wname = systemSettings->wifiSsid, .wpass = systemSettings->wifiPassword, .devicePrefix = DEVICE_NAME_PREFIX, .usePowerSave = &(systemSettings->wifiPowerSave), .useStaticAddr = &(systemSettings->useStaticAddr), .ip4addr = systemSettings->ip4addr, .ip4gw = systemSettings->ip4gw, .ip4mask = systemSettings->ip4mask, .dns = systemSettings->dnsAddr }; ESP_LOGI(TAG, "Initializing WiFi"); nxInitWifi(&wifiSettings); } static void onWifiConnected(void) { nxInitSntpClient(SNTP_RETRIES, systemSettings->sntpAddr, systemSettings->tzEnv); strcpy(mqttSettings.brokerAddr, appSettings->mqttHost); strcpy(mqttSettings.apiTopic, appSettings->mqttApiUri); strcpy(mqttSettings.hbTopic, appSettings->mqttHbUri); strcpy(mqttSettings.user, appSettings->mqttUser); strcpy(mqttSettings.password, appSettings->mqttPassword); mqttSettings.hbIntervalSec = appSettings->mqttHbIntervalSec; mqttSettings.caCrt = appSettings->mqttUseTls ? ca_crt_start : NULL; mqttSettings.messageCb = onMqttMessage; nxStartMqttClient(&mqttSettings); nxSetMqTriggerHttpCallbacks(&httpCallbacks); nxStartMqTriggerHttpServer(); } static void onAppSettingsUpdate(void) { ESP_LOGI(TAG, "App settings updated"); } static void onMqttMessage(const char* msg) { ESP_LOGI(TAG, "MQTT MESSAGE RECEIVED: %s", msg); }