Connect Sparkfun ESP8266 Thing Dev to Azure IoT Hub in the cloud

connection between DHT22, Thing Dev, and IoT Hub

What you will do

Connect Sparkfun ESP8266 Thing Dev to an IoT hub you will create. Then run a sample application on ESP8266 to collect temperature and humidity data from a DHT22 sensor. Finally, send the sensor data to your IoT hub.

Note

If you are using other ESP8266 boards, you can still follow these steps to connect it to your IoT hub. Depending on the ESP8266 board you are using, you may need to reconfigure the LED_PIN. For example, if you are using ESP8266 from AI-Thinker, you may change it from 0 to 2. Don't have a kit yet?: Click here

What you will learn

  • How to create an IoT hub and register a device for Thing Dev.
  • How to connect Thing Dev with the sensor and your computer.
  • How to collect sensor data by running a sample application on Thing Dev.
  • How to send the sensor data to your IoT hub.

What you will need

parts needed for the tutorial

To complete this operation, you need the following parts from your Thing Dev Starter Kit:

  • The Sparkfun ESP8266 Thing Dev board.
  • A Micro USB to Type A USB cable.

You also need the following 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.
  • Mac or PC that is running Windows or Ubuntu.
  • Wireless network for Sparkfun ESP8266 Thing Dev to connect to.
  • Internet connection to download the configuration tool.
  • Arduino IDE version 1.6.8 (or newer), earlier versions will not 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

  1. Sign in to the Azure portal.

  2. Select Create a resource > Internet of Things > IoT Hub.

    Screenshot of Azure portal navigation to IoT Hub

  3. In the IoT hub pane, enter the following information for your IoT hub:

    • Subscription: Choose the subscription that you want to use to create this IoT hub.

    • Resource group: Create a resource group to host the IoT hub or use an existing one. For more information, see Use resource groups to manage your Azure resources.

    • Region: Select the closest location to you.

    • Name: Create a name for your IoT hub. 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.

    IoT Hub basics window

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

  5. Choose your Pricing and scale tier. For this article, select the F1 - Free tier if it's still available on your subscription. For more information, see the Pricing and scale tier.

    IoT Hub size and scale window

  6. Select Review + create.

  7. Review your IoT hub information, then click Create. Your IoT hub might take a few minutes to create. You can monitor the progress in the Notifications pane.

Now that you have created an IoT hub, locate the important information that you use to connect devices and applications to your IoT hub.

In your IoT hub navigation menu, open Shared access policies. Select the iothubowner policy, and then copy the Connection string---primary key of your IoT hub. For more information, see Control access to IoT Hub.

Note

You do not need the iothubowner connection string for this set-up tutorial. However, you may need it for some of the tutorials or different IoT scenarios after you complete this set-up.

Get your IoT hub connection string

Register your device in the IoT hub

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

    Add a device in the IoT Devices of your IoT hub

  2. Enter a Device ID for the new device. Device IDs are case sensitive.

    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. Click Save.

  4. After the device is created, open the device from the list in the IoT devices pane.

  5. Copy the Connection string---primary key to use later.

    Get the device connection string

Connect ESP8266 Thing Dev with the sensor and your computer

Connect a DHT22 temperature and humidity sensor to ESP8266 Thing Dev

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, we will use the following wiring:

Start (Sensor) End (Board) Cable Color
VDD (Pin 27F) 3V (Pin 8A) Red cable
DATA (Pin 28F) GPIO 2 (Pin 9A) White cable
GND (Pin 30F) GND (Pin 7J) Black cable

Now your Sparkfun ESP8266 Thing Dev should be connected with a working sensor.

connect dht22 with ESP8266 Thing Dev

Connect Sparkfun ESP8266 Thing Dev to your computer

Use the Micro USB to Type A USB cable to connect Sparkfun ESP8266 Thing Dev to your computer as follows.

connect feather huzzah to your computer

Add serial port permissions – Ubuntu only

If you use Ubuntu, make sure a normal user has the permissions to operate on the USB port of Sparkfun ESP8266 Thing Dev. To add serial port permissions for a normal user, 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 uucp or dialout that 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. Log out Ubuntu and log in it again for the change to take effect.

Collect sensor data and send it to your IoT hub

In this section, you deploy and run a sample application on Sparkfun ESP8266 Thing Dev. The sample application blinks the LED on Sparkfun ESP8266 Thing Dev 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-SparkFun-ThingDev-client-app.git
    

Install the package for Sparkfun ESP8266 Thing Dev in Arduino IDE:

  1. Open the folder where the sample application is stored.
  2. Open the app.ino file in the app folder in Arduino IDE.

    open the sample application in arduino ide

  3. In the Arduino IDE, click File > Preferences.

  4. In the Preferences dialog box, click the icon next to the Additional Boards Manager URLs text box.
  5. In the pop-up window, enter the following URL, and then click OK.

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

    point to a package url in arduino ide

  6. In the Preference dialog box, click OK.

  7. Click Tools > Board > Boards Manager, and then search for esp8266. ESP8266 with a version of 2.2.0 or later should be installed.

    the esp8266 package is installed

  8. Click Tools > Board > Sparkfun ESP8266 Thing Dev.

Install necessary libraries

  1. In the Arduino IDE, click Sketch > Include Library > Manage Libraries.
  2. Search for the following library names one by one. For each of the library 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 enable 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 with Control-s.

Deploy the sample application to Sparkfun ESP8266 Thing Dev

  1. In the Arduino IDE, click Tool > Port, and then click the serial port for Sparkfun ESP8266 Thing Dev.
  2. Click Sketch > Upload to build and deploy the sample application to Sparkfun ESP8266 Thing Dev.

Note

If you are using macOS you could probably see the following messages during uploading. warning: espcomm_sync failed,error: espcomm_open failed. Please open your ternimal window and finish below actions to solve this issue.

cd /System/Library/Extensions/IOUSBFamily.kext/Contents/PlugIns
sudo mv AppleUSBFTDI.kext AppleUSBFTDI.disabled
sudo touch /System/Library/Extensions

Enter your credentials

After the upload completes successfully, follow the steps to enter your credentials:

  1. In the Arduino IDE, click Tools > Serial Monitor.
  2. In the serial monitor window, notice the two drop-down lists on the bottom 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 Sparkfun ESP8266 Thing Dev. If you click the reset button on the Sparkfun ESP8266 Thing Dev board, the sample application asks you if you want to erase the information. Enter Y to have the information erased and you are asked to provide the information again.

Verify the sample application is running successfully

If you see the following output from the serial monitor window and the blinking LED on Sparkfun ESP8266 Thing Dev, the sample application is running successfully.

final output in arduino ide

Next steps

You have successfully connected a Sparkfun ESP8266 Thing Dev 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: