NodeMCU (ESP8266) & Azure IoT Hub

Since JFall 2016, I have two NodeMCU, ESP8266 based kits at home. I got them since a colleague that actually went to JFall got them at the stand of a competitor and didn’t know what to do with them. On one of the Microsoft Azure Advisory Board Yammer spaces, I read there was a dedicated Arduino Azure IoT SDK. Interesting enough to try it out myself, so let’s connect one of the NodeMCUs to Azure IoT Hub.

Prerequisites

Before we can actually code and configure the ESP8266, we need some hardware and software. Let’s see what is required.

The hardware

The hardware needed to get started is pretty simple.

  • 1 NodeMCU, ESP8266 based
  • 1 micro USB cable
  • 1 breadboard to pin the NodeMCU on, to protect it’s pins

The software

NodeMCUs come with a default Lua based firmware. But to use the Arduino Azure IoTHub libraries, it needs to run C. What we need to accomplish that;

To have our messages delivered at an Azure IoT Hub we also need;

  • A Microsoft Azure account with an active subscription (I assume you have one)
  • The Azure IoT Hub Device Explorer

Quick set-up

Before we get started with coding;

  1. Install the Arduino IDE software and launch the IDE
    • File > Preferences, check the “Display line numbers option
    • Add the URL http://arduino.esp8266.com/stable/package_esp8266com_index.json to the “Addition Boards Manager URLs” field and press the “Ok” button
    • Tools > Boards > Boards Manager, install the ESP8266 platform
    • Sketch > Include Library > Manage Libraries, install the
      AzureIoTHub, AzureIoTProtocol_HTTP, AzureIoTProtocol_MQTT and AzureIoTUtility libraries.
    • Close Arduino IDE
  2. Install the CP210x USB to UART driver and make sure you reboot your machine afterward (I suffered strange behaviors using the driver without rebooting)
  3. Plug the ESP8266 to the breadboard
  4. Plug the USB cable into a USB port on your computer
  5. Plug the micro USB connector to the ESP8266
  6. Launch the Arduino IDE and via Tools > Port, check if there is a COMxx port selected

And with that, we have the hardware connected and our software development environment set up.

Creating an Azure IoT Hub

How to create an Azure IoT Hub instance is well described by Microsoft on Github. Just follow the steps and continue here when done.

Provisioning your ESP8266 in Azure IoT Hub

Here is a great tutorial how to install the device explorer and how to use it to provision devices. Install the device explorer and provision the ESP8266.

Make sure you copy and paste the connection string to Notepad, OneNote or a sticky note.

Let’s code

This is the easiest piece of coding you will do today; File > Examples > AzureIoTProtocol_MQTT, choose simplesample_mqtt. This opens an example sketch with three files; simplesample_mqtt.ino, simplesample_mqtt.c and simplesample_mqtt.h. And that is it, coding done.

Now change the following lines in the simplesample_mqtt.ino file to add your SSID and passkey.

static char ssid[] = "yourNetwork"; // your network SSID (name)
static char pass[] = "yourPassword"; // your network password (use for WPA, or use as key for WEP)

And finally add the the connection string of the provisioned ESP8266 in the device explorer to this line in the simplesample_mqtt.c file.

/*String containing Hostname, Device Id & Device Key in the format: */
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
static const char* connectionString = "[device connection string]";
view raw simplesample_mqtt.c hosted with ❤ by GitHub

Save your sketch with crtl-s (you will be prompted to save it elsewhere since examples are read-only). Choose Sketch > Upload from the menu and while the code is being uploaded, choose Tools > Serial Monitor to open the serial monitor to watch the debug output of the ESP8266.

Serial Monitor - failed Wifi connection

Now, wait a minute? It connects to WiFi (it gets a DHCP IP address, get a mask and gateway) but states it is failing. Let’s check the Device Explorer.

No data in the Device Explorer

Hmm, no messages received at the IoTHub. I will spare you the time it took for me to find why this happens. But in short, the code below assumes that line 99 returns WL_CONNECTED instantly.

// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// unsuccessful, retry in 4 seconds
Serial.print("failed ... ");
delay(4000);
Serial.print("retrying ... ");
}

Googling shows, that most ESP8266s need some time to complete the wifi connection and to return a WL_CONNECTED status. Actually, this WiFi connection problem has been issued on the original Github repository. Of course, I made a pull request there to fix it at the origin. But for now, replace existing code with the code below in simplesample_mqtt.ino.

// Applied a change in code here compared to the original code. WiFi.begin needs more time to reach WL_CONNECTED status.
// Committed as Pull Request #42 as fix to Issue #33 of the repo: https://github.com/Azure/azure-iot-arduino-protocol-mqtt.
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
delay(5000);
if (WiFi.status() == WL_CONNECTED) {
break;
}
// unsuccessful, retry in 5 seconds
Serial.println("failed to connect... ");
Serial.println("retrying to connect... ");
}

Upload the code to the NodeMCU again; Sketch > Upload and watch the Serial Monitor again.

Seems connected, right? Double check on your WiFi router if you see a connected device like mine below.

ESP8266 WiFi connected

Let’s check if the Device Explorer monitors some data on the IoTHub.

And check the serial monitor as last to see a connected IoTHub confirmation there too.

Et voila, your ESP8266 is connected to wifi, provisioned as a known device in the IoTHub and the IoTHub receives its data.

One comment:

Leave a Reply

Your email address will not be published. Required fields are marked *