Build an IoT soil moisture project using Arduino with an IFTTT alert system

This tutorial describes how to build an IoT soil moisture sensor that monitors the soil moisture using Arduino. Moreover, this Arduino IoT monitoring system implements a notification system so that when the soil gets too dry it sends a notification.

As you know already, IoT is one of the most important topics nowadays, that promises to shape our future and the way we live. One interesting aspect of IoT is the fact we can experiment with an IoT system building it by ourselves. There are several prototyping boards available in the market and we can build amazing IoT projects without spending too much.

IoT soil moisture project overview

The idea that stands behind this project is building an Arduino IoT system that monitors the soil moisture using a soil sensor detecting when it gets too dry. The Arduino MKR1000 controls the sensor sending the data to the Carriots IoT platform (now Altair SmartCore). This platform, in turn, stores the data coming from the sensor and detects when the values stored are under a threshold level. We will see later how to analyze the data. By now, we can assume that the Carriots IoT platform is able somehow to invoke an IFTTT service that will send a short message to the user alerting him. Building this IoT system we can explore how to use several components of the IoT ecosystem. Moreover, this project displays the humidity soil status using a LEDs matrix. Let’s see how to build it.

Retrieving data from the soil moisture sensor connected to Arduino

In this first step, we have to read the sensor data. This IoT project uses YL-38 + YL-69 sensor. This is an analog sensor that can be inserted into the soil we want to monitor. The picture below shows how to connect the YL-38 and YL69 (moisture sensor) to Arduino:

iot system to monitor the soil moisture

The code is very simple, we read values from the A1 pin and then we calculate the humidity:

float moistureHum = analogRead(A1);
moistureHum = (1023 - moistureHum) * 100 /1023;

This is very simple, not much to explain about it.  The value stored in moistureHum is the one we will send to the IoT cloud platform. Moreover, to do it, Arduino MKR1000 has to connect to the internet so that it can send data. The code below describes how to connect Arduino to the WiFi:

#include "WiFi101.h"
WiFiClient client;
void setup() {
  if (WiFi.status() == WL_NO_SHIELD) {
   Serial.println("WiFi shield not present");
   while (true);
}Code language: PHP (php)

where the connectToWifi() is:

void connectToWifi() {
  while ( status != WL_CONNECTED) {
   Serial.print("Attempting to connect to WPA SSID: ");
   // Connect to WPA/WPA2 network:
   status = WiFi.begin(ssid, pass);
   // wait 10 seconds for connection:
}Code language: JavaScript (javascript)

In this code, you have to provide the ssid and the pass (WiFi password).

That’s all. We can manage in this sketch the LEDs matrix showing the humidity level as described in the code attached to this article. Now we can focus our attention on the IoT cloud platform.

Connecting Arduino to the IoT platform Carriots

Before sending data to the cloud, we have to configure our IoT platform in order to manage the data. Carriots uses a hierarchical structure to group and to manage devices. Therefore, we have to create this structure before using our device. At the top of this structure, there is the Project. Once you are logged in, you have to click on Hierarchy and then Project filling the data required:

building an IoT system

The next step is creating the service filling the data required:

IoT system DIY

Finally, you have to create the group:

how to use carriots

The steps are very fast and you do it only once. The last step is configuring our device. It represents the physical device we are using to send data. This device belongs to the group created in the last step, which in turn belongs to the service. The service belongs to the project. The configuration step is very easy as shown in the picture below:

connect arduino to the clud

That’s all. The configuration steps are complete. We have to connect the Arduino device to Carriots and start sending data. In this step, it is important the developer id shown in the picture above. It represents the unique device identifier we will use to bind the data coming from our Arduino device to the Carriots device. Another important parameter is your API Key. You can find it in Settings -> Api key menu. In order to send data,  add this function to the sketch shown above:

void sendData(float data) {
  if (client.connect(server,80)) {
    Serial.println("Connected to the server....");
    String jsonData = "{\"protocol\":\"v2\",\"device\":\""+DEVICE_ID+ "\",\"at\":\"now\",\"data\":{\"soil\":\""+
   // Make a HTTP request
   client.println("POST /streams HTTP/1.1");
   client.println("Accept: application/json");
   client.println("User-Agent: Arduino-Carriots");
   client.println("Content-Type: application/json");
   client.print("carriots.apikey: ");
   client.print("Content-Length: ");
   int thisLength = jsonData.length();
   client.println("Connection: close");
}Code language: JavaScript (javascript)

Notice that the function sends a JSON payload containing the data read from the sensor. This function must be invoked in the loop() method. Running the sketch, we can notice that the device sends data to the Carriots as shown below:

iot sensor data: soil moisture sensor connected to Arduino

Implementing an Arduino alert system using IFTTT

The next step is monitoring the data. Usually, in an IoT system, we are not only interested in acquiring data from the sensor but we want to monitor such information to take corrective actions when the values are out of a specific interval. There are several actions we can take, in this example we want to inform the user alerting him/her. Even if Carriots has a built-in email system, we prefer to integrate Carriots with another useful and interesting platform called IFTTT. This platform provides several integration services we can use and integrate into our IoT projects.

In order to alert the user we need two components:

  1. a monitoring data system
  2. alerting system

As a monitoring data system, this IoT system uses Carriots listener. A listener is a process that analyzes the incoming values and applies a specific rule. When the rule is verified then it invokes a script. The interesting aspect of Carriots is that we can use Groovy as a scripting language to invoke external services.

The alerting system is built on IFTTT. Before completing the work on Carriots, it is useful to configure the IFTTT. As stated before, we want to send a short message when the humidity hits a threshold level. In order to achieve it, we configure a short message service in IFTTT. It is required you have a free account in order to complete this task.

Configuring IFTTT system

As a first step,  it is necessary to create a new Applet:

ifft configure IoT applet

Now click on plus sign to add the service and search for Maker service:

ifft maker service

Select Maker webhooks to enable IoT maker. Now we have to configure the maker service adding the event name that triggers the sending message process:

configure IFFT maker service

Finally,  enable the sending message service configuring all the required parameters like the destination number and the message body:

ifft sending message

That’s all. Now we can focus our attention on the listener in Carriots platform. Let us create a new listener that invoke the URL related to the applet we have just created. When the listener invokes the URL, IFFT sends a short message. The picture below shows how to configure the listener:

how to configure carriots listener

The last step is configuring the expression. We can write it using Groovy as described in this example.

That’s all, now you can test the project verifying that when the soil moisture is lower than the threshold level, we will get a short message on our mobile.

if you are interested you can learn How to build an Arduino data logger.

At the end of this tutorial, you have learned how to use the soil moisture sensor with Arduino. Using the IoT moisture sensor, it is possible to monitor the soil humidity and check if it is too dry. In this case, using Arduino integrated with IFTTT, we send an alert to the user.

    1. Fazrol Nizam February 27, 2018

    Add Your Comment