|
| 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 | +} |
0 commit comments