receiver.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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>",data);
  51. #ifdef WIFI_ENABLED
  52. sendUDPMessage(dataStr, true);
  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>",data);
  62. #ifdef WIFI_ENABLED
  63. sendUDPMessage(dataStr, true);
  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>%u",data,startPulseWidth);
  71. #ifdef WIFI_ENABLED
  72. sendUDPMessage(dataStr, false);
  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. // RR2A1904CE 0000 906600 F0 = 006.690 = 6.69 = 109,62903 cm2
  84. // Sequence: 563412
  85. // 1 cubic mm of water = 0,001 gram
  86. // 7 grams of water = 7000 mm2
  87. // Area of the rain sensor is 7697.69 sq.mm. It flips every ~7grams of water
  88. // Every flip is therefore 0.90936402542048 mm or rain
  89. // The value that is reported is in 1/1000 cubic inches of water.
  90. // Multiply with 16387 / 1000 to get cm2
  91. double total = 100 * (ch_data_p[4+12]-'0') +
  92. 10 * (ch_data_p[5+12]-'0') +
  93. (ch_data_p[2+12]-'0') +
  94. 0.1 * (ch_data_p[3+12]-'0') +
  95. 0.01 * (ch_data_p[0+12]-'0') +
  96. 0.001 * (ch_data_p[1+12]-'0');
  97. total = (total * 16387) / 1000; // Convert to cubic centimeters
  98. char ch_data[21];
  99. strncpy(ch_data,ch_data_p, sizeof(ch_data));
  100. ESP_LOGI("RX", "OREGON: <RR%s> %6.4f cm2", ch_data, total );
  101. #ifdef WIFI_ENABLED
  102. sprintf(dataStr,"<RR%s>",ch_data);
  103. sendUDPMessage(dataStr, true);
  104. #endif
  105. Oregon_ResetDecoder();
  106. }
  107. // ESIC (Fridge / Freezer Sensors)
  108. data = nextPulseESICSensor(width);
  109. if( data != -1 ) {
  110. int32_t value = (data & 0xFFFE) >> 1;
  111. value = (value - 6400) * 10 / 128;
  112. int32_t id = (data>>12) & 0x007;
  113. ESP_LOGI("RX", "ESIC: <TE%08llX> %u %4.1f",data,id, (float)value/10);
  114. sprintf(dataStr,"<TE%08llX>",data);
  115. #ifdef WIFI_ENABLED
  116. sendUDPMessage(dataStr, true);
  117. #endif
  118. }
  119. }
  120. }
  121. vTaskDelete(NULL);
  122. }
  123. void initReceiver() {
  124. Oregon_ResetDecoder();
  125. ClasO_ResetDecoder();
  126. ProovesmartSensor_ResetDecoder();
  127. ESIC_ResetDecoder();
  128. ESP_LOGI("RX", "Receiver has been initialized.");
  129. xTaskCreatePinnedToCore(&receiverTask, "receiverTask", 8192, NULL, tskIDLE_PRIORITY + 5, NULL, 0);
  130. }