#include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/queue.h" #include "freertos/task.h" #include "esp_log.h" #include "serial.h" #include "uart.h" //#include "supervision.h" //#include "http_client.h" #include "wifi.h" #ifdef SERIAL_ENABLED // Global defines int serialRxCounter = 0; void serialRxTask(void *pvParameters) { const bool LOG_PRINT=false; int mode = -1; // Force a reset int reset = 1; unsigned char value[200]; unsigned char *valP; ESP_LOGI("SERIAL", "serialRxTask starting. Core:%d\n",xPortGetCoreID()); while(1) { while( serialAvailable() ) { const unsigned char c = serialRead(); //printf("%c",c); if( mode == 0 && c == '{' ) { if(LOG_PRINT) printf("Rx start "); mode = 1; } else if( mode == 1 ) { if( c == '}' ) { // Check for end of value *(valP) = '\0'; reset = 1; if(LOG_PRINT) printf("\nData:%s\n",value); char type[10]; char id[21]; char data[10]; const int no = sscanf((const char *)value,"%9[^,],%20[^,],%9s",type,id,data); if(LOG_PRINT) printf("No:%d\n",no); if( no == 3 ) { if(LOG_PRINT) printf("-%s- -%s- -%s-\n",type,id,data); if( strcmp(type,"VPP") == 0 ) { int vpp = 0; const int no = sscanf(data,"%d",&vpp); if( no == 1 ) { ESP_LOGI("SERIAL", "VPP: %d",vpp); #ifdef MQTT_ENABLED sendMQTTMessage("/sensors/energy/waterHeater", data); #endif } } else if( strcmp(type,"OWT") == 0 ) { float t = 0.0f; const int no = sscanf(data,"%f",&t); if( no == 1 ) { char js[50]; sprintf(js,"{\"id\":\"%s\",\"temp\":%s}",id,data); ESP_LOGI("SERIAL", "%s",js); #ifdef MQTT_ENABLED sendMQTTMessage("/sensors/energy/owTempSensor", js); #endif } } } else { ESP_LOGE("SERIAL", "ERROR 2"); } } else if( c < ',' || c > 'Z') { ESP_LOGE("SERIAL", "ERROR 3"); reset = 1; } else { *(valP++) = c; } } if( reset == 1 ) { serialRxCounter++; valP = value; value[0]='\0'; mode = 0; reset = 0; } } vTaskDelay(100 / portTICK_PERIOD_MS); } vTaskDelete(NULL); } void initSerial() { initUart(); xTaskCreate(serialRxTask, "Serial_RX_Task", 10000, NULL, 10, NULL); } #endif