11#include < Arduino.h>
2+ #include < led_ctrl.h>
23#include < log.h>
4+ #include < low_power.h>
35#include < lte.h>
6+ #include < sequans_controller.h>
47
5- // NB: For this example it is important that this variable is volatile so the
6- // compiler won't optimize away the code we have in the loop function and thus
7- // make it so that the miss out on the event. This is due to compiler
8- // optimization. If something more is done during the loop function, this won't
9- // be a problem.
10- volatile bool power_save_got_abrupted = false ;
11-
12- void power_save_abrupted (void ) { power_save_got_abrupted = true ; }
13-
14- String
15- convertAwakeMultiplierToString (const AwakeUnitMultiplier awake_multiplier) {
16- switch (awake_multiplier) {
17- case AwakeUnitMultiplier::TWO_SECONDS:
18- return " TWO_SECONDS" ;
19- case AwakeUnitMultiplier::ONE_MINUTE:
20- return " ONE_MINUTE" ;
21- case AwakeUnitMultiplier::SIX_MINUTES:
22- return " SIX_MINUTES" ;
23- default :
24- return " UNKNOWN" ;
25- }
26- }
8+ #ifdef __AVR_AVR128DB48__ // MINI
279
28- String
29- convertSleepMultiplierToString (const SleepUnitMultiplier sleep_multiplier) {
30- switch (sleep_multiplier) {
31- case SleepUnitMultiplier::TEN_MINUTES:
32- return " TEN_MINUTE" ;
33- case SleepUnitMultiplier::ONE_HOUR:
34- return " ONE_HOUR" ;
35- case SleepUnitMultiplier::TEN_HOURS:
36- return " TEN_HOURS" ;
37- case SleepUnitMultiplier::TWO_SECONDS:
38- return " TWO_SECONDS" ;
39- case SleepUnitMultiplier::THIRTY_SECONDS:
40- return " THIRTY_SECONDS" ;
41- case SleepUnitMultiplier::ONE_MINUTE:
42- return " ONE_MINUTE" ;
43- default :
44- return " UNKNOWN" ;
45- }
46- }
10+ #define SerialDebug Serial3
11+
12+ #else
13+ #ifdef __AVR_AVR128DB64__ // Non-Mini
14+
15+ #define SerialDebug Serial5
16+
17+ #else
18+ #error "INCOMPATIBLE_DEVICE_SELECTED"
19+ #endif
20+ #endif
21+
22+ #define TIMING_PIN PIN_PE7
4723
4824void setup () {
4925
50- Log .begin (115200 );
26+ LedCtrl .begin ();
5127
52- // Set callback for when we leave power save mode
53- Lte. onPowerSaveAbrupted (power_save_abrupted );
28+ Log. begin ( 115200 );
29+ Log. setLogLevel (LogLevel::DEBUG );
5430
5531 // Start LTE modem, configure the power save configuration and wait until we
5632 // are connected to the operator
5733 //
58- // Here we say that we want to sleep for 30 seconds * 3 = 90 seconds = 1.5
59- // minutes and that we want our awake period to be at 2 seconds * 8 = 16
60- // seconds
61- Lte.begin (true ,
62- PowerSaveConfiguration{SleepUnitMultiplier::THIRTY_SECONDS,
63- 3 ,
64- AwakeUnitMultiplier::TWO_SECONDS,
65- 8 });
34+ // Here we say that we want to sleep for 30 seconds * 2 = 60 seconds each
35+ // time we invoke sleep
36+ LowPower.begin (SleepMultiplier::THIRTY_SECONDS, 2 );
37+ Lte.begin ();
6638
6739 while (!Lte.isConnected ()) {
6840 Log.info (" Waiting for connection...\r\n " );
@@ -71,58 +43,14 @@ void setup() {
7143
7244 Log.info (" Connected to operator!\r\n " );
7345
74- // After we are connected, we can check which schedule for the network
75- // contact the operator gave us, which may deviate from what we requested
76- //
77- // In the end, the operator has the final say in this, so we have to use
78- // what we are given
79- //
80- // The following is just for debug purposes
81- PowerSaveConfiguration current_power_save_cfg =
82- Lte.getCurrentPowerSaveConfiguration ();
83-
84- String sleep_multiplier_string =
85- convertSleepMultiplierToString (current_power_save_cfg.sleep_multiplier );
86- String awake_multiplier_string =
87- convertAwakeMultiplierToString (current_power_save_cfg.awake_multiplier );
88-
89- Log.infof (" Power saving configuration given by operator. Sleep multiplier: "
90- " %s, sleep value: %d, awake multiplier: %s, awake value: %d\r\n " ,
91- sleep_multiplier_string.c_str (),
92- current_power_save_cfg.sleep_value ,
93- awake_multiplier_string.c_str (),
94- current_power_save_cfg.awake_value );
95-
96- // Now we attempt to enter power save mode. We do this in a loop as it might
97- // fail if the LTE modem is currently busy doing work.
98- while (!Lte.attemptToEnterPowerSaveMode ()) {
99- Log.info (" Failed to put LTE modem in sleep, retrying...\r\n " );
100- }
101-
102- Log.info (" LTE modem is sleeping!\r\n " );
46+ pinConfigure (TIMING_PIN, PIN_DIR_OUTPUT);
10347}
10448
10549void loop () {
10650
107- // When we got abrupted from power save mode, which will happen when the
108- // modem goes out of sleep in the period we have specified or receives some
109- // message, we have to manually put it back to sleep
110- if (power_save_got_abrupted) {
111- Log.info (" Power save abrupted/sleep finished, doing work...\r\n " );
51+ digitalWrite (TIMING_PIN, digitalRead (TIMING_PIN) ? 0 : 1 );
11252
113- // Do work, check messages, report status etc...
114-
115- Log.info (" Attempting to put LTE modem back into sleep...\r\n " );
116-
117- // The modem might be active doing work and this request for power save
118- // mode might fail before the timeout, so we retry until we succeed
119- //
120- // An extended timeout can be given by
121- // Lte.attemptToEnterPowerSaveMode(waiting_time_ms). The default is
122- // 60000 ms = 60 seconds
123- while (!Lte.attemptToEnterPowerSaveMode ()) {}
124-
125- Log.info (" Success!\r\n " );
126- power_save_got_abrupted = false ;
127- }
53+ SleepStatusCode status_code = LowPower.sleep (SleepMode::REGULAR);
54+ Log.infof (" Got out of sleep with status code %d, doing work...\r\n " ,
55+ status_code);
12856}
0 commit comments