wireless_temp_sensor_receiver.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Configure which sensors to compile/use
  2. #define ENABLE_CLAS_O_RX
  3. #define ENABLE_TELLDUS_RX
  4. // ---- Declare the functions and variables needed for the Clas O-namespace ----
  5. #ifdef ENABLE_CLAS_O_RX
  6. namespace clas_o {
  7. extern uint32_t x_data;
  8. boolean nextPulse (uint16_t width);
  9. void resetDecoder();
  10. }
  11. #endif
  12. // ---- Declare the functions and variables needed for the Telldus-namespace ----
  13. #ifdef ENABLE_TELLDUS_RX
  14. namespace telldus {
  15. extern uint64_t x_data;
  16. extern uint32_t meanPulseWidth;
  17. boolean nextPulse (uint16_t width);
  18. void resetDecoder();
  19. }
  20. #endif
  21. // Define which pins to use for different tasks
  22. const int rxPinA = 7; // 433MHz RX, Connected to pin 7
  23. const int ledPin = 17; // The RX LED has a defined Arduino pin
  24. // This code is only run once at startup
  25. void setup()
  26. {
  27. // Setup mode of each pin that we will use
  28. pinMode(ledPin, OUTPUT); // Set LED-pin as an output
  29. pinMode(rxPinA, INPUT);
  30. // Call the function that configures the interrupts
  31. cli(); // Clear interrupt
  32. setup_timer_interrupt();
  33. sei(); // Enable interrupt
  34. // Setup the different serial communication channels
  35. Serial.begin(115200); // Serial monitor (Over USB, when debugging with Arduino)
  36. Serial1.begin(9600); // HW-UART (To Raspberry)
  37. }
  38. // Convert from 12-bit unsigned data to 32-bit signed data
  39. static int32_t convertToSignedTemp(uint16_t value)
  40. {
  41. return ( (value >> 11) == 0 )? value : ((-1 ^ 0xFFF) | value);
  42. }
  43. // This code is called over and over again
  44. void loop()
  45. {
  46. static uint16_t width; // Stores the length of the received pulse
  47. static unsigned long totalRx = 0; // The total number of received pulses
  48. char str[100];
  49. const uint32_t now = millis(); // Time in mS
  50. if ( rcvDeQueue(&width) ) {
  51. totalRx++; // Increase the total received pulses
  52. #ifdef ENABLE_CLAS_O_RX
  53. if( clas_o::nextPulse(width) ) {
  54. static uint32_t old_rx_time = 0;
  55. static uint32_t old_x_data = 0;
  56. if( (now > (old_rx_time + 500)) || (old_x_data != clas_o::x_data) ) {
  57. const int16_t value = convertToSignedTemp( clas_o::x_data & 0xFFF ); // Temperature
  58. const uint16_t id = (clas_o::x_data>>12) & 0x007; // Id
  59. sprintf(str,"<TR:%u:%d:0>\n",id,value);
  60. Serial.print(str);
  61. old_x_data = clas_o::x_data;
  62. }
  63. clas_o::resetDecoder();
  64. old_rx_time = now;
  65. }
  66. #endif
  67. #ifdef ENABLE_TELLDUS_RX
  68. if( telldus::nextPulse(width) ) {
  69. const int16_t value = convertToSignedTemp( (telldus::x_data & 0x00FFF0000) >> 16 ); // Temperature
  70. const int16_t value2 = (telldus::x_data & 0x00000FF00) >> 8; // Moist
  71. const uint8_t id = (telldus::x_data & 0xFF0000000) >> 28; // Id
  72. sprintf(str,"<Tr:%u:%d:%d>\n",id,value,value2);
  73. //sprintf(str,"Id:%u Temp:%d.%d Mean:%u\n",id,value/10,value%10,telldus::meanPulseWidth);
  74. Serial.print(str);
  75. telldus::resetDecoder();
  76. }
  77. #endif
  78. }
  79. }