README.rst
1.. zephyr:code-sample:: mqtt-azure
2 :name: Microsoft Azure IoT Hub MQTT
3 :relevant-api: bsd_sockets mqtt_socket tls_credentials random_api
4
5 Connect to Azure IoT Hub and publish messages using MQTT.
6
7Overview
8********
9
10This sample application demonstrates how an MQTT client
11can publish messages to an Azure Cloud IoT hub based on MQTT protocol.
12
13- Acquire a DHCPv4 lease
14- Establish a TLS connection with Azure Cloud IoT hub
15- Publish data to the Azure cloud
16- SOCKS5 supported
17- DNS supported
18
19The source code of this sample application can be found at:
20:zephyr_file:`samples/net/cloud/mqtt_azure`.
21
22Requirements
23************
24
25- Azure Cloud account
26- Azure IOT Cloud credentials and required information
27- Freedom Board (FRDM-K64F)
28- Network connectivity
29
30Building and Running
31********************
32
33This application has been built and tested on the NXP FRDMK64F.
34Certs are required to authenticate to the Azure Cloud IoT hub.
35Current certs in :zephyr_file:`samples/net/cloud/mqtt_azure/src/digicert.cer` are
36copied from `<https://github.com/Azure/azure-iot-sdk-c/blob/master/certs/certs.c>`_
37
38Configure the following Kconfig options based on your Azure Cloud IoT Hub
39in your own overlay config file:
40
41- ``SAMPLE_CLOUD_AZURE_USERNAME`` - Username field use::
42
43 {iothubhostname}/{device_id}/?api-version=2018-06-30,
44
45 where ``{iothubhostname}`` is the full CName of the IoT hub.
46
47- ``SAMPLE_CLOUD_AZURE_PASSWORD`` - Password field, use an SAS token.
48- ``SAMPLE_CLOUD_AZURE_CLIENT_ID`` - ClientId field, use the deviceId.
49- ``SAMPLE_CLOUD_AZURE_HOSTNAME`` - IoT hub hostname
50- ``SAMPLE_CLOUD_AZURE_SERVER_ADDR`` - IP address of the Azure MQTT broker
51- ``SAMPLE_CLOUD_AZURE_SERVER_PORT`` - Port number of the Azure MQTT broker
52
53You'll also need to set these Kconfig options if you're running
54the sample behind a proxy:
55
56- ``SAMPLE_SOCKS_ADDR`` - IP address of SOCKS5 Proxy server
57- ``SAMPLE_SOCKS_PORT`` - Port number of SOCKS5 Proxy server
58
59On your Linux host computer, open a terminal window, locate the source code
60of this sample application (i.e., :zephyr_file:`samples/net/cloud/mqtt_azure`) and type:
61
62.. zephyr-app-commands::
63 :zephyr-app: samples/net/cloud/mqtt_azure
64 :board: frdm_k64f
65 :conf: "prj.conf <overlay.conf>"
66 :goals: build flash
67 :compact:
68
69Also this application can be tested with QEMU. This is described in
70:ref:`networking_with_qemu`. Set up Zephyr and NAT/masquerading on host
71to access Internet and use :file:`overlay-qemu_x86.conf`.
72DHCP support is not enabled with QEMU. It uses static IP addresses.
73
74Sample overlay file
75===================
76
77This is the overlay template for Azure IoT hub and other details:
78
79.. code-block:: cfg
80
81 CONFIG_SAMPLE_CLOUD_AZURE_USERNAME="<username>"
82 CONFIG_SAMPLE_CLOUD_AZURE_PASSWORD="<SAS token>"
83 CONFIG_SAMPLE_CLOUD_AZURE_CLIENT_ID="<device id>"
84 CONFIG_SAMPLE_CLOUD_AZURE_HOSTNAME="<IoT hub hostname>"
85 CONFIG_SAMPLE_SOCKS_ADDR="<proxy addr>"
86 CONFIG_SAMPLE_SOCKS_PORT=<proxy port>
87 CONFIG_SAMPLE_CLOUD_AZURE_SERVER_ADDR="<server ip addr, if DNS disabled set this>"
88 CONFIG_SAMPLE_CLOUD_AZURE_SERVER_PORT=<server port, if DNS disabled set this>
89
90Sample output
91=============
92
93This is the output from the FRDM UART console, with:
94
95.. code-block:: console
96
97 [00:00:03.001,000] <inf> eth_mcux: Enabled 100M full-duplex mode.
98 [00:00:03.010,000] <dbg> mqtt_azure.main: Waiting for network to setup...
99 [00:00:03.115,000] <inf> net_dhcpv4: Received: 10.0.0.2
100 [00:00:03.124,000] <inf> net_config: IPv4 address: 10.0.0.2
101 [00:00:03.132,000] <inf> net_config: Lease time: 43200 seconds
102 [00:00:03.140,000] <inf> net_config: Subnet: 255.255.255.0
103 [00:00:03.149,000] <inf> net_config: Router: 10.0.0.10
104 [00:00:06.157,000] <dbg> mqtt_azure.try_to_connect: attempting to connect...
105 [00:00:06.167,000] <dbg> net_sock_tls.tls_alloc: (0x200024f8): Allocated TLS context, 0x20001110
106 [00:00:19.412,000] <dbg> mqtt_azure.mqtt_event_handler: MQTT client connected!
107 [00:00:19.424,000] <dbg> mqtt_azure.publish_message: mqtt_publish OK
108 [00:00:19.830,000] <dbg> mqtt_azure.mqtt_event_handler: PUBACK packet id: 63387
109 [00:00:31.842,000] <dbg> mqtt_azure.publish_message: mqtt_publish OK
110 [00:00:51.852,000] <dbg> mqtt_azure.publish_message: mqtt_publish OK
111 [00:00:51.861,000] <dbg> mqtt_azure.mqtt_event_handler: PUBACK packet id: 38106
112
113You can also check events or messages information on Azure Portal.
114
115Cloud to device communication
116=============================
117
118Goto IoT devices section in Azure Portal. Click on the device from
119IoT devices. If you have configured multiple devices, select correct device.
120Goto Message to Device section. Enter text in Message Body section.
121Click on Send Message.
122
123See `Azure Cloud MQTT Documentation
124<https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support>`_.
125