/Arduino-NodeMCU-ESP8266-Secure-Azure-IoT-Hub-Client

Arduino NodeMCU ESP8266 Secure Azure IoT Hub Client publishers BMP180 Temperature and Air Pressure Sensor to Azure IoT Hub via HTTPS/REST

Primary LanguageArduino

Temperature, Pressure and Light Sensor Streaming over HTTPS to Azure IoT Hub or Event Hubs

Platform

This project is implemented and tested on the following development boards:-

  1. NodeMCU V2 (also known as V1.0),
  2. WeMos D1 Mini
  3. and SparkFun ESP8266 Thing

Platform: The ESP8266 is a great commodity priced platform that has really come to life with Arduino support.

Firmware: Flashed with Arduino core for ESP8266 WiFi chip V2.0 firmware with HTTPS (TLS) support, making this a viable platform for secure IoT data streaming.

Purpose: The solution can securely stream data directly to Azure IoT Hub or Azure Event Hub over HTTPS calling Azure REST APIs.

Acknowledgments: Thanks to Štěpán Bechynský "Proof of Concept – NodeMCU, Arduino and Azure Event Hub" project I've migrated my "Arduino NodeMCU ESP8266 MQTT" project and added IoT Hub support to stream data directly to Azure IoT Hub or Azure Event Hubs over HTTPS.

Security: Check out the Publish.ino sketch, the main bit of magic is to create the Azure IoT Hub Shared Access Signature.

String createIotHubSas(char *key, String url){  
    String stringToSign = url + "\n" + expire;

    // START: Create signature
    // https://raw.githubusercontent.com/adamvr/arduino-base64/master/examples/base64/base64.ino
    
    int keyLength = strlen(key);
    
    int decodedKeyLength = base64_dec_len(key, keyLength);
    char decodedKey[decodedKeyLength];  //allocate char array big enough for the base64 decoded key
    
    base64_decode(decodedKey, key, keyLength);  //decode key
    
    Sha256.initHmac((const uint8_t*)decodedKey, decodedKeyLength);
    Sha256.print(stringToSign);  
    char* sign = (char*) Sha256.resultHmac();
    // END: Create signature
    
    // START: Get base64 of signature
    int encodedSignLen = base64_enc_len(HASH_LENGTH);
    char encodedSign[encodedSignLen];
    base64_encode(encodedSign, sign, HASH_LENGTH); 
    
    // SharedAccessSignature
    return "sr=" + url + "&sig="+ urlEncode(encodedSign) + "&se=" + expire;
    // END: create SAS  
}

##Overview of steps to deploying the solution

  1. Setup your Azure IoT Hub. There is a free 8000 message a day subscription to get started.
  2. Register your device with Azure IoT Hub.
  3. Flash the EEPROM with Wifi, Geo location, plus your Azure IoT Hub (or Event Hub) host, device id and key.
  4. Update the main AzureClient.ino sketch
  5. Deploy the solution to either your NodeMCU, WeMos or Sparkfun ESP8266 Dev devices.
  6. View data with Device Explorer

##Azure IoT Hub Setup

Creating an Azure IoT Hub (there is a free 8000 message/day limited subscription)

##Register your Device with IoT Hub

Register Devices for your newly created IoT Hub.

  • Use the Device Explorer utility from the Azure IoT SDKs Tools directory

DeviceExplorer

##NodeMCU or WeMos EEPROM Configuration

Before uploading the Azure.ino sketch to your NodeMCU or WeMos you first need to configure the SetEEPROMConfiguration.ino sketch with one or more Wifi SSIDs/Passwords and the device geo location. Upload this sketch to burn these settings to the device EEPROM. After this you deploy the AzureClient sketch which will read this configuration information from the EEPROM.

  1. SetEEPROMConfiguration.ino sets the following
  • Wi-Fi SSID and password pairs, put in priority order.
  • Wifi - Is the number of WiFi ssid and password pairs
  • Azure IoT Hub or Event Bus Host name eg "MakerDen.azure-devices.net", Device ID, and Key. For IoT Hub get this information from the Device Explorer, for Event Hub, get from Azure Management Portal.
  • Geo location of the device
  • Deploy this app to the NodeMCU to write configuration settings to EEPROM

##Configure the main AzureClient.ino solution

