Intro: IoT RC Car With Intel Edison ( IntelIoT)
Let us first understand what we are trying to achieve here. We have all sorts of RC toys for our kids and then there are video and mobile games. One this is for sure that every one just loves cars, be it the real, or be it in the 3D gaming or a toy car.
I have a four year old boy Rupansh and he is just fascinated by the cars. He creates model cars with all my spare parts, loves books that have those automobile photos, watches kids car channels in Youtube and plays monster turck games in mobiles. So I had a idea why not to combine the fun of mobile gaming with that of RC toys and why not control them with mobiles?
I had started of with Arduino and a Bluetooth module and built one for him. It was good. But I wanted a bit more. With Bluetooth we are restricted in terms of distance. Even though RC cars are for small range fun, but what if we want to extend the framework to a real RC car game model with long distance racing capabilities? What if I could control it from physically anywhere ( with some sorts of IP camera to see what I was doing?). What if the concept could be incubated into real long distance practical concept?
So I started digging into Internet of Things and got Arduino into internet through a PC with Serial communication. It was fun, but not exactly what I wanted. I wanted the control system to be physically isolated!
Then I attended one workshop of IoT in Pune, India organized by Intel corporation. They provided with an Intel Edison board, Arduino breakout board, Grove shield and some cool compatible sensors and actuators. It got me rolling on that day that if I could build a RC toy car and actually have it controlled through Edison, it would be exactly what I am looking for. During two days of hackathon, I made one working model. As I had no exposure to Edison prior to that day, all I did was did a remote hack and controlled the remote through Edison and the Edison took the command from a WebService hosted in my site. Entire code was done in Arduino. Edison runs Arduino code as just another Unix service with restricted memory and resource allocation which makes it hard to do anything substantial "Web" thing with Arduino code. None the less I could manage to create a working demo and got Best video demo award for making it's video and sharing that.
Once I was back from Pune, I wanted a really good, fast working model with the board. So I started digging with Linux. Modified the kernel with bitbake. Got many features. Played around with curl and xmllint. As we keep working on a technology and try out things, we start knowing the pros and cons of it. We come to know the speed aspect, the stability, the form factor.
So after about two weeks of trails and errors, "this and thats" I finally achived my goal. I could now control RC toys through IoT and yes, pretty fast and real time too. But the biggest take away with hack based control was that I did not have to alter the actual toy. I hacked a RC remote and got it connected to internet. I choose a popular 27Mhz module. So that you can take the system and can control just about any RC toy having 27MHz receiver.
So I thought why not just share the whole architecture so that few of you who have small kids can gift your kids that great Gizmo. Trust me your kid is going to love it.
So let's get started with our cool project.
Here are the things we are going to do in this project.
1. Understanding the Architecture of the project
2. Setup an Intel Edison board ( We should just escape this topic with appropriate pointers that helps you get started)
3. Understanding and Working With MQTT
4. The Hardware hack
5. Android Mobile App
6. Demo of our project
Step 1: Understanding the Architecture
It's always a good thing to have an overview of what we are trying to do before we start with the real work. It serves two purposes, firstly we know what we are doing, second it gives you an option to bypass few things if you already are aware of the things.
So if you look at the diagram, you can see that the project majorly comprises of four units:
a) A RC toy car
b) A RC Transmitter hack with Intel Edison
c) Control Service which we shall call the broker and
d) Mobile App
Generally Robotics freaks and DIY guys would hack the RC car and would prefer getting the skeleton stripped for a nice looking hardware hack. But as we are talking of giving more fun to kids we are not really looking to destroy those cool looking toys. But if you are a geek and really want to adopt this model to your robotic project then you are your own.
So a) is left alone.
RC hack with Edison is the heart of our project. It will include some hardware hack and some software. So we will divide this stage into few steps.
In stage b) our goal is to to connect the Edison to internet, which is really out of the box ( well, sort of). Then our goal is to get this board receive remote commands. Now this is where we need to understand the possibilities, the choices and finally the technique that we settle with. We need to prepare the Edison board such that it can receive command from a remote server and then controls digital pins according to the command.
A typical home control module would have two steps: ON and OFF. Devices would vary. But for hacking a remote, and emulating it through commands needs little more thought which we shall see in due time and step.
Once we are able to receive commands remotely by Edison and able to control it's GPIO pins, we can go for hardware hack. We then can come back combining the hack with Edison to get a RC Remote over Internet.
The we shall build an Android Client to generate the commands from mobile. That's it!
Mobile will become your remote by integrating it with real hardware through a remote broker.
Let's go to Step 2 and Configure our Edison board.
Step 2: Getting Started With Intel Edison Board
Intel Edison is a relatively new kid in IoT arena, built on the same line of Galileo, but with lot more power, flexibility and features. It runs a full Linux Intel distributes and prefers Linux Yocto for Edison. Yocto is lightweight as embedded software should be. There are many libraries and packages that you can add over it and there are few libraries that needs an entire kernel rebuild. We recently also had Debian based Ubilinux for Edison. I installed it and played around with Ubilinux too, and by the end did not find anything it offers other than the ability of apt-get. Yocto doesn't have a full fledged repo like Debian, but it's now having a distro called opkg. So I would prefer to stick to official distribution rather than third party Ubilinux untill there is any significant advantage to it.
Many of you may already have an Edison board, a few might be planning to get one and just starting off. For starters I would like to provide a little overview of the hardware platform before we move ahead.
I am using Edison with Arduino breakout board ( Amazon Link). Edison is a tiny microcomputer which fits nicely on this board. Advantage of this board is that it is Arduino compatible. So you can use your favorite Arduino sketches easily.
I am also using a Grove starter kit(Amazon Link). This has a base shield that sits nicely on the top of breakout board. This shield offers several four pin Grove connectors which maps some of digital I/Os of Arduino compatible pins, four analog ports that maps A0-A3 of Arduino compatible pins, I2C ports for connecting devices like RGB backlight LCD, Accelerometer GY521 and so on and a UART port. The beauty of this board is that you can actually hook the supplementary accessories like sensors, buzzers, LEDs that comes with this kit without any soldering. So those who always wanted to "make something" but were afraid to get started because they did not know soldering would certainly have no excuses with the Seed Studio Grove shield.
So if you are among those who already have this kit and quickly want to get started could jump to next step. For those of you who have just acquired a fresh Intel Edison+Arduino Breakout Board+Grove starter kit need to setup your board first. Intel's getting started with Edison guide should be your starting point. Once you have the operating system and WiFi setup in your board, we are ready with our work.
Most of you must already have hands on experience working with hardware circuits in general and also with Arduino. But some of you might not have much of hardware experience. For those, I advise to install Arduino IDE and install Edison board package in the IDE. Arduino sketches are quite simple and are a great catalyst to your confidence booster.
If you are unsure of how to hook and work with the hardware, the two videos might help you to get started. With our board setup, WiFi running, a bit of hardware knowledge we are ready to move ahead. So let's go to next step and learn how to control our hardware from the board through remote commands.
Step 3: Understanding MQTT and Working With MQTT With Edison
MQTT is a Publisher-Subscriber based light weight communication protocol for machine to machine and things-to-things communication. It gives a much desired flexibility in IoT framework. Generally machines and things communicate through HTTP over web. HTTP packets have their own header which increases the packet length. MQTT packets on the other hand just embeds data in TCP/UDP packets and is therefore less byte consuming which ultimately results in better communication speed with lower latency. It also introduces Quality of Service(QoS) into the protocol providing much desired reliability. Inherently the protocol is non-secured, but if implemented over SSL , it provides a secured communication.
If you are a perfectionist and geek, this protocol specification document is a good place to learn the details of the protocols. For those who just wants a quick understanding of the mechanism rather than the skeleton,this article is a good bookmark and reading resource.
So in short MQTT connects two different devices through a broker. One of these device is a publisher which can either publish it's sensor information or controlling data to the broker in a particular channel, the other one is a subscriber which subscribe to that channel. Whenever there is some data available in the channel, subscriber gets the data asynchronously. Both these end peers are called clients ( yes, even the publisher is called client).
Mosquitto is one of the most popular open source broker which also provides client tools and therefore is a great choice. It is extremely light weight and linux compatible.
For installing Mosquitto, all you have to do is login to your Edison board through SSH using a software like Putty.
If you want to know the details of working with Edison using Putty, you can check the linked tutorial.
Once you have logged into the Edison board through Putty, you have to follow few simple steps to install Mosquito.
type following command in your shell and press enter.
tar xzf mosquitto-1.3.5.tar.gz
[Note, if you are in your SD card directory ( /media/sdcard) and want to install it there, you need to use follwing command for untar
tar xzf mosquitto-1.3.5.tar.gz --no-same-owner
make WITH_SRV=no add user mosquitto
That's just all. MQTT broker and client tools are installed and broker running on your Edison. After reboot, the broker automatically starts. Mosquito also offers an online broker test.mosquito.org for quickly testing pub-sub protocol.
For testing MQTT online, open another putty session and login to the board through SSH. Now type following command in the window.
mosquitto_sub -h test.mosquitto.org -p 1883 -t CHANNEL_NAME
you can use any meaningful channel name like YOUR_NAME/SOME_CHANNEL_NAME. I use a channel name like "Rupam/Edison/ControlSignal" as my channel name. You can use any format you wish.
When you execute the above command, a process will wait for reception of command.
Now open another session and execute following command
mosquitto_pub -h test.mosquitto.org -p 1883-t CHANNEL_NAME-m "SOME_COMMAND"
( Do not forget to use double quote while specifying command)
This will publish "SOME_COMMAND" into your channel and you will see the data in subscriber window. You can enter different commands and can see them appear in subscriber window.
To test if your local broker is running or not, just replace test.mosquitto.org with YOUR_EDISON_IP_ADDRESS in both pub and sub windows. Don't forget to terminate waiting sub client by hitting ctrl+c.
This is the same IP with which you have logged into Edison through Putty.
Okey. We now have Edison board ready and running MQTT. Time to do hardware hack.
Step 4: Hacking RC Remote
Open the 27Mhz ( well this hack works for all RC remotes and not restricted to 27Mhz). You will see a tiny circuit connected with the battery box. Remove all buttons/ keys and take the circuit out. You will find four ( or more) press switches. You can press them and see your toy operating.
The objective of this hack is to bypass this hardware switches with programmable switches. You might find this codeproject tutorial of Getting Started With Arduino Hardware Platform by me helpful if you are a hardware starter. How to control DC motors with transistor will give a good understanding of the mechanism we want to adopt here.
First we will bypass the battery and make the receiver powered up from our Edison board. Just cut the red and black wires that connects this tiny board with remote battery pack.
One of the best thing with Grove is that it offers you plug and play options. So you can plug different sensors and other components through Grove ports and play around with them. Ours is a hack, will involve hardware. But I do not want to spoil the fun of plug and play. So our hack would keep the integrity of the Grove shield and should be completely plug and play just like other accessories supplied with the starter kit.
Observe closely to any data port in the base shield marked D4/D5 etc. You can see there are four pins: vcc, ground, signal and NC. First insert a Grove connector to one of the data ports and cut it in the middle ( so we are making two connectors here!!!). Now connect the red vcc wire of your remote unit with the corresponding vcc of the grove port connected wire. Do the same with ground. Now if you press any of press button in remote you will see remote light glowing. Ok, we now have a RC remote unit powered through Grove shield. All we need to do now is bypass these hardware switches through transistor.
We will go with an NPN transistor (I am using BC548). When it is supplied with base voltage it connects emitter ( pin 1) with collector (pin 3). So we will solder two wires in the hardware switch two ends and them with pin 1 and 3 of BC548 respectively. In order to prevent the transistor to turn on with leak voltage it is a good idea to connect base ( center pin 2) with a suitable resistor. I prefer a 470 Ohm resistor. Recall that the Grove connector has a NC line and a signal line. All you need to do is connect Signal wire with the other end of resistor. We are done hacking one remote switch.
You can now control this switch with the blink sketch by changing pin 13 to whatever data port you have it connected. If you are able to turn on and off the RC remote boards LED, switch on your car and you would see the movement being controlled through Edison. Alright now do it for other three switches in the remote board. The good thing with this approach is that you can still use the hardware press switches of your remote board. So remote board can be controlled through program or can be used just as you were using it through these buttons. Finally we are down with our hack. Just remove the Grove connectors from board and you have your own plug and play Grove RC remote :)
Time to get this hack work over IoT with MQTT. So in next step we shall get this "Grove RC Remote" receiving commands over MQTT protocol.
Step 5: Getting It All to Work!
We will use Java script ( with Node Js) for our particular work. Now let me confess it before you actually find out that this is for the first time in my life that I wrote a java script. I am more of .Net, Matlab programmer and therefore never find JS amusing. But for this particular project, I felt JS is the best and easiest option.
Edison comes preloaded with Node js and a library to control the GPIO pins called mraa. What I liked most about Node.js is no nonsense approach for installing a library. To install a MQTT js client all you have to do is install the library with following simple command.
That's all! Now we can work with both MQTT and gpio pins from Edison.
I created the java script through vi editor ( I am rest assured that there are better ways of editing and checking your java scripts like using Intel XDK).
The objective of this script would be to subscribe to a mqtt channel and Control gpio based on commands received.
Here is the code.
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://192.168.1.101');
// mqtt:// is important. Else code will not work. So if you are testing with mosquitto.org
var mraa = require('mraa')
////////////////// Remote Pins//////////////////
var fPin = new mraa.Gpio(6)
var bPin = new mraa.Gpio(5)
var rPin = new mraa.Gpio(3)
var lPin = new mraa.Gpio(4)
//////////////Set up Pins/////////////////////////
// # at the end is important. Else your code is not going to work
var payload = packet.payload.toString()
if (payload === 'FORWARD')
if (payload === 'REVERSE')
if (payload === 'RIGHT')
if (payload === 'LEFT')
if (payload === 'STOP')
if (payload === 'NO')
////////////////////////////////////////////// Code Ends////////////////////////////////////////////
The reason for using a local service is so that you can reduce the latency and experience near real time controlling. If you want to play around with a remote server, feel free to do so.
I have connected four grove connectors to four pins. Let me admit here that I also wanted a speed control, but at the time of writing this tutorial, I could not manage to get PWM work properly with Node.js. Four connectors coming from FORWARD, BACK, RIGHT and LEFT buttons of the RC remote are named as fPin, bPin,rPin and lPin respectively. You can change the pin numbers at the top of code if your connection is different.
Having our code, it's now time to run it.
You can just type
in shell prompt and hit enter ( note no .js after the file name). You will see the shell waiting to receive the command.
You can test if it is working or not just by using command line publishing service we did above with FORWARD, REVERSE, NO, LEFT, RIGHT and STOP command.
The logic is, once we press forward button, car should keep moving forward. Alongside you can press LEFT or RIGHT button, it should enable left and RIGHT movement. When you release those buttons, car will still move forward or reverse untill you don't release those buttons. So for releasing RIGHT and LEFT we use NO command. For stopping everything, we use STOP command.
Download Android APK provided with this tutorial and install the APK. Run the app and change server address. Do not forget to turn on wifi in your Android phone and log into the same wifi network that your Edison is connected to.
Enjoy your new remote control for RC toys.