Version 2 of the ESP8266 Azure IoT Hub client library has been released and is available at ESP8266 Secure Azure IoT Hub Client Version 2
Temperature, Pressure and Light Sensor Streaming over HTTPS to Azure IoT Hub or Event Hubs.
All parts are readily available from various sources including online electronics stores, eBay, aliexpress...
PlatformThis project implemented on the NodeMCU V2 (also known as V1.0) and WeMos D1 Mini development boards on the ESP8266 platform flashed with Arduino core for ESP8266 WiFi chip V2.0 firmware. The solution can stream data securely directly to Azure IoT Hub or Azure Event Hub over HTTPS calling Azure REST APIs.
The ESP8266 is a great commodity priced platform that has really come to life with Arduino support.With "Arduino core for ESP8266 WiFi chip" Version 2.0 there is now Secure Client HTTPS (TLS) support making this a viable platform for secure IoT data streaming.
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.
Check out the Publish.ino sketch, the main bit of magic is to create the Azure IoT Hub Shared Access Signature from the Azure IoT Hub Developer Guide. See function createIotHubSas.
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- Setup your Azure IoT Hub. There is a free 8000 message a day subscription to get started.
- Register your device with Azure IoT Hub.
- Flash the EEPROM with Wifi and Geolocation data.
- Update the main AzureClient.ino sketch
- Add your Azure IoT Hub or Event Hub connection string.
- Define what platform you are targeting - NodeMCU or WeMos.
- What sensors you are using, if any - options are DemoMode, bmp180 or dht11 sensors.
- Deploy the solution to either your NodeMCU or WeMos devices.
Creating an Azure IoT Hub (there is a free 8000 message/day limited subscription)
Register your Device with IoT HubRegister Devices for your newly created IoT Hub.
- Use the Device Explorer utility from the Azure IoT SDKs Tools directory
- or you can create your own utility by following the instructions in the Create an Azure IoT Hub link.
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.
- 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
You need to configure the following settings in the main section on the AzureClient.ino file.
// cloud configurationsCloudMode cloudMode = IoTHub;
// ClodeMode enumeration: IoTHub or EventHub
// device configurationBoardType boardType = WeMos;
// BoardType enumeration: NodeMCU or WeMosSensorMode sensorMode = DemoMode;
// OperationMode enumeration: DemoMode (if no sensors attached), Bmp180Mode (bmp085 and bmp180), Dht11ModeDisplayMode displayMode = NoDisplay;
// DisplayMode enumeration: NoDisplay or LedMatrixLightSensor lightSensor = None;
// LightSensor enumeration: None, Enabled
Data SchemaThe 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 HubOk, 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 BoardThere 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- NodeMCU v2 - Lua based ESP8266 development kit
- BMP180 Barometric Pressure Sensor
- Adafruit Mini 8x8 LED Matrix w/I2C Backpack
- 1 x Light Dependent Resistor
- 1 x 10k resistor
- 1 x 400 Tie Point Interlocking Solderless Breadboard
No wiring required, just solder the supplied header pins for the WeMos and the DHT Sensor shield.
- NodeMCU - On Windows, Mac and Linux you will need to install the latest CP210x USB to UART Bridge VCP Drivers.
- WeMos - On Windows and Mac install the latest Ch340G drivers. No drivers required for Linux.
- Arduino IDE 1.6.5 As at Dec, 2015, Arduino 1.6.6 has several issues, so to stick with 1.6.5
- As at Dec 2015, ESP8266 Board Manager 2.0.0 or better required for HTTPS/TLS Secure Client support.
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 ManagerStarting 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).
- Install Arduino 1.6.5 from the Arduino website.
- Start Arduino and open Preferences window.
- 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.
- 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).
- Select NodeMUC or WeMos D1 Mini Board: Tools -> Board -> NodeMCU 1.0 (ESP-12E module) or WeMos D1 Mini
- 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.
Be sure to read the ESP8266 Arduino Core Documentation - there are some minor gotchas.
Comments