Connect Adafruit Feather HUZZAH ESP8266 to Azure IoT Hub in the cloud

Connection between DHT22, Feather HUZZAH ESP8266, and IoT Hub

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.

Note

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 0 to 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

Parts needed for the tutorial

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:

Note

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.

  1. Log in to the Azure portal.

  2. Choose +Create a resource, then choose Internet of Things.

  3. Click Iot Hub from the list on the right. You see the first screen for creating an IoT hub.

    Screenshot showing creating a hub in the Azure portal

    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 Use resource groups to manage your Azure resources.

    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.

    Important

    The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.

  4. Click Next: Size and scale to continue creating your IoT hub.

    Screenshot showing setting size and scale for a new IoT hub using the Azure portal

    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.

  5. Click Review + create to review your choices. You see something similar to this screen.

    Screenshot reviewing information for creating the new IoT hub

  6. 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.

  1. Click on your hub to see the IoT Hub pane with Settings, and so on. Click Shared access policies.

  2. In Shared access policies, select the iothubowner policy.

  3. Under Shared access keys, copy the Connection string -- primary key to be used later.

    Show how to retrieve the connection string

    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

  1. In your IoT hub navigation menu, open IoT Devices, then click Add to register a new device in your IoT hub.

    Create device identity in portal

  2. Provide a name for your new device, such as myDeviceId, and click Save. This action creates a new device identity for your IoT hub.

    Add a new device

    Important

    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.

  3. 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.

    Device connection string

Note

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.

Connections reference

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

For more information, see Adafruit DHT22 sensor setup and Adafruit Feather HUZZAH Esp8266 Pinouts.

Now your Feather Huzzah ESP8266 should be connected with a working sensor.

Connect DHT22 with Feather Huzzah

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.

Connect Feather Huzzah 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:

  1. 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 uucp or dialout is the group owner name of the USB port.

  2. 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.

  3. 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:

  1. Open a command prompt or a terminal window.

  2. Go to a folder where you want the sample application to be stored.

  3. 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.

  4. Open the folder where the sample application is stored.

  5. Open the app.ino file in the app folder in the Visual Studio Code.

    Open the sample application in Visual Studio Code

  6. In the Visual Studio Code, enter F1.

  7. Type Arduino and select Arduino: Board Manager.

  8. In the Arduino Board Manager tab, click Additional URLs.

    VS Code Arduino Board Manager

  9. In the User Settings window, copy and paste the following at the end of the file

    "arduino.additionalUrls": "http://arduino.esp8266.com/stable/package_esp8266com_index.json"
    

    Configure Arduino package URL in VS Code

  10. Save the file and close the User Settings tab.

  11. Click Refresh Package Indexes. After the refresh finishes, search for esp8266.

  12. Click Install button for esp8266.

    Boards Manager indicates that ESP8266 with a version of 2.2.0 or later is installed.

    The esp8266 package is installed

  13. Enter F1, then type Arduino and select Arduino: Board Config.

  14. Click box for Selected Board: and type esp8266, then select Adafruit HUZZAH ESP8266 (esp8266).

    Select esp8266 board

Install necessary libraries

  1. In the Visual Studio Code, enter F1, then type Arduino and select Arduino: Library Manager.

  2. Search for the following library names one by one. For each library that you find, click Install.

    • AzureIoTHub
    • AzureIoTUtility
    • AzureIoTProtocol_MQTT
    • ArduinoJson
    • 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:

  1. Open the config.h file in the app folder.

  2. Locate the following line of code and change the value from false to true:

    define SIMULATED_DATA true
    

    Configure the sample application to use simulated data

  3. Save the file.

Deploy the sample application to Feather HUZZAH ESP8266

  1. In the Visual Studio Code, click on the status bar, and then click the serial port for Feather HUZZAH ESP8266.

  2. Enter 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:

  1. Open Arduino IDE, click Tools > Serial Monitor.

  2. In the serial monitor window, notice the two drop-down lists in the lower-right corner.

  3. Select No line ending for the left drop-down list.

  4. Select 115200 baud for the right drop-down list.

  5. 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 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.

Final output in Arduino IDE

Next steps

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 other IoT scenarios, see the following: