Building Connected Things with an ESP8266 and Microsoft Azure
Sending Device-to-Cloud (D2C) Messages
Table of Contents
- Table of Contents
- Bill of Materials
- Connecting your Weather Station to the Cloud
- Startup Scripts for your ESP8266 to use the Cloud
- Create the Lua Program in ESPlorer
- Run the App on the Device
- Conclusion & Next Steps
Bill of Materials
What you will need:
- The ThingLabs Weather Station created in the ‘ThingLabs Weather’ lab.
Connecting your Weather Station to the Cloud
This lab will show you how to restructure your existing simple application into an application that uses the MQTT protocol to send telemetry data to Azure. On the Azure side we’re using the MQTT gateway to accept messages instead of the default AMQP protocol IoTHub’s support.
From Wikipedia’s MQTT Article:
MQTT[1] (formerly MQ Telemetry Transport) is an ISO standard (ISO/IEC PRF 20922)[2] publish-subscribe based “light weight” messaging protocol for use on top of the TCP/IP protocol. It is designed for connections with remote locations where a “small code footprint” is required or the network bandwidth is limited. The publish-subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message. Andy Stanford-Clark and Arlen Nipper of Cirrus Link Solutions authored the first version of the protocol in 1999.
The structure of the NodeMCU MQTT client is documented to provide both telemetry and command processing. For this lab we focus just on telemetry. An example is provided from the NodeMCU documents:
Startup Scripts for your ESP8266 to use the Cloud
Before you can run Cloud code on your device you need a couple of important pieces:
- You need to connect to a wifi network
- You need to set the clock (The ESP8266 has a real-time clock that’s reliable once it’s set)
- You need some extra code to generate security tokens to access Azure.
The following two scripts work together to initialize your ESP8266 to have these pieces. NodeMCU will look for an init.lua when it boots and execute it, so you have to be sure it’s correct.
The init.lua provided below invokes the startup.lua to set the clock (which requires wifi), so you need to save the startup.lua first, before you save the init.lua or things will break.
In Esplorer create a new script and paste the following code into it. Then save the file as your startup.lua and save to the ESP8266.
In Esplorer create a new script and paste the following code into it. Then save the file as your init.lua and save to the ESP8266.
Create the Lua Program in ESPlorer
As in the previous lab, you’ll write lua code in ESplorer. This code (below), operates the photocell and LED as a nightlight.
- Launch ESPlorer.jar, select your serial port, and press the ‘Open’ button
- Create a new script in Esplorer with these contents. Save it as Lab04.lua
If you recall, you should have your connection from before and it should look something like this:
HostName=ThingLabsIoTHub.azure-devices.net;DeviceId=ThingLabs-esp8266;SharedAccessKey=XBoriIl+BZTpz6Yk84SPtDxel+wINQy1izIOfEQ4E3w=
From this, you insert the DeviceId(ThingLabs-esp8266) as your DEVICE, the HostName (ThingLabsIoTHub.azure-devices.net) as your IOTHUB, and the SharedAccesskey (XBoriIl+BZTpz6Yk84SPtDxel+wINQy1izIOfEQ4E3w=) as your IOTHUB_DEVICE_CONNECTION_KEY. It should look like this:
DEVICE = “THingLabs-esp8266” IOTHUB = “ThingLabsIoTHub.azure-devices.net” IOTHUB_DEVICE_CONNECTION_KEY = “XBoriIl+BZTpz6Yk84SPtDxel+wINQy1izIOfEQ4E3w=”
Run the App on the Device
To run the application you will save it to the ESP8266, reset the device, then invoke the code.
- Press the button ‘Save to ESP’ on the lower left of the ESPlorer interface.
- Push the reset button on the ES8266
- When it’s done booting, click the ‘Reload’ button on the right side. You should see a list of files on the ESP8266
- Double click the file on the right hand side that you saved. This should execute your code.
Monitor the Messages Being Received by Azure IoT Hub
Using the Device Explorer utility for Windows you installed in the previous lab, you can monitor the messages being received in Azure IoT Hub.
- Open the Data tab.
- Select the device from the drop-down list.
- Click Monitor to begin monitoring messages as they come into your Azure IoT Hub.
Conclusion & Next Steps
Congratulations! In this lab, you updated the Weather Station application to send messages to Azure IoT Hub. The core concepts you’ve learned are:
- Using the Azure IoT MQTT Gateway to receive data sent using MQTT to send device-to-cloud (D2C) messages.
- Sending messages as a continuous stream (e.g. once per second from a continuously measuring sensor).
At this point, nothing interesting is happening in the cloud with that data you are sending to Azure. It is simply being persisted for a default amount of time (1-day) and then being dropped. In the next lab, you will create a web application to visualize the data.