repeater.ino 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "queue.h"
  2. boolean disableRx = true;
  3. const int rxPinA = 7; // RX, Connected to pin 7
  4. const int enablePin = 4; // Enable, Connected to RTX pin 6: 0=Power down, 1=Active
  5. const int txEnablePin = 5; // rx/TX, Connected to RTX pin 5: 0=Rx, 1=Tx
  6. const int txPin = 6; // TX, Connected to RTX pin 4
  7. //********************************************************************
  8. // SETUP
  9. //********************************************************************
  10. void setup() {
  11. disableRx = true;
  12. pinMode(rxPinA, INPUT);
  13. pinMode(enablePin, OUTPUT);
  14. pinMode(txEnablePin, OUTPUT);
  15. pinMode(txPin, OUTPUT);
  16. digitalWrite(enablePin, LOW);
  17. digitalWrite(txEnablePin, LOW);
  18. digitalWrite(txPin, LOW);
  19. // ------------------ INIT THE TRANCEIVER -------------------------
  20. // From powerdown mode (pin 4-5-6 low),
  21. // 1. Drive high pin 6 (ENABLE)
  22. // 2. After 20us drive high pin 5 (RX/TX)200us, hold on 40us
  23. // 3. Drive down 20us pin 6 (ENABLE).
  24. digitalWrite(enablePin, HIGH); delayMicroseconds( 20 );
  25. digitalWrite(txEnablePin, HIGH); delayMicroseconds( 200 ); digitalWrite(txEnablePin, LOW);
  26. delayMicroseconds( 40 );
  27. digitalWrite(enablePin, LOW); delayMicroseconds( 20 ); digitalWrite(enablePin, HIGH);
  28. delayMicroseconds( 200 );
  29. // ------------------ INIT THE TRANCEIVER -------------------------
  30. Serial.begin(9600); // USB Serial
  31. initLuxSensor(); // Init the lux sensor on i2c
  32. rcvInitQueue();
  33. cli(); // Clear interrupt
  34. setup_timer_interrupt();
  35. sei(); // Enable interrupt
  36. disableRx = false;
  37. }
  38. //********************************************************************
  39. // INTERRUPT FUNCTIONS
  40. //********************************************************************
  41. void rxInterrupt() {
  42. static unsigned char currValue = 0;
  43. static unsigned short samples = 0;
  44. static unsigned short newSamples = 0;
  45. //return; // FOR DEBUG, Disables nexa receive
  46. // Sample the pin value
  47. unsigned char value = digitalRead(rxPinA);
  48. if( value == currValue ) {
  49. samples++;
  50. samples+=newSamples;
  51. newSamples=0;
  52. }
  53. else {
  54. newSamples++;
  55. }
  56. if( newSamples == 3 ) {
  57. rcvEnQueue(samples * 20);
  58. samples = newSamples;
  59. newSamples = 0;
  60. currValue = value;
  61. }
  62. }
  63. void stopInterrupts() {
  64. EIMSK &= 0xFC; // Clear bit 1:0 in Enable Interrupt register
  65. }
  66. //********************************************************************
  67. // LOOP
  68. //
  69. // Test-code: <NT1B5C1D83>
  70. //********************************************************************
  71. #define SER_RX_MODE_WAIT 0
  72. #define SER_RX_MODE_RCV 1
  73. #define SER_RX_MODE_END 2
  74. extern unsigned long x_data; // Receive data from Nexa
  75. extern unsigned long temp1_x_data; // Receive data from temp1
  76. void loopB() {
  77. uint32_t lux = readLuxSensor();
  78. Serial.println(lux);
  79. delay(1000);
  80. }
  81. void loop() {
  82. static int rxMode = SER_RX_MODE_WAIT;
  83. static int rxNum = 0;
  84. //static unsigned char rxChars[20];
  85. static unsigned long last_rx_time = 0;
  86. static unsigned char txDataReady = 0;
  87. static unsigned long previous_nexa_x_data = 0;
  88. static unsigned long nexa_old_time=0;
  89. static unsigned long previous_temp1_x_data = 0;
  90. static unsigned long temp1_old_time=0;
  91. //static char hex[100];
  92. static boolean firstRun = true;
  93. static unsigned int width;
  94. static unsigned long txCode = 0;
  95. static unsigned long previous_lux_time = 20000; // Start 1st measure after 20 sec
  96. static unsigned long previous_now = 0; // Get the current time
  97. unsigned long now = millis(); // Get the current time
  98. // Check millis() for wrapping (after 49 days)
  99. if( previous_now > now ) {
  100. last_rx_time = 0; // Reset last rx time
  101. previous_lux_time = 0;
  102. }
  103. // Display version information 10 sec after startup
  104. if( firstRun == true && now > 10000 ) {
  105. Serial.println("433MHz Repeater module. (Arduino nano) Ver: 170211 14:46"); // **** VERSION ****
  106. firstRun = false;
  107. }
  108. if( now > previous_lux_time ) {
  109. // Yes ! It's time to measure lux value
  110. int32_t lux = readLuxSensor();
  111. if( lux > 0xFFFF ) lux=0xFFFF;
  112. if( lux < 0 ) lux=0;
  113. //Serial.println(lux);
  114. // Set next measure to 3 minutes +/- 20 sec
  115. previous_lux_time = now + 170000 + (random(20)*1000);
  116. txDataReady = 1;
  117. txCode = 0x6AD50000;
  118. txCode |= (lux & 0x0000FFFF);
  119. blinkTheLED();
  120. }
  121. if( rcvDeQueue(&width) ) {
  122. // ********** NEXA ***********
  123. if( nextPulseNexa(width) ) {
  124. last_rx_time = now; // Set last rx time to now
  125. if( x_data != previous_nexa_x_data || now > (nexa_old_time+1000) ) {
  126. unsigned long remote_id = (x_data & 0xFFFFFFC0) >>6;
  127. int group = (x_data & 0x00000020) >>5;
  128. int onOff = (x_data & 0x00000010) >>4;
  129. int channel = (x_data & 0x0000000C) >>2;
  130. int button = (x_data & 0x00000003);
  131. Serial.println("Nexa");
  132. if( remote_id == 11067169UL && txDataReady == 0 ) {
  133. remote_id++;
  134. txDataReady = 1;
  135. txCode = ((remote_id << 6) & 0xFFFFFFC0 );
  136. txCode |= ((group << 5) & 0x00000020 );
  137. txCode |= ((onOff << 4) & 0x00000010 );
  138. txCode |= ((channel << 2) & 0x0000000C );
  139. txCode |= ((button ) & 0x00000003 );
  140. //Serial.println(remote_id);
  141. //Serial.println(onOff);
  142. }
  143. previous_nexa_x_data = x_data;
  144. }
  145. nexa_old_time = now;
  146. resetDecoder();
  147. blinkTheLED();
  148. }
  149. }
  150. if( txDataReady == 1 && (now > (last_rx_time+200)) ) { // Transmit if it has passed more than 100mS since last rx
  151. txDataReady = 0;
  152. sendNexaCode(txCode);
  153. }
  154. previous_now = now; // Store to check for wrapping of millis()
  155. }