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
#
IntroductionMicrosoft 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 configurationWizFi360-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 connectionAfter 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:
Type | Command | Response |
---|---|---|
Query | AT+CWMODE_CUR? | +CWMODE:<mode> OK |
Set | AT+CWMODE_CUR=<mode> | OK |
Defined values:
Mode | Value |
---|---|
1 | Station mode |
2 | SoftAP mode (factory default) |
3 | Station+SoftAP mode |
#
2. Enable DHCPAT Command: AT+CWDHCP_CUR Syntax:
Type | Command | Response |
---|---|---|
Query | AT+CWDHCP_CUR? | +CWDHCP_CUR:<para> OK |
Set | AT+CWMODE_CUR=<para>,<en> | OK |
Defined values:
Parameter | Value |
---|---|
0 | DIsable SoftAP DHCP & Station DHCP. |
1 | Enable SoftAP DHCP & Disable Station DHCP. |
2 | Disable SoftAP DHCP & enable Station DHCP. |
3 | Enable SoftAP DHCP & Station DHCP. (factory default) |
#
3. List available APsAT Command: AT+CWLAP Syntax:
Type | Command | Response |
---|---|---|
Query | AT+CWLAP | +CWLAP:([<ecn>,<ssid>,<rssi>,<mac>,<channel>,<wps>]) |
Defined values:
Parameter | Value |
---|---|
<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 APAT Command: AT+CWJAP_CUR Syntax:
Type | Command | Response |
---|---|---|
Set | AT+CWJAP_CUR=<ssid>,<pwd>,[<bssid>] | +CWJAP_CUR:<ssid>,<bssid>,<channel>,<rssi> OK |
Defined values:
Parameter | Value |
---|---|
<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 setAT Command: AT+AZSET
Syntax:
Type | Command | Response |
---|---|---|
Set | AT+AZSET=<iothub_name>,<device_id>,<device_key> | OK |
Defined values:
Parameter | Value |
---|---|
<hub ID> | string parameter. IoT Hub ID |
<device ID> | string parameter. IoT Device ID |
<key> | string parameter, IoT Device Key |
#
6. Set MQTT TopicAT Command: AT+MQTTTOPIC
Syntax:
Type | Command | Response |
---|---|---|
Set | AT+MQTTTOPIC=<publish topic>,<subscribe topic>,<subscribe topic2>,<subscribe topic3> | OK |
Defined values:
Parameter | Value |
---|---|
<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 AzureAT Command: AT+AZCON
Syntax:
Type | Command | Response |
---|---|---|
Set | AT+AZCON | CONNECT 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 messageAT Command: AT+MQTTPUB
Syntax:
Type | Command | Response |
---|---|---|
Set | AT+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 downloadAfter 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 parametersUpdate your credentials (WiFi ssid, WiFi pwd, Hub ID, Device ID, Device Key) in order to connect to Azure IoT Hub.
Press Run Program to build and run project.
Use Serial monitor to check if code was successfully uploaded to Nucleo board.
#
Results- Press "Start" button in Telemetry Section in IoT Hub Explorer.
"Start" button shall be pressed before sending data using MQTTPUB commands.
- Check data sent with MQTTPUB command.