Skip to content

Commit 681518d

Browse files
committed
MCU8MASS-1785 Add Azure example and restructure the other examples for specifically AWS and custom brokers. This commit also fixes a bug with writing the device certificate to the modem for Azure provisioning
1 parent 93282fe commit 681518d

File tree

8 files changed

+412
-137
lines changed

8 files changed

+412
-137
lines changed

examples/mqtt_polling_aws/mqtt_polling_aws.ino renamed to examples/mqtt_aws/mqtt_aws.ino

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,36 @@
2222
char mqtt_sub_topic[128];
2323
char mqtt_pub_topic[128];
2424

25-
bool initMQTTTopics() {
26-
ECC608.begin();
25+
bool initTopics() {
26+
ATCA_STATUS status = ECC608.begin();
27+
28+
if (status != ATCA_SUCCESS) {
29+
Log.errorf("Failed to initialize ECC, error code: %X\r\n", status);
30+
return false;
31+
}
2732

2833
// Find the thing ID and set the publish and subscription topics
29-
uint8_t thingName[128];
30-
size_t thingNameLen = sizeof(thingName);
34+
uint8_t thing_name[128];
35+
size_t thing_name_length = sizeof(thing_name);
36+
37+
status =
38+
ECC608.readProvisionItem(AWS_THINGNAME, thing_name, &thing_name_length);
3139

32-
// -- Get the thingname
33-
ATCA_STATUS status =
34-
ECC608.readProvisionItem(AWS_THINGNAME, thingName, &thingNameLen);
3540
if (status != ATCA_SUCCESS) {
36-
Log.error("Could not retrieve thingname from the ECC");
41+
Log.errorf(
42+
"Could not retrieve thingname from the ECC, error code: %X\r\n",
43+
status);
3744
return false;
3845
}
3946

40-
sprintf(mqtt_sub_topic, MQTT_SUB_TOPIC_FMT, thingName);
41-
sprintf(mqtt_pub_topic, MQTT_PUB_TOPIC_FMT, thingName);
47+
snprintf(mqtt_sub_topic,
48+
sizeof(mqtt_sub_topic),
49+
MQTT_SUB_TOPIC_FMT,
50+
thing_name);
51+
snprintf(mqtt_pub_topic,
52+
sizeof(mqtt_pub_topic),
53+
MQTT_PUB_TOPIC_FMT,
54+
thing_name);
4255

4356
return true;
4457
}
@@ -48,23 +61,22 @@ void setup() {
4861
LedCtrl.begin();
4962
LedCtrl.startupCycle();
5063

51-
Log.info("Starting MQTT Polling for AWS example\r\n");
64+
Log.info("Starting MQTT for AWS example\r\n");
5265

53-
if (initMQTTTopics() == false) {
66+
if (!initTopics()) {
5467
Log.error("Unable to initialize the MQTT topics. Stopping...");
5568
while (1) {}
5669
}
5770

5871
if (!Lte.begin()) {
5972
Log.error("Failed to connect to operator");
60-
61-
// Halt here
6273
while (1) {}
6374
}
6475

65-
// Attempt to connect to the broker
76+
// Attempt to connect to AWS
6677
if (MqttClient.beginAWS()) {
67-
Log.infof("Connecting to broker");
78+
79+
Log.infof("Connecting to AWS");
6880

6981
while (!MqttClient.isConnected()) {
7082
Log.rawf(".");
@@ -73,19 +85,18 @@ void setup() {
7385

7486
Log.rawf(" OK!\r\n");
7587

76-
// Subscribe to the topic
7788
MqttClient.subscribe(mqtt_sub_topic);
89+
7890
} else {
7991
Log.rawf("\r\n");
80-
Log.error("Failed to connect to broker");
81-
82-
// Halt here
92+
Log.error("Failed to connect to AWS");
8393
while (1) {}
8494
}
8595

8696
// Test MQTT publish and receive
8797
for (uint8_t i = 0; i < 3; i++) {
88-
bool published_successfully =
98+
99+
const bool published_successfully =
89100
MqttClient.publish(mqtt_pub_topic, "{\"light\": 9, \"temp\": 9}");
90101

91102
if (published_successfully) {
@@ -109,6 +120,7 @@ void setup() {
109120
}
110121

111122
Log.info("Closing MQTT connection");
123+
112124
MqttClient.end();
113125
}
114126

examples/mqtt_azure/mqtt_azure.ino

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @brief This example demonstrates how to connect to the Azure IoT Hub using
3+
* the ATECC608 cryptographic chip on the AVR-Iot Cellular mini. Please make
4+
* sure your board is provisioned first with the provision sketch.
5+
*/
6+
7+
#include <Arduino.h>
8+
9+
#include <ecc608.h>
10+
#include <led_ctrl.h>
11+
#include <log.h>
12+
#include <lte.h>
13+
#include <mqtt_client.h>
14+
15+
#define MQTT_PUB_TOPIC_FMT "devices/%s/messages/events/"
16+
#define MQTT_SUB_TOPIC_FMT "%s/messages/devicebound/#"
17+
18+
static char mqtt_sub_topic[128];
19+
static char mqtt_pub_topic[128];
20+
21+
bool initTopics() {
22+
ATCA_STATUS status = ECC608.begin();
23+
24+
if (status != ATCA_SUCCESS) {
25+
Log.errorf("Failed to initialize ECC, error code: %X\r\n", status);
26+
return false;
27+
}
28+
29+
// Find the device ID and set the publish and subscription topics
30+
uint8_t device_id[128];
31+
size_t device_id_length = sizeof(device_id);
32+
33+
status =
34+
ECC608.readProvisionItem(AZURE_DEVICE_ID, device_id, &device_id_length);
35+
36+
if (status != ATCA_SUCCESS) {
37+
Log.errorf(
38+
"Could not retrieve device ID from the ECC, error code: %X\r\n",
39+
status);
40+
return false;
41+
}
42+
43+
snprintf(mqtt_sub_topic,
44+
sizeof(mqtt_sub_topic),
45+
MQTT_SUB_TOPIC_FMT,
46+
device_id);
47+
snprintf(mqtt_pub_topic,
48+
sizeof(mqtt_pub_topic),
49+
MQTT_PUB_TOPIC_FMT,
50+
device_id);
51+
52+
return true;
53+
}
54+
55+
void setup() {
56+
Log.begin(115200);
57+
LedCtrl.begin();
58+
LedCtrl.startupCycle();
59+
60+
Log.info("Starting MQTT for Azure example\r\n");
61+
62+
if (!initTopics()) {
63+
Log.error("Unable to initialize the MQTT topics. Stopping...");
64+
while (1) {}
65+
}
66+
67+
if (!Lte.begin()) {
68+
Log.error("Failed to connect to operator");
69+
while (1) {}
70+
}
71+
72+
// Attempt to connect to Azure
73+
if (MqttClient.beginAzure()) {
74+
Log.infof("Connecting to Azure IoT Hub");
75+
76+
while (!MqttClient.isConnected()) {
77+
Log.rawf(".");
78+
delay(500);
79+
}
80+
81+
Log.rawf(" OK!\r\n");
82+
83+
MqttClient.subscribe(mqtt_sub_topic);
84+
85+
} else {
86+
Log.rawf("\r\n");
87+
Log.error("Failed to connect to Azure IoT Hub");
88+
while (1) {}
89+
}
90+
91+
// Test MQTT publish and receive
92+
for (uint8_t i = 0; i < 3; i++) {
93+
94+
const bool published_successfully =
95+
MqttClient.publish(mqtt_pub_topic, "{\"light\": 9, \"temp\": 9}");
96+
97+
if (published_successfully) {
98+
Log.info("Published message");
99+
} else {
100+
Log.error("Failed to publish\r\n");
101+
}
102+
103+
delay(2000);
104+
105+
String message = MqttClient.readMessage(mqtt_sub_topic);
106+
107+
// Read message will return an empty string if there were no new
108+
// messages, so anything other than that means that there were a
109+
// new message
110+
if (message != "") {
111+
Log.infof("Got new message: %s\r\n", message.c_str());
112+
}
113+
114+
delay(2000);
115+
}
116+
117+
Log.info("Closing MQTT connection");
118+
119+
MqttClient.end();
120+
}
121+
122+
void loop() {}

examples/mqtt_polling/mqtt_polling.ino renamed to examples/mqtt_custom_broker/mqtt_custom_broker.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void setup() {
2828
LedCtrl.begin();
2929
LedCtrl.startupCycle();
3030

31-
Log.info("Starting MQTT Polling example");
31+
Log.info("Starting MQTT with custom broker example (polling mode)");
3232

3333
// Establish LTE connection
3434
if (!Lte.begin()) {

0 commit comments

Comments
 (0)