This article demonstrates concept of connected cars using Intel EDISON with simple, off-the-shelf sensors and hardware in our Connected Cars application. Specifically, we will be controlling the speed and orientation of a small car/wheeled-robot using Node-RED. While we control the speed and orientation of one car connected to Edison, another similar car will imitate the same i.e. changing speed and orientation of CAR1 will affect the speed and orientation of CAR2.
P.S. This is just a concept which can further be advanced.
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 - TWO
- Thumb Joystick - ONE
- Groove Rotary Angle Sensor - ONE
- 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 for Direction Only
Make the connections for CAR 1 as shown above in the diagram. Attach joystick only with Node-MCU.
Make the connections for CAR 2 as shown above in the diagram. Attach DC Motor only with Node-MCU.
Step 4: Build the System for Speed Only
Attach the POT only with Node-MCU as shown in the figure.
CAR 2 (Same as above)
Make connections same as above step with motor.
Step 5: Architecture Diagram
Check the above diagram for architecture used in this system.
Step 6: 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 above.
Step 7: 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 firstname.lastname@example.org
2. Install the Node-RED package:
npm install -g —unsafe-perm node-red
3. Install the iobroker.vis package using npm
npm install -g email@example.com
4. Install the node-red-vis package using npm
npm install -g firstname.lastname@example.org
Step 8: 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 9: Installing Mosquitto on Edison
On to your putty type in:
tar xzf mosquitto-1.3.5.tar.gz
Once the mosquitto and all other packages are installed, we will move forward with the fun part!
Step 10: Uploading the Sketch on Node-MCU
Connect the Node-MCU to your computer and download Arduino library 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:
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 11: 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:
1. 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
2. Click OK to close the preference Tab.
3. After completing the above steps, go to Tools and board, and then select board Manager
4. Navigate to esp8266 by esp8266 community and install the software for Arduino.
5. Once all the above process are completed we are ready to program our Node-MCU with Arduino IDE.
6. Save and upload the sketch on your Node-MCU module.
Step 12: 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:
- Set up an Edison device with Mosquitto to become a simple MQTT broker.
- Use the Node-RED development tool to easily send and receive MQTT messages.
- Subscribe edge node devices (Node-MCU) to messages published to the broker.
Step 13: 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.
3. Start Node-RED (node connection development tool) and run it as a background process.
4. Start Mosquitto (message broker) and run it as a background process.
mosquitto -p 1887 &
P.S. Remember the “1887” 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 14: Adding Flows on Node-RED Canvas
Input Nodes MQTT (Sensors)
We will use an input node to receive messages from the broker; the Speed of CAR1 from Rotary sensor.
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 1887. Enter a Topic of ‘speed’.
3. Run the flow. Click Deploy at the upper right to run this new flow.
Output Node MQTT (CAR 2)
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 1889. Enter a Topic of ‘CAR2’.
Step 15: 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 as given above.
2. Click on Deploy and open a new tab with your Edison ip address on port 1880:
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 the widgets, the dashboard should look like the image above.
8. Now you can rotate the Rotary sensor to change the speed of CAR1 and the CAR2 will act accordingly.
9. Also, using Joystick separately on CAR1 will impact on direction of CAR2.
Step 16: 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.