Image of eBits Academy airquality illustration

Save Your Plants with ESP32 & IFTTT!

  • September 07, 2021
  • |
  • Jesper Nielsen

 

Can you even manage to kill a cactus? Take a look! This small project can help prevent your plant from withering because you forget to water it. It uses two sensors: one measuring both temperature and humidity and another measuring soil moisture. Connecting an ESP32 to the sensors and WiFi lets the ESP32 send notifications to your phone using IFTTT, for example when soil moisture reaches a lower threshold.

There is a quick guide for experienced Arduino and sensor users who want to get started immediately. For a fuller guide and some enjoyable reading, continue after the contents.

Download the code from GitHub. It was written in Platform IO. To compile it in Arduino IDE, copy the contents of main.cpp, located in src.

GitHub:  GitHub - Mowglli/1.-ESP32-IFTTT

CONTENTS

 

To build this project, you need the components below. They are all available from eBits.

Quantity

Component

Link

1

ESP-WROOM-32

ESP-WROOM-32 development board – ebits.dk

1

Soil moisture sensor with comparator

Soil moisture sensor with comparator – ebits.dk

1

Temperature and humidity sensor - DHT11

Temperature and humidity sensor - DHT11 – ebits.dk

1

Breadboard 165mm x 55mm

Breadboard 165mm x 55mm – ebits.dk

1

120 Dupont cables, 10cm

120 Dupont cables, 10cm or 20cm long, 3 types – ebits.dk

Or
1 Save the Plants Kit Save the Plants Kit

TESTING AND VALIDATING THE SENSORS

Below, we explain sensor setup and validation of readings in Serial Monitor.

DHT11

To read values from the DHT11 sensor, include a library in the project. You can then store event.temperatur and event.humidity in variables.

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

//DHT11 sensor
#define DHTPIN 23                 // Digital pin forbundet til  DHT11 sensor 
#define DHTTYPE    DHT11          // I biblioteket DHT skal vi definere type
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
SOIL MOISTURE SENSOR

The soil moisture sensor connects to a suitable ADC input. The sensor output must remain within the ESP32 input’s allowed voltage; never connect a 5V output directly. On classic ESP32, use ADC1 with WiFi. In this 12-bit example, analogRead returns a raw value from 0–4095. The formula below only creates an inverted 0–100 scale, not calibrated soil moisture percentage. Calibrate dry/wet references in the actual soil and adjust the threshold:

//Soil sensor
#define sensorPin 36
int soil_sensor, output_value;
float output_value_pct;

$${Output\_value\_pct=100-((\frac{soil\_sensor}{4095.00})*100)}$$

