IoT Part 4: Automating the system and using the Blynk API

This topic will contain a table of contents

Introduction

Today you will complete the plant monitoring system by fully automating it. This will enable you to have the system work by itself, and check your plant from your phone
For this you will need everything you have learned so far.

Explanation of using functions

Before you start expanding your script from last week you can exercise with writing and using functions a little bit more.

Firs setup a new script:

  • Create new script file
  • add the following code:
#include <Arduino.h>

void setup() {
}

void loop() {
exampleFunction();
}
  • move all other files to the test folder
    add this to the setup fuction:
Serial.begin(9600);

Functions

An important part of programming is using functions. A function is a piece of code that can be run by calling it. This is very usefull when you need some parts of your function be ran multiple times.
The setup and loop part of your script are functions, but you can make them yourself too!

  • Before your setup script add the following code:
void exampleFunction(char* argument){
  Serial.println("This is printed by the example function");
  delay(1000);
  Serial.println("The argument this function received was: ");
  delay(1000);
  Serial.println(argument);
}

Please note that function arguments need to have their data type specified. char* means text, but you can also use int for integers or float for decimal numbers.

You can no call this function with the following code:

exampleFunction("TestArgument");
  • Put this line in your loop function.
  • Upload your script

If you have done everything correctly you will see that the code in the example function will be executed and it will print the argument you gave the function as well.

Giving water automatically

The first part you will add is automating the water
When the sensor detects that the soil is to dry the pump should start running. However the it takes some time for the water to reach the sensor, and it could happen that there is already way to much water before the pump stops. Therefore we will implement the following rules:

Check every hour if soil is wet enough
if the soil is too dry give water for 5 seconds

Make sure all your sensors are still connected:

  • LDR
  • DHT sensor
  • Relay
  • RF Transmitter

Now the pump needs to be added. Because you are still in prototyping phase you can connect the wires as shown in the picture. Make sure the red wires don’t touch anything else or you might melt some things!

Now you can start working on the code. The way to do this should be familiar by now. You will get some general instructions to make this work. If there are any questions about the specific code don’t hesitate to ask them!.

  • Add a new timer that runs every 5 seconds, make it call a function that is called checkWater
  • Add the function checkWater, and read your SoilMoisture value in it
  • Add an if statement to the checkWater function, here is an example:
if (500 < 1000)
{
Serial.println("500 is smaller than 1000!");
delay(5000);
}
  • Inside the if statement make sure to turn on your pump
  • After the if statement make sure to turn off your pump

Replace the 500 with the variable of your SoilMoisture sensor.

Test the system, the pump should run 5 seconds every 30 seconds until you touch the sensor with your fingers.

Controlling your light

The next part is monitoring and controlling the light level
You want the light to turn on when its dark, so your plant can grow for 24 hours a day.
You could use the LDR sensor to detect if it gets dark, but then it could get confused by the light that is shining on your plant. Instead, you can turn off the lamp above your plant and measure the ambient light level to get a valid reading of the ambient light.

The way you can fix this is:

  • Add a timer to check your light with the function checkLight. Do this every 15 seconds for testing mode, later on you can put the timer on a longer period for example an hour.
  • In the checkLight function do the following things
    • Turn off the KlikAanKlikUit with the RF transmitter
    • Measure the LDR
    • Add an if statement with values appropriate for your LDR readings

This way the light will always stay on when it gets dark. To get around this you could try to get the time on your device using NTP. That is out of the scope for now, but this way you could look into it yourself if you like.

Link: ESP32 NTP Example

Optional exercises

Even when a system is fully automated you still want control over everything
For example when you want to watch a movie and you want the light not to turn on.

If you are done with all the previous exercises you can either start working on a web-based dashboard using the API or try to implement additional features. Here are some ideas you can implement.

Overrides

You can add some additional buttons to your Blynk dashboard

  • Control the light from your phone, and make sure the automated light settings won’t be applied anymore.
  • Give extra water manually. For each button press give 5 seconds of water, then turn the pump off again.
  • Add a slider to precisely control the amount of water your plant will get

Notifications

An important part of any automated system is an emergency backup. If something unexpected happens it is nice to get notified, so you can save your plant. You can get the Blynk app to send you a notification on your phone. For example:

  • Check if the moisture level changes after giving water, if not the water reservoir may be empty.
  • Check if the light level changes after turning the light on, if not the light may be broken
  • Check if the ESP is still running, if it crashes you can fix it
  • Check if the temperature is to high, so you can move your plant out of the sun

The link with a working example can be found here:
Blynk Widget Example

Using the Blynk API

Blynk provides an API that can be used to retrieve information about your sensors from other devices without having to do a lot of programming. You could for example put values from your system onto your own website.

API Basics

The basic usage is explained on the documentation website from Blynk:
https://blynkapi.docs.apiary.io/

The most simple examples are getting a pin value end setting a pin value.

Paste the following link in your browser. Replace the blynk Auth token with your own token:
http://blynk-cloud.com/auth_token/get/V1
This will show the value of pin V1 in your webbrowser.

Webpage example

In order to display this information on a webpage you could write some javascript code on your webpage. Create a HTML file with the following content:

<html>
<meta http-equiv="refresh" content="60" />

<head>
<script>

var auth_token = 'YOURAUTHTOKEN';

function get_pin(pin, element) {

fetch('http://blynk-cloud.com/' + auth_token + '/get/' + pin)
.then(response => response.json())
.then(data => {
  window[pin] = data;
  document.getElementById(element).innerHTML = data;
})
.catch(error => console.error(error))

}

get_pin('V1', "Humidity");

</script>

</head>

<body>

<div id="Humidity" style="color:red">V1</div>

</body>


This webpage will:

  • Refresh every 60 seconds
  • Use the Auth token you provide in the function get_pin
  • Get pin value from pin V1 and put it in the division with id “Humidity”
  • Display this value on your page

This only works properly if running locally. When hosting on an external website most browsers will block the Blynk API request because it is not running on a secure connection!!!

It is very easy to expand this example, just add more pins and elements on your web page. Here is a small example how to do that.

In the <script> section under get_pin(‘V1’):

get_pin('V2', 'Temperature');

In the <body>section under the first <div>:

<div id='Temperature' style="color:red">V2</div>
1 Like