Creating alerts to water your plant

Before creating all the exciting wireless gardening projects you'll find in this chapter, we are going to start with one important thing: testing whether the sensor is working properly!

For that, here is the sketch to check that the sensor is functioning correctly:

// Library
#include <SHT1x.h>

// Pins
#define dataPin  4
#define clockPin 5

// Create instance for the sensor
SHT1xsht1x(dataPin, clockPin);

void setup()
{
Serial.begin(115200); // Open serial connection to report values to host
Serial.println("Starting up");
}

void loop()
{
  // Variables
float temp_c;
float temp_f;
float humidity;

  // Read values from the sensor
temp_c = sht1x.readTemperatureC();
temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();

  // Print the values to the serial port
Serial.print("Temperature: ");
Serial.print(temp_c, DEC);
Serial.print("C / ");
Serial.print(temp_f, DEC);
Serial.print("F. Humidity: ");
Serial.print(humidity);
Serial.println("%");

  // Wait 2 seconds
delay(2000);
}

This sketch is pretty straightforward: we create an instance of the SHT library, and in the loop() function we just make measurements and print the results on the Serial monitor.

Just copy this sketch inside the Arduino IDE, put the ESP8266 in bootloader mode, and upload the code to the board. Then, open the Serial monitor. This is what you should see inside the Serial monitor:

Creating alerts to water your plant

If you have values that make sense (temperature around room temperature, for example), it means your sensor is working properly. If not, please check all the connections again, and make sure that you inserted the 10K Ohm resistor between the data and VCC pins of the sensor. For me, this is what made the sensor not work during the first trial.

Now, we are going to set up automated alerts on our phone whenever the plant is getting too dry. The first step to do that is to create an account at IFTTT, which we already used earlier in the book:

https://ifttt.com/

I will also assume that you already have a Maker and Pushover channels. If not, please refer to Chapter 6, Machine-to-Machine Communications, in which we already used IFTTT.

Next, create a new recipe with the Maker channel:

Creating alerts to water your plant

You need to insert alert as the event name:

Creating alerts to water your plant

For the target channel, we'll use Pushover to receive notifications on your mobile device:

Creating alerts to water your plant

Now, insert a message inside the notification message field:

Creating alerts to water your plant

Once the recipe is created, we need to actually configure the board so it automatically sends alerts whenever the humidity of the plant is getting too low. As the code is quite complex here, I will only highlight the most important points. It starts by including the correct libraries:

#include <ESP8266WiFi.h>
#include <SHT1x.h>

Then, we need to define your Wi-Fi name and password:

const char* ssid = "wifi-name";
const char* password = "wifi-pass";

We also define a humidity threshold, under which the project will automatically send an alert:

float threshold = 30.00;

Next, we define the IFTTT parameters. This is where you need to insert the key of your IFTTT Maker channel:

const char* host = "maker.ifttt.com";
const char* eventName   = "alert";
const char* key = "ifttt-key";

In the loop() function of the sketch, after taking the measurements from the sensor, we check whether the humidity is below the threshold:

if (humidity < threshold) {

If that's the case, we prepare the request we'll send to the IFTTT server:

String url = "/trigger/";
url += eventName;
url += "/with/key/";
url += key;

We then send this request:

client.print(String("GET ") + url + " HTTP/1.1
" +
                 "Host: " + host + "
" + 
                 "Connection: close

");
int timeout = millis() + 5000;
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
      }
    }

If the request is sent, we also wait a long time before the next alert, to not continuously receive alerts from the project:

delay(10 * 60 * 1000);

It's now time to test this first project of the chapter! Make sure to grab the complete code and modify the credentials, such as the IFTTT settings. Then, upload the code to the board. If the humidity is indeed below the threshold, you will soon receive a notification on your phone stating that the plant needs to be watered:

Creating alerts to water your plant
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.18.101