@ -45,6 +45,9 @@ static AppSettings* appSettings = NULL;
static WifiSettings wifiSettings ;
static MqttSettings mqttSettings ;
static bool wifiStarted = false ;
static bool mqttStarted = false ;
static const MqTriggerHttpCallbacks httpCallbacks = {
. getRoot = rootHandler ,
. getJs = jsHandler ,
@ -66,12 +69,22 @@ void app_main(void)
nxInitStorage ( ) ;
// uncomment to force default NVS initialization for development
// uint8_t zero = 0;
// nxStorageWrite("ledinit", &zero, 1);
// nxStorageWrite("sysinit", &zero, 1);
nxInitSystemSettings ( nxStorageWrite , nxStorageRead ) ;
nxInitAppSettings ( nxStorageWrite , nxStorageRead , onAppSettingsUpdate ) ;
systemSettings = nxGetSystemSettings ( ) ;
appSettings = nxGetAppSettings ( ) ;
// uncomment to force WiFi settings for development
// strcpy(systemSettings->wifiSsid, "androidAp");
// strcpy(systemSettings->wifiPassword, "password");
// systemSettings->useStaticAddr = false;
nxStartRestarter ( systemSettings - > rsSchedule , systemSettings - > tzEnv ) ;
nxSetWifiConnectedCallback ( onWifiConnected ) ;
@ -83,9 +96,6 @@ void app_main(void)
static void startWifi ( void )
{
systemSettings - > useStaticAddr = true ;
systemSettings - > ip4addr [ 3 ] = 91 ;
wifiSettings = ( struct WifiSettings ) {
. wname = systemSettings - > wifiSsid ,
. wpass = systemSettings - > wifiPassword ,
@ -106,6 +116,13 @@ static void startWifi(void)
static void onWifiConnected ( void )
{
nxUpdateStatus ( STATUS_OK ) ;
if ( wifiStarted ) {
return ;
}
wifiStarted = true ;
systemSettings - > deviceName = nxGetWifiDeviceName ( ) ;
nxInitSntpClient ( SNTP_RETRIES , systemSettings - > sntpAddr , systemSettings - > tzEnv ) ;
@ -127,7 +144,9 @@ static void onWifiConnected(void)
mqttSettings . disconnectedCb = onMqttDisconnected ;
mqttSettings . errorCb = onMqttError ;
nxStartMqttClient ( & mqttSettings ) ;
if ( ! nxStartMqttClient ( & mqttSettings ) ) {
onMqttError ( ) ;
}
nxSetMqTriggerHttpCallbacks ( & httpCallbacks ) ;
nxStartMqTriggerHttpServer ( ) ;
@ -154,8 +173,14 @@ static void onWifiError()
static void onMqttConnected ( )
{
ESP_LOGI ( TAG , " MQTT CONNECTED " ) ;
if ( nxMqttSubscribe ( appSettings - > mqttApiUri , API_QOS ) ) {
if ( ! mqttStarted ) {
xTaskCreate ( & hbTask , " hb_task " , 2048 , NULL , 1 , NULL ) ;
mqttStarted = true ;
}
// (re)subscribe
if ( nxMqttSubscribe ( appSettings - > mqttApiUri , API_QOS ) ) {
nxUpdateStatus ( STATUS_OK ) ;
}
}
@ -186,16 +211,23 @@ static void hbTask(void* param)
}
while ( 1 ) {
ESP_LOGI ( TAG , " Sending MQTT heartbeat " ) ;
char timeStr [ DT_FORMAT_LEN ] ;
nxGetTimeStr ( timeStr ) ;
char hbMessage [ DT_FORMAT_LEN + strlen ( nxGetWifiDeviceName ( ) ) + 2 ] ;
strcpy ( hbMessage , nxGetWifiDeviceName ( ) ) ;
strcat ( hbMessage , " " ) ;
strcat ( hbMessage , timeStr ) ;
nxMqttPublish ( appSettings - > mqttHbUri , HB_QOS , hbMessage , strlen ( hbMessage ) , 1 ) ;
if ( nxMqttIsConnected ( ) ) {
ESP_LOGI ( TAG , " Sending MQTT heartbeat " ) ;
char timeStr [ DT_FORMAT_LEN ] ;
nxGetTimeStr ( timeStr ) ;
char hbMessage [ DT_FORMAT_LEN + strlen ( nxGetWifiDeviceName ( ) ) + 2 ] ;
strcpy ( hbMessage , nxGetWifiDeviceName ( ) ) ;
strcat ( hbMessage , " " ) ;
strcat ( hbMessage , timeStr ) ;
if ( nxMqttPublish ( appSettings - > mqttHbUri , HB_QOS , hbMessage , strlen ( hbMessage ) , 1 ) < 0 ) {
ESP_LOGE ( TAG , " Cannot publish heartbeat message " ) ;
}
}
else {
ESP_LOGW ( TAG , " Skipping MQTT heartbeat due to the disconnected client " ) ;
}
vTaskDelay ( appSettings - > mqttHbIntervalSec * 1000 / portTICK_PERIOD_MS ) ;
}