receiver.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "esp_sleep.h"
  9. #include "esp_log.h"
  10. #include "driver/uart.h"
  11. #include "driver/rtc_io.h"
  12. #include "esp_intr_alloc.h"
  13. #include "esp_attr.h"
  14. #include "driver/timer.h"
  15. #include <stddef.h>
  16. #include "esp_intr_alloc.h"
  17. #include "receiver.h"
  18. #include "transceiver.h"
  19. #include "rxTimer.h"
  20. #include "Sensors/ClasOSensor.h"
  21. #include "Sensors/proovesmartSensor.h"
  22. #include "Sensors/nexa.h"
  23. #include "Sensors/oregon.h"
  24. #include "Sensors/esic.h"
  25. #include "led.h"
  26. #include "udp_client.h"
  27. #include "wifi.h"
  28. uint32_t dataArr[1000];
  29. uint32_t dataArrIdx = 0;
  30. static int convertToSignedTemp(unsigned int value)
  31. {
  32. //int signValue;
  33. return ( (value >> 11) == 0 )? value : ((-1 ^ 0xFFF) | value);
  34. }
  35. void receiverTask(void *pvParameter)
  36. {
  37. ESP_LOGI("RX", "Receiver task starting.");
  38. uint32_t width;
  39. char dataStr[UDP_QUEUE_OBJ_LENGTH];
  40. while (1) {
  41. while( xQueueReceive( rxQueue, &width, portMAX_DELAY ) == pdTRUE ) {
  42. dataArr[dataArrIdx++] = width;
  43. if( dataArrIdx == 1000 ) dataArrIdx=0;
  44. // Receive the Clas Ohlsson Sensors
  45. int64_t data = nextPulseClasOSensor(width);
  46. if( data != -1 ) {
  47. int value = convertToSignedTemp( data & 0xFFF );
  48. unsigned char id = (data>>12) & 0x007;
  49. ESP_LOGI("RX", "ClasO: <TR%08llX> %u %4.1f",data,id, (float)value/10);
  50. sprintf(dataStr,"<TR%08llX>\n",data);
  51. #ifdef WIFI_ENABLED
  52. sendUDPMessage(dataStr);
  53. #endif
  54. }
  55. // Receive the Telldus / Proovesmart Sensors
  56. data = nextPulseProovesmartSensor(width);
  57. if( data != -1 ) {
  58. const int value = (data & 0x00FFF0000) >> 16;
  59. const int id = (data & 0xFF0000000) >> 28;
  60. ESP_LOGI("RX", "Proove: <Tr%016llX> ID:%d %4.1f",data,id,(float)value/10);
  61. sprintf(dataStr,"<Tr%016llX>\n",data);
  62. #ifdef WIFI_ENABLED
  63. sendUDPMessage(dataStr);
  64. #endif
  65. }
  66. // Receive the NEXA Sensors
  67. data = nextPulseNEXA(width);
  68. if( data != -1 ) {
  69. ESP_LOGI("RX", "NEXA: <NR%08llX>",data);
  70. sprintf(dataStr,"<NR%08llX>\n",data);
  71. #ifdef WIFI_ENABLED
  72. sendUDPMessage(dataStr);
  73. #endif
  74. }
  75. // Receive the Oregon (Rain) Sensor
  76. char *ch_data_p = nextPulseOregonSensor(width);
  77. if( ch_data_p != NULL ) {
  78. // Nibbles 17..12 = Total Rain = LSD is 0.001 inch (0,0254 mm)
  79. // 0 1 2 3 4 5 6 7 8 9 Rate Total
  80. // RR2A1904CE 0000 601600 70 = 001.660 0000 001660
  81. // RR2A1904CE 2001 702000 60 = 002.070 = 0,41 0120 002070
  82. // RR2A1904CE 0001 902400 A0 = 002.490 = 0,42 002490
  83. // Sequence: 563412
  84. // 1 cubic mm of water = 0,001 gram
  85. // 7 grams of water = 7000 mm2
  86. // Area of the rain sensor is 7697.69 sq.mm. It flips every ~7grams of water
  87. // Every flip is therefore 0.90936402542048 mm or rain
  88. // The value that is reported is in 1/1000 cubic inches of water.
  89. // Multiply with 16387 / 1000 to get cm2
  90. double total = 100 * (ch_data_p[4+12]-'0') +
  91. 10 * (ch_data_p[5+12]-'0') +
  92. (ch_data_p[2+12]-'0') +
  93. 0.1 * (ch_data_p[3+12]-'0') +
  94. 0.01 * (ch_data_p[0+12]-'0') +
  95. 0.001 * (ch_data_p[1+12]-'0');
  96. total = (total * 16387) / 1000;
  97. char ch_data[21];
  98. strncpy(ch_data,ch_data_p, sizeof(ch_data));
  99. ESP_LOGI("RX", "OREGON: <RR%s> %6.4f cm2", ch_data, total );
  100. #ifdef WIFI_ENABLED
  101. sprintf(dataStr,"<RR%s>\n",ch_data);
  102. sendUDPMessage(dataStr);
  103. #endif
  104. Oregon_ResetDecoder();
  105. }
  106. // ESIC (Fridge / Freezer Sensors)
  107. if( nextPulseESICSensor(width) ) {
  108. ESP_LOGE("RX", "NO WAY");
  109. ESIC_ResetDecoder();
  110. blinkTheLED();
  111. }
  112. }
  113. }
  114. vTaskDelete(NULL);
  115. }
  116. void initReceiver() {
  117. Oregon_ResetDecoder();
  118. ClasO_ResetDecoder();
  119. ProovesmartSensor_ResetDecoder();
  120. ESP_LOGI("RX", "Receiver has been initialized.");
  121. xTaskCreatePinnedToCore(&receiverTask, "receiverTask", 8192, NULL, tskIDLE_PRIORITY + 5, NULL, 0);
  122. }