receiver.c 4.3 KB

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