123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #include <stdio.h>
- #include <string.h>
- #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 "config.h"
- #include "displayAndSend.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",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 %.4fmV",vpp,vpp*(3300.0f / 4095.0f));
- addDataToQueue(type_VPP,0.0,vpp);
- }
- }
- else if( strcmp(type,"OWT") == 0 ) {
- float t = 0.0f;
- const int no = sscanf(data,"%f",&t);
-
- if( no == 1 ) {
- if( strcmp(id,"28412E7B0D00002A") == 0 ) {
- addDataToQueue(type_TempA,t,0);
- }
- else if( strcmp(id,"28FF22DA551603C3") == 0 ) {
- addDataToQueue(type_TempB,t,0);
- }
- else {
- ESP_LOGE("SERIAL","Unknown OW-ID:%s",id);
- }
- //char js[50];
- //sprintf(js,"{\"id\":\"%s\",\"temp\":%s}",id,data);
- //ESP_LOGI("SERIAL", "%s",js);
- }
- }
- }
- 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
|