Get to Know ESP32 #9: Data Visualization from DHT22
Reading data from sensors one by one might be confusing and boring. Here’s how to provide graphs for easier understanding.
Hello again! In this blog, we’re going to take a look at my project about data visualization. We’re going to read data from DHT22 sensor which tells us about temperature and humidity, then we’ll turn the data into a graph. For the first time in forever *insert Frozen soundtrack* I’m not going to use randomnerdtutorials, but instead we’re going to use hackster as guidance.
So, in this project we’re going to use the help of Circus of Things platform. That way, we don’t have to deal with server.
Preparation
Hardware
Here are the tools neeeded for this project. We’re no longer going to need to use resistor, since after last project I had learned that my sensor already had built-in resistor. *shame on me, but okay, we’re learning together!*
- 1 NodeMCU ESP-32S development board with 30 pins (or any ESP32)
- 2 male-to-male wire jumpers
- 3 female-to-male wire jumpers
- 1 DHT22 sensor
- 1 Laptop with Arduino IDE installed and configured
- 1 Type-A USB to Micro-USB cable
Library
Since we’re going Circus of Things platform, we will need a library to enable ESP32 read or write from the platform. To download the library, make an account first here. You can sign up using Facebook or Google Account too, so it won’t take long before you’re ready.
Then, go to Development tab and install the library necessary for ESP32. As usual, after downloading a library, open the zipped file, then extract it to Arduino installation libraries. An admin permission will be needed to proceed.
Circuit
For this project, DHT22 is connected to ESP32 by GPIO4. The circuit is based on the schema below.
Simple, right? Here’s the circuit I managed to build.
Set Up
Next, go to Workshop tab.
Create two new signals by clicking on the ‘+’ button that is available on the left part of the website. In my case, the two signals are for Temperature and Humidity. You can change the name of signal and add details on the Information part.
As you can see, the value remains 0.00. All good? Let’s continue.
Code
What we have to do now is to provide data from ESP32.
#include <CircusESP32Lib.h>
#include <DHT.h>
// ------------------------------------------------
// These are the CircusESP32Lib related declarations
// ------------------------------------------------
char ssid[] = "<SSID>"; // Place your wifi SSID here
char password[] = "<Password>"; // Place your wifi password here
char token[] = "<Token>"; // Place your token, find it in 'account' at Circus. It will identify you.
char server[] = "www.circusofthings.com";
char temperature_key[] = "<temp_token>"; // Place the Key of the signal you created at Circus Of Things for the Temperature
char humidity_key[] = "<hum_token>"; // Place the Key of the signal you created at Circus Of Things for the Humidity
CircusESP32Lib circusESP32(server,ssid,password); // The object representing an ESP32 to whom you can order to Write or Read
// ------------------------------------------------
// These are the Temperature Example related declarations
// ------------------------------------------------
#define DHTPIN 4 // digital of your ESP32 connected to DHT22
#define DHTTYPE DHT22 // exact model of temperature sensor DHT22 for the general library
DHT dht(DHTPIN, DHTTYPE); // The object representing your DHT22 sensor
void setup() {
Serial.begin(115200); // Remember to match this value with the baud rate in your console
dht.begin(); // Set the DHT22 ready
circusESP32.begin(); // Let the Circus object set up itself for an SSL/Secure connection
}
void loop() { // Now that all is set up, let's begin with the tasks
delay(10000);
// Let the library get the Temperature that DHT22 probe is measuring.
float t = dht.readTemperature();
if (isnan(t))
t=-1; // if so, check the connection of your DHT22 sensor... something is disconnected ;-)
float h = dht.readHumidity();
if (isnan(h))
h=-1; // if so, check the connection of your DHT22 sensor... something is disconnected ;-)
// Show values, just for debuging
Serial.println("");
Serial.print("Temperature: ");
Serial.println(t);
Serial.print("Humidity: ");
Serial.println(h);
// Report the values gathered by the sensor to the Circus
circusESP32.write(temperature_key,t,token); // Report the temperature measured to Circus.
circusESP32.write(humidity_key,h,token); // Report the humidity measured to Circus.
}
After you copy this code to your IDE, there are a few adjustments that you need to do.
First, Wi-Fi. You’re going to need SSID and password of the connection you’re currently using.
//Wi-Fi
char ssid[] = "<SSID>"; // Place your wifi SSID here
char password[] = "<Password>"; // Place your wifi password here
Second, account token. To see this, go to Account and on the App data part, you’re going to see your token. Copy that and fill the code below.
//Account Token
char token[] = "<Token>"; // Place your token, find it in 'account' at Circus. It will identify you.
Third, signal token. To see this, go to Workshop. The key will be available below the Title, copy and paste it according to the signal.
//Signal Token
char temperature_key[] = "<temp_token>"; // Place the Key of the signal you created at Circus Of Things for the Temperature
char humidity_key[] = "<hum_token>"; // Place the Key of the signal you created at Circus Of Things for the Humidity
Fourth, and last, the sensor and pin you’re using. I’m using DHT22 as a sensor, and to connect to ESP32 I choose GPIO4 for this project.
#define DHTPIN 4 // digital of your ESP32 connected to DHT22
#define DHTTYPE DHT22 // exact model of temperature sensor DHT22 for the general library
Demonstration
Below is the signal preview after the code is uploaded.
To visualize the data and see the graph, go to Dashboard tab. Add a new panel, and on the right click on View. Pick the data you’re going to use in the graph. I’m going to put both humidity and temperature data on one graph, therefore I add two views to the panel. Then, switch the Panel to Timeline mode. You’ll get something that looks like this.
Fun fact: I used a hand-fan as an experiment to change the humidity and temperature! As you can see, the temperature didn’t change much, but the humidity had a recognizable change.
Errors?
In this project, I encountered a few errors due to forgetting to change the DHT Type (original code has DHT11 as the type) which caused the calculations to be odd, and forgetting to change the pin, causing calculations unable to read from the sensor.
Okay, that’s all for this project. See you, adios!