Connect to Zentser: ESP8266 and DHT22 for Temperature and Humidity readings

DHT22 is the best sensor to get started with. In one inexpensive sensor, you can accurately read temperature or humidity. Today, I'll show you how to connect it to Zentser so that you can proactively monitor your temperature or humidity readings on the go.

For the visual learners, here's additional video instructions:


Requirements


Mobile App:

Zentser for iOS (install)

Or

Zentser for Android (install)


Software:

PlatformIO IDE (download) on top of Visual Studio Code (download)


Hardware:

ESP8266 (buy) low-cost microcontroller with Wi-Fi

DHT22 (buy) accurate temperature & humidity sensor



Instructions


Zentser Mobile App


Install the app

Get it on Google Play

Hopefully the first step is easy and intuitive. We’re at a point where a four year old can install a dozen apps on a phone that was unlocked for one minute. 


Sign up

After installing and launching the app, sign up. You can register by validating your email and creating an account. Alternatively, authenticate with your social Google or Facebook identity.

Sign Up screen
Sign Up screen


In the app, create a new device 

Before you click “Create device” button, I recommend to edit your “Device name” and “Sensor name” text fields. As a naming best practice:

  • For “Device name” enter where or what you are measuring. For example, I would enter ‘Basement’ for tracking that the temperature there is above freezing
  • For “Sensor name” enter what you are measuring. Optionally, include the units. For example, I would enter “Temp (F)”

Now click “Create device”

Add your first device
Add your first device
Give it a name
Give it a name

Get your custom code

Once you see generated code for your device, click on “Export code” to send yourself a copy. You will need to get it over to a computer to program your ESP2866 microcontroller. You can mail the code to yourself or save it in a cloud storage file. 

Copy custom code
Copy custom code
Save or Send code to yourself
Save or Send code to yourself


Connect the hardware


Here’s the hardware I use for my personal configuration:

  • ESP8266 - a low-cost microcontroller with Wi-Fi
  • DHT22 - an accurate temperature & humidity sensor

If you're familiar with sensors, you might be asking why I'm not using DHT11 instead. Well, in my experience, the smaller range and more significant variance make DHT11 impractical. While DHT11 is one of the cheapest sensors, you will get what you pay for in real-world applications. So let's invest a few extra dollars and buy more accurate readings.

You can use different parts. Just note that the pinout can be completely different on different ESP8266 boards and DHT22 devices. Triple check your positives and negatives before connecting the wires. If you smell a burning smell, you know you fried something. Toss the burnt part and try again.

If you know what you’re doing, add resistors, breadboards, solder your connections, etc. If you’re like me, connect the three wires.

Hardware wiring
Hardware wiring

Now connect your ESP8266 microcontroller to a micro-USB, then plug a regular USB end into your computer.

Note: Don’t use USB charge-only cables!

Make sure to use a data cable USB and save yourself hours of troubleshooting red herring errors.

Program ESP8266 from your computer


Get the IDE

If you haven’t yet, download and install “Visual Studio Code.” This is not the same as “Visual Studio.” 

Then, specifically for microcontrollers, install “PlatformIO IDE” extension:

PlatforIO Install
  1. Open VSCode Extension Manager
  2. Search for official PlatformIO IDE extension
  3. Install PlatformIO IDE.


Get the template code

Download the latest zip file (here). Extract the zip file.


In VS Code, choose File > Open Folder… 


Browse to “...\esp-zentser-sdk\examples\001-DHT-Temperature”

Select the final “001-DHT-Temperature” folder. Your Explorer window should look like this:

Edit the code
Edit the code


Open the ‘main.cpp’ file in ‘src’ folder.


Edit the code


To connect your sensors to the internet, you’ll need to provide Wi-Fi credentials. 

Edit the following code and double check for typos:

// Make sure to enter your WiFi and Password
// to connect the device to the internet
char const *ssid = "YOUR_WIFI";
char const *wiFiPassword = "YOUR_WIFI_PASSWORD";

Enter your custom certificates. This is the code you sent or saved earlier from a mobile app. The snippet contains certificates required for validation of your connected device and device’s identifiers. So replace the sample code below with the code from the mobile app:

static const char caCert[] PROGMEM = R"EOF(
/* CA CERTIFICATE */
)EOF";

static const char cert[] PROGMEM = R"KEY(
/* CERTIFICATE */
)KEY";

static const char privateKey[] PROGMEM = R"KEY(
/* PRIVATE_KEY */
)KEY";

String deviceId = "YOUR_DEVICE_ID"; // Zentser Device ID
String sensorId = "YOUR_SENSOR_ID"; // Zentser Sensor ID

Further custom edits may be required. If you plan to connect different sensors, setup different pin configurations, or collect other measurements (e.g. humidity or temp in celsius), make those code edits now.


Once you are satisfied with the changes, build and deploy the code to the microcontroller.

Build and deploy onto ESP8266
Build and deploy onto ESP8266

After the successful build and deployment, check the Serial Monitor to confirm that the microcontroller successfully connected to the internet, read the sensor, and sent the data to Zentser.

Observe the Serial Monitor that it works

Almost done

You have just:

  • created a Zentser device
  • hardwired physical components
  • and successfully programmed your device

If you’re not an engineer, that’s quite a fit. If you are an engineer, it’s still quite an accomplishment to celebrate. Now you have an active sensor sending you data you can access on the go.


Go back into your mobile Zentser app. You should now see a numeric value presented for your device. If you drill down, you can even see timeline graphs with your temperature readings.


Next steps

Now that you have configured your sensor to report to a Zentser app, you can take it to the next level.


Automate with Notifications

In the app, you can set up high or low thresholds for your readings. That way, you will get notified in real-time of any abnormalities.

This goes beyond checking for temperature or humidity. With different sensors connected to Zentser, you can detect and alert about:

  • barometric pressure changes
  • soil moisture for plants
  • water leaks
  • excessive noise
  • light overexposure
  • flammable and toxic gases

I would love to hear how you set up your sensor notifications. If you have some cool configs, or if you run into any issues, please comment below.


Now let's automate some everyday things with Zentser!

For updates on advance measuring techniques and sensors, please sign up for our list
Thanks for signing up! We will send you informative content on how to use sensors to make your life better!
Oops! Something went wrong while submitting the form.
Latest POSTS
Copyrights © zentser.io | All rights reserved.