2
2
* @brief This example demonstrates how to connect to the Azure IoT Hub using
3
3
* the ATECC608 cryptographic chip on the AVR-Iot Cellular mini. Please make
4
4
* sure your board is provisioned first with the provision sketch.
5
+ *
6
+ * With Azure, we use a wildcare for subscription, so we need to enable a
7
+ * callback for the received messages so that we can grab the specific topic.
5
8
*/
6
9
7
10
#include < Arduino.h>
@@ -18,6 +21,19 @@ const char MQTT_SUB_TOPIC_FMT[] PROGMEM = "devices/%s/messages/devicebound/#";
18
21
static char mqtt_sub_topic[128 ];
19
22
static char mqtt_pub_topic[128 ];
20
23
24
+ static volatile bool got_message_event = false ;
25
+ static char message_topic[384 ];
26
+ static volatile uint16_t message_length = 0 ;
27
+
28
+ static void onReceive (const char * topic,
29
+ const uint16_t length,
30
+ __attribute__ ((unused)) const int32_t id) {
31
+ strcpy (message_topic, topic);
32
+ message_length = length;
33
+
34
+ got_message_event = true ;
35
+ }
36
+
21
37
bool initTopics () {
22
38
ATCA_STATUS status = ECC608.begin ();
23
39
@@ -67,6 +83,7 @@ void setup() {
67
83
// Attempt to connect to Azure
68
84
if (MqttClient.beginAzure ()) {
69
85
MqttClient.subscribe (mqtt_sub_topic);
86
+ MqttClient.onReceive (onReceive);
70
87
} else {
71
88
while (1 ) {}
72
89
}
@@ -83,15 +100,19 @@ void setup() {
83
100
Log.error (F (" Failed to publish\r\n " ));
84
101
}
85
102
86
- delay (2000 );
103
+ if (got_message_event) {
104
+
105
+ String message = MqttClient.readMessage (message_topic,
106
+ message_length);
87
107
88
- String message = MqttClient.readMessage (mqtt_sub_topic);
108
+ // Read message will return an empty string if there were no new
109
+ // messages, so anything other than that means that there were a
110
+ // new message
111
+ if (message != " " ) {
112
+ Log.infof (F (" Got new message: %s\r\n " ), message.c_str ());
113
+ }
89
114
90
- // Read message will return an empty string if there were no new
91
- // messages, so anything other than that means that there were a
92
- // new message
93
- if (message != " " ) {
94
- Log.infof (F (" Got new message: %s\r\n " ), message.c_str ());
115
+ got_message_event = false ;
95
116
}
96
117
97
118
delay (2000 );
0 commit comments