Plant Monitor Using ESP32 Thing and Blynk

by amandaott10 in Circuits > Sensors

10643 Views, 11 Favorites, 0 Comments

Plant Monitor Using ESP32 Thing and Blynk

IMG_3307.JPG
IMG_3304.JPG

Overview

The goal of this project is to create a compact device able to monitor the conditions of a houseplant. The device enables the user to check the soil moisture level, humidity level, temperature, and the "feels-like" temperature from a smartphone using the Blynk App. Additionally, the user will receive an email alert when conditions become unsuitable for the plant. For example, the user will receive a reminder to water the plant when soil moisture levels drop below a suitable level.

Requirements

IMG_3273.JPG
IMG_3275.JPG
IMG_3277.JPG

This project uses a Sparkfun ESP32 thing, a DHT22 sensor, and an Electronic Brick Soil Moisture Sensor. Additionally, a wifi network and the Blynk app are required. Preferably, a waterproof enclosure should be created to contain the ESP32 thing. While this example uses a standard outlet for a power source, the addition of a rechargeable battery, solar panel, and charge controller would enable the device to be powered through renewable energy.

Blynk

IMG_3292.PNG
IMG_3294.PNG
IMG_3293.PNG

To being, download the Blynk app and create a new project. Take note of the authentication token--it will be used in the code. Create new display widgets in the Blynk app and select the corresponding virtual pins defined in the code. Set the refresh interval to push. Each widget should be assigned its own virtual pin.

Arduino IDE

Screenshot (6).png

Download Arduino IDE. Follow the download instructions for the ESP32 thing driver and the demo to ensure wifi connectivity. Download the Blynk and DHT libraries included in the code. Fill in the authentication token, wifi password, wifi username, and email in the final code. Use the demo code for the soil moisture sensor to find the minimum and maximum values for the soil type. Record and replace these values in the final code. Replace the minimum values for the temperature, soil moisture, and humidity for the plant in the final code. Upload the code.

Build It

Plant sensor_bb.png
IMG_3300.JPG
IMG_3302.JPG
IMG_3303.JPG
IMG_3305.JPG
IMG_3306.JPG

First, connect the soil moisture sensor to 3.3V, ground, and input pin 34. Note, it is integral the switch is set to A because the analog setting for this sensor will be used. Next, connect the DHT sensor to 3.3V, ground, and input pin 27. The DHT22 sensor requires a 10K Ohm resistor between the VCC and the data out pin. Be sure to check the DHT diagram to ensure it is wired properly. Configure the ESP32 inside a waterproof enclosure with the moisture sensor in the soil and the DHT sensor above the surface. Connect to a power source and enjoy data on your plant's environment.

Code

//Included libraries 
#define BLYNK_PRINT Serial
#include 
#include 
#include 
#include "DHT.h"
//DHT sensor information 
#define DHTTYPE DHT22  // DHT 22  (AM2302), AM2321
#define DHTPIN 27 // Digital pin connected to the DHT sensor
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.
//define input pins and outputs
int soil_sensor = 34; //define analog input pin number connected to moisture sensor
int output_value ;//define as output
int moisturelevel;//define as output
int notified = 0; //define notifed as 0
int timedelay= 60000L; //set timer to run get data once every minute or 60,000 miliseconds
//set minimum values for plant
int min_moisture =20;
int min_temperature =75;
int min_humidity =60;
// You should get Auth Token in the Blynk App.
char auth[] = "Auth_Token_Here";
// Your WiFi credentials.
char ssid[] = "Wifi_Network_Here";
char pass[] = "Wifi_Password_Here";
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void Sensors () //main function to read sensors and push to blynk 
{
 output_value = analogRead(soil_sensor);// Read analog signal from soil_sensor and define as output_value
 //Map output_vlaue from min,max values to 100,0 and constrain between 0,100
 //Use sample code and serial monitor to find min and max values for individual sensor and soil type for better calibration
 moisturelevel = constrain ( map(output_value, 1000, 4095, 100, 0), 0, 100);
 float h = dht.readHumidity(); // Read humidity 
 float t = dht.readTemperature(); // Read temperature as Celsius (the default)
 float f = dht.readTemperature(true); // Read temperature as Fahrenheit (isFahrenheit = true) 
 // Compute heat index in Fahrenheit (the default)
 float hif = dht.computeHeatIndex(f, h);
 // Check if any reads failed and exit early (to try again).
 if (isnan(h) || isnan(t) || isnan(f)) {
 Serial.println(F("Failed to read from DHT sensor!"));
  return;
 }
 //This connects vales to virtual pins defined in the widgets in the Blynk app
  Blynk.virtualWrite(V5, moisturelevel );// Send moisture level to virtual pin 5
  Blynk.virtualWrite(V6,f);// Send temperature to virtual pin 6
  Blynk.virtualWrite(V7,h);// Send humidity to virtual pin 7 
  Blynk.virtualWrite(V8,hif);// Send heat index to virtual pin 8
 if (notified==0)
{
 if (moisturelevel <= min_moisture) // If moisturelevel is equal to or below min value
 {
  Blynk.email("Email_Here", "Plant Monitor", "Water Plant!"); // Send email to water plant 
 }
 delay (15000); // Blynk emails must be 15 seconds apart. Delay 15000 millisecons 
 if (f <= min_temperature) // If temperature is equal to or below min value 
 {
  Blynk.email("Email_Here", "Plant Monitor", "Temperature Low!"); // Send email that temperature is low
 }
 delay (15000); // Blynk emails must be 15 seconds apart. Delay 15000 millisecons 
 if (h <= min_humidity) // If humidity is equal to or below min value 
 {
  Blynk.email("Emial_Here", "Plant Monitor", "Humidity Low!"); // Send email that humidity is low
 }
notified = 1;
timer.setTimeout(timedelay *5, resetNotified); // multipy timedelay by number of minutes wanted between repeat warning emails 
}
}   
void resetNotified() //function called to reset email frequency  
{
  notified = 0;
}
void setup()
{
  Serial.begin(9600); // Debug console
  Blynk.begin(auth, ssid, pass); // connect to blynk 
  timer.setInterval(timedelay, Sensors); // Setup a function to be called every minute or what timedelay is set to
  dht.begin(); //run DHT sensor
}
//Void loop should only contain blynk.run and timer
void loop()
{
  Blynk.run(); // Run blynk 
  timer.run(); // Initiates BlynkTimer
}