SMS connected greenhouse

Introduction

This tutorial guides you through:

  • Building the project, with details of the hardware requirements and how to put them together to create the SMS Connected Greenhouse.
  • Creating the software to acquire water sensor data and send a text message.
  • Running the sketch and receiving SMS alerts directly from the LinkIt ONE development board.

At the end of the tutorial there are details on where to go for additional information on the LinkIt ONE development board and how to create software for it.

Before you start

If you haven’t built a LinkIt project before, details on downloading and installing the Arduino IDE and LinkIt ONE SDK then configuring the IDE and upgrading the board firmware are provided in the LinkIt ONE quick start guide. Complete this before you continue if you haven’t already set up your development environment.

Building the Greenhouse's SMS hardware

This section describes the hardware and electronics needed to build the greenhouse along with details on how to put them together.

What you need

To build the greenhouse’s hardware, in addition to a LinkIt ONE development board, you need the following components:

ComponentDescriptionSource
SensorGrove-Water SensorSeed Studio
ShieldGrove Base Shield v2Seeed Studio
AntennaGSM/GPRS antenna covering 850/900/1800/1900 MHz frequency range and classified as Class 12.LinkIt ONE Development Kit

Putting the components together

This section provides step-by-step instructions on putting the greenhouse's SMS hardware together.

Step 1 — Grove base shield

Attach the Grove-Base shield to the LinkIt ONE development board, as shown below.

Attaching the Grove base shield

Step 2 — GSM antenna

Attach the GSM antenna to its corresponding pin on the LinkIt ONE development board, as shown below.

Connecting the antenna

Step 3 — SIM card

Insert a SIM card supporting GSM/GPRS communication with a network provider operating in the 850/900/1800/1900 MHz frequency range and classified as Class 12, as shown below.

Inserting the SIM card

Step 4 — Water sensor

Attach the water sensor to A1 on the Grove Base shield, as shown below.

Connecting the water sensor

Create your Greenhouse's SMS software

Your greenhouse's software provides low water warnings. It needs an Arduino sketch to send an SMS, using the onboard GSM capabilities and a water sensor. This section describes the software required as follows:

  • Overview of the Arduino sketch.
  • Configuring device and GPRS settings.
  • Acquiring and sending the sensor data.

Overview of the Arduino sketch

To create the device that will acquire the water sensor data and pass the information using SMS messaging, an Arduino sketch is implemented, verified and uploaded to the LinkIt ONE development board. The source code is provided in a GitHub repository.

The content of a sketch is as follows:

  • Definitions of header files.
  • Definitions of variables.
  • setup() function that initializes resources, such as the water sensor module.
  • loop() function that continuously listens to and processes events from hardware and software modules such as those for GPRS. The loop() function runs forever — until the device is shutdown.

Start your Arduino project

Create a new Arduino sketch — software that will run on the LinkIt ONE development board.

  1. Open Arduino IDE and by default a new sketch is created and displayed.
  2. Create a folder named GreenHouse in your documents folder and on the File menu click Save As, navigate to the new folder, name the sketch GreenHouse.ino and save it.

    Empty project in Arduino IDE

Add the header files for the supporting libraries

The first part of your code adds the libraries required for the GreenHouse.ino Arduino sketch.

Add LGSM library

Following code is needed to include the GSM library in your sketch so that it’s possible to send and receive SMS in a later stage.

#include "LGSM.h"

More details about these libraries can be found in the GSM API guide.

Define the variables

Next the static variables used in the sketch are defined.

  1. WATER_SENSOR is the pin where the water sensor is connected to the Grove base shield. In this example the pin is A1.

    #define WATER_SENSOR A1
  2. contact_no is a global variable which holds the mobile number which will be sent the SMS.

    const char* contact_no = "0123456789";

Initiate the setup() function

Now the setup() function can be defined, to start the serial communication and check the water level.

  1. Initiate Serial communication with a baud rate of 9600, for future debugging purposes. 

    void setup() {
        Serial.begin(9600);
  2. Establish a connection to the GSM network in a while loop by calling the LSMS.ready() method of the LSMS class. Allow one second of waiting time before reconnecting to the GSM network in the while loop by calling the delay(1000) function. In order to provide feedback during the execution of the sketch, send a message before and after the while loop to the serial output. In addition to the messages, send a full stop during each iteration of the while loop in order to identify the state of the sketch while it’s running. 

    Serial.println("Initialize GSM for SMS");
     while (!LSMS.ready()){
          delay(1000);
          Serial.println(".");
      }
      Serial.println("GSM ready for sending SMS");

Add the loop() function

The loop() function checks the level of the water tank and sends a text message if it's empty.

  1. Calling the loop function.

    Within the loop function the check_water_level() function is called and then the sketch waits for 20 seconds by calling the delay(20000)

    void loop() {
        check_water_level();
        delay(20000);
    }
  2. Checking the water level.

    The water level is checked by calling the check_water_level() function, where the analog input is read by calling the analogRead() function, passing the pin you connected the sensor to. This returns an integer value. The water sensor returns a value higher than 60, if there is no water in the tank. This will then trigger sending the SMS to the user. 

    void check_water_level(){
     
            Serial.print("Water Level:");
            int water_level = analogRead(WATER_SENSOR);
            Serial.println(water_level);
            if (water_level > 60){
            send_low_water_SMS();
        }
    }
  3. Sending a message (SMS).

    Call send_low_water_SMS() to send a message. The SMS is initialized by calling LSMS.beginSMS(contact_no) and passing the number you want to send to. The message is set by calling LSMS.print(“message text”) and passing the message text as a string.

    The message is sent by calling LSMS.endSMS(). This function will return a Boolean value that indicates if the message was successfully sent or if it failed.

    You can also send status messages to the serial output, for debugging purposes, by calling Serial.println()

        void send_low_water_SMS(){
        LSMS.beginSMS(contact_no);
     
        LSMS.print("Water tank is running low!");
        if (LSMS.endSMS()){
            Serial.println("SMS successfully sent");
        }
        else{
            Serial.println("SMS failed to send");
        }
    }

Run your application

With the code complete, you can now upload the sketch to your board. Once this is done, you can monitor the serial output in Arduino and will see something similar to the following.

Serial output content of the greenhouse's SMS example sketch

Now add or remove water, check that the value changes and the SMS sending stop or start, as expected.

Your next steps

In this tutorial you have implemented an application that warns a person that their greenhouse is running low on water by sending an SMS using the LinkIt ONE development board, a water sensor, a SIM card and Arduino IDE programming environment.

For more information on MediaTek LinkIt ONE development and prototyping board refer to LinkIt ONE developer’s guide. For more tutorials, please visit our tutorial section where you’ll find more reference projects.