Connect Adafruit Feather HUZZAH ESP8266 to Azure IoT Hub in the cloud
What you do
Connect Adafruit Feather HUZZAH ESP8266 to an IoT hub that you create. Then you run a sample application on ESP8266 to collect the temperature and humidity data from a DHT22 sensor. Finally, you send the sensor data to your IoT hub.
If you're using other ESP8266 boards, you can still follow these steps to connect it to your IoT hub. Depending on the ESP8266 board you're using, you might need to reconfigure the
LED_PIN. For example, if you're using ESP8266 from AI-Thinker, you might change it from
2. Don't have a kit yet? Get it from the Azure website.
What you learn
- How to create an IoT hub and register a device for Feather HUZZAH ESP8266
- How to connect Feather HUZZAH ESP8266 with the sensor and your computer
- How to collect sensor data by running a sample application on Feather HUZZAH ESP8266
- How to send the sensor data to your IoT hub
What you need
To complete this operation, you need the following parts from your Feather HUZZAH ESP8266 Starter Kit:
- The Feather HUZZAH ESP8266 board
- A Micro USB to Type A USB cable
You also need the following things for your development environment:
- An active Azure subscription. If you don't have an Azure account, create a free Azure trial account in just a few minutes.
- A Mac or PC that is running Windows or Ubuntu.
- A wireless network for Feather HUZZAH ESP8266 to connect to.
- An Internet connection to download the configuration tool.
- Visual Studio Code extension for Arduino.
The Arduino IDE version used by Visual Studio Code extension for Arduino has to be version 1.6.8 or later. Earlier versions don't work with the AzureIoT library.
The following items are optional in case you don’t have a sensor. You also have the option of using simulated sensor data.
- An Adafruit DHT22 temperature and humidity sensor
- A breadboard
- M/M jumper wires
Create an IoT hub
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.
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.
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.
For details about the other tier options, see Choosing the right IoT Hub tier.
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.
Retrieve connection string for IoT hub
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.
For more information, see Access control in the "IoT Hub developer guide."
Register a new device in the IoT hub
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.
The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid any sensitive information while naming it.
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.
The IoT Hub identity registry only stores device identities to enable secure access to the IoT hub. It stores device IDs and keys to use as security credentials, and an enabled/disabled flag that you can use to disable access for an individual device. If your application needs to store other device-specific metadata, it should use an application-specific store. For more information, see IoT Hub developer guide.
Connect Feather HUZZAH ESP8266 with the sensor and your computer
In this section, you connect the sensors to your board. Then you plug in your device to your computer for further use.
Connect a DHT22 temperature and humidity sensor to Feather HUZZAH ESP8266
Use the breadboard and jumper wires to make the connection as follows. If you don’t have a sensor, skip this section because you can use simulated sensor data instead.
For sensor pins, use the following wiring:
|Start (Sensor)||End (Board)||Cable Color|
|VDD (Pin 31F)||3V (Pin 58H)||Red cable|
|DATA (Pin 32F)||GPIO 2 (Pin 46A)||Blue cable|
|GND (Pin 34F)||GND (PIn 56I)||Black cable|
Now your Feather Huzzah ESP8266 should be connected with a working sensor.
Connect Feather HUZZAH ESP8266 to your computer
As shown next, use the Micro USB to Type A USB cable to connect Feather HUZZAH ESP8266 to your computer.
Add serial port permissions (Ubuntu only)
If you use Ubuntu, make sure you have the permissions to operate on the USB port of Feather HUZZAH ESP8266. To add serial port permissions, follow these steps:
Run the following commands at a terminal:
ls -l /dev/ttyUSB* ls -l /dev/ttyACM*
You get one of the following outputs:
- crw-rw---- 1 root uucp xxxxxxxx
- crw-rw---- 1 root dialout xxxxxxxx
In the output, notice that
dialoutis the group owner name of the USB port.
Add the user to the group by running the following command:
sudo usermod -a -G <group-owner-name> <username>
<group-owner-name>is the group owner name you obtained in the previous step.
<username>is your Ubuntu user name.
Sign out of Ubuntu, and then sign in again for the change to appear.
Collect sensor data and send it to your IoT hub
In this section, you deploy and run a sample application on Feather HUZZAH ESP8266. The sample application blinks the LED on Feather HUZZAH ESP8266, and sends the temperature and humidity data collected from the DHT22 sensor to your IoT hub.
Get the sample application from GitHub
The sample application is hosted on GitHub. Clone the sample repository that contains the sample application from GitHub. To clone the sample repository, 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/Azure-Samples/iot-hub-feather-huzzah-client-app.git
Next, install the package for Feather HUZZAH ESP8266 in Visual Studio Code.
Open the folder where the sample application is stored.
Open the app.ino file in the app folder in the Visual Studio Code.
In the Visual Studio Code, enter
Type Arduino and select Arduino: Board Manager.
In the Arduino Board Manager tab, click Additional URLs.
In the User Settings window, copy and paste the following at the end of the file
Save the file and close the User Settings tab.
Click Refresh Package Indexes. After the refresh finishes, search for esp8266.
Click Install button for esp8266.
Boards Manager indicates that ESP8266 with a version of 2.2.0 or later is installed.
F1, then type Arduino and select Arduino: Board Config.
Click box for Selected Board: and type esp8266, then select Adafruit HUZZAH ESP8266 (esp8266).
Install necessary libraries
In the Visual Studio Code, enter
F1, then type Arduino and select Arduino: Library Manager.
Search for the following library names one by one. For each library that you find, click Install.
DHT sensor library
Adafruit Unified Sensor
Don’t have a real DHT22 sensor?
The sample application can simulate temperature and humidity data in case you don’t have a real DHT22 sensor. To set up the sample application to use simulated data, follow these steps:
config.hfile in the
Locate the following line of code and change the value from
define SIMULATED_DATA true
Save the file.
Deploy the sample application to Feather HUZZAH ESP8266
In the Visual Studio Code, click <Select Serial Port> on the status bar, and then click the serial port for Feather HUZZAH ESP8266.
F1, then type Arduino and select Arduino: Upload to build and deploy the sample application to Feather HUZZAH ESP8266.
Enter your credentials
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
The credential information is stored in the EEPROM of Feather HUZZAH ESP8266. If you click the reset button on the Feather HUZZAH 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 Feather HUZZAH ESP8266, the sample application is running successfully.
Read the messages received by your hub
One way to monitor messages received by your IoT hub from your device is to use the Azure IoT Tools for Visual Studio Code. To learn more, see Use Azure IoT Tools for Visual Studio Code to send and receive messages between your device and IoT Hub.
For more ways to process data sent by your device, continue on to the next section.
You have successfully connected a Feather HUZZAH ESP8266 to your IoT hub, and sent the captured sensor data to your IoT hub.
To continue to get started with Azure IoT Hub and to explore all extended IoT scenarios, see the following:
Send feedback about: