Microsoft Azure

In order to use Microsoft Azure cloud service, a "Generic MQTT server" library is required. The onboarding sequence is outlined below.

Prerequisites:

  • ESP8266 NodeMCU or ESP32 development board
  • Microsoft Azure cloud service account

Onboarding sequence

Create an IoT Hub

  • Login to https://portal.azure.com
  • Create a new IoT Hub. Give it a name, in this tutorial cesanta2 is used
  • Choose to pin it on a dashboard
  • Once the hub is deployed, you should see it on your dashboard:

Open device explorer

  • On the left hand side menu, open a device explorer
  • Click on the Add button to register a new device
  • We need a device ID!

Flash default Mongoose OS firmware

Start mos tool, install the firmware for your board, and configure WiFi

Find out the device ID

  • Click on "Device Config" tab
  • Click "Expert View" button to see all config
  • Find device.id section
  • Copy the value into clipboard

Register device on Azure

  • Switch back to the Azure device registration page
  • Paste device ID into the field
  • Make sure you have "Autogenerate keys" checkbox checked
  • Click Save
  • The device should appear in the list:

Generate device password

Unfortunately Microsoft made it quite difficult to generate device passwords. The sequence is below:

  • Download Microsoft Device Explorer utility from Azure GitHub page
  • Run it. You shoud see a "Iot Hub Connection String" input field:

  • Switch to Azure console, navigate to "Shared Access policies" -> "iothubowner":

  • Copy "Connection String - primary Key", paste into the "IoT Hub Connection String" field
  • Click on Update:

  • Switch to the management tab
  • Click on the device in the list. Click on the "SAS Token" button
  • Set TTL to "365" in the dialog - that's 1 year
  • Click on Generate
  • Copy only part that is between green arrows: from "SharedAccessSignature" to the end:

Configure MQTT setting on device side

  • Switch to the "Expert View" device configuration page
  • Find mqtt section and change the following fields:
    • mqtt.enable=true
    • mqtt.server=YOUR_HUB_NAME.azure-devices.net:8883
    • mqtt.client_id=DEVICE_ID
    • mqtt.user=YOUR_HUB_NAME.azure-devices.net/DEVICE_ID
    • mqtt.pass=SharedAccessSignature............
    • mqtt.ssl_ca_cert=ca.pem
  • Click on Save Settings button when done

Modify device logic to send messages to Azure

  • In the Mongoose OS Web UI, switch to the "Device Files" tab
  • Click on init.js
  • Paste the following code that sends free RAM telemetry messages to Azure
  • Click "Save + Reboot" button:
  load('api_config.js');
  load('api_mqtt.js');
  load('api_sys.js');
  load('api_timer.js');

  let topic = 'devices/' + Cfg.get('device.id') + '/messages/events/';

  Timer.set(1000, true /* repeat */, function() {
    let msg = JSON.stringify({ ram: Sys.free_ram() });
    let ok = MQTT.pub(topic, msg, 1);
    print(ok, topic, '->', msg);
  }, null);

The device log should append a line each second, indicating successful report. In this case, we're reporting 35k of free RAM:

[Aug 18 10:48:02.570] 1 devices/esp8266_060046/messages/events/ -> {"ram":36068} 
[Aug 18 10:48:03.570] 1 devices/esp8266_060046/messages/events/ -> {"ram":36068} 
[Aug 18 10:48:04.571] 1 devices/esp8266_060046/messages/events/ -> {"ram":36068}

Verify stats on the Azure side

  • Switch to the Azure console
  • Navigate to the Metrics on the left side menu
  • Choose "Telemetry messages sent"
  • The graph should show the increasing trend: