Skip to main content

Connect to Azure IoT Hub using Arduino Mega 2560 + WizFi360 Azure AT Command

Getting Started#

Hardware Requirement#

  • Arduino Mega 2560 board
  • Desktop or laptop computer
  • USB cable
  • WizFi360-EVB-Shield

Software Requirement#

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#

In this guide we will use Arduino Mega2560 and WizFi360-EVB-SHield. In Arduino Code we will use UART1 for ocmmunication with WizFi360-EVB-Shield. To do so we need to connect TX1, RX1 pins of Arduino board with RXD and TXD pins of WizFi360-EVB-Shield. Also, switch RXD/TXD Selector OFF in order to enable communication through pin not USB.

To use DHT11 sensor from WizFi360-EVB-Shield connect D14 pin on EVB board to D14 pin on Arduino Mega.

Device connection#

After configuring hardware connect Arduino Mega2560 board with PC. CHeck port number in Device Manager.

If everything setup correctly, then COM port can be checked in Device manager as shown on picture above.

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#

Download sample Arduino code#

Please download Arduino sample code from link below and open ino project file.

Sample can be found at below path.

samples/Wi-Fi/Arduino_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 credentials
char ssid[] = "XXXXXXXXXXXXXXXXXX"; // your network SSID (name)
char pass[] = "XXXXXXXXXXXXXXXXXX"; // your network password
//Azure Hub & Device credentials
char IotHubname[] = "XXXXXXXXXXXXXXX";
char DeviceId[] = "XXXXXXXXXXXXX";
char DevicePrimaryKey[] = "XXXXXXXXXXXXXXXXXX";

As shown on below picture, check your board & port, then compile code.

When compile is done, upload to board. When upload is done, you will see 'avrdude done. Thank you.' message.

Use Serial monitor to check if code was successfully uploaded to Adruino Mega2560 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.