COMBINED SENSOR FUNCTION()
void sensors(){
  float temp;
  int hum;

   sensors_event_t event;
  //print adc udlæsning:
   Serial.print(F("ADC-aflæsning(0 - 4095)"));
   Serial.println(analogRead(sensorPin));

    //Udregn til procent 0% er tør, 100% er vådt
    soil_sensor = analogRead(sensorPin);
    //output_value  =  (soil_sensor / 4095.00);
    output_value_pct =  (100 - ( (soil_sensor/4095.00) * 100 ) );
    Serial.print(F("Jorfugtighed: "));
    Serial.print(output_value_pct);
    Serial.println(F("%"));

  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Fejl ved aflæsning af temperatur!"));
  }
  else {
    Serial.print(F("Temperatur: "));
    Serial.print(event.temperature);
    Serial.println(F("°C"));
  }
  temp = event.temperature;
 
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Fejl ved aflæsning af Fugtighed!"));
  }
  else {
    Serial.print(F("Fugtighed: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
  }
    hum = event.relative_humidity;
  Serial.println("");

    if(output_value_pct <= 30 ) // hvis under 30 pct, sendes der besked til telefon
    {
      IFFT_notifikation(output_value_pct, hum, temp);
    }
    else
    {}
}
PRINTING TO SERIAL MONITOR

WIFI SETUP ON ESP32

Setting up WiFi on ESP32 is straightforward, and many YouTube guides are available. You use a library and call a few functions. The solution below times how long the connection attempt has been running; if it has not succeeded within 20 seconds, the ESP32 stops. You can add a reboot command here.

STEP 1 - Libraries and Definitions:

Replace MITSSID and PASSWORD with your network name and password.

#include <WiFi.h>
//Wifi info
#define WIFI_NETWORK "SSID"
#define WIFI_PASSWORD "PASSWORD"
#define WIFI_TIMEOUT_MS 20000 //20 ms

STEP 2 - Create the Connection Function

Insert this function outside void loop and void setup. Calling it connects to the network specified in step 1. Once connected, it also prints the IP address of the ESP32 if you need it for other projects.

void connectToWiFi(){
  Serial.print("Connecting to Wifi..");
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);

  unsigned long startAttempTime = millis();

  while(WiFi.status() !=WL_CONNECTED && millis()- startAttempTime < WIFI_TIMEOUT_MS){
    Serial.print(".");
    delay(100);
  }
  if(WiFi.status() !=WL_CONNECTED){
    Serial.println("Failed!");
    //En genstart kan sættes her
  }
  else{
    Serial.println(" ");
    Serial.print("Connected to Wifi with IP: ");
    Serial.println(WiFi.localIP());
  }
}

STEP 3 - Call the Function

Call the function above in void setup(). Also call serial.begin(baudrate) so you can see whether the connection succeeded.

void setup() {
  //Initialiser serial monitor
  Serial.begin(9600);
  //Initialiser Wifi
  connectToWiFi();
}

 

IFTTT SETUP

STEP 1 - Set Up IFTTT (If This)

Go to My Applets - IFTTT, and create an account if you do not already have one. Then click Create in the right-hand corner. You will see If This and Then That. Click add beside If This.

Search for webhooks and select it. 

Then select Receive a web request, and  enter an event_name such as soil_sensor. Finish with Create Trigger.

STEP 2 - Set Up IFTTT (Then That)

Now select Then That and click add. Now search for notifications, and the option shown below on the right appears

Now select Send a notification from the IFTTT app.

 

STEP 3 - Create Text with the Desired Values

You can now write the text sent to your phone. It can include the trigger name, three different values you send, and the time the notification was sent. 

Below is the setup I use

 

STEP 4 - Key and API

To make an HTTP request, we need a unique key and a link to send the trigger and desired values to. Find them in the Webhooks documentation. The image below shows the unique key and API. You can also test it with your phone by clicking Test 

STEP 4 - Libraries and Definitions:

At String key = "nøgle", enter the key obtained in step 3. Choose your own event name.

#include <HTTPClient.h>
//IFFT setup
String key = "key"; //nøgle
String event_name= "soil_moisture_email";

STEP 5 - Create the Webhooks Communication Function:

void IFFT_notifikation(float value1,int value2,float value3){
HTTPClient http;
http.begin("https://maker.ifttt.com/trigger/"+event_name+"/with/key/"+key+"?value1="+value1+"&value2="+value2+"&value3="+value3+"");
http.GET();
http.end();
Serial.print("Notifikation sendt!");
}

STEP 6 - Call the Function

Here, I check the soil moisture value. If it is <= 30, IFFT_notifikation() is called and sends the required values to IFTTT.

    if(output_value_pct <= 30) //hvis under eller lig med 30 sendes notifikation
    {
      IFFT_notifikation(output_value_pct, hum, temp);
    }
    else
    {}

 Now compile the program and test it 

TEST

The test below was performed by lifting the soil moisture sensor out of the soil to bring the reading below the 30% trigger threshold. The notification arrives on the phone shortly afterwards.

 

QUICK GUIDE

Components Needed for the Project:

Quantity

Component

Link

1

ESP-WROOM-32

ESP-WROOM-32 development board – ebits.dk

1

