serial.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/queue.h"
  6. #include "freertos/task.h"
  7. #include "esp_log.h"
  8. #include "serial.h"
  9. #include "uart.h"
  10. //#include "supervision.h"
  11. //#include "http_client.h"
  12. #include "wifi.h"
  13. #ifdef SERIAL_ENABLED
  14. // Defines
  15. // Global defines
  16. int serialRxCounter = 0;
  17. void serialRxTask(void *pvParameters)
  18. {
  19. const bool LOG_PRINT=false;
  20. int mode = -1; // Force a reset
  21. int reset = 1;
  22. unsigned char value[200];
  23. unsigned char *valP;
  24. ESP_LOGI("SERIAL", "serialRxTask Core:%d\n",xPortGetCoreID());
  25. while(1)
  26. {
  27. while( serialAvailable() ) {
  28. const unsigned char c = serialRead();
  29. //printf("%c",c);
  30. if( mode == 0 && c == '{' ) {
  31. if(LOG_PRINT) printf("Rx start ");
  32. mode = 1;
  33. }
  34. else if( mode == 1 ) {
  35. if( c == '}' ) { // Check for end of value
  36. *(valP) = '\0';
  37. reset = 1;
  38. if(LOG_PRINT) printf("\nData:%s\n",value);
  39. char type[10];
  40. char id[21];
  41. char data[10];
  42. const int no = sscanf((const char *)value,"%9[^,],%20[^,],%9s",type,id,data);
  43. if(LOG_PRINT) printf("No:%d\n",no);
  44. if( no == 3 ) {
  45. if(LOG_PRINT) printf("-%s- -%s- -%s-\n",type,id,data);
  46. if( strcmp(type,"VPP") == 0 ) {
  47. int vpp = 0;
  48. const int no = sscanf(data,"%d",&vpp);
  49. if( no == 1 ) {
  50. ESP_LOGI("SERIAL", "VPP: %d",vpp);
  51. #ifdef MQTT_ENABLED
  52. sendMQTTMessage("/sensors/energy/waterHeater", data);
  53. #endif
  54. }
  55. }
  56. else if( strcmp(type,"OWT") == 0 ) {
  57. float t = 0.0f;
  58. const int no = sscanf(data,"%f",&t);
  59. if( no == 1 ) {
  60. char js[50];
  61. sprintf(js,"{\"id\":\"%s\",\"temp\":%s}",id,data);
  62. ESP_LOGI("SERIAL", "%s",js);
  63. #ifdef MQTT_ENABLED
  64. sendMQTTMessage("/sensors/energy/owTempSensor", js);
  65. #endif
  66. }
  67. }
  68. }
  69. else {
  70. ESP_LOGE("SERIAL", "ERROR 2");
  71. }
  72. }
  73. else if( c < ',' || c > 'Z') {
  74. ESP_LOGE("SERIAL", "ERROR 3");
  75. reset = 1;
  76. }
  77. else {
  78. *(valP++) = c;
  79. }
  80. }
  81. if( reset == 1 ) {
  82. serialRxCounter++;
  83. valP = value;
  84. value[0]='\0';
  85. mode = 0;
  86. reset = 0;
  87. }
  88. }
  89. vTaskDelay(100 / portTICK_PERIOD_MS);
  90. }
  91. vTaskDelete(NULL);
  92. }
  93. #endif