Skip to content

Commit f1a1e53

Browse files
rveerama1jukkar
authored andcommitted
samples: net: mqtt: Add a sample to connect to Azure
This samples demonstrates how to connect to Azure Cloud IoT hub which is based on MQTT protocol. User has to create an account in Azure Cloud and provide those details using Kconfig options. This sample first acquires DHCPv4 address and opens a secure connection with Azure cloud. Then opens a MQTT connection and publish messages randomly. Signed-off-by: Ravi kumar Veeramally <[email protected]>
1 parent 50631d6 commit f1a1e53

File tree

10 files changed

+800
-0
lines changed

10 files changed

+800
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
5+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
6+
project(mqtt-azure)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})

samples/net/mqtt_azure/Kconfig

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Kconfig - Private config options for mqtt-azure sample app
2+
3+
#
4+
# Copyright (c) 2019 Intel Corporation
5+
#
6+
# SPDX-License-Identifier: Apache-2.0
7+
#
8+
9+
mainmenu "Networking mqtt-azure sample application"
10+
11+
config SAMPLE_CLOUD_AZURE_USERNAME
12+
string "Azure cloud username"
13+
help
14+
Username of your Azure account.
15+
16+
config SAMPLE_CLOUD_AZURE_PASSWORD
17+
string "Azure cloud password"
18+
help
19+
Password of your Azure account.
20+
21+
config SAMPLE_CLOUD_AZURE_CLIENT_ID
22+
string "Azure cloud IoT hub MQTT client ID"
23+
help
24+
Device ID which you have setup in IoT hub account.
25+
26+
config SAMPLE_CLOUD_AZURE_HOSTNAME
27+
string "Azure cloud IoT hub hostname"
28+
help
29+
Hostname of your Azure cloud IoT hub.
30+
31+
config SAMPLE_CLOUD_AZURE_SERVER_ADDR
32+
string "Azure cloud server address"
33+
help
34+
Azure cloud server address.
35+
36+
config SAMPLE_CLOUD_AZURE_SERVER_PORT
37+
int "Azure cloud server port number"
38+
default 8883
39+
help
40+
Azure cloud server port number.
41+
42+
config SAMPLE_SOCKS_ADDR
43+
string "Proxy address"
44+
depends on SOCKS
45+
help
46+
Provide proxy address if you are running this application
47+
behind proxy.
48+
49+
config SAMPLE_SOCKS_PORT
50+
int "Proxy port"
51+
depends on SOCKS
52+
help
53+
Provide proxy port if you are running this application
54+
behind proxy.
55+
56+
source "Kconfig.zephyr"

samples/net/mqtt_azure/README.rst

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

samples/net/mqtt_azure/prj.conf

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
CONFIG_NETWORKING=y
2+
3+
# Disable IPv6 support
4+
CONFIG_NET_IPV6=n
5+
6+
# Enable IPv4 support
7+
CONFIG_NET_IPV4=y
8+
CONFIG_NET_IF_MAX_IPV4_COUNT=2
9+
CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=3
10+
11+
# Enable TCP support
12+
CONFIG_NET_TCP=y
13+
14+
# Enable Sockets support
15+
CONFIG_NET_SOCKETS=y
16+
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
17+
18+
# Enable DHCPv4 support
19+
CONFIG_NET_DHCPV4=y
20+
21+
# Enable SOCKS5 proxy support
22+
CONFIG_SOCKS=n
23+
24+
# Enable MQTT Lib support
25+
CONFIG_MQTT_LIB=y
26+
CONFIG_MQTT_LIB_TLS=y
27+
28+
# Enable Mbed TLS configuration
29+
CONFIG_MBEDTLS=y
30+
CONFIG_MBEDTLS_BUILTIN=y
31+
CONFIG_MBEDTLS_ENABLE_HEAP=y
32+
CONFIG_MBEDTLS_HEAP_SIZE=100000
33+
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=10240
34+
CONFIG_MBEDTLS_PEM_CERTIFICATE_FORMAT=y
35+
36+
# Network configuration
37+
CONFIG_NET_CONFIG_SETTINGS=y
38+
39+
# Network connection manager
40+
CONFIG_NET_CONNECTION_MANAGER=y
41+
42+
CONFIG_NET_MGMT_EVENT_STACK_SIZE=1024
43+
CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=5
44+
CONFIG_NET_MGMT_EVENT_LOG_LEVEL_DBG=n
45+
46+
CONFIG_DNS_RESOLVER=y
47+
CONFIG_DNS_SERVER_IP_ADDRESSES=y
48+
CONFIG_DNS_SERVER1="8.8.8.8"
49+
CONFIG_DNS_RESOLVER_ADDITIONAL_BUF_CTR=2
50+
CONFIG_NEWLIB_LIBC=y
51+
52+
CONFIG_INIT_STACKS=y
53+
CONFIG_NET_SHELL=y
54+
55+
CONFIG_MAIN_STACK_SIZE=4096
56+
57+
# Enable Logging support
58+
CONFIG_LOG_IMMEDIATE=y
59+
CONFIG_NET_LOG=y
60+
CONFIG_NET_TCP_LOG_LEVEL_DBG=n
61+
CONFIG_NET_SOCKETS_LOG_LEVEL_DBG=n
62+
CONFIG_MQTT_LOG_LEVEL_DBG=n
63+
CONFIG_NET_DHCPV4_LOG_LEVEL_INF=n
64+
CONFIG_NET_IF_LOG_LEVEL_DBG=n
65+
CONFIG_MBEDTLS_DEBUG=n
66+
#CONFIG_MBEDTLS_DEBUG_LEVEL=4
67+
CONFIG_SOCKS_LOG_LEVEL_DBG=n
68+
CONFIG_NET_CONFIG_LOG_LEVEL_DBG=y
69+
CONFIG_NET_CONNECTION_MANAGER_LOG_LEVEL_DBG=n
70+
CONFIG_DNS_RESOLVER_LOG_LEVEL_DBG=n
71+
CONFIG_NET_CONTEXT_LOG_LEVEL_DBG=n

