Skip to content

This repository contains a sample application showing how to connect a Wio Terminal to Azure IoT Hub to send telemetry and receive commands.

Notifications You must be signed in to change notification settings

SeeedJP/wioterminal-aziot-example

 
 

Repository files navigation

Welcome to wioterminal-aziot-example

License: MIT

This application shows you how to connect your Wio Terminal from Seeed to Azure IoT Hub. It is built on top of the Azure SDK for Embedded C, a small footprint, easy-to-port library for communicating with Azure services.

As the Wio Terminal is one of PlatformIO's (many!) supported platforms, the application is conveniently made available as a PlatformIO project. This means that you don't have to worry about installing the multiple Arduino libraries the Wio Terminal requires for Wi-Fi & TLS, and you don't need to manually install any other third-party library either! All dependencies are automatically fetched from Github by the PlatformIO Library Manager.

Supported device attestation methods

The following table summarizes supported device attestation/authentication methods :

Service Enrollment Authentication Support status
Azure IoT Hub - Symmetric Key Need recompile
Azure IoT Hub - X.509 Self-Signed Not Supported
Azure IoT Hub - X.509 CA-Signed Not Supported
Azure IoT DPS Group Symmetric Key Supported
Azure IoT DPS Group CA Certificate Not Supported
Azure IoT DPS Group Intermediate Certificate Not Supported
Azure IoT DPS Individual Symmetric Key Supported
Azure IoT DPS Individual X.509 Not Supported
Azure IoT DPS Individual TPM Not Supported

Update the Wi-Fi Firmware

Please follow instruction at Wio Terminal's network overview page to update the firmware.

You must use the version of Wi-Fi firmware specified in Azure IoT Firmware. See the release page.

Configure directly connect to Azure IoT Hub

If you use provisioning service, skip this section.

Configure Azure IoT Hub

  1. Create an Azure IoT Hub. See here. Do "Create an IoT hub" section only.
  2. Select IoT devices page in your Azure IoT Hub on Azure portal.
  3. Select +New.
  4. Input Device ID and click Save button.

Configure Application

  1. Open include/config.h in text editor.
  2. Comment out #define USE_CLI.
  3. Modify [wifi ssid] to your Wi-Fi SSID.
  4. Modify [wifi password] to your Wi-Fi password.
  5. Modify [Azure IoT Hub host name] to hostname of your Azure IoT Hub.
  6. Modify [device id] to the Device ID of the device.
  7. Modify [symmetric key] to primary key or secondary key of the device.

Configure use provisioning to connect to Azure IoT Hub

If you DON'T use provisioning service, skip this section.

Create Azure IoT Hub

  1. Create an Azure IoT Hub. See here. Do "Create an IoT hub" section only.

Configure Azure IoT Hub DPS

  1. Create an Azure IoT Hub DPS. See here. Do "Create a new IoT Hub Device Provisioning Service" section only.
  2. Link Azure IoT Hub and Azure IoT Hub DPS. See here. Do "Link the IoT hub and your Device Provisioning Service" section only.
  3. Select Manage enrollments page in your Azure IoT Hub DPS on Azure portal.
  4. Select +Add individual enrollment.
  5. Change X.509 at Mechanism to Symmetric Key.
  6. Input Registration ID.
  7. Input IoT Hub Device ID.
  8. Click Save button.

Configure application

  1. Open include/config.h in text editor.
  2. Comment out #define USE_CLI.
  3. Uncomment #define USE_DPS.
  4. Modify [wifi ssid] to your Wi-Fi SSID.
  5. Modify [wifi password] to your Wi-Fi password.
  6. Modify [id scope] to hostname of your Azure IoT Hub DPS.
  7. Modify [registration id] to the Registration ID of the indivisual enrollment.
  8. Modify [symmetric key] to primary key or secondary key of the individual enrollment.

Running the application

Use the PlatformIO IDE (VS Code extension) or the PlatformIO command-line interface to deploy the application to your Wio Terminal.

Once running, the application will connect to IoT Hub and:

  • send telemetry—the acceleration values from the 3-axis acceleration sensor—every 2 second.
  • listen to a ringBuzzer command that, when triggered from the Cloud will... ring the buzzer! The duration is provided as a command parameter.

Testing the Application

You can use the Azure IoT Explorer to test that your Wio Terminal is properly connected to Azure IoT Hub, i.e that it is regularly sending telemetry data, and responding to commands.

Testing that telemetry is correctly sent to Azure IoT Hub

To check that telemetry data is correctly sent, you will want to subscribe to the telemetry feed of your device. Since this code leverages IoT Plug and Play, Azure IoT Explorer will provide you with extra information regarding the data model for your acceleration sensor data (e.g the unit of measurement being "g") if you enable the "Show modeled events" option.

Sending a command from Azure IoT Hub

In order to send the ringBuzzer command, head over to the "Commands" section of the IoT Plug and Play default component of your device, enter a duration, and send the command.

A few words on the Azure SDK for Embedded C and how it's been ported to Wio Terminal

Note: As of today, the Azure SDK for Embedded C is still being actively developed, therefore, it hasn't been officially released as an Arduino or PlatformIO library. To make it easier for you to get started, the Azure IoT client libraries have been included in the lib/azure-sdk-for-c folder. You can synchronize them with the latest version from the Embedded C SDK github repository by running the lib/download_aziot_embedded_c_lib.sh script.

You can read more on the Azure IoT client library here, but in a nutshell, here's what had to be done to get the Wio Terminal connected:

  • As this application uses symmetric keys to authenticate, a security token needs to be generated.
    • Since the generated token has an expiration date (typically set to a few hours in the future), we need to know the current date and time. We use an NTP library to get the current time from a time server.
    • The token includes an HMAC-SHA256 signature string that needs to be base64-encoded. Luckily, the recommended WiFi+TLS stack of the Wio Terminal already includes Mbed TLS, making it relatively simple to compute HMAC signatures (ex. mbedtls_md_hmac_starts) and perform base64 encoding (ex. mbedtls_base64_encode).
  • The Azure IoT client libraries help with crafting MQTT topics that follow the Azure IoT conventions, but you still need to provide your own MQTT library. In fact, that is a major difference with the historical Azure IoT C SDK, for which the MQTT implementation was baked into it. Since it is widely supported and just worked out-of-the-box, this application uses the PubSubClient MQTT library from Nick O'Leary.
  • And of course, one has to implement their own application logic. For this application, this meant using the Wio Terminal's acceleration sensor driver to get acceleration data every 2 seconds, or hooking up the ringBuzzer command to actual embedded code that rings the buzzer.

Author

👤 Benjamin Cabé

👤 Seeed K.K.

🤝 Contributing

Contributions, issues and feature requests are welcome!

Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2020 Benjamin Cabé.
Copyright © 2020 Seeed K.K.

This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator

About

This repository contains a sample application showing how to connect a Wio Terminal to Azure IoT Hub to send telemetry and receive commands.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 98.0%
  • C 2.0%