serial.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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",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 %.4fmV",vpp,vpp*(3300.0f / 4095.0f));
  52. addDataToQueue(type_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,"28412E7B0D00002A") == 0 ) {
  60. addDataToQueue(type_TempA,t,0);
  61. }
  62. else if( strcmp(id,"28FF22DA551603C3") == 0 ) {
  63. addDataToQueue(type_TempB,t,0);
  64. }
  65. else {
  66. ESP_LOGE("SERIAL","Unknown OW-ID:%s",id);
  67. }
  68. //char js[50];
  69. //sprintf(js,"{\"id\":\"%s\",\"temp\":%s}",id,data);
  70. //ESP_LOGI("SERIAL", "%s",js);
  71. }
  72. }
  73. }
  74. else {
  75. ESP_LOGE("SERIAL", "ERROR 2");
  76. }
  77. }
  78. else if( c < ',' || c > 'Z') {
  79. ESP_LOGE("SERIAL", "ERROR 3");
  80. reset = 1;
  81. }
  82. else {
  83. *(valP++) = c;
  84. }
  85. }
  86. if( reset == 1 ) {
  87. serialRxCounter++;
  88. valP = value;
  89. value[0]='\0';
  90. mode = 0;
  91. reset = 0;
  92. }
  93. }
  94. vTaskDelay(100 / portTICK_PERIOD_MS);
  95. }
  96. vTaskDelete(NULL);
  97. }
  98. void initSerial() {
  99. initUart();
  100. xTaskCreate(serialRxTask, "Serial_RX_Task", 10000, NULL, 10, NULL);
  101. }
  102. #endif