Introduction: Garden Monitor & Control System Using Intel Edison, Node-Red & Twilio

About: Researcher. Learner. Technologist. Plays with IoT. 1 Patent. Electronics Hacker. M.S (Intelligent Systems and Robotics), University of Essex UK. https://in.linkedin.com/in/shreyasupasane

This article demonstrates using Intel EDISON with simple, off-the-shelf sensors and hardware in our garden control system. Specifically, we will be gathering soil humidity and environment temperature data from an industry grade sensor and relaying that data to the dashboard via Node-Red APIs. From there, we can analyze our soil moisture and environment temperature data in real-time.

Additionally, we will be sending soil moisture levels (once they are LOW) via SMS using Twilio API’s which will notify us when to turn ON/OFF the relay using our dashboard. Solenoid can be attached to the relay further. This autonomous combination of hardware, software and networking is now being referred to as IoT (Inter-Operability of Things).

This article assumes you have experience uploading code using Arduino IDE and login to the Edison board using putty.

Step 1: Hardware Components We Will Use in Our Demo

• Intel Edison Development board with a network connection

• Node-MCU Wifi Module

• SHT10 based Soil Moisture Sensor

• 5 Volt Relay Seeed Groove Kit

• Single-bit Connecting Wires Female-to-Female • USB and Power cables

Step 2: Software We Will Write

• A sketch program running on Node-MCU

• A Node-RED flows running on Intel Edison

You can download the code samples here.

Step 3: Build the System

Connect the SHT 10 sensor according to the diagram.

Step 4: Connect the Intel® Edison Board to Your Computer for Development

While plugging in every cable is not required all the time, a cable setup for initial development purposes with the Arduino* expansion board would look like the photo.

Step 5: Install the Packages on Intel Edison

Now we have connected the Edison to our computer using putty, we will install the packages required to run our software on Edison.

Login to Edison with ‘root’ and password set up by you.

1. After successful login, update the npm (node package manager) to the latest version by typing this command followed by the enter key:

npm install -g npm@3.3.7

2. Install the Node-RED package:

npm install -g —unsafe-perm node-red

3. Install the iobroker.vis package using npm

npm install -g iobroker.vis@0.10.5

4. Install the node-red-vis package using npm

npm install -g node-red-vis@0.4.2

5. Install Twilio package using npm

npm install -g node-red-node-twilio

Step 6: MQTT

MQTT is a light-weight protocol used for Machine to Machine (M2M) communication.

MQTT used a publish/subscribe message forwarding model built on top of TCP/IP protocol. Central to the MQTT protocol is an MQTT server or broker that is accessible to both publishers and subscribers. Using MQTT, one can build a sensor network where various sensors can publish their sensor values in the form of a message unique to each sensor. Actuators can subscribe to different messages that they can act upon. The MQTT broker will take care of forwarding messages from publishers to subscribers.

Example: Microcontroller A can read the state of a switch and publish the state of the switch as a message in the form "switch = on" to an MQTT server. Microcontroller B somewhere on the internet subscribed to the MQTT message "switch". Now whenever a user pushes the switch, microcontroller A will publish a message to the MQTT broker. The broker will forward the message to a list of subscribers. When microcontroller B receives the message, it can parse the content, determine the state of the switch and turn on or off a light accordingly.

More details on MQTT can be found at http://mqtt.org.

The Edison Yocto OS came with a small MQTT broker called RSMB (Really Small Message broker). Unfortunately, there isn't a bundled MQTT client for the purpose of testing. In this tutorial, we will build another MQTT broker, Mosquitto, mostly for the clients and we will use these clients to connect to our Arduino sketches.

Step 7: Installing Mosquitto on Edison

On to your putty type in:

wget http://mosquitto.org/files/source/mosquitto-1.3.5.tar.gz

tar xzf mosquitto-1.3.5.tar.gz cd mosquitto-1.3.5

make WITH_SERV=no user add mosquitto

Once the mosquitto and all other packages are installed, we will move forward with the fun part!

Step 8: Uploading the Sketch on Node-MCU

Connect the Node-MCU to your computer and download two Arduino libraries from here:

1. Pub-Sub Client (Library)

2. SHT1x-master (Library)

Include these two libraries onto your Arduino IDE.
After successfully importing the .zip files of library we can confirm whether they are installed properly or not.

You can see two libraries ‘PubSubClient’ and ‘SHT1x-master’ are added successfully.

Please note: These two libraries are compulsory to be installed for running this tutorial and the libraries provided by the author are tested and works well as required.

Before uploading the sketch on Node-MCU, type in:

ifconfig

on your Edison board via putty to check the ip address of your device.

Your inet addr may be different and will depend on your network.
Edit the sketch before uploading.

Edit the “ssid" with your WiFi network ssid, “password” with your WiFi password and
“mqtt_server" with your Edison board ip address.

Step 9: Configuring Node-MCU With Arduino IDE

As Node-MCU is not included by default in the boards manager on Arduino IDE, we will

add it.

• Go to files and click on the preference in the Arduino IDE.

• Copy http://arduino.esp8266.com/stable/package_esp8266com_index.json URL in the Additional boards Manager

• Click OK to close the preference Tab.

• After completing the above steps, go to Tools and board, and then select board Manager.

• Navigate to esp8266 by esp8266 community and install the software for Arduino.

• Once all the above process been completed we are ready to program our Node-MCU with Arduino IDE.

• Save and upload the sketch on your Node-MCU module.

Step 10: Running Node-RED & Mosquitto on Edison

MQTT has emerged as a standard messaging protocol for IoT. It can be used over TCP/IP networks and is very lightweight. The standard follows a publish-subscribe ("pub/sub") model; here, Edison plays a key role on the network as the "message broker".

In this section, we will:

1. Set up an Edison device with Mosquitto to become a simple MQTT broker.

2. Use the Node-RED development tool to easily send and receive MQTT messages.

3. Subscribe edge node devices (Node-MCU) to messages published to the broker.

Step 11: Starting a Session

Log in to the terminal on your Edison device via ssh or the serial connection (if you are not already logged in).

Make a note of the board IP address using ifconfig (which we will refer to as localhost in later discussion).

ifconfig

3. Start Node-RED (node connection development tool) and run it as a background process.

node-red &

4. Start Mosquitto (message broker) and run it as a background process.

mosquitto -p 1883 &

P.S. Remember the “1883” must be same as the port of mosquitto defined in our Node- MCU sketch!

5. Start a browser on your computer. Point it to the localhost IP address of your Edison device that you noted previously, on port 1880.

Example: http://192.168.1.5:1880

P.S. Your computer must be connected to the same WiFi network as the Edison WiFi.

6. Edison is now set up as a broker and is also running Node-RED, so we are ready to set up publish-subscribe messaging.

Step 12: Adding Flows on Node-RED Canvas

Input Nodes MQTT (Sensors)

We will use an input node to receive messages from the broker; the humidity and temperature data.

1. Create a flow on the Node-RED canvas.

• Select an Input node: Drag an "MQTT" input node to the right of the first node. This node will be receiving messages from the broker via MQTT.

2. Configure this node by double-clicking on it.

• MQTT node: Configure the node to send a message by entering the localhost IP address of the broker, at port 1883. Enter a Topic of ‘garden_moisture’.

• Similarly, for temperature, Configure the node to send a message by entering the localhost IP address of the broker, at port 1883. Enter a Topic of ‘temperature’.

3. Along with the MQTT input node type, the Debug output node type helps greatly in troubleshooting a flow. For a demonstration, we will start from the canvas.

  • Output node: Drag a "debug" output node from the palette in the left pane onto the canvas, to the right of your Inject node.
  • Connection: Connect the nodes by dragging a second wire from the right side of the Inject node to the left side of the debug node.

4. Run the flow. Click Deploy at the upper right to run this new flow.

5. Look at the debug output. Click on the debug tab just under the Deploy button. Now you can see the history of the messages being sent from the MQTT node.

6. In the incoming messages under the debug tab, the numeric value now changes according to the humidity (garden_moisture) and the temperature of the surrounding environment as indicated in the image.

Output Node MQTT (Relay)

Last time, we used an input node to receive messages from the broker; this time we want
to send messages to the broker to control the relay.

1. Create a flow on the Node-RED canvas.

• Select an Output node: Drag an "MQTT" output node to the right of the first node. This node will be sending messages to the broker via MQTT.

2. Configure this node by double-clicking on it.
• MQTT node: Configure the node to receive a message by entering the localhost IP address of the broker, at port 1883. Enter a Topic of ‘relay’.

Step 13: Dashboard Node-RED Vis

Now we have seen above we have configured all the MQTT input and output nodes and we are able to receive sensor data on Node-RED, we will now use this data in our visualisation tool called ‘vis’.

1. Add a flow on the Node-RED canvas.

• Select an vis node: Drag an "vis" node from the advanced menu on the left to the canvas and connect it with other nodes.

2. Click on Deploy and open a new tab with your Edison ip address on port 1880:

http://192.168.1.3:1880/vis/edit.html

3. Initially you will get a warning, main/vis-views-json Not exist.

4. Click on OK. Another warning message will appear. Click OK.

5. You will get a vis dashboard on ‘DemoView’ after removing those warnings.

6. Now add your new view for this tutorial from the Views tab on the left corner and name it anything.

7. Add a background color of your choice from the Attributes tab on the right.

8. Add widgets like weather, HTC clock from the left side of pane to fill the dashboard.

9. Now the most important part; we will add two widgets for showing the “humidity &
temperature" on our dashboard and for switching ON/OFF the “relay”. Remember above we have given “topic” name of each input & output MQTT nodes on Node- RED canvas. For “humidity & temperature” we have given “temperature” as the topic name and for relay we have given “relay” as the topic name.

So, add two widgets (Inner temperature and Giva labs iButton) from the left pane onto the vis canvas.

Each widget Attributes tab (on the right) has some properties. We will have to add temperature topic in the Object ID of ‘Inner temperature’ widget and relay topic in the Object ID of ‘Giva labs iButton’ widget. Remember Object ID under Common drop down menu under Attributes.

9. Now the most important part; we will add two widgets for showing the “humidity &
temperature" on our dashboard and for switching ON/OFF the “relay”. Remember above we have given “topic” name of each input & output MQTT nodes on Node- RED canvas. For “humidity & temperature” we have given “temperature” as the topic name and for relay we have given “relay” as the topic name.

So, add two widgets (Inner temperature and Giva labs iButton) from the left pane onto the vis canvas.

Each widget Attributes tab (on the right) has some properties. We will have to add temperature topic in the Object ID of ‘Inner temperature’ widget and relay topic in the Object ID of ‘Giva labs iButton’ widget. Remember Object ID under Common drop down menu under Attributes.

10. If you want to add text under widget, you can use HTML widget and set the text
accordingly.

11. Now once you've added all the widgets, you can view your dashboard on:

http://192.168.1.3:1880/vis/index.html

The values of humidity and temperature will get updated real time!

12. You can toggle the ON/OFF switch from the dashboard to activate and deactivate the relay attached to Node-MCU. Humidity in % and temperature in degree celsius is displayed on the Inner temperature widget.

Step 14: Sending SMS With Node-RED Twilio

We have completed designing our dynamic dashboard above using Node-RED vis package. Now let’s add some smartness into our system. We will integrate the notification feature into our existing system to notify Soil/Garden moisture level once it gets LOW to a set level on your smartphone using an SMS.

Telephony for the cloud era, Twilio's platform lets you easily create scalable voice, VoIP, and SMS text-messaging applications.

We will implement creating Twilio account, adding Twilio Node into our Node-RED canvas.

1. Create a Twilio account (if you don’t have it). Go to www.twilio.com and create a new account or login. There are many tutorials on how to create a Twilio account.

2. Get your Twilio account SSID and Token.

3. Also note down your Number. We will use these three things for configuring Twilio Node.

4. Add the Twilio node from left pane under mobile in Node-RED canvas.

5. Double click on twill node and add a new twilio-api, add receivers phone number with
country code (+91___________) and choose Use local credentials.

6. Add your Account SID, your phone twilio number and token.

7. We will write a Node-RED function to process on sensor data and act on them.

8. Build a message processing function node. Select a Function node from left pane of Node-RED canvas.

9. Double-click it and insert the simple JavaScript code:

if((msg.payload <=85) && (flag == 0)

{

flag = 1;

return msg;

}

else if(msg.payload > 85)

{

flag =0;

}

This simple script will monitor the humidity level from the input garden_moisture MQTT node each second and will send msg.payload to the Twilio node for sending an SMS once the humidity of soil gets LOW.

10. Now also add template node into Node-RED canvas, in which we will write a template for our SMS and join the Function & template node to twilio node.

11. In the template node, write your customised SMS text you want:

Your garden's moisture level is {{payload}}% which is LOW. We recommend you to turn on your sprinkler using Dashboard.

P.S. { {payload} } will contain the Humidity value!

12. So, once the humidity value goes below 85%, you will receive an SMS from Twilio on your phone.

Step 15: Summary

Node-RED allows you to quickly create and debug an application that receives messages from edge devices, intelligently processes them, and sends back appropriate responses, all with very little programming. You can then embed your Node-RED flow directly within an end product.

The real-world application of these principles is just as easy as what you saw. The task complexity is limited only by how much JavaScript function code you are willing to write. Edison and MQTT seamlessly manage all of the messaging handling intricacy in the middle.