How to Monitor Soil Moisture Using Blink IoT

In this tutorial, I will show you how you can monitor the moisture in your house plant’s soil. This can be extremely useful for anyone who enjoys home gardening and wants to start deploying and remotely monitoring connected electronic devices at any scale.

Parts Required

Note: Since only one analogue pin is required to follow this tutorial, an Arduino Nano, ESP32 NodeMCU board or any similar board can be used instead. Just be sure to change the pins in the code if necessary.

The Soil Moisture Sensor

As indicated by my colleagues in a previous post, the fork-shaped probe with two exposed conductors acts as a variable resistor whose resistance varies according to the water content in the soil. This resistance is inversely proportional to the soil moisture, and thus more water in the soil means better conductivity and will result in a lower resistance value [1].

Figure 1 – Soil Hygrometer Humidity Detection Moisture Sensor

Circuit

Regarding the circuit needed for this tutorial, you just need to follow the instructions given in our previous post. You should have something similar to what is shown in Figure 2.

Figure 2 – Circuit

Blink IoT

The Blink IoT Platform is a comprehensive software that enables the prototyping, deployment, and remote management of connected electronic devices at any scale [2]. However, the platform suffered a massive update, which made the instructions given in our previous post obsolete. Thus, this post aims to cover all the steps necessary for you to start upgrading your projects with Blink.

Let’s start by opening Blink on our computer. Select “Start Free”.

Sign up to Blink and login to your account.

Once logged in, start by adding a template by selecting “New Template”.

Define a name and select “ESP8266” from the Hardware dropdown. If you want, you can also add a description for your template. Click “Done” to go to the next step.

Once created, your template still needs some configurations. To do so, start by clicking “Configure template”.

On this page, you can add a template image. In my case, for instance, I added a plant image by selecting “Add Image” and then choosing the intended image from my computer.

Then, select “Set Up Datastream”.

After that, select “New Datastream” and then “Virtual Pin”.

In the Virtual Pin Datastream window, you will note that some fields are prefilled. Just confirm that the defined pin is “V0”, and the data type is “Integer” and then specify the MAX value to 950, which according to the sensor’s datasheet, is the maximum output value (sensor in water). Click “Create” to move on.

Next, select “Web Dashboard”.

In the Web Dashboard window, look for “Gauge” in the Widget Box and drag it to the canvas.

Place your cursor on the inserted gauge and click on the gear to access the Gauge Settings.

Define a title for your gauge and select “Virtual Pin Vo” from the datastream dropdown. You can also change the gauge’s bar colour based on its value just like I did. Once you are done, click “Save”.

After that, click “Save” in the top right corner.

Next, select “Search” from the left sidebar and then select “New Device”.

In the New Device window, click on “From template”.

Make sure to select the created template and then define a name for your device. In my case, I chose to name it “ESP8266”.

The next step is to copy the code showing in the top right corner to our Arduino IDE code. This code contains the Template ID, Device Name and Authorization Token for the proper communication between your hardware and your Blink template. This should be declared at the very top of your code!

Finally, once you run and upload the code, you should expect to see the values from the moisture sensor appearing in your template’s gauge.

At this point, we can install Blink’s mobile app. To do so, download the app and log in to your account. After that, click on “Developer Mode”.

As you can see, the template you created shall be visible. Click on it to configure the dashboard.

Just as I did on my computer, I want to insert a gauge widget. To do so, click on the grid or on the “+” sign in the top right corner of the screen.

Search for “Gauge” and select it.

The gauge will appear in the grid. You can change its size by clicking on it for a second.

After defining its size, click on the gauge to access the Gauge Settings. Then, select “Datastream”.

In the Select Data Stream window, make sure to select the same Virtual pin as you did on the computer (in this case, it is V0).

Next, go back to the Gauge Settings and click on “Design”.

In this window, you can define your gauge’s design as you want.

Once you are done, you should expect to see the values from the moisture sensor appearing in the gauge.

Code

In the Arduino IDE, after uploading the code, open the Serial Monitor and check if the following appears. It confirms that the communication between your hardware and Blink is established.

Figure 3 – Serial Monitor’s output in the Arduino IDE

To know more about Blink and how to adequately develop your code, check out the Blink documentation page. It can be extremely helpful.

#define BLYNK_TEMPLATE_ID      "MyTemplateID"
#define BLYNK_TEMPLATE_NAME    "MyTemplateName"
#define BLYNK_AUTH_TOKEN       "MyAuthToken"

#define BLYNK_PRINT Serial       // used by Blynk to access Serial Monitor for troubleshooting

#define DrySoil 300   
#define WetSoil 500
// Sensor pins
#define sensorPower 12
#define sensorPin A0

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

// Declaring a global variabl for sensor data
int sensorVal;

BlynkTimer timer; // Creating a timer object

void setup() {

  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  timer.setInterval(1000L, myTimerEvent); //Staring a timer
  
  pinMode(sensorPower, OUTPUT);

  // Initially keep the sensor OFF
  digitalWrite(sensorPower, LOW);

}

void myTimerEvent() // This loop defines what happens when timer is triggered
{

  // This function describes what will happen with each timer tick
  // e.g. writing sensor value to datastream V5
  Blynk.virtualWrite(V0, sensorVal);

}

void loop() {

  // Reading sensor from hardware analog pin A0
  sensorVal = analogRead(A0); // this is an example of reading sensor data

  // Runs Blynk
  Blynk.run(); 
  
  // runs BlynkTimer
  timer.run();
  
  //get the reading from the function below and print it
  int moisture = readSensor();
  Serial.print("Analog Output: ");
  Serial.println(moisture);

  // Determine status of our soil
  if (moisture < DrySoil) {
    Serial.println("Dry Soil. Time for watering!");
  } else if (moisture >= DrySoil && moisture < WetSoil) {
    Serial.println("Perfect. Well done!");
  } else {
    Serial.println("Wet Soil. Be careful!");
  }
  
  delay(1000);  // Take a reading every second for testing
          
  Serial.println();
}

//  This function returns the analog soil moisture measurement
int readSensor() {
  digitalWrite(sensorPower, HIGH);  // Turn the sensor ON
  delay(10);              // Allow power to settle
  int val = analogRead(sensorPin);  // Read the analog value form sensor
  digitalWrite(sensorPower, LOW);   // Turn the sensor OFF
  return val;             // Return analog moisture value
}

That’s it! If you enjoyed this tutorial, you can visit our YouTube channel and watch our many tutorials. Thanks for following us and be sure to rate, comment and share our content.

References

[1] https://www.geekering.com/categories/embedded-sytems/arduino/rubenmarques/how-to-detect-soil-moisture/

[2] https://docs.blynk.io/en/

Towards the Future !!! 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock