serial.c 2.5 KB

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