Code examples for the Internet of things: smart watering
- Transfer
Recently, we published educational code examples for various projects that form the Internet of things. Today we will talk about an automatic irrigation system. It was built using the Intel IoT Developer Kit, Intel Edison, it uses cloud platforms, various APIs and other technologies.
Here are the features the automatic irrigation system that we will create based on Intel Edison has:
An irrigation system allows you to set an irrigation schedule by opening a web page stored on Intel Edison using your smartphone or other suitable device. It automatically polls the humidity sensor and displays the received data on a web page.
If it is assumed that the water pump is turned on, but the water flow sensor indicates that the water is not moving through the pipes, a text message will be sent to the specified number via Twilio. Thus, the irrigation system, which is out of order, can be quickly repaired.
Additionally, the system can keep an event log using code from the Intel IoT Examples Datastore, which runs in the Microsoft Azure environment.
The hardware part of the project is the Grove Environment & Agriculture Kit, which includes:
To get started, copy the How-To Intel IoT Code Samples repository to the Git computer using the following command:
If you prefer to download the ZIP archive with the necessary materials, open this page and click on the Download ZIP button on the right. After downloading the archive, unzip it and find the folder with the code for this project (watering-system).
In the Intel XDK IoT Edition, select the command Import Your Node.js Project:
Import the Node.js project.
Then go to the folder where the files for the training project are located and select it.
Selecting the folder with the project code
Now you need to connect the Intel Edison board to the computer and download the program to the board. To do this, you can use the IoT Device menu (IoT device), which is located in the lower left part of the window. If the system automatically recognized the connected board, select the appropriate line in the menu.
Intel Edison Connection
If the card was not automatically recognized, select Add Manual Connection, in the window that appears, in the Address field, enter 192.168.2.15, in the Port field, enter 58888. Save using the Connect button connection.
In addition, the program can be installed on Intel Edison manually. To do this, connect to the board via SSH and run the following command:
Then go to the folder with the example. If Git is not already installed on Edison, you can do this by connecting to the board via SSL and running the following command:
Prototype irrigation system assembly
It is necessary that the Groove (Grove Shield) expansion card, to which sensors and actuators will be connected, be connected to the switching Arduino-compatible card. Verify that the small VCC switch is in the 5V position on the Grove expansion board.
It is necessary to supply power to the Intel Edison from the external adapter that comes with the Starter Kit, or replace it with a suitable external power source (12V, 1.5A). You can also use a 5V USB battery.
In addition, you will need a breadboard and an additional 5 V power supply for the water pump. Please note that the pump needs a separate power source. You cannot use the same source for both Intel Edison and the pump, that is, you will need either two batteries or two adapters.
To connect a water pump, you will need a relay with a dry Groove reed switch. The connection is as follows:
If you choose the path to run the sample code on Intel Edison yourself, you will need to resolve some dependencies.
To get the Node.js modules necessary for the example to run on Edison, run the following command:
In order for the system to be able to send text messages, a Twilio account and API key are required . Messages cannot be sent without a key. The example will work without sending messages, but in real projects, this feature is very useful.
In order to configure the example to work with your Twilio account, you need to set the "
If you wish, you can store the data generated by this example in a database deployed using Microsoft Azure, Node.js and the Redis data warehouse. For details on how to set up a cloud server, see here .
If you have a Microsoft Azure account and have a server ready to receive data, you need to make changes to config.json, namely, write the data to connect to the server in the "
If your version of the example uses both sending text messages using Twilio and storing data in Microsoft Azure, the settings will look like this:
Before starting the program, save the changes made to the files. After that, click on the Upload icon to upload the code to Intel Edison.
Unloading a project on Intel Edison
After unloading, run the program on the device using the Run icon.
Starting a project
If you, when working on a project, made changes to the code, you can use the Upload and Run command. A fresh version of the program will be downloaded and launched on Intel Edison. While the Edison program is running, you will see messages similar to those shown in the figure below.
Messages from a program that runs on Intel Edison
To start the program manually, connect to Intel Edison via SSH and run the following command:
To configure the irrigation schedule, a one-page web interface is used. You can open this page by requesting it from a web server that runs on the same Intel Edison board on which the main program runs.
The page for setting the irrigation schedule The
server is assigned port 3000. Thus, if Intel Edison is connected to a Wi-Fi network and assigned an IP address 192.168.1.13, then the server address (provided that they are connected to it from the same local network ) will be http://192.168.1.13//000 .
In order to find the IP address of Intel Edison, you can use the following command:
After its implementation, you will see something like the following:
The Intel Edison address can be found after "inet". In our case, it is 192.168.1.13.
We talked about how, on the basis of Intel Edison, to assemble an irrigation system, which includes a set of sensors and a water pump. A system connected to the Internet can be controlled from any device that can open web pages. If you use cloud services in this example to store data and send messages, you can collect statistics on the operation of the device, and in case of an emergency, get a text message to the specified mobile phone number and quickly fix the breakdown.
Other code examples from the How-To series of Intel IoT Code Samples are available on the Intel Developer Zone . The full code for the example discussed here can be found on GitHub .
- How to connect to Intel Edison, a platform designed to prototype and develop wearable computing solutions and IoT devices.
- How to interact with the Edison I / O interface and sensors using MRAA and UPM from the Intel IoT Developer Kit, a self-contained hardware and software solution designed to help developers explore the possibilities of the Internet of things and create innovative products.
- How to run example code in Intel XDK IoT Edition, an IDE for creating applications that interact with sensors and actuators. This development environment allows you to quickly get started developing applications for Intel Edison and Intel Galileo.
- How to set up a web application server that will store irrigation system data using Azure Redis Cache in Microsoft Azure. This is a set of cloud services for IoT solutions that support data analysis, machine learning and many useful tools that simplify connecting devices to the cloud and allow you to quickly bring an IoT project to working mode.
- How to call Twilio API services to send text messages to mobile phones.
System features
Here are the features the automatic irrigation system that we will create based on Intel Edison has:
- Turning the water pump on and off according to a custom schedule.
- Checking whether the pump really pumps water when it receives the appropriate command. For this, a water flow sensor is used.
- Organization of access to the system using the built-in web interface, which allows you to set a watering schedule.
- Storage of information about the system in a cloud storage.
- Sending text messages to notify consumers that a system malfunction has occurred.
Features of work
An irrigation system allows you to set an irrigation schedule by opening a web page stored on Intel Edison using your smartphone or other suitable device. It automatically polls the humidity sensor and displays the received data on a web page.
If it is assumed that the water pump is turned on, but the water flow sensor indicates that the water is not moving through the pipes, a text message will be sent to the specified number via Twilio. Thus, the irrigation system, which is out of order, can be quickly repaired.
Additionally, the system can keep an event log using code from the Intel IoT Examples Datastore, which runs in the Microsoft Azure environment.
Hardware
The hardware part of the project is the Grove Environment & Agriculture Kit, which includes:
- Intel Edison with Arduino circuit board.
- Grove humidity sensor .
- Water pump .
- Water flow sensor .
- Relay with dry reed switch Grove.
Software
- Intel XDK IoT Edition
- Microsoft Azure Account
- Twilio Account
Preliminary preparations
To get started, copy the How-To Intel IoT Code Samples repository to the Git computer using the following command:
$ git clone https://github.com/intel-iot-devkit/how-to-code-samples.git
If you prefer to download the ZIP archive with the necessary materials, open this page and click on the Download ZIP button on the right. After downloading the archive, unzip it and find the folder with the code for this project (watering-system).
Adding an Application to the Intel XDK IoT Edition
In the Intel XDK IoT Edition, select the command Import Your Node.js Project:
Import the Node.js project.
Then go to the folder where the files for the training project are located and select it.
Selecting the folder with the project code
Now you need to connect the Intel Edison board to the computer and download the program to the board. To do this, you can use the IoT Device menu (IoT device), which is located in the lower left part of the window. If the system automatically recognized the connected board, select the appropriate line in the menu.
Intel Edison Connection
If the card was not automatically recognized, select Add Manual Connection, in the window that appears, in the Address field, enter 192.168.2.15, in the Port field, enter 58888. Save using the Connect button connection.
Manual installation of the program on Intel Edison
In addition, the program can be installed on Intel Edison manually. To do this, connect to the board via SSH and run the following command:
$ git clone https://github.com/intel-iot-devkit/how-to-code-samples.git
Then go to the folder with the example. If Git is not already installed on Edison, you can do this by connecting to the board via SSL and running the following command:
$ opkg install git
Connecting Grove Sensors
Prototype irrigation system assembly
It is necessary that the Groove (Grove Shield) expansion card, to which sensors and actuators will be connected, be connected to the switching Arduino-compatible card. Verify that the small VCC switch is in the 5V position on the Grove expansion board.
It is necessary to supply power to the Intel Edison from the external adapter that comes with the Starter Kit, or replace it with a suitable external power source (12V, 1.5A). You can also use a 5V USB battery.
In addition, you will need a breadboard and an additional 5 V power supply for the water pump. Please note that the pump needs a separate power source. You cannot use the same source for both Intel Edison and the pump, that is, you will need either two batteries or two adapters.
To connect a water pump, you will need a relay with a dry Groove reed switch. The connection is as follows:
- Connect one end of the Grove cable to the relay, and the other end to the D4 port on the Grove expansion board.
- Connect one of the wires of the water pump to a 5 V power supply for the pump.
- Connect the other wire of the pump to one of the relay power connectors.
- Connect the other relay power connector to the ground of the pump power source.
- Connect the water flow sensor by connecting the red wire to the “5V” terminal, the black wire to the “GND” terminal, and the yellow wire to digital output 2 on the Grove expansion board.
- Connect one end of the Grove cable to the humidity sensor and the other to port A0 on the Grove expansion board.
Manual installation on Intel Edison
If you choose the path to run the sample code on Intel Edison yourself, you will need to resolve some dependencies.
To get the Node.js modules necessary for the example to run on Edison, run the following command:
npm install
Settings: sending messages and cloud storage
In order for the system to be able to send text messages, a Twilio account and API key are required . Messages cannot be sent without a key. The example will work without sending messages, but in real projects, this feature is very useful.
In order to configure the example to work with your Twilio account, you need to set the "
TWILIO_ACCT_SID
" and " TWILIO_AUTH_TOKEN
" parameters in the config.json file . In them, respectively, you need to specify the API key and authentication token.{
"TWILIO_ACCT_SID": "YOURAPIKEY",
"TWILIO_AUTH_TOKEN": "YOURTOKEN"
}
If you wish, you can store the data generated by this example in a database deployed using Microsoft Azure, Node.js and the Redis data warehouse. For details on how to set up a cloud server, see here .
If you have a Microsoft Azure account and have a server ready to receive data, you need to make changes to config.json, namely, write the data to connect to the server in the "
SERVER
" and " AUTH_TOKEN
" parameters.{
"SERVER": "http://intel-examples.azurewebsites.net/logger/watering-system",
"AUTH_TOKEN": "s3cr3t"
}
If your version of the example uses both sending text messages using Twilio and storing data in Microsoft Azure, the settings will look like this:
{
"TWILIO_ACCT_SID": "YOURAPIKEY",
"TWILIO_AUTH_TOKEN": "YOURTOKEN",
"SERVER": "http://intel-examples.azurewebsites.net/logger/watering-system",
"AUTH_TOKEN": "s3cr3t"
}
Launching a program from Intel XDK IoT Edition
Before starting the program, save the changes made to the files. After that, click on the Upload icon to upload the code to Intel Edison.
Unloading a project on Intel Edison
After unloading, run the program on the device using the Run icon.
Starting a project
If you, when working on a project, made changes to the code, you can use the Upload and Run command. A fresh version of the program will be downloaded and launched on Intel Edison. While the Edison program is running, you will see messages similar to those shown in the figure below.
Messages from a program that runs on Intel Edison
Manual start of the program
To start the program manually, connect to Intel Edison via SSH and run the following command:
node index.js
Watering schedule setup
To configure the irrigation schedule, a one-page web interface is used. You can open this page by requesting it from a web server that runs on the same Intel Edison board on which the main program runs.
The page for setting the irrigation schedule The
server is assigned port 3000. Thus, if Intel Edison is connected to a Wi-Fi network and assigned an IP address 192.168.1.13, then the server address (provided that they are connected to it from the same local network ) will be http://192.168.1.13//000 .
How to determine the IP address of Intel Edison
In order to find the IP address of Intel Edison, you can use the following command:
ip addr show | grep wlan
After its implementation, you will see something like the following:
3: wlan0: mtu 1500 qdisc pfifo_fast qlen 1000
inet 192.168.1.13/24 brd 192.168.1.255 scope global wlan0
The Intel Edison address can be found after "inet". In our case, it is 192.168.1.13.
conclusions
We talked about how, on the basis of Intel Edison, to assemble an irrigation system, which includes a set of sensors and a water pump. A system connected to the Internet can be controlled from any device that can open web pages. If you use cloud services in this example to store data and send messages, you can collect statistics on the operation of the device, and in case of an emergency, get a text message to the specified mobile phone number and quickly fix the breakdown.
Other code examples from the How-To series of Intel IoT Code Samples are available on the Intel Developer Zone . The full code for the example discussed here can be found on GitHub .