samples/net/mqtt_azure/sample.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sample:
2+
description: MQTT sample app to Azure cloud
3+
name: mqtt-azure
4+
tests:
5+
sample.net.mqtt_azure:
6+
harness: net
7+
platform_whitelist: sam_e70_xplained frdm_k64f qemu_x86
8+
tags: net mqtt cloud
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef __CONFIG_H__
8+
#define __CONFIG_H__
9+
10+
#define SERVER_ADDR CONFIG_SAMPLE_CLOUD_AZURE_SERVER_ADDR
11+
#define SERVER_PORT CONFIG_SAMPLE_CLOUD_AZURE_SERVER_PORT
12+
13+
#if defined(CONFIG_SOCKS)
14+
#define SOCKS5_PROXY_ADDR CONFIG_SAMPLE_SOCKS_ADDR
15+
#define SOCKS5_PROXY_PORT CONFIG_SAMPLE_SOCKS_PORT
16+
#endif
17+
18+
#define MQTT_CLIENTID CONFIG_SAMPLE_CLOUD_AZURE_CLIENT_ID
19+
20+
#define APP_SLEEP_MSECS 8000
21+
22+
#define APP_MQTT_BUFFER_SIZE 1024
23+
24+
#endif /* __CONFIG_H__ */
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"-----BEGIN CERTIFICATE-----\r\n"
2+
"MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ\r\n"
3+
"RTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJlclRydXN0MSIwIAYD\r\n"
4+
"VQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTAwMDUxMjE4NDYwMFoX\r\n"
5+
"DTI1MDUxMjIzNTkwMFowWjELMAkGA1UEBhMCSUUxEjAQBgNVBAoTCUJhbHRpbW9y\r\n"
6+
"ZTETMBEGA1UECxMKQ3liZXJUcnVzdDEiMCAGA1UEAxMZQmFsdGltb3JlIEN5YmVy\r\n"
7+
"VHJ1c3QgUm9vdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKMEuyKr\r\n"
8+
"mD1X6CZymrV51Cni4eiVgLGw41uOKymaZN+hXe2wCQVt2yguzmKiYv60iNoS6zjr\r\n"
9+
"IZ3AQSsBUnuId9Mcj8e6uYi1agnnc+gRQKfRzMpijS3ljwumUNKoUMMo6vWrJYeK\r\n"
10+
"mpYcqWe4PwzV9/lSEy/CG9VwcPCPwBLKBsua4dnKM3p31vjsufFoREJIE9LAwqSu\r\n"
11+
"XmD+tqYF/LTdB1kC1FkYmGP1pWPgkAx9XbIGevOF6uvUA65ehD5f/xXtabz5OTZy\r\n"
12+
"dc93Uk3zyZAsuT3lySNTPx8kmCFcB5kpvcY67Oduhjprl3RjM71oGDHweI12v/ye\r\n"
13+
"jl0qhqdNkNwnGjkCAwEAAaNFMEMwHQYDVR0OBBYEFOWdWTCCR1jMrPoIVDaGezq1\r\n"
14+
"BE3wMBIGA1UdEwEB/wQIMAYBAf8CAQMwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3\r\n"
15+
"DQEBBQUAA4IBAQCFDF2O5G9RaEIFoN27TyclhAO992T9Ldcw46QQF+vaKSm2eT92\r\n"
16+
"9hkTI7gQCvlYpNRhcL0EYWoSihfVCr3FvDB81ukMJY2GQE/szKN+OMY3EU/t3Wgx\r\n"
17+
"jkzSswF07r51XgdIGn9w/xZchMB5hbgF/X++ZRGjD8ACtPhSNzkE1akxehi/oCr0\r\n"
18+
"Epn3o0WC4zxe9Z2etciefC7IpJ5OCBRLbf1wbWsaY71k5h+3zvDyny67G7fyUIhz\r\n"
19+
"ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS\r\n"
20+
"R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp\r\n"
21+
"-----END CERTIFICATE-----\r\n"

0 commit comments

Comments
 (0)