Welcome to this tutorial on setting an ESP8266 microcontroller up for retriving sensor measurements, and sending these to the Azure IoTHub.
- A Windows PC
- ESP8266 developer board. like this www.banggood.com
- A account on Microsoft Azure
- Installed the latest version of arduino IDE
- To work with a real sensor, use the DHT11 or DHT22 (but simulaton is possible)
** Please use Ardunino version 1.6.8 or above
In the Arduino IDE, Select should Preferences” in the File Menu.
The Arduino IDE, need to be avare of the definition for ESP8266, so a community driven board definition is added
Type “http://arduino.esp8266.com/stable/package_esp8266com_index.json” into the field for Additional Boards Manager URL.
Select Boards Manager via the Tool Menu.
From the boards manager pop up, scroll until you find the esp8266 board. Select the latest version and install.
Now that the Arduino IDE is aware of the ESP8266, we need to tell it to use this board Select it via the Tools -> Board menu.
Match other settings with the settings here:
The Arduino IDE is now set up and ready to for you to start to work with :-)
If this is your first time connecting your development board, it will start installing drivers for the USB-serial port
in the windows device list, you can read the port number of the new device
right click the windows start button, and select Device manager
Chose the right port in the Arduino IDE -> Tools -> Port
Now its time to upload the first simple program: Hello World
Get the sample application from GitHub
The sample application is hosted on GitHub.
To clone the sample repository to your computer, follow these steps:
- Open a command prompt or a terminal window.
- Go to a folder where you want the sample application to be stored.
- Run the following command: git clone https://github.com/kaareseras/IoTHub_ESP8266.git
**If you have not installed GIT, you can download it here: https://git-for-windows.github.io/
In you Arduino IDE:
- Open the HelloWorld.ino file from the HelloWorld folder
- Make sure that the right port is selected
- Click the right arrow in the top left
The Arduino IDE will now compile and upload the code to the ESP processor, and if all goes well, the onboard blue LED schould start to blink.
Lets move on to a bit harder challenge
- Open the ScanWifi.ino file from the ScanWifi folder
- Make sure that the right port is selected
- Click the right arrow in the top left
The Arduino IDE will now compile and upload the code to the ESP processor When the code is uploaded, click the Serial Monitor icon in the top right connor, make sure to set the serial speed to 115200 Baud
The Serial Monitor should now list all the wi-fi networks that the ESP8266 can detect
This section describes how to create an IoT hub using the Azure portal.
- Log in to the Azure portal.
- Choose +Create a resource, then Search the Marketplace for the IoT Hub.
- Select IoT Hub and click the Create button. You see the first screen for creating an IoT hub.
Fill in the fields.
Subscription: Select the subscription to use for your IoT hub.
Resource Group: You can create a new resource group or use an existing one. To create a new one, click Create new and fill in the name you want to use. To use an existing resource group, click Use existing and select the resource group from the dropdown list. For more information, see Manage Azure Resource Manager resource groups.
Region: This is the region in which you want your hub to be located. Select the location closest to you from the dropdown list.
IoT Hub Name: Put in the name for your IoT Hub. This name must be globally unique. If the name you enter is available, a green check mark appears.
- Click Next: Size and scale to continue creating your IoT hub.
On this screen, you can take the defaults and just click Review + create at the bottom.
Pricing and scale tier: You can choose from several tiers depending on how many features you want and how many messages you send through your solution per day. The free tier is intended for testing and evaluation. It allows 500 devices to be connected to the IoT hub and up to 8,000 messages per day. Each Azure subscription can create one IoT Hub in the free tier.
IoT Hub units: The number of messages allowed per unit per day depends on your hub's pricing tier. For example, if you want the IoT hub to support ingress of 700,000 messages, you choose two S1 tier units.
Advanced / Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. Most IoT hubs only need four partitions.
- Click Review + create to review your choices. You see something similar to this screen.
- Click Create to create your new IoT hub. Creating the hub takes a few minutes.
After your hub has been created, retrieve the connection string for the hub. This is used to connect devices and applications to your hub.
- Click on your hub to see the IoT Hub pane with Settings, and so on. Click Shared access policies.
- In Shared access policies, select the iothubowner policy.
- Under Shared access keys, copy the Connection string -- primary key to be used later.
In this section, you create a device identity in the identity registry in your IoT hub. A device cannot connect to IoT hub unless it has an entry in the identity registry. For more information, see the "Identity registry" section of the IoT Hub developer guide
- In your IoT hub navigation menu, open IoT Devices, then select Add to register a new device in your IoT hub
- Provide a name for your new device, such as myDeviceID, and select Save. This action creates a new device identity for your IoT hub.
- After the device is created, open the device from the list in the IoT devices pane. Copy the Connection string---primary key to use later.
Now everything is ready to start working on the actual code for the device
Open the app.ino file in the app folder in the Arduino IDE.
The sketch we need to import all the Microsoft SDKs and som other Libs:
- In the Arduino IDE select : Sketch -> Include libraries -> Manage libraries.
- Search for the following library names one by one. For all libraries, click Install.
- AzureIoTHub
- AzureIoTUtility
- AzureIoTProtocol_MQTT
- ArduinoJson Important Select version 5.13.5
- DHT sensor library
- Adafruit Unified Sensor
Currently there is a few bugs that we need to fix before moving on
Open the file Arduino.H from the folder C:\Users\username\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.2\cores\esp8266
*Replace username in URL with the correct name from your computer
delete line 137: #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
Open the file WiFiClientSecure.h from the folder C:\Users\username\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.2\libraries\ESP8266WiFi\src
*Replace username in URL with the correct name from your computer
Replace the code in the file with this code:
//#include "WiFiClientSecureAxTLS.h"
//using namespace axTLS;
/**********************************
* !! Now BearSSL is the default !!
*
* While not advised,
* Use legacy API without updating with:
*/
#define USING_AXTLS
#include <ESP8266WiFi.h>
//#include <WiFiClientSecure.h>
#include "WiFiClientSecureAxTLS.h"
using namespace axTLS;
/*
*
**********************************/
#include "WiFiClientSecureBearSSL.h"
#ifndef USING_AXTLS
// do not default to BearSSL API ("using" has no "unusing" counterpart)
using namespace BearSSL;
#endif
Open the userConfig.h file in the app folder.
// Physical device information for board and sensor
#define DEVICE_ID "myDeviceId " Change to match the DeviceID in Azure
#define DHT_TYPE DHT11 Change to math the sensor you have
- Save the file
- Attach DHT11 to device Very important to wire it correctly
Deploy the application to ESP8266 board
After the upload completes successfully, follow these steps to enter your credentials:
- Open Arduino IDE, click Tools > Serial Monitor.
- In the serial monitor window, notice the two drop-down lists in the lower-right corner.
- Select No line ending for the left drop-down list.
- Select 115200 baud for the right drop-down list.
- In the input box located at the top of the serial monitor window, enter the following information if you are asked to provide them, and then click Send.
- Wi-Fi SSID
- Wi-Fi password
- Device connection string
Note
The credential information is stored in the EEPROM of ESP8266. If you click the reset button on the ESP8266 board, the sample application asks if you want to erase the information. Enter Y to have the information erased. You are asked to provide the information a second time. Verify the sample application is running successfully If you see the following output from the serial monitor window and the blinking LED on ESP8266, the sample application is running successfully.
Congratulations, your device is now sending sensordata to IoTHub
Use Azure IoT Tools for Visual Studio Code to send and receive messages between your device and IoT Hub
Azure IoT Tools is a useful Visual Studio Code extension that makes IoT Hub management and IoT application development easier. This article focuses on how to use Azure IoT Tools for Visual Studio Code to send and receive messages between your device and your IoT hub.
Note
Some of the features mentioned in this article, like cloud-to-device messaging, device twins, and device management, are only available in the standard tier of IoT hub.
You learn how to use Azure IoT Tools for Visual Studio Code to monitor device-to-cloud messages and to send cloud-to-device messages. Device-to-cloud messages could be sensor data that your device collects and then sends to your IoT hub. Cloud-to-device messages could be commands that your IoT hub sends to your device to blink an LED that is connected to your device.
- Use Azure IoT Tools for Visual Studio Code to monitor device-to-cloud messages.
- Use Azure IoT Tools for Visual Studio Code to send cloud-to-device messages.
- An active Azure subscription.
- An Azure IoT hub under your subscription.
- Visual Studio Code
- Azure IoT Tools for VS Code or open this link in Visual Studio Code.
- In Explorer view of VS Code, expand Azure IoT Hub Devices section in the bottom left corner.
- Click Select IoT Hub in context menu.
- A pop-up will show in the bottom right corner to let you sign in to Azure for the first time.
- After you sign in, your Azure Subscription list will be shown, then select Azure Subscription and IoT Hub.
- The device list will be shown in Azure IoT Hub Devices tab in a few seconds.
- Right-click your device and select Start Monitoring D2C Message.
- The monitored messages will be shown in OUTPUT > Azure IoT Hub Toolkit view.
- To stop monitoring, right-click the OUTPUT view and select Stop Monitoring D2C Message.
To send a message from your IoT hub to your device, follow these steps:
- Right-click your device and select Send C2D Message to Device.
- Enter the message in input box. (“start” or “stop”)
- Results will be shown in OUTPUT > Azure IoT Hub Toolkit view.
You learn how to visualize real-time sensor data that your Azure IoT hub receives by using Power BI. If you want to try toe visualize the data in your IoT hub with Web Apps, please see Use Azure Web Apps to visualize real-time sensor data from Azure IoT Hub.
What you do
- Get your IoT hub ready for data access by adding a consumer group.
- Create, configure, and run a Stream Analytics job for data transfer from your IoT hub to your Power BI account.
- Create and publish a Power BI report to visualize the data.
What you need
- A Power BI account.
Consumer groups provide independent views into the event stream that enable apps and Azure services to independently consume data from the same Event Hub endpoint. In this section, you add a consumer group to your IoT hub's built-in endpoint that is used later in this tutorial to pull data from the endpoint. To add a consumer group to your IoT hub, follow these steps:
- In the Azure portal, open your IoT hub.
- On the left pane, select Built-in endpoints, select Events on the right pane, and enter a name under Consumer groups. Select Save.
Let's start by creating a Stream Analytics job. After you create the job, you define the inputs, outputs, and the query used to retrieve the data.
Create a Stream Analytics job
- In the Azure portal, click Create a resource > Internet of Things > Stream Analytics job.
- Enter the following information for the job.
Job name: The name of the job. The name must be globally unique.
Resource group: Use the same resource group that your IoT hub uses.
Location: Use the same location as your resource group.
- Click Create.
- Open the Stream Analytics job.
- Under Job Topology, click Inputs.
- In the Inputs pane, click Add stream input, and then enter the following information: **Input alias: The unique alias for the input and select Provide IoT Hub settings manually below. Source: Select IoT hub. Endpoint: Click Messaging. Consumer group: Select the consumer group you just created.
- Click Create.
- Under Job Topology, click Outputs.
- In the Outputs pane, click Add and Power BI, and then enter the following information: Output alias: The unique alias for the output. Group Workspace: Select your target group workspace. Dataset Name: Enter a dataset name. Table Name: Enter a table name.
- Click Authorize, and then sign into your Power BI account.
- Click Create.
- Under Job Topology, click Query.
- Replace [YourInputAlias] with the input alias of the job.
- Replace [YourOutputAlias] with the output alias of the job.
- Click Save.
In the Stream Analytics job, click Start > Now > Start.
Once the job successfully starts, the job status changes from Stopped to Running.
- Ensure the ESP8266 Board is running.
- Sign in to your Power BI account.
- Click the workspace you used, My Workspace.
- Click Datasets. You should see the dataset that you specified when you created the output for the Stream Analytics job.
- For the dataset you created, click Add Report (the first icon to the right of the dataset name).
- Create a line chart to show real-time temperature over time.
- On the report creation page, add a line chart.
- On the Fields pane, expand the table that you specified when you created the output for the Stream Analytics job.
- Drag EventEnqueuedUtcTime to Axis on the Visualizations pane.
- Drag temperature to Values. A line chart is created. The x-axis displays date and time in the UTC time zone. The y-axis displays temperature from the sensor.
- Create another line chart to show real-time humidity over time. To do this, follow the same steps above and place EventEnqueuedUtcTime on the x-axis and humidity on the y-axis.
- Click Save to save the report.
- Click Reports on the left pane, and then click the report that you just created.
- Click File > Publish to web.
- Click Create embed code, and then click Publish.
You're provided the report link that you can share with anyone for report access and a code snippet to integrate the report into your blog or website.
Microsoft also offers the Power BI mobile apps for viewing and interacting with your Power BI dashboards and reports on your mobile device.