|
1 | | -/** |
2 | | - * This example uses polling for the LTE module and the MQTT module when |
3 | | - * checking for new messages. |
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 | | - |
14 | | -#define MQTT_SUB_TOPIC "mchp_topic" |
15 | | -#define MQTT_PUB_TOPIC "mchp_topic" |
16 | | - |
17 | | -#define MQTT_THING_NAME "someuniquemchp" |
18 | | -#define MQTT_BROKER "test.mosquitto.org" |
19 | | -#define MQTT_PORT 1883 |
20 | | -#define MQTT_USE_TLS false |
21 | | -#define MQTT_USE_ECC false |
22 | | -#define MQTT_KEEPALIVE 60 |
23 | | - |
24 | | -volatile bool lteConnected = false; |
25 | | -volatile bool mqttConnected = false; |
26 | | - |
27 | | -void mqttDisconnectHandler() { mqttConnected = false; } |
28 | | - |
29 | | -void lteDisconnectHandler() { lteConnected = false; } |
30 | | - |
31 | | -void connectMqtt() { |
32 | | - MqttClient.onConnectionStatusChange(NULL, mqttDisconnectHandler); |
33 | | - |
34 | | - mqttConnected = MqttClient.begin(MQTT_THING_NAME, |
35 | | - MQTT_BROKER, |
36 | | - MQTT_PORT, |
37 | | - MQTT_USE_TLS, |
38 | | - MQTT_KEEPALIVE, |
39 | | - MQTT_USE_ECC); |
40 | | - |
41 | | - if (mqttConnected) { |
42 | | - Log.info("Connecting to broker..."); |
43 | | - while (!MqttClient.isConnected()) { |
44 | | - Log.info("Connecting..."); |
45 | | - delay(500); |
46 | | - |
47 | | - // If we're not connected to the network, give up |
48 | | - if (!lteConnected) { |
49 | | - return; |
50 | | - } |
51 | | - } |
52 | | - |
53 | | - Log.info("Connected to broker!"); |
54 | | - |
55 | | - MqttClient.subscribe(MQTT_SUB_TOPIC); |
56 | | - } else { |
57 | | - Log.error("Failed to connect to broker"); |
58 | | - } |
59 | | -} |
60 | | - |
61 | | -void connectLTE() { |
62 | | - |
63 | | - Lte.onConnectionStatusChange(NULL, lteDisconnectHandler); |
64 | | - |
65 | | - // Start LTE modem and wait until we are connected to the operator |
66 | | - Lte.begin(); |
67 | | - |
68 | | - while (!Lte.isConnected()) { |
69 | | - Log.info("Not connected to operator yet..."); |
70 | | - delay(5000); |
71 | | - } |
72 | | - |
73 | | - Log.info("Connected to operator!"); |
74 | | - lteConnected = true; |
75 | | -} |
76 | | - |
77 | | -void setup() { |
78 | | - Log.begin(115200); |
79 | | - LedCtrl.begin(); |
80 | | - LedCtrl.startupCycle(); |
81 | | - |
82 | | - Log.info("Starting initialization of MQTT Polling"); |
83 | | -} |
84 | | - |
85 | | -static uint32_t counter = 0; |
86 | | - |
87 | | -void loop() { |
88 | | - |
89 | | - if (mqttConnected) { |
90 | | - |
91 | | - String message = MqttClient.readMessage(MQTT_SUB_TOPIC); |
92 | | - |
93 | | - // Read message will return an empty string if there were no new |
94 | | - // messages, so anything other than that means that there were a new |
95 | | - // message |
96 | | - if (message != "") { |
97 | | - Log.infof("Got new message: %s\r\n", message.c_str()); |
98 | | - } |
99 | | - |
100 | | - String message_to_publish = String("Hello world: " + String(counter)); |
101 | | - |
102 | | - bool publishedSuccessfully = |
103 | | - MqttClient.publish(MQTT_PUB_TOPIC, message_to_publish.c_str()); |
104 | | - |
105 | | - if (publishedSuccessfully) { |
106 | | - Log.infof("Published message: %s\r\n", message_to_publish.c_str()); |
107 | | - counter++; |
108 | | - } else { |
109 | | - Log.error("Failed to publish"); |
110 | | - } |
111 | | - } else { |
112 | | - // MQTT is not connected. Need to establish connection |
113 | | - |
114 | | - if (!lteConnected) { |
115 | | - // We're NOT connected to the LTE Network. Establish LTE connection |
116 | | - // first |
117 | | - Log.info("LTE is not connected. Establishing..."); |
118 | | - connectLTE(); |
119 | | - } |
120 | | - |
121 | | - connectMqtt(); |
122 | | - } |
123 | | - |
124 | | - delay(1000); |
125 | | -} |
| 1 | +/** |
| 2 | + * This example uses polling for the LTE module and the MQTT module when |
| 3 | + * checking for new messages. |
| 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 | + |
| 14 | +#define MQTT_SUB_TOPIC "mchp_topic" |
| 15 | +#define MQTT_PUB_TOPIC "mchp_topic" |
| 16 | + |
| 17 | +#define MQTT_THING_NAME "someuniquemchp" |
| 18 | +#define MQTT_BROKER "test.mosquitto.org" |
| 19 | +#define MQTT_PORT 1883 |
| 20 | +#define MQTT_USE_TLS false |
| 21 | +#define MQTT_USE_ECC false |
| 22 | +#define MQTT_KEEPALIVE 60 |
| 23 | + |
| 24 | +volatile bool lteConnected = false; |
| 25 | +volatile bool mqttConnected = false; |
| 26 | + |
| 27 | +void mqttDisconnectHandler() { mqttConnected = false; } |
| 28 | + |
| 29 | +void lteDisconnectHandler() { lteConnected = false; } |
| 30 | + |
| 31 | +void connectMqtt() { |
| 32 | + MqttClient.onConnectionStatusChange(NULL, mqttDisconnectHandler); |
| 33 | + |
| 34 | + mqttConnected = MqttClient.begin(MQTT_THING_NAME, |
| 35 | + MQTT_BROKER, |
| 36 | + MQTT_PORT, |
| 37 | + MQTT_USE_TLS, |
| 38 | + MQTT_KEEPALIVE, |
| 39 | + MQTT_USE_ECC); |
| 40 | + |
| 41 | + if (mqttConnected) { |
| 42 | + Log.info("Connecting to broker..."); |
| 43 | + while (!MqttClient.isConnected()) { |
| 44 | + Log.info("Connecting..."); |
| 45 | + delay(500); |
| 46 | + |
| 47 | + // If we're not connected to the network, give up |
| 48 | + if (!lteConnected) { |
| 49 | + return; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Log.info("Connected to broker!"); |
| 54 | + |
| 55 | + MqttClient.subscribe(MQTT_SUB_TOPIC); |
| 56 | + } else { |
| 57 | + Log.error("Failed to connect to broker"); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void connectLTE() { |
| 62 | + |
| 63 | + Lte.onConnectionStatusChange(NULL, lteDisconnectHandler); |
| 64 | + |
| 65 | + // Start LTE modem and wait until we are connected to the operator |
| 66 | + Lte.begin(); |
| 67 | + |
| 68 | + while (!Lte.isConnected()) { |
| 69 | + Log.info("Not connected to operator yet..."); |
| 70 | + delay(5000); |
| 71 | + } |
| 72 | + |
| 73 | + Log.info("Connected to operator!"); |
| 74 | + lteConnected = true; |
| 75 | +} |
| 76 | + |
| 77 | +void setup() { |
| 78 | + Log.begin(115200); |
| 79 | + LedCtrl.begin(); |
| 80 | + LedCtrl.startupCycle(); |
| 81 | + |
| 82 | + Log.info("Starting initialization of MQTT Polling"); |
| 83 | +} |
| 84 | + |
| 85 | +static uint32_t counter = 0; |
| 86 | + |
| 87 | +void loop() { |
| 88 | + |
| 89 | + if (mqttConnected) { |
| 90 | + |
| 91 | + String message = MqttClient.readMessage(MQTT_SUB_TOPIC); |
| 92 | + |
| 93 | + // Read message will return an empty string if there were no new |
| 94 | + // messages, so anything other than that means that there were a new |
| 95 | + // message |
| 96 | + if (message != "") { |
| 97 | + Log.infof("Got new message: %s\r\n", message.c_str()); |
| 98 | + } |
| 99 | + |
| 100 | + String message_to_publish = String("Hello world: " + String(counter)); |
| 101 | + |
| 102 | + bool publishedSuccessfully = |
| 103 | + MqttClient.publish(MQTT_PUB_TOPIC, message_to_publish.c_str()); |
| 104 | + |
| 105 | + if (publishedSuccessfully) { |
| 106 | + Log.infof("Published message: %s\r\n", message_to_publish.c_str()); |
| 107 | + counter++; |
| 108 | + } else { |
| 109 | + Log.error("Failed to publish"); |
| 110 | + } |
| 111 | + } else { |
| 112 | + // MQTT is not connected. Need to establish connection |
| 113 | + |
| 114 | + if (!lteConnected) { |
| 115 | + // We're NOT connected to the LTE Network. Establish LTE connection |
| 116 | + // first |
| 117 | + Log.info("LTE is not connected. Establishing..."); |
| 118 | + connectLTE(); |
| 119 | + } |
| 120 | + |
| 121 | + connectMqtt(); |
| 122 | + } |
| 123 | + |
| 124 | + delay(1000); |
| 125 | +} |
0 commit comments