Skip to main content

Connect to Azure IoT Hub using NUCLEO-L476RG + WizFi360 Azure AT Command

Getting Started#

Hardware Requirement#

  • NUCLEO-L476RG
  • Desktop or laptop computer
  • USB cable
  • WizFi360-EVB-Shield

Software Requirement#

  • Microsoft Azure Account (To create Azure account press here.)
  • Preferred Serial Terminal (TeraTerm, YAT, etc.)
  • Azure IoT Explorer
  • MBED Studio / MBED Online Compiler

Introduction#

Microsoft Azure is cloud computing service. Using WizFi360 we can connect to Azure services, transmit data and monitor status.

In this document we will guide how to connect Arduino Mega 2560 + WizFi360 to MS Azure Services. Thic process consists of following steps:

  • Azure IoT Hub preparation
  • IoT device registration
  • Connect to Azure IoT and transmit data

For Azure IoT Hub setup and IoT device creation please refer to [Azure Cloud Introduction].

For this guide WizFi360-EVB-Shield Evaluation board was used.

Device preparation#

Hardware configuration#

WizFi360-EVB-Shield will be installed on top of NUCLEO-L476RG. Therefore DIP Switch and jumper cables shall be connected as following:

  • SW1 : Off
  • SW2 : Off
  • SW3 : On
  • D2 : UART Tx
  • D8 : UART Rx

2. Device connection#

After connecting hardware, connect NUCLEO-L476RG to Desktop or Laptop using USB Cable.

Check COM Port from Device Manager.

If COM port cannot be found in Device manager, check link below and follow instructions.

  • [ST-LINK, ST-LINK/V2, ST-LINK/V2-1 USB driver][Link-St_Link_St_Link_V2_St_Link_V2_1_Usb_Driver]

AT commands#

1. Set current WiFi mode (not saved in flash)#

AT Command: AT+CWMODE_CUR Syntax:

TypeCommandResponse
QueryAT+CWMODE_CUR?+CWMODE:<mode>
OK
SetAT+CWMODE_CUR=<mode>OK

Defined values:

ModeValue
1Station mode
2SoftAP mode (factory default)
3Station+SoftAP mode

2. Enable DHCP#

AT Command: AT+CWDHCP_CUR Syntax:

TypeCommandResponse
QueryAT+CWDHCP_CUR?+CWDHCP_CUR:<para>
OK
SetAT+CWMODE_CUR=<para>,<en>OK

Defined values:

ParameterValue
0DIsable SoftAP DHCP & Station DHCP.
1Enable SoftAP DHCP & Disable Station DHCP.
2Disable SoftAP DHCP & enable Station DHCP.
3Enable SoftAP DHCP & Station DHCP. (factory default)

3. List available APs#

AT Command: AT+CWLAP Syntax:

TypeCommandResponse
QueryAT+CWLAP+CWLAP:([<ecn>,<ssid>,<rssi>,<mac>,<channel>,<wps>])

Defined values:

ParameterValue
<ecn>0: Open
1: WEP
2: WPA_PSK
3: WPA2_PSK
4:WPA_WPA2_PSK
<ssid>string parameter. AP ssid
<rssi>signal strength
<mac>string parameter. AP mac
<wps>0: Disable WPS
1: Enable WPS

4. Connect to AP#

AT Command: AT+CWJAP_CUR Syntax:

TypeCommandResponse
SetAT+CWJAP_CUR=<ssid>,<pwd>,[<bssid>]+CWJAP_CUR:<ssid>,<bssid>,<channel>,<rssi>
OK

Defined values:

ParameterValue
<ssid>string parameter. Target AP ssid. MAX: 32 bytes
<pwd>string parameter. Target AP password. MAX: 64-byte ASCII
<bssid>string parameter, target AP' MAC address, used in case if there are several APs with same SSID.

5. Azure IoT Hub configuration set#

AT Command: AT+AZSET

Syntax:

TypeCommandResponse
SetAT+AZSET=<iothub_name>,<device_id>,<device_key>OK

Defined values:

ParameterValue
<hub ID>string parameter. IoT Hub ID
<device ID>string parameter. IoT Device ID
<key>string parameter, IoT Device Key

6. Set MQTT Topic#

AT Command: AT+MQTTTOPIC

Syntax:

TypeCommandResponse
SetAT+MQTTTOPIC=<publish topic>,<subscribe topic>,<subscribe topic2>,<subscribe topic3>OK

Defined values:

ParameterValue
<publish topic>string parameter, topic where WizFi360 will publish
<subscribe topic>string parameter, topic where WizFi360 will subscribe
<subscribe topic2>string parameter, topic where WizFi360 will subscribe
<subscribe topic3>string parameter, topic where WizFi360 will subscribe

Note:

  • This command shall be set before connecting to broker.
  • <subscribe topic2> & <subscribe topic3> are available in Firmware v1.0.5.0 and later.

7. Connect to Azure#

AT Command: AT+AZCON

Syntax:

TypeCommandResponse
SetAT+AZCONCONNECT
OK

Note: • Before sending this ocmmand, AT+AZSET command & AT+MQTTTOPIC command shall be set. • After connection data can be sent to Azure server using AT+MQTTPUB command. • For more details please refer to https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support.

8. Publish a message#

AT Command: AT+MQTTPUB

Syntax:

TypeCommandResponse
SetAT+MQTTPUB=<message>OK

Note: • This command shall be used when MQTT connection is established. • Topic where data will be published is set with AT+MQTTTOPIC command, user shall define topic before connecting to broker.

Running sample code#

Mbed sample code download#

After Sample Download, open and launch project in File > Open Workspace.

Sample code can be found at following path.

samples/Wi-Fi/Mbed_Azure_Atcmd_Wizfi360

Sample code also can be imported into Online Compiler from following link:

https://os.mbed.com/users/vikshin/code/Mbed-Azure-Atcmd-WizFi360/

Modify parameters#

Update your credentials (WiFi ssid, WiFi pwd, Hub ID, Device ID, Device Key) in order to connect to Azure IoT Hub.

/* WiFi info */
char ssid[] = "XXXXXXXXXXXXXXXXXXXXXXXX";
char password[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
/* Azure info */
char hub_name[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
char device_id[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
char device_primary_key[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX=";

Press Run Program to build and run project.

Use Serial monitor to check if code was successfully uploaded to Nucleo board.

Results#

  1. Press "Start" button in Telemetry Section in IoT Hub Explorer.

    "Start" button shall be pressed before sending data using MQTTPUB commands.

  2. Check data sent with MQTTPUB command.