BayEOSArduino Library
RF24RouterBoard.h
1 /*
2  * This is a header file, designed to make scetches of Low Current Board easier
3  *
4  * It depends on some conventions:
5  * 1. bayeos.client must be named "client"
6  * 2. Dallas is called "ds"
7  * 3. Changes in CONSTANTS must be declared before include
8  */
9 
10 #include <Sleep.h>
11 #include <RTClib.h>
12 #include <BayEOSBufferSPIFlash.h>
13 #include <RF24.h>
14 RF24 radio(9, 10);
15 uint16_t rx1_count, rx1_error;
16 unsigned long rf24_switchon_time, rf24_switchoff_time;
17 
18 #ifdef NRF24_2CHANNEL
19 RF24 radio2(A0, A1);
20 uint16_t rx2_count, rx2_error;
21 #endif
22 
23 #ifdef NRF24_3CHANNEL
24 RF24 radio3(A2, A3);
25 uint16_t rx3_count, rx3_error;
26 #endif
27 
28 uint8_t tx_res, tx_error;
29 SPIFlash flash(8);
30 BayEOSBufferSPIFlash myBuffer;
31 uint8_t gprs_status; // 1 = 0n, 0 = Off
32 uint8_t rf24_status; // 1 = 0n, 0 = Off
33 
34 #ifndef BAT_MULTIPLIER
35 #define BAT_MULTIPLIER 3.3 * 200 / 100 / 1023
36 #endif
37 
38 #ifndef TICKS_PER_SECOND
39 #define TICKS_PER_SECOND 16
40 #endif
41 
42 #ifndef SAMPLING_INT
43 #define SAMPLING_INT 128
44 #endif
45 
46 #ifndef BAUD_RATE
47 #define BAUD_RATE 38400
48 #endif
49 
50 #ifndef MIN_VOLTAGE_FULL
51 #define MIN_VOLTAGE_FULL 3.95 /*minimum voltage for full operation */
52 #endif
53 
54 #ifndef MIN_VOLTAGE
55 #define MIN_VOLTAGE 3.75 /*minimum voltage for send operation */
56 #endif
57 
58 #define LED_PIN 5
59 
60 #ifndef LED_RX1
61 #define LED_RX1 4
62 #endif
63 #define LED_RX2 3
64 #define LED_RX3 2
65 
66 #ifndef POWER_PIN
67 #define POWER_PIN 7
68 #endif
69 
70 #ifndef WLAN_PIN
71 #define WLAN_PIN 7
72 #endif
73 
74 #ifndef EEPROM_BUFFER_STATUS_POS
75 #define EEPROM_BUFFER_STATUS_POS 300
76 #endif
77 
78 #ifndef EEPROM_BUFFER_STATUS_BYTE
79 #define EEPROM_BUFFER_STATUS_BYTE 0xb0 /* indicates whether there is valid data in the flash buffer */
80 #endif
81 
82 volatile uint16_t ticks;
83 volatile uint8_t action;
84 
85 #define ISSET_ACTION(nr) ((1 << nr) & action)
86 #define UNSET_ACTION(nr) (action &= ~(1 << nr))
87 
88 #ifndef ACTION_COUNT
89 #define ACTION_COUNT 1
90 #endif
91 
92 #ifndef CHECKSUM_FRAMES
93 #define CHECKSUM_FRAMES 0
94 #endif
95 
96 #ifndef RESET_COUNT
97 #define RESET_COUNT 8
98 #endif
99 
100 #if TICKS_PER_SECOND > 3
101 #define LED_TICK_DIV (TICKS_PER_SECOND / 4)
102 #else
103 #define LED_TICK_DIV 1
104 #endif
105 
106 #if RESET_COUNT
107 volatile uint8_t action0_pending_count = 0;
108 #endif
109 
110 volatile uint8_t seconds;
111 volatile unsigned long current_micros, last_micros;
112 volatile uint8_t adjust_osccal_flag;
113 volatile uint8_t led_blink = 0;
114 volatile uint8_t led_blink_rx1 = 0;
115 volatile uint8_t led_blink_rx2 = 0;
116 volatile uint8_t led_blink_rx3 = 0;
117 
118 #ifndef SENDING_INTERVAL
119 #define SENDING_INTERVAL 120000L
120 #endif
121 
122 #ifndef NEXT_TRY_INTERVAL
123 #define NEXT_TRY_INTERVAL 300000L
124 #endif
125 
126 #ifndef MAX_BUFFER_AVAILABLE
127 #define MAX_BUFFER_AVAILABLE 2000
128 #endif
129 
130 float batLCB;
131 RTC_Timer2 myRTC;
132 
133 #ifdef RTC_SECOND_CORRECT
134 volatile long rtc_seconds_correct;
135 #endif
136 
137 #if defined(GPRS_CONFIG)
138 #include <BayTCPSim900.h>
139 BayGPRS client(Serial, 0); // No Power Pin
140 #elif defined(SIM800_CONFIG)
141 #include <BaySIM800.h>
142 #include <EEPROM.h>
143 BaySIM800 client = BaySIM800(Serial);
144 #elif defined(WLAN_CONFIG)
145 #include <BayTCPESP8266.h>
146 BayESP8266 client(Serial, WLAN_PIN);
147 #elif defined(SERIAL_CLIENT)
148 #if DEBUG_CLIENT
149 #include <BayDebug.h>
150 BayDebug client(Serial);
151 #else
152 #include <BaySerial.h>
153 BaySerial client(Serial);
154 #endif
155 #else
156 #include <BaySerial.h>
157 BaySerialESP client(Serial, WLAN_PIN);
158 #define ESP01 1
159 #endif
160 
161 /*
162  * ISR for timer2
163  * increments RTC-Time
164  * sets action bits
165  */
166 ISR(TIMER2_OVF_vect)
167 {
168  ticks++;
169  if (adjust_osccal_flag)
170  {
171  last_micros = current_micros;
172  current_micros = micros();
173  adjust_osccal_flag++;
174  if (adjust_osccal_flag > 2)
175  {
176  if ((current_micros - last_micros) > (1005000L / TICKS_PER_SECOND))
177  {
178  if (OSCCAL)
179  OSCCAL--;
180  else
181  adjust_osccal_flag = 0; // reached limit!
182  }
183  else if ((current_micros - last_micros) < (995000L / TICKS_PER_SECOND))
184  {
185  if (OSCCAL < 255)
186  OSCCAL++;
187  else
188  adjust_osccal_flag = 0; // reached limit!
189  }
190  else
191  {
192  // Timing is ok :-)
193  adjust_osccal_flag = 0;
194  }
195  }
196  }
197 
198  if ((ticks % TICKS_PER_SECOND) == 0)
199  {
200  myRTC._seconds++; // RTC_Timer2.get() and adjust() are interrupt save now!
201  seconds++;
202 #ifdef RTC_SECOND_CORRECT
203 
204 #if RTC_SECOND_CORRECT < 0
205  rtc_seconds_correct--;
206  if (rtc_seconds_correct <= RTC_SECOND_CORRECT)
207  {
208  rtc_seconds_correct = 0;
209  myRTC._seconds--;
210  seconds--;
211  }
212 #else
213  rtc_seconds_correct++;
214  if (rtc_seconds_correct >= RTC_SECOND_CORRECT)
215  {
216  rtc_seconds_correct = 0;
217  myRTC._seconds++;
218  seconds++;
219  }
220 
221 #endif
222 #endif
223  uint16_t tick_mod = myRTC._seconds % SAMPLING_INT;
224  if (tick_mod < ACTION_COUNT)
225  {
226 #if RESET_COUNT
227  if (tick_mod == 0)
228  {
229  if (ISSET_ACTION(0))
230  action0_pending_count++;
231 
232  else
233  action0_pending_count = 0;
234  if (action0_pending_count > RESET_COUNT)
235  asm volatile(" jmp 0"); // restart programm
236  }
237 #endif
238  action |= (1 << tick_mod);
239  }
240  else
241  {
242  action |= (1 << 7);
243  }
244  }
245 
246  if ((led_blink > 0) && ((ticks % LED_TICK_DIV) == 0))
247  {
248  if (digitalRead(LED_PIN))
249  led_blink--;
250  digitalWrite(LED_PIN, !digitalRead(LED_PIN));
251  }
252  if ((led_blink_rx1 > 0) && ((ticks % LED_TICK_DIV) == 0))
253  {
254  if (digitalRead(LED_RX1))
255  led_blink_rx1--;
256  digitalWrite(LED_RX1, !digitalRead(LED_RX1));
257  }
258 #ifdef NRF24_2CHANNEL
259  if ((led_blink_rx2 > 0) && ((ticks % LED_TICK_DIV) == 0))
260  {
261  if (digitalRead(LED_RX2))
262  led_blink_rx2--;
263  digitalWrite(LED_RX2, !digitalRead(LED_RX2));
264  }
265 #endif
266 #ifdef NRF24_3CHANNEL
267  if ((led_blink_rx3 > 0) && ((ticks % LED_TICK_DIV) == 0))
268  {
269  if (digitalRead(LED_RX3))
270  led_blink_rx3--;
271  digitalWrite(LED_RX3, !digitalRead(LED_RX3));
272  }
273 #endif
274 }
275 
276 /*
277  * Adjust the OSCCAL of internal oscillator using TIMER2 RTC
278  */
279 void adjust_OSCCAL(void)
280 {
281  adjust_osccal_flag = 1;
282  while (adjust_osccal_flag)
283  {
284  delay(1);
285  }
286 }
287 
288 /*
289  * blinkLED is done by timer2
290  */
291 void blinkLED(uint8_t times)
292 {
293  if (LED_PIN > -1)
294  led_blink = times;
295 }
296 
297 void sleepLCB()
298 {
299  Sleep.sleep(TIMER2_ON, SLEEP_MODE_PWR_SAVE);
300 }
301 
302 void delayLCB(uint16_t _millis)
303 {
304  _millis /= (1000 / TICKS_PER_SECOND);
305  noInterrupts();
306  uint16_t end_ticks = ticks + _millis + 2;
307  interrupts();
308  do
309  {
310  sleepLCB();
311  } while ((ticks != end_ticks));
312 }
313 
314 /*
315  * init Time2 and LED pin
316  */
317 void initLCB()
318 {
319 #if TICKS_PER_SECOND == 128
320  Sleep.setupTimer2(1); // init timer2 to 0,0078125sec
321 #elif TICKS_PER_SECOND == 16
322  Sleep.setupTimer2(2); // init timer2 to 0,0625sec
323 #elif TICKS_PER_SECOND == 4
324  Sleep.setupTimer2(3); // init timer2 to 0,25sec
325 #elif TICKS_PER_SECOND == 2
326  Sleep.setupTimer2(4); // init timer2 to 0,5sec
327 #elif TICKS_PER_SECOND == 1
328  Sleep.setupTimer2(5); // init timer2 to 1sec
329 #else
330 #error unsupported TICKS_PER_SECOND
331 #endif
332 
333  pinMode(LED_PIN, OUTPUT);
334  pinMode(LED_RX1, OUTPUT);
335 #ifdef NRF24_3CHANNEL
336  pinMode(LED_RX2, OUTPUT);
337 #endif
338 #ifdef NRF24_3CHANNEL
339  pinMode(LED_RX3, OUTPUT);
340 #endif
341 
342  myBuffer.init(flash); // This will restore old pointers
343  myBuffer.setRTC(myRTC, RTC_RELATIVE_SECONDS); // Nutze RTC relativ!
344 #ifndef SIM800_CONFIG
345  myBuffer.skip();
346 #endif
347  // We could also try to use absolute times received from GPRS!
348  client.setBuffer(myBuffer);
349  pinMode(POWER_PIN, OUTPUT);
350  digitalWrite(POWER_PIN, HIGH);
351 #if defined(GPRS_CONFIG)
352  client.readConfigFromStringPGM(PSTR(GPRS_CONFIG));
353 #elif defined(SIM800_CONFIG)
354  client.readConfigFromStringPGM(PSTR(SIM800_CONFIG));
355 #elif defined(WLAN_CONFIG)
356  client.readConfigFromStringPGM(PSTR(WLAN_CONFIG));
357 #elif defined(SERIAL_CLIENT)
358 #if DEBUG_CLIENT
359  client.begin(38400, 1);
360 #else
361  client.begin(38400);
362 #endif
363 #else
364  client.begin(38400);
365  client.powerUp();
366  while (client.isReady())
367  {
368  blinkLED(2);
369  delay(1500);
370  }
371 #ifdef ROUTER_NAME
372  while (uint8_t res = client.setConfig(ROUTER_NAME, BaySerialESP_NAME))
373  {
374  if (res == 10 + strlen(ROUTER_NAME))
375  break;
376  blinkLED(res);
377  delay(res * 500 + 2000);
378  }
379 #endif
380 #ifdef GATEWAY_IP
381  while (uint8_t res = client.setConfig(GATEWAY_IP, BaySerialESP_GATEWAY))
382  {
383  if (res == 10 + strlen(GATEWAY_IP))
384  break;
385  blinkLED(res);
386  delay(res * 500 + 2000);
387  }
388 #endif
389 #ifdef GATEWAY_USER
390  while (uint8_t res = client.setConfig(GATEWAY_USER, BaySerialESP_USER))
391  {
392  if (res == 10 + strlen(GATEWAY_USER))
393  break;
394  blinkLED(res);
395  delay(res * 500 + 2000);
396  }
397 #endif
398 #ifdef GATEWAY_PW
399  while (uint8_t res = client.setConfig(GATEWAY_PW, BaySerialESP_PW))
400  {
401  if (res == 10 + strlen(GATEWAY_PW))
402  break;
403  blinkLED(res);
404  delay(res * 500 + 2000);
405  }
406 #endif
407  client.powerDown();
408 #endif
409 
410  blinkLED(3);
411  adjust_OSCCAL();
412 #if defined(GPRS_CONFIG) || defined(SIM800_CONFIG)
413  delayLCB(1000);
414  tx_res = client.begin(38400);
415 #if defined(SIM800_CONFIG)
416  while (true)
417  {
418  unsigned long time = client.now().get();
419  if (time > 3600L * 24 * 365 * 20)
420  {
421  myRTC.adjust(time);
422  break;
423  }
424  blinkLED(2);
425  delay(2000);
426  }
427  if (EEPROM.read(EEPROM_BUFFER_STATUS_POS) != EEPROM_BUFFER_STATUS_BYTE)
428  {
429  myBuffer.reset();
430  EEPROM.write(EEPROM_BUFFER_STATUS_POS, EEPROM_BUFFER_STATUS_BYTE);
431  }
432 #else
433  if (!tx_res)
434  myRTC.adjust(client.now());
435 #endif
436 #elif defined(WLAN_CONFIG)
437  tx_res = client.begin(38400);
438 #elif defined(SERIAL_CLIENT)
439  client.sendMessage("Router started");
440 #else
441  client.powerUp();
442  tx_res = 0;
443 #endif
444 
445 #ifndef SERIAL_CLIENT
446  blinkLED(tx_res + 1);
447  /*
448  1 == OK
449  2 == NO Communication
450  3 == PIN failed
451  4 == PIN locked
452  5 == Not CREG
453  6 == Not CGATT
454  7 == No SIM Card
455  */
456  delay(2000);
457  client.startFrame(BayEOS_Message);
458  client.addToPayload("Router started");
459  client.writeToBuffer();
460  tx_res = client.sendMultiFromBuffer();
461  blinkLED(tx_res + 1);
462  delay(1000 + tx_res * 500);
463 
464  /*
465  1 == OK
466  2 == no success
467  3 == timeout
468  4 == network timeout
469  5 == gprs modem timeout
470  */
471  // if (myRTC.now().get() < 2000) myBuffer.skip(); //got no time! skip the unread frames in Buffer!!
472 #endif
473  gprs_status = 1;
474  rf24_switchon_time = myRTC.get() + 1;
475  rf24_switchoff_time = myRTC.get();
476 }
477 
478 void initRF24(void)
479 {
480  radio.begin();
481  radio.powerUp();
482  radio.setChannel(NRF24_CHANNEL);
483  radio.setPayloadSize(32);
484  radio.enableDynamicPayloads();
485  radio.setCRCLength(RF24_CRC_16);
486  radio.setDataRate(RF24_250KBPS);
487  radio.setPALevel(RF24_PA_MAX);
488  radio.setRetries(15, 15);
489  radio.setAutoAck(true);
490  radio.openReadingPipe(0, pipe_0);
491  radio.openReadingPipe(1, pipe_1);
492  radio.openReadingPipe(2, pipe_2);
493  radio.openReadingPipe(3, pipe_3);
494  radio.openReadingPipe(4, pipe_4);
495  radio.openReadingPipe(5, pipe_5);
496 
497  radio.startListening();
498 #ifdef NRF24_2CHANNEL
499  radio2.begin();
500  radio2.powerUp();
501  radio2.setChannel(NRF24_2CHANNEL);
502  radio2.setPayloadSize(32);
503  radio2.enableDynamicPayloads();
504  radio2.setCRCLength(RF24_CRC_16);
505  radio2.setDataRate(RF24_250KBPS);
506  radio2.setPALevel(RF24_PA_MAX);
507  radio2.setRetries(15, 15);
508  radio2.setAutoAck(true);
509  radio2.openReadingPipe(0, pipe_0);
510  radio2.openReadingPipe(1, pipe_1);
511  radio2.openReadingPipe(2, pipe_2);
512  radio2.openReadingPipe(3, pipe_3);
513  radio2.openReadingPipe(4, pipe_4);
514  radio2.openReadingPipe(5, pipe_5);
515  radio2.startListening();
516 #endif
517 #ifdef NRF24_3CHANNEL
518  radio3.begin();
519  radio3.powerUp();
520  radio3.setChannel(NRF24_3CHANNEL);
521  radio3.setPayloadSize(32);
522  radio3.enableDynamicPayloads();
523  radio3.setCRCLength(RF24_CRC_16);
524  radio3.setDataRate(RF24_250KBPS);
525  radio3.setPALevel(RF24_PA_MAX);
526  radio3.setRetries(15, 15);
527  radio3.setAutoAck(true);
528  radio3.openReadingPipe(0, pipe_0);
529  radio3.openReadingPipe(1, pipe_1);
530  radio3.openReadingPipe(2, pipe_2);
531  radio3.openReadingPipe(3, pipe_3);
532  radio3.openReadingPipe(4, pipe_4);
533  radio3.openReadingPipe(5, pipe_5);
534  radio3.startListening();
535 #endif
536  rf24_status = 1;
537 }
538 
539 void powerDownRF24(void)
540 {
541  radio.powerDown();
542 #ifdef NRF24_2CHANNEL
543  radio2.powerDown();
544 #endif
545 #ifdef NRF24_3CHANNEL
546  radio3.powerDown();
547 #endif
548  rf24_status = 0;
549 }
550 
551 uint8_t handleRF24(void)
552 {
553  if (!rf24_status)
554  0;
555  uint8_t pipe_num, len;
556  uint8_t payload[32];
557  char origin[] = "A0";
558 #ifdef RF24_P1_LETTER
559  origin[0] = RF24_P1_LETTER;
560 #endif
561  uint8_t count = 0;
562  uint8_t rx = 0;
563  while (radio.available(&pipe_num))
564  {
565  count++;
566  if (len = radio.getDynamicPayloadSize())
567  {
568  rx++;
569  origin[1] = '0' + pipe_num;
570  client.startOriginFrame(origin, 1); // Routed Origin!
571  if (len > 32)
572  len = 32;
573  radio.read(payload, len);
574  for (uint8_t i = 0; i < len; i++)
575  {
576  client.addToPayload(payload[i]);
577  }
578 #if WITH_RF24_CHECKSUM
579  if (!client.validateChecksum())
580  {
581 #ifdef SERIAL_CLIENT
582  client.sendOrBuffer();
583 #else
584  client.writeToBuffer();
585 #endif
586  led_blink_rx1 = 1;
587  rx1_count++;
588  }
589  else
590  rx1_error++;
591 #else
592 #ifdef SERIAL_CLIENT
593  client.sendOrBuffer();
594 #else
595  client.writeToBuffer();
596 #endif
597  led_blink_rx1 = 1;
598  rx1_count++;
599 #endif
600  }
601  else
602  {
603  rx1_error++;
604  }
605  if (count > 10)
606  break;
607  }
608 
609 #ifdef NRF24_2CHANNEL
610  count = 0;
611 #ifdef RF24_P2_LETTER
612  origin[0] = RF24_P2_LETTER;
613 #else
614  origin[0] = 'B';
615 #endif
616 
617  while (radio2.available(&pipe_num))
618  {
619  count++;
620  if (len = radio2.getDynamicPayloadSize())
621  {
622  rx++;
623  origin[1] = '0' + pipe_num;
624  client.startOriginFrame(origin, 1); // Routed Origin!
625  // Fetch the payload
626  if (len > 32)
627  len = 32;
628  radio2.read(payload, len);
629  for (uint8_t i = 0; i < len; i++)
630  {
631  client.addToPayload(payload[i]);
632  }
633 #if WITH_RF24_CHECKSUM
634  if (!client.validateChecksum())
635  {
636 #ifdef SERIAL_CLIENT
637  client.sendOrBuffer();
638 #else
639  client.writeToBuffer();
640 #endif
641  led_blink_rx2 = 1;
642  rx2_count++;
643  }
644  else
645  rx2_error++;
646 #else
647 #ifdef SERIAL_CLIENT
648  client.sendOrBuffer();
649 #else
650  client.writeToBuffer();
651 #endif
652  led_blink_rx2 = 1;
653  rx2_count++;
654 #endif
655  }
656  else
657  {
658  rx2_error++;
659  }
660  if (count > 10)
661  break;
662  }
663 
664 #endif
665 
666 #ifdef NRF24_3CHANNEL
667  count = 0;
668 #ifdef RF24_P3_LETTER
669  origin[0] = RF24_P3_LETTER;
670 #else
671  origin[0] = 'C';
672 #endif
673 
674  while (radio3.available(&pipe_num))
675  {
676  count++;
677  if (len = radio3.getDynamicPayloadSize())
678  {
679  rx++;
680  origin[1] = '0' + pipe_num;
681  client.startOriginFrame(origin, 1); // Routed Origin!
682  // Fetch the payload
683  if (len > 32)
684  len = 32;
685  radio3.read(payload, len);
686  for (uint8_t i = 0; i < len; i++)
687  {
688  client.addToPayload(payload[i]);
689  }
690 #if WITH_RF24_CHECKSUM
691  if (!client.validateChecksum())
692  {
693 #ifdef SERIAL_CLIENT
694  client.sendOrBuffer();
695 #else
696  client.writeToBuffer();
697 #endif
698  led_blink_rx3 = 1;
699  rx3_count++;
700  }
701  else
702  rx3_error++;
703 #else
704 #ifdef SERIAL_CLIENT
705  client.sendOrBuffer();
706 #else
707  client.writeToBuffer();
708 #endif
709  led_blink_rx3 = 1;
710  rx3_count++;
711 #endif
712  }
713  else
714  {
715  rx3_error++;
716  }
717  if (count > 10)
718  break;
719  }
720 
721 #endif
722  if (count > 10)
723  initRF24();
724 
725  delay(2);
726  return rx;
727 }
728 
729 void checkAction0(void)
730 {
731  if (!ISSET_ACTION(0))
732  return;
733  UNSET_ACTION(0);
734 #ifndef SERIAL_CLIENT
735  digitalWrite(POWER_PIN, HIGH);
736  analogReference(DEFAULT);
737  if (!gprs_status)
738  delayLCB(1000);
739 #if ESP01
740  delayLCB(100);
741 #endif
742  adjust_OSCCAL();
743  batLCB = BAT_MULTIPLIER * analogRead(A7);
744 
745 #ifdef BOARD_ORIGIN
746  client.startDataFrameWithOrigin(BayEOS_Float32le, BOARD_ORIGIN, 0, 1);
747 #else
748  client.startDataFrame();
749 #endif
750  client.addChannelValue(millis() / 1000);
751  client.addChannelValue(myBuffer.writePos());
752  client.addChannelValue(myBuffer.readPos());
753 #if ESP01
754  client.addChannelValue(0);
755 #else
756  if (gprs_status)
757  client.addChannelValue(client.getRSSI());
758  else
759  client.addChannelValue(0);
760 #endif
761  client.addChannelValue(batLCB);
762  client.addChannelValue(tx_error);
763  client.addChannelValue(tx_res);
764  client.addChannelValue(rx1_count);
765  client.addChannelValue(rx1_error);
766  rx1_count = 0;
767  rx1_error = 0;
768 #ifdef NRF24_2CHANNEL
769  client.addChannelValue(rx2_count);
770  client.addChannelValue(rx2_error);
771  rx2_count = 0;
772  rx2_error = 0;
773 #endif
774 #ifdef NRF24_3CHANNEL
775  client.addChannelValue(rx3_count);
776  client.addChannelValue(rx3_error);
777  rx3_count = 0;
778  rx3_error = 0;
779 #endif
780  client.writeToBuffer();
781 
782  if (!gprs_status && batLCB > 3.9)
783  {
784  client.begin(38400);
785  initRF24();
786  gprs_status = 1;
787  }
788 
789  if (batLCB > MIN_VOLTAGE)
790  {
791  if (batLCB < MIN_VOLTAGE_FULL)
792  {
793  if (rf24_switchon_time > rf24_switchoff_time)
794  { // RF24 was active
795  if ((myRTC.get() - rf24_switchon_time) > 3600)
796  {
797  rf24_switchoff_time = myRTC.get();
798  powerDownRF24();
799  gprs_status = 0;
800  }
801  }
802  else
803  { // RF24 was inactive
804  if ((myRTC.get() - rf24_switchoff_time) > 5 * 3600)
805  {
806  rf24_switchon_time = myRTC.get();
807  initRF24();
808  }
809  }
810  }
811  else if (!rf24_status)
812  initRF24();
813 
814  if (rf24_status && !gprs_status)
815  {
816  client.begin(38400);
817  gprs_status = 1;
818  }
819  }
820  else
821  {
822  if (rf24_status)
823  powerDownRF24();
824  gprs_status = 0;
825  }
826 
827  if (gprs_status)
828  {
829 #if ESP01
830  client.powerUp();
831 #endif
832  tx_res = client.sendMultiFromBuffer(3000);
833  if (tx_res)
834  tx_error++;
835  else
836  tx_error = 0;
837  blinkLED(tx_res + 1);
838 
839  if (tx_error > 5 && (tx_error % 5) == 0)
840  {
841  digitalWrite(POWER_PIN, LOW);
842  delay(1000);
843  digitalWrite(POWER_PIN, HIGH);
844  client.begin(38400);
845  }
846 
847  while (!tx_res && myBuffer.available() && !ISSET_ACTION(0))
848  {
849  handleRF24();
850  tx_res = client.sendMultiFromBuffer(3000);
851  blinkLED(tx_res + 1);
852 #if defined(SIM800_CONFIG)
853  unsigned long time = client.now().get();
854  if (time > 3600L * 24 * 365 * 20)
855  {
856  myRTC.adjust(time);
857  }
858 #endif
859  }
860  }
861  else
862  digitalWrite(POWER_PIN, LOW);
863 #if ESP01
864  client.powerDown();
865 #endif
866 #endif
867 }
Definition: BayDebug.h:54
unsigned long available(void)
Definition: BayEOSBuffer.cpp:78
void reset(void)
Definition: BayEOSBuffer.cpp:168
void setRTC(RTC &rtc, uint8_t timeType=RTC_ABSOLUTE_SECONDS)
Definition: BayEOSBuffer.cpp:258
void skip(void)
Definition: BayEOSBuffer.cpp:173
Definition: BayEOSBufferSPIFlash.h:6
void init(SPIFlash &flash, uint8_t flush_skip=20)
Definition: BayEOSBufferSPIFlash.cpp:7
void startDataFrameWithOrigin(uint8_t subtype, const char *o, uint8_t checksum=0, uint8_t routed=0)
Definition: BayEOS.cpp:27
void setBuffer(BayEOSBuffer &buffer, uint16_t max_skip=0)
Definition: BayEOS.h:452
uint8_t addChannelValue(float v, uint8_t channel_number=0)
Definition: BayEOS.cpp:61
uint8_t writeToBuffer(void)
Definition: BayEOS.cpp:322
uint8_t sendMessage(const char *s)
Definition: BayEOS.cpp:295
void startFrame(void)
Definition: BayEOS.cpp:8
void startOriginFrame(const char *o, uint8_t routed=0)
Definition: BayEOS.cpp:20
uint8_t addToPayload(uint8_t b)
Definition: BayEOS.h:469
void startDataFrame(uint8_t subtype=BayEOS_Float32le, uint8_t checksum=0)
Definition: BayEOS.cpp:12
uint8_t sendOrBuffer(void)
Definition: BayEOS.cpp:369
Definition: BayTCPSim900.h:144
Definition: BaySIM800.h:16
Definition: BaySerial.h:135
Definition: BaySerial.h:106
Definition: RTClib.h:39
Definition: SPIFlash.h:32