These examples connect the Pico LTE to Azure IoT Hub over MQTT on the cellular connection: they publish device-to-cloud data to the device twin and receive cloud-to-device messages. The board authenticates with X.509 certificates.
Before you begin
Hardware
- Sixfab Pico LTE or Pico LTE 2Registered and active in Sixfab Connect, connected to the computer with the micro USB cable.
Software
- Thonny with MicroPython and the Pico LTE SDKSet up as in the quickstart.
- config.jsonIn the root of the board: Prepare the configuration file.
- Azure accountSign in or register at the Azure portal.
- OpenSSLOn the computer, to create the certificates.
Create IoT hub
-
Start new IoT hub.
In the Azure portal, search for IoT Hub, open the service and click Create IoT Hub.


-
Set resource group and name.
Select a resource group, or click Create new, and name the hub: the IoT hub name goes into
config.json. Check the other settings, such as the pricing in the Management tab, and click Review + create.
-
Create hub.
Review the settings and click Create. When Azure has deployed the hub, click Go to resource.

Create certificates and device
-
Create certificates.
Follow these steps of Microsoft’s X.509 tutorial on the computer:
-
Add intermediate CA certificate to hub.
In the hub’s menu, search for Certificates and open it, then click Add.


Enter a certificate name, select
certificateDirectory/certs/azure-iot-test-only.intermediate.cert.pem, tick Set certificate status to verified on upload and click Save.
-
Add device.
In the hub’s menu, search for Devices, open it and click Add Device.


Enter the device ID, the device name used when you created its certificate; it also goes into
config.json. Select X.509 CA Signed as the authentication type and click Save.
-
Download root certificate.
Download the DigiCert Global Root G2 certificate, the root certificate of Azure IoT Hub, and save it as
cacert.pem.
Upload certificates to board
-
Create folder named cert.
Outside the certificate directory of the previous section.
-
Copy certificates into it, renamed.
certificateDirectory/certs/[DEVICE_NAME].cert.pem→client.pemcertificateDirectory/private/[DEVICE_NAME].key.pem→user_key.pem- the downloaded root certificate,
cacert.pem
-
Upload cert folder to root of board.


On the first run, the SDK moves the certificates into the module’s secure storage and deletes them from the board’s file system.
Run examples
The scripts are in the examples/azure folder of the SDK repository.
-
Set hub and device in config.json.
{ "azure": { "hub_name": "YOUR_AZURE_IOT_HUB_NAME", "device_id": "YOUR_DEVICE_ID" } } -
Run mqtt_publish.py.
Open
examples/azure/mqtt_publish.pyin Thonny and click Run.
INFO: Publishing data to Azure IoT Hub... INFO: Certificates uploaded secure storage. Deleting from file system... INFO: Certificates found in modem. INFO: Result {'status': 0, 'response': ['OK'], 'interval': 0} -
Check device twin.
In the hub, open Devices, select the device and click Device Twin.

The device twin is a JSON document of the device’s configuration and data: the published data is under
reported. Click Refresh if it does not show yet.
-
Set hub, device and topic in config.json.
{ "azure": { "hub_name": "YOUR_AZURE_IOT_HUB_NAME", "device_id": "YOUR_DEVICE_ID", "mqtts": { "sub_topics": [ ["[SUB_TOPIC/1]", [QOS]], ["[SUB_TOPIC/2]", [QOS]] ] } } }The cloud-to-device topic is
devices/[DEVICE_ID]/messages/devicebound/#:"sub_topics": [["devices/[DEVICE_ID]/messages/devicebound/#", 1]]. -
Open Message to Device.
In the hub, open Devices, select the device and click Message to Device: the form sends a cloud-to-device message.

-
Run mqtt_subscribe.py.
Open
examples/azure/mqtt_subscribe.pyin Thonny and click Run.
-
Send message.
Send a message from the Message to Device form. The log shows it:
INFO: Subscribing to topics... INFO: {'status': 0, 'response': ['+QMTSUB: 0,1,0,1'], 'interval': 0} INFO: Reading messages from subscribed topics... INFO: [] INFO: [] INFO: ['4,"devices/first_device/messages/devicebound/%24.to=%2Fdevices%2Ffirst_device%2Fmessages%2FdeviceBound&%24.ct=text%2Fplain%3B%20charset%3DUTF-8&%24.ce=utf-8","Hello Pico"']