Skip to content

Commit 3b2719d

Browse files
committed
Merge branch 'develop' of ssh://bitbucket.microchip.com/mcu8mass/avr-iot-cellular-arduino-library into low-power/decrease-power-consumption
2 parents fe9f03f + 9b41804 commit 3b2719d

File tree

7 files changed

+280
-30
lines changed

7 files changed

+280
-30
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* @brief This example demonstrates a more robust MQTT program which takes into
3+
* consideration network disconnection and broker disconnection.
4+
*/
5+
6+
#include <Arduino.h>
7+
8+
#include <ecc608.h>
9+
#include <led_ctrl.h>
10+
#include <log.h>
11+
#include <lte.h>
12+
#include <mqtt_client.h>
13+
#include <sequans_controller.h>
14+
15+
#define MQTT_PUB_TOPIC_FMT "%s/sensors"
16+
17+
static char mqtt_pub_topic[128];
18+
19+
static volatile bool connecteded_to_network = false;
20+
static volatile bool connected_to_broker = false;
21+
22+
void connectedToBroker(void) { connected_to_broker = true; }
23+
void disconnectedFromBroker(void) { connected_to_broker = false; }
24+
25+
void disconnectedFromNetwork(void) { connecteded_to_network = false; }
26+
27+
bool initMQTTTopics() {
28+
ECC608.begin();
29+
30+
// Find the thing ID and set the publish and subscription topics
31+
uint8_t thingName[128];
32+
uint8_t thingNameLen = sizeof(thingName);
33+
34+
// -- Get the thingname
35+
uint8_t err = ECC608.getThingName(thingName, &thingNameLen);
36+
if (err != ECC608.ERR_OK) {
37+
Log.error("Could not retrieve thingname from the ECC");
38+
return false;
39+
}
40+
41+
sprintf(mqtt_pub_topic, MQTT_PUB_TOPIC_FMT, thingName);
42+
43+
return true;
44+
}
45+
46+
static bool connectLTE() {
47+
// Connect with a maximum timeout value of 30 000 ms, if the connection is
48+
// not up and running within 30 seconds, abort and retry later
49+
if (!Lte.begin(30000)) {
50+
Log.warn("Failed to connect to operator.");
51+
return false;
52+
} else {
53+
return true;
54+
}
55+
}
56+
57+
static bool connectMqtt() {
58+
59+
uint32_t start = millis();
60+
61+
// Attempt to connect to the broker
62+
if (MqttClient.beginAWS()) {
63+
Log.infof("Connecting to broker");
64+
65+
// Wait for 60 seconds max
66+
while (!MqttClient.isConnected() && millis() - start < 60000) {
67+
Log.rawf(".");
68+
delay(500);
69+
}
70+
71+
if (millis() - start >= 60000) {
72+
Log.rawf(" Failed to connect\r\n");
73+
return false;
74+
}
75+
76+
Log.rawf(" OK!\r\n");
77+
78+
connected_to_broker = true;
79+
} else {
80+
Log.rawf("\r\n");
81+
Log.error("Failed to connect to broker");
82+
83+
return false;
84+
}
85+
86+
return true;
87+
}
88+
89+
void setup() {
90+
Log.begin(115200);
91+
LedCtrl.begin();
92+
LedCtrl.startupCycle();
93+
94+
Log.info("Starting MQTT with Connection Loss Handling\r\n");
95+
96+
if (initMQTTTopics() == false) {
97+
Log.error("Unable to initialize the MQTT topics. Stopping...");
98+
while (1) {}
99+
}
100+
101+
Lte.onDisconnect(disconnectedFromNetwork);
102+
MqttClient.onConnectionStatusChange(connectedToBroker,
103+
disconnectedFromBroker);
104+
105+
SequansController.begin();
106+
107+
SequansController.writeCommand("at+cmee=2");
108+
}
109+
110+
/**
111+
* @brief Used to keep track of the number of publishes.
112+
*/
113+
static uint32_t counter = 0;
114+
115+
/**
116+
* @brief Counts the times the publish has failed due to a timeout.
117+
*/
118+
static uint32_t failed_publishes = 0;
119+
120+
/**
121+
* @brief Used for keeping track of time so that a message is published every
122+
* 10th second under normal operations (when connected to the network and
123+
* broker).
124+
*/
125+
static uint32_t timer = 0;
126+
127+
void loop() {
128+
129+
if (!connecteded_to_network) {
130+
Log.info("Not connected to the network. Attempting to connect!");
131+
if (connectLTE()) {
132+
connecteded_to_network = true;
133+
}
134+
}
135+
136+
if (!connected_to_broker && connecteded_to_network) {
137+
Log.info("Not connected to broker. Attempting to connect!");
138+
139+
if (connectMqtt()) {
140+
connected_to_broker = true;
141+
}
142+
}
143+
144+
if (millis() - timer > 10000) {
145+
if (connected_to_broker) {
146+
char message_to_publish[8] = {0};
147+
sprintf(message_to_publish, "%lu", counter);
148+
149+
bool published_successfully = MqttClient.publish(mqtt_pub_topic,
150+
message_to_publish,
151+
AT_LEAST_ONCE,
152+
60000);
153+
if (published_successfully) {
154+
Log.infof("Published message: %s. Failed publishes: %d.\r\n",
155+
message_to_publish,
156+
failed_publishes);
157+
} else {
158+
failed_publishes++;
159+
}
160+
161+
counter++;
162+
}
163+
164+
timer = millis();
165+
}
166+
}

