Contents



Description

Microsoft Azure is a cloud computing platform and an online portal that allows you to access and manage cloud services and resources provided by Microsoft. These services and resources include storing your data and transforming it, depending on your requirements. To get access to these resources and services, all you need to have is an active internet connection and the ability to connect to the Azure portal.

Azure Platform supports to forward the data through HTTPs and MQTT, this article describes how to connect Milesight LoRaWAN Gateway to Microsoft Azure using MQTT.

Note: Milesight gateways only support sending uplinks to Azure via MQTT, downlink is not supported.


Requirement

1. Milesight LoRaWAN Gateway (UG56/UG6x/UG8x)

2. Microsoft Account

3. Visual Studio Code App installed in windows (optional)


Configuration

Step1. Apply for Azure Account and Login the Portal

Create a Microsoft account then login Azure by the following URL: https://portal.Azure.com.


Step2. Create IoT Hub after Login

From the Azure homepage, select the + Create a resource button, and then enter IoT Hub in the Search the Marketplace field. Select IoT Hub from the search results, and then select Create


You will need to input and choose the subscription, Region and set the IoT Hub name there.


Step3. Create a Device

You will need to input the Device ID, which will be used in the Gateway's 'Client ID' field for MQTT communication. In this example, the name is UG65


Click save, then the configuration in Microsoft Azure side is finished.


More detailed info about the portal configuration, you can also refer to this page


Step4. Create SAS Token for Gateway

Azure supports to generate SAS tokens with CLI extension command or Visual Studio code, click here for more details. 


Generate via CLI Command

1. Click here to go to Azure Console, then create a storage and select Bash environment.


2. Type below command to generate the SAS token:

az iot hub generate-sas-token --connection-string 'HostName=myhub.azure-devices.net;DeviceId=mydevice;SharedAccessKeyName=iothubowner;SharedAccessKey=12345' --du 31536000

Note: 

  • Replace the connection string as your device primary connection string.
  • “--du 31536000” means the valid token duration in seconds, please adjust the duration as required. Click here for more advanced command parameters.


Generate result:


Generate via Visual Studio Code

Download Visual Studio Code, and install it into your Windows PC, search and install the Azure IoT Hub in the Visual Studio code.

Login using your Microsoft account, you can see the IoT Hub here.

Press ctrl+shift+p key, Choose 'generate SAS Token for device'.

Choose the device UG65 in this example. If you create other device in the same portal, it will display here as well.

Configure the expiration time, in this example is 300

You will get the SAS Token

Note: To extend the SAS token’s expire time, you can set 31,536,000 seconds for one year or more, then you will not need to worry about renewing it.



Step5. Configure the MQTT Details in Milesight Gateway

Login Milesight Gateway’s web interface, create an application in Network Server > Applications page.

Input the application name and the description, then click Save

Edit the Application we created just now, then choose MQTT in the Type list.

The details can be found on the device connection string:

Broker Address: HostName 

Broker Port: 8883

ClientID: Device ID

Username: HostName/{Device ID}

Password: SAS Token (Generate in Step4)

TLS: enabled and select as CA signed server certificate

Publish topic: devices/{Device ID}/messages/events/


For more info about the MQTT details definition, try to click this URL.

Note: The Client ID should be unique for each gateway you want to configure in Azure.



Step6. Check the Device Status

Copy the Token in the MQTT configuration, click Save, then we can check the MQTT connection status here:

You can also get the device status info in Visual Studio Code as well by Get Device Info.


Step7. Check the Sensor Data

Try to add a LoRaWAN sensor in Device page, input the DevEUI, AppKey, choose the Application we create just now.

Click Save&Apply, then power on the sensor. When the sensor is online, you can see data uplinks on Packets page.
Click here to go to Azure Console, then create a storage and select Bash environment. 

Here is an example log output:

naytelwmykt@Azure:~$ az extension add --name azure-iot 

naytelwmykt@Azure:~$ az iot hub monitor-events --hub-name ug65 --output table 

Starting event monitor, use ctrl-c to stop...


You will get the event like this, which is the uplink data from the Gateway.


event: origin: ug65 

payload: "{"applicationID":"1","applicationName":"app","data":"AwAABAEA","devEUI":"24E124C0002A1234","deviceName":"n1","fCnt":5,"fPort":85,"rxInfo":[{"altitude":0,"latitude":0,"loRaSNR":8.5,"longitude":0,"mac":"24e124fffef0395d","name":"local_gateway","rssi":-28,"time":"2020-06-03T08:14:23.868355Z"}],"time":"2020-06-03T08:14:23.868355Z","txInfo":{"adr":true,"codeRate":"4/5","dataRate":{"bandwidth":125,"modulation":"LORA","spreadFactor":7},"frequency":868300000}}"


 

Since the Data here is still the original one, refer to article How to Use Payload Codec on Milesight Gateway to add decoder to make it understandable.

After adding, you will see the data print in Visual Studio Code in next uplink.

event: 
origin: ug65
payload:
{
"battery": 92,
"temperature": 30.8,
"humidity": 50.5,
"activity": 73,
"illumination": 28,
"infrared": 20,
"infrared_and_visible": 121,
"co2": 1255,
"tvoc": 7,
"pressure": 1004.7
}

Note: If you configure the sensor from other company, you need to contact their support to get the Decoder Script (Java Script), then configure it there.

For more commands about Azure CLI, try to refer to this page.


 


Trouble Shooting

If MQTT connection status is Connect Failed, you will need to check the network settings of the gateway, make sure you can ping the MQTT Broker’s address correctly. Then check the MQTT configuration details, make sure the value you input follows the rule.

If you didn't find the data output when the MQTT is connected, try to check the "lora-app-server.log" inside the Gateway's system log, and check whether you can see the logs like this. You can download it through Maintenance >Log page.


 This is the log that shows the gateway has forwarded the data to Azure through MQTT

time="2020-06-03T16:15:26+08:00" level=info msg="urhandler/mqtt: publishing message" deveui=24E124C0002A1234 qos=0 topic=devices/ug85/messages/events/

You can also Deliver Ticket and attach the full description and the log package for a deeper check.