| title | Sending temperature data to Azure IoT Hub with a NodeMCU ESP8266 and a DHT22 sensor | |||||
|---|---|---|---|---|---|---|
| tags |
|
|||||
| date | 2020-04-01 02:32:41 -0700 |
Basic knowledge of moving around the Azure Portal. If you don't know how this works. There are plenty of guide online. (Pre)Programming knowledge.
Install the following software:
-
Python Download here
-
Arduino IDE Download here
-
Arduino Libraries
- DHT_sensor_library
- ArduinoJson (version 5.x)
- AzureIoTHub
- AzureProtocol_MQTT
- AzureIoTUtility
-
(Optional)VS Code Plugin for arduino Checkout here
-
(Optional)VS Code intellisense Checkout here
Obtain the following hardware:
- DHT22
- NodeMCU ESP 8266
- Breadboard + breadboard cables
Azure cloud
- Have an already runningIoT Hub
- Azure account with to create an IoT Hub (Free tier is available)
To setup the board 3 pins need to be connected from the board to the sensor.
- Connect pin
D2on the board toDATon the sensor - Connect ground
Gon the board toGNDon the sensor - Connect 3.3V
3Von the board toVCCon the sensor
- Install esp8266 driver File -> preference past the following line at: 'Additional Boards Manger Urls:'
http://arduino.esp8266.com/stable/package_esp8266com_index.json - Install the board: Tools -> Board: -> Board Manager and search on 'esp8266' and install (minimal version 2.5.2)
- Select the board: Tools -> Board: and select 'NodeMCu 1.0(ESP 12E Module)'
- Install libraries Tools -> Manage Libraries... and typ in the name of the libraries
- Configure the compiler so it could compile the Azure IoT packages
- Navigate to and open the
Arduino.hfile (in my case)%localappdata%\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266 - Comment out line
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))at line 137 - Navigate and open
platform.txtat%localappdata%\Arduino15\packages\esp8266\hardware\esp8266\2.6.3 - Add the following 2 keywords to line that contains
build.extra_flags=(line 80)-DDONT_USE_UPLOADTOBLOB -DUSE_BALTIMORE_CERT
- Navigate to and open the
- Copy the example code folder to your arduino sketch directory
- Open the Code and press the 'verify/ compile button' or press
CTRL+Rif the code compiles the software is setup correctly
note: that the code is based on the following documentation: https://github.com/Azure/azure-iot-arduino
Configure IoT Hub
-
Go to the Azure Portal and search for IoT Hub.
-
Press the 'Create IoT Hub' button or 'Add' button if there is already some created.
-
Fill in the field and pres on 'Next: Size and scale >
-
Here you can change the tier from Standard tier to basic tier (note: there can only be one free IoT Hub an a subscription)
-
Fill in any other fields to your likings and press 'Review + Create'.
Setup an device within IoT Hub
-
Once the IoT Hub is created navigate to that resource.
-
On the left bar select 'IoT devices' and create a new one.
-
Give it a logic name and keep it on Symmetric key and save the device.
-
Click on the newly created device to see the 'Primary Connection String' This is needed within our code on the next step.
-
Navigate back to the IoT Hub and select 'Shared access policies' then click on 'service' this should popup a window with more keys. These are also need for the next step.
- Open the code folder in the arduino IDE
- Navigate to
iot_configs.hand fill in or edit the following fields- IOT_CONFIG_WIFI_SSID
- IOT_CONFIG_WIFI_PASSWORD
- DEVICE_CONNECTION_STRING
- DHT_PIN
- INTERVAL
When all configurations are set it is time to compile the code and send it to the device.
-
To send the code you need to select the right settings. by default they are oke.
-
Change the COM port to the matching device. (Unplug the device and check what COM port is missing to determine what port you need to select)
-
Once the right COM port is selected press the "upload" button or press
CTRL+Uto upload
-
Create a new console app and include the following code Get template
-
Fill in the missing keys that are acquired from the Azure Portal
-
Run the code to seen the packages come in from IoT Hub via the Event Hub.
-
Run the arduino "Serial Monitor" Tools -> Serial Monitor to monitor the outgoing readings and make sure the Baud rate is set to
115200.

