Connect Sparkfun ESP8266 Thing Dev to Azure IoT Hub in the cloud
- Raspberry Pi web simulator
- Simulated device with .NET
- Simulated device with Java
- Simulated device with Node.js
- IoT DevKit AZ3166 with VS Code
- Raspberry Pi with Node.js
- Raspberry Pi with Python
- Raspberry Pi with C
- Intel Edison with Node.js
- Intel Edison with C
- Adafruit Feather HUZZAH ESP8266 with Arduino IDE
- Sparkfun ESP8266 Thing Dev with Arduino IDE
- Adafruit Feather M0 with Arduino IDE
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.
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
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
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
- Sign in to the Azure portal.
Select Create a resource > Internet of Things > IoT Hub.
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.
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.
Select Next: Size and scale to continue creating your IoT hub.
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.
Select Review + create.
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.
You do not need this 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.
Register a device in the IoT hub for your device
In your IoT hub navigation menu, open IoT devices, then click Add to register a device in your IoT hub.
Enter a Device ID for the new device. Device IDs are case sensitive.
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.
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.
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 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.
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:
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
dialoutthat is 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.
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:
- 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-SparkFun-ThingDev-client-app.git
Install the package for Sparkfun ESP8266 Thing Dev in Arduino IDE:
- Open the folder where the sample application is stored.
Open the app.ino file in the app folder in Arduino IDE.
In the Arduino IDE, click File > Preferences.
- In the Preferences dialog box, click the icon next to the Additional Boards Manager URLs text box.
In the pop-up window, enter the following URL, and then click OK.
In the Preference dialog box, click OK.
Click Tools > Board > Boards Manager, and then search for esp8266. ESP8266 with a version of 2.2.0 or later should be installed.
Click Tools > Board > Sparkfun ESP8266 Thing Dev.
Install necessary libraries
- In the Arduino IDE, click Sketch > Include Library > Manage Libraries.
- Search for the following library names one by one. For each of the library 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 enable the sample application to use simulated data, follow these steps:
- Open the
config.hfile in the
Locate the following line of code and change the value from
define SIMULATED_DATA true
Deploy the sample application to Sparkfun ESP8266 Thing Dev
- In the Arduino IDE, click Tool > Port, and then click the serial port for Sparkfun ESP8266 Thing Dev.
- Click Sketch > Upload to build and deploy the sample application to Sparkfun ESP8266 Thing Dev.
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:
- In the Arduino IDE, click Tools > Serial Monitor.
- In the serial monitor window, notice the two drop-down lists on the bottom 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 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.
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:
- Manage cloud device messaging with iothub-explorer
- Save your Azure IoT hub messages to Azure data storage
- Use Power BI to visualize real-time sensor data from your IoT hub
- Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
- Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
- Manage devices with iothub-explorer
- Use Logic Apps for remote monitoring and notifications