examples/sandbox/sandbox.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void setup() {
288288
attachInterrupt(PIN_PD2, sendHeartbeatInterrupt, FALLING);
289289

290290
// Set PF6 as input (reset button)
291-
pinConfigure(PIN_PF6, PIN_DIR_INPUT);
291+
pinConfigure(PIN_PF6, PIN_DIR_INPUT | PIN_PULLUP_ON);
292292
attachInterrupt(PIN_PF6, resetInterrupt, FALLING);
293293

294294
sei();

src/lte.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "lte.h"
2+
23
#include "led_ctrl.h"
34
#include "log.h"
45
#include "mqtt_client.h"
@@ -97,14 +98,22 @@ static void timezoneCallback(__attribute__((unused)) char* buffer) {
9798
got_timezone = true;
9899
}
99100

100-
bool LteClass::begin(const bool print_messages) {
101+
bool LteClass::begin(const uint32_t timeout_ms, const bool print_messages) {
102+
103+
const uint32_t start = millis();
101104

102105
// If low power is utilized, sequans controller will already been
103106
// initialized, so don't reset it by calling begin again
104107
if (!SequansController.isInitialized()) {
105108
SequansController.begin();
106109
}
107110

111+
// Disconnect before configuration if already connected
112+
SequansController.writeCommand(AT_DISCONNECT);
113+
114+
delay(100);
115+
SequansController.clearReceiveBuffer();
116+
108117
SequansController.registerCallback(TIMEZONE_CALLBACK, timezoneCallback);
109118

110119
SequansController.writeCommand(AT_ENABLE_TIME_ZONE_UPDATE);
@@ -154,7 +163,7 @@ bool LteClass::begin(const bool print_messages) {
154163
Log.infof("Connecting to operator");
155164
}
156165

157-
while (!isConnected()) {
166+
while (!isConnected() && millis() - start < timeout_ms) {
158167
LedCtrl.toggle(Led::CELL, true);
159168
delay(500);
160169

@@ -163,6 +172,20 @@ bool LteClass::begin(const bool print_messages) {
163172
}
164173
}
165174

175+
if (millis() - start >= timeout_ms) {
176+
177+
Log.rawf(" Was not able to connect to the network within the timeout "
178+
"of %d ms. Consider increasing the timeout or checking your "
179+
"cellular coverage.\r\n",
180+
timeout_ms);
181+
182+
SequansController.unregisterCallback(CEREG_CALLBACK);
183+
SequansController.unregisterCallback(TIMEZONE_CALLBACK);
184+
SequansController.writeCommand(AT_DISCONNECT);
185+
186+
return false;
187+
}
188+
166189
if (print_messages) {
167190
Log.rawf(" OK!\r\n");
168191
}
@@ -267,6 +290,7 @@ void LteClass::end(void) {
267290
while (isConnected() && millis() - start < 2000) {}
268291
SequansController.unregisterCallback(CEREG_CALLBACK);
269292

293+
SequansController.clearReceiveBuffer();
270294
SequansController.end();
271295
}
272296

src/lte.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ class LteClass {
3030
* @brief Initializes the LTE module and its controller interface. Connects
3131
* to the network.
3232
*
33+
* @param timeout_ms The amount of time to wait for connection before
34+
* aborting.
3335
* @param print_messages If set to true, the messages related to connection
3436
* will be logged.
3537
*
3638
* @return True if initialization was successful and connection was made.
3739
*/
38-
bool begin(const bool print_messages = true);
40+
bool begin(const uint32_t timeout_ms = 600000,
41+
const bool print_messages = true);
3942

4043
/**
4144
* @brief Disables the interface with the LTE module. Disconnects from

0 commit comments

Comments
 (0)