Skip to main content

Connect to Azure IoT Hub using WizFi360 Azure AT Command

Getting Started#

Hardware Requirement#

  • Desktop or laptop computer
  • MicroUSB cable
  • WizFi360-EVB-Shield

Software Requirement#

Login to Azure Portal.

※ In this guide we will procedd with free account. To learn how to create IoT Hub please refer to Azure Cloud Service Introduction.

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 used in Standalone mode. Therefore DIP Switch and jumper cables shall be connected as following:

  • SW1 : On
  • SW2 : Off
  • SW3 : Off

Device connection#

After connecting hardware, connect WizFi360-EVB-Shield to Desktop or Laptop using USB Cable.

Check COM Port from Device Manager.

If COM port cannot be found in Device manager, check drivers from link below.

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#

Connect in serial terminal & code execution#

For connection use following configuration in terminal: 115200-8-N-1, None.

WiFi setting#

1. Set Wifi station mode#

CommandResponse
AT+CWMODE_CUR=1 // station modeOK

2. Set DHCP Enable#

CommandResponse
AT+CWDHCP_CUR=1,1 // DHCP enable on Station modeOK

3. Get possible Wi-Fi AP List for connection#

CommandResponse
AT+CWLAP+CWLAP : (3,"ssid",-57,"mac address",1,1) // encryption method, ssid, rssi, mac address, channel, wps

4. Connect to Wi-Fi AP#

CommandResponse
AT+CWJAP_CUR="ssid","password"WIFI CONNECTED
WIFI GOT IP

5. Query WizFi360 device' IP address#

CommandResponse
AT+CIPSTA_CUR?+CIPSTA_CUR:ip:”192.168.10.13”
+CIPSTA_CUR:gateway:”192.168.10.1”
+CIPSTA_CUR:network:”255.255.255.0”

Connect to Azure services using next AT commands#

1. Set Azure connection#

CommandResponse
AT+AZSET="iothub_name","device_id","device_key"OK

2. Set MQTT Topic#

CommandResponse
AT+MQTTTOPIC="/devices/{device_id}/messages/events/","/devices/{device_id}/messages/devicebound/#"

Example
AT+MQTTTOPIC="/devices/testDevice/messages/events/","/devices/testDevice/messages/devicebound/#"
OK

Note: MQTT Topic follows the rules defined in Azure IoT Hub. Refer to document: Communicate with your IoT hub using the MQTT protocol: Using the MQTT protocol directly (as a device)

3. Connect to Azure#
CommandResponse
AT+AZCONOK
4. Publish data#
CommandResponse
AT+MQTTPUB="{"deviceId":"WizFi360","temperature":28.16,"humidity":46.04}"OK

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.