You need to configure the initDeviceConfig() function in the AzureClient.ino file.

void initDeviceConfig() {  // Example device configuration
    device.boardType = Other;            // BoardType enumeration: NodeMCU, WeMos, SparkfunThing, Other (defaults to Other). This determines pin number of the onboard LED for wifi and publish status. Other means no LED status 
    device.deepSleepSeconds = 0;         // if greater than zero with call ESP8266 deep sleep (default is 0 disabled). GPIO16 needs to be tied to RST to wake from deepSleep. Causes a reset, execution restarts from beginning of sketch
    cloud.cloudMode = IoTHub;            // CloudMode enumeration: IoTHub and EventHub (default is IoTHub)
    cloud.publishRateInSeconds = 90;     // limits publishing rate to specified seconds (default is 90 seconds)
    cloud.sasExpiryDate = 1737504000;    // Expires Wed, 22 Jan 2025 00:00:00 GMT (defaults to Expires Wed, 22 Jan 2025 00:00:00 GMT)
}

Then upload the sketch to your device.

##Viewing Data

From Device Explorer, head to the Data tab, select your device, enable consumer group then click Monitor.

IoT Hub Data

Data Schema

The AzureClient sketch streams data in the following JSON format, of course you can change this:)

{"Dev":"DeviceId","Geo":"2011","Celsius":27,"hPa":1016,"Humidity":50,"Light":99,"Utc":"2015-12-06T23:07:04","Id":103}

##Azure IoT Hub and Azure Event Hub

Ok, so you've read this far and you may be wondering what is Azure IoT Hub and Azure Event Hub.

IoT Hub is designed to "Connect, monitor, and control millions of IoT assets", Azure Event Hubs is designed for internet scale data ingestion. Unlock the value of that data with Stream Analytics, Power Bi and preconfigured IoT Hub solutions such as Remote monitoring .

##Physical Board

There are a number of ESP8266 based development boards available so be sure to check out this great article "Comparison of ESP8266 NodeMCU development boards".

The two dev boards that captured my interest are the NodeMCU V2 and the WeMos D1 Mini and this project supports both.

###NodeMCU V2 Hardware

  1. NodeMCU v2 - Lua based ESP8266 development kit
  2. BMP180 Barometric Pressure Sensor
  3. Adafruit Mini 8x8 LED Matrix w/I2C Backpack
  4. 1 x Light Dependent Resistor
  5. 1 x 10k resistor
  6. 1 x 400 Tie Point Interlocking Solderless Breadboard

schematic

###WeMos D1 Mini Hardware

  1. WeMos D1 Mini
  2. DHT Shield or the DHT Pro Shield.

No wiring required, just solder the supplied header pins for the WeMos and the DHT Sensor shield.

WeMos D1 Mini

Software Requirements

###Drivers

  1. NodeMCU - On Windows, Mac and Linux you will need to install the latest CP210x USB to UART Bridge VCP Drivers.
  2. WeMos - On Windows and Mac install the latest Ch340G drivers. No drivers required for Linux.

Arduino IDE

  1. Arduino IDE 1.6.5 As at Dec, 2015, Arduino 1.6.6 has several issues, so to stick with 1.6.5
  2. As at Dec 2015, ESP8266 Board Manager 2.0.0 or better required for HTTPS/TLS Secure Client support.

Visual Studio

There an fantastic plugin for Visual Studio that adds Arduino support from Visual Micro. IntelliSence, auto complete, debugging, it doesn't get much better:)

###Installing ESP8266 support with the Arduino Boards Manager

Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32 and 64 bit).

  1. Install Arduino 1.6.5 from the Arduino website.
  2. Start Arduino and open Preferences window.
  3. Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  4. Open Boards Manager from Tools > Board menu and install esp8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).
  5. Select NodeMUC or WeMos D1 Mini Board: Tools -> Board -> NodeMCU 1.0 (ESP-12E module) or WeMos D1 Mini
  6. Set Port and Upload Speed: Tools. Note, you may need to try different port speeds to successfully flash the device. Faster is better as each time you upload the code to your device you are re-flashing the complete ROM not just your code.

##ESP8266 Arduino Core Documentation

Be sure to read the ESP8266 Arduino Core Documentation - there are some minor gotchas.