Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
5. Home Environment Monitoring¶
In this chapter, we will use Blynk to create a home environment monitor. You can measure the temperature, humidity, and light intensity of a room using the DHT11 and photoresistor. By sending these values to Blynk, you will be able to know the environment of your home via the internet.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
3 in 1 Starter Kit |
380+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
|---|---|
- |
1. Build the Circuit
Note
The ESP8266 module requires a high current to provide a stable operating environment, so make sure the 9V battery is plugged in.
2. Edit Dashboard
For recording humidity values, create a Datastream of type Virtual Pin on the Datastream page. Set the DATA TYPE to Double and MIN and MAX to 0 and 100. Also set the units to Percentage, %.
Then create a Datastream of type Virtual Pin for recording the temperature. Set DATA TYPE to
Double, MIN and MAX to-30and50, and units to Celsius, °C.
Also create a Datastream of type Virtual Pin to record the light intensity. Use the default data type - Integer, with MIN and MAX set to
0and1024.
Go to the Wed Dashboard page, drag two Label widgets and set their data streams to V4 and V5 respectively, and drag a Gauge widget and set the data stream to V6. Also in the widget setting, you can enable Change color based on value and select the appropriate color to make the widget look better and more intuitive.
3. Run the Code
Open the
5.home_environment_monitoring.inofile under the path of3in1-kit\iot_project\5.home_environment_monitoring, or copy this code into Arduino IDE.Note
The
DHT sensor libraryis used here, you can install it from the Library Manager.
Replace the
Template ID,Device Name, andAuth Tokenwith your own. You also need to enter thessidandpasswordof the WiFi you are using. For detailed tutorials, please refer to 1.3 Connecting the R3 board to Blynk.After selecting the correct board and port, click the Upoad button.
Open the Serial monitor(set baudrate to 115200) and wait for a prompt such as a successful connection to appear.
Note
If the message
ESP is not respondingappears when you connect, please follow these steps.Make sure the 9V battery is plugged in.
Reset the ESP8266 module by connecting the pin RST to GND for 1 second, then unplug it.
Press the reset button on the R3 board.
Sometimes, you may need to repeat the above operation 3-5 times, please be patient.
Now, you will see the current ambient temperature, humidity and light intensity on Blynk.
If you want to use Blynk on mobile devices, please refer to How to use Blynk on mobile device?.
How it works?
These two functions are used to get the temperature, humidity and light intensity of the room.
int readLight(){
return analogRead(lightPin);
}
bool readDHT() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return false;
}
return true;
}
With the Blynk Timer, the ambient temperature, humidity, and light intensity are obtained every second and sent to the data stream on the Blynk Cloud, from which the widgets display the data.
void myTimerEvent()
{
bool chk = readDHT();
int light = readLight();
if(chk){
Blynk.virtualWrite(V4,humidity);
Blynk.virtualWrite(V5,temperature);
}
Blynk.virtualWrite(V6,light);
}