Soil moisture sensor with comparator

Soil moisture sensor with comparator – ebits.dk

1

Temperature and humidity sensor - DHT11

Temperature and humidity sensor - DHT11 – ebits.dk

1

Breadboard 165mm x 55mm

Breadboard 165mm x 55mm – ebits.dk

1

120 Dupont cables, 10cm

120 Dupont cables, 10cm or 20cm long, 3 types – ebits.dk

Code

Download the code from GitHub. It was written in Platform IO. To compile in Arduino IDE, copy the contents of main.cpp, found in  src.

GitHub:  GitHub - Mowglli/1.-ESP32-IFTTT

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

//DHT11 sensor
#define DHTPIN 23                 // Digital pin forbundet til  DHT11 sensor 
#define DHTTYPE    DHT11          // I biblioteket DHT skal vi definere type
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;

//Soil sensor
#define sensorPin 36
int soil_sensor, output_value;
float output_value_pct;

Sensor Function

void sensors(){
  float temp;
  int hum;

   sensors_event_t event;
  //print adc udlæsning:
   Serial.print(F("ADC-aflæsning(0 - 4095)"));
   Serial.println(analogRead(sensorPin));

    //Udregn til procent 0% er tør, 100% er vådt
    soil_sensor = analogRead(sensorPin);
    //output_value  =  (soil_sensor / 4095.00);
    output_value_pct =  (100 - ( (soil_sensor/4095.00) * 100 ) );
    Serial.print(F("Jorfugtighed: "));
    Serial.print(output_value_pct);
    Serial.println(F("%"));

  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Fejl ved aflæsning af temperatur!"));
  }
  else {
    Serial.print(F("Temperatur: "));
    Serial.print(event.temperature);
    Serial.println(F("°C"));
  }
  temp = event.temperature;
 
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Fejl ved aflæsning af Fugtighed!"));
  }
  else {
    Serial.print(F("Fugtighed: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
  }
    hum = event.relative_humidity;
  Serial.println("");

    if(output_value_pct <= 30 ) // hvis under 30 pct, sendes der besked til telefon
    {
      IFFT_notifikation(output_value_pct, hum, temp);
    }
    else
    {}

}

 WiFi Function

The function has a timer that stops connection attempts after 20 seconds. You can add an action to that if statement, such as rebooting.

#include <WiFi.h>
//Wifi info
#define WIFI_NETWORK "SSID"
#define WIFI_PASSWORD "PASSWORD"
#define WIFI_TIMEOUT_MS 20000 //20 ms
void connectToWiFi(){
  Serial.print("Connecting to Wifi..");
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);

  unsigned long startAttempTime = millis();

  while(WiFi.status() !=WL_CONNECTED && millis()- startAttempTime < WIFI_TIMEOUT_MS){
    Serial.print(".");
    delay(100);
  }
  if(WiFi.status() !=WL_CONNECTED){
    Serial.println("Failed!");
    //En genstart kan sættes her
  }
  else{
    Serial.println(" ");
    Serial.print("Connected to Wifi with IP: ");
    Serial.println(WiFi.localIP());
  }
}

IFTTT Setup

On IFTTT.com, use Webhooks and notifications. Remember the trigger name and how many values you send. You need the unique key and link from the documentation. 

#include <HTTPClient.h>
//IFFT setup
String key = "key"; //nøgle
String event_name= "soil_moisture_email";
void IFFT_notifikation(float value1,int value2,float value3){
HTTPClient http;
http.begin("https://maker.ifttt.com/trigger/"+event_name+"/with/key/"+key+"?value1="+value1+"&value2="+value2+"&value3="+value3+"");
http.GET();
http.end();
Serial.print("Notifikation sendt!");
}

TEST

The test below was performed by lifting the soil moisture sensor out of the soil to bring the reading below the 30% trigger threshold. The notification arrives on the phone shortly afterwards.

 

Leave a comment

Please note, comments need to be approved before they are published.