Introduction: Intel Edison Based Smart Water Meter

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 Smart Water Meter application. Specifically, we will be gathering water consumption level from a Hall-Effect based Water Flow sensors and relaying that data to the smartphone via Node-Red APIs. From there, we can analyze our water consumption data in real-time.

Additionally, we will be turning ON/OFF the relay using our smartphone. Motors 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
  • Hall-effect based Water Flow 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 water flow sensor, relay to node-mcu as shown in 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 UI package using npm

npm install -g node-red-contrib-ui


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: Micro-controller 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. Micro-controller B somewhere on the internet subscribed to the MQTT message "switch". Now whenever a user pushes the switch, micro-controller A will publish a message to the MQTT broker. The broker will forward the message to a list of subscribers. When micro-controller 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

useradd 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)

Include this library 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 library ‘PubSubClient’ is added successfully.

Please note: This library is compulsory to be installed for running this tutorial and the library provided by the author is 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_esp8266c...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 are 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

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

2. 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 water consumption 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 ‘water_flow’.

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.

P.S. Remember to pour a glass of water to the inlet of Water Flow Sensor.

6. In the incoming messages under the debug tab, the numeric value now changes according to the water flows from the meter 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 ‘Motor’.

Step 13: SmartPhone UI Using Node-RED Contrib-ui

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 ‘ui’.

1. Add flows on the Node-RED canvas.

  • Drag “chart”, “gauge”, “switch” and “show toast” nodes from the ui menu on the left to the canvas.
  • Edit the Gauge node with the values shown.
  • Edit the chart node with the values shown.
  • Edit the switch node with the values shown.
  • Connect the switch node output to the MQTT ‘Motor’ input node & show toast node as shown.

The final view on Node-RED canvas should look like the image provided.

2. Click on Deploy and open the URL given below on to your smartphone web browser:

http://192.168.1.3:1880/ui

3. You can see the dynamic dashboard showing you the Gauge, Line chart and a Switch you integrated above.

4. You can toggle the Motor On/Off switch to control your relay attached to Node-MCU.

5. Go on with adding some more nodes on Node-RED and see the data on your smartphone.

Step 14: 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.