Controlling a toy car with Evothings Studio, Raspberry Pi and your mobile device.

Eric SvenssonBlogs, Tutorials

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+

Using Evothings Studio and with only basic JavaScript code, node.js, socket.io, a Raspberry Pi and simple electronics, we were quickly able to gain control of a cheap radio controlled toy car with the use of our mobile device.

Check out the video below, and if you are interested in how we managed to do this within a couple of hours – keep reading. You are also very welcome to talk with us about these kinds of projects or application development in our forums.

The car was originally controlled with a radio remote control and the goal was to instead use our mobile devices. We decided to use the socket.io JavaScript library to send messages to a node.js server running on the Raspberry Pi, which then sent electrical signals to the original remote control. Evothings Studio enabled us to instantly start developing and testing our application.

The mobile device was connected to the Raspberry Pi through an Ad-Hoc WiFi network and while we initially expected some latency issues, we were happy to see how well everything worked, especially JavaScript, on both devices. No substantial delay could be noticed between pressing a button in the mobile application and getting a response from the car as the video shows. We give credits to the WebSockets implementation for this.

Our application was written in simple HTML5, CSS and JavaScript. We created a joystick using an existing library and made it send directional messages to the node.js server. The socket.io library made this task really simple. Look at the code below to see this for yourself.

Client HTML code:

<h1>Raspberry Car</h1>
<div id="canvas_container">
    <canvas id="stage" width="1024" height="768"></canvas>
</div>

Client JavaScript code:

var socket = io.connect('http://192.168.2.2:8085')
socket.on('connect', function() 
{
    hyper.log('connected!')
})

var thumbStick = new ThumbStick()
thumbStick.init()
$(window).resize(thumbStick.onResizeCanvas.bind(thumbStick))

thumbStick.addEventListener(
    ['UP', 'DOWN', 'LEFT', 'RIGHT',
    'STOPUP', 'STOPDOWN', 'STOPLEFT', 'STOPRIGHT'],
    function(directionEvent) 
    {
        socket.emit('direction', directionEvent)
    })

A simple script on the server received the messages and controlled the Raspberry Pi’s GPIO port. By using a salvaged floppy disk cable we connected this port to a simple electronic circuit composed of only four resistors and four transistors. The transistors were used as switches that caused the buttons on the original remote control to be activated, which sent radio signals to the car.

Server JavaScript code:

io.sockets.on('connection', function (socket) 
{
    // Listen for direction messages from the app.
    socket.on('direction', function (data)
    {
        console.log('received direction: ' + data)

        // Choose the right command for sending a signal to the car's remote.
        command = ''
        if ( data === 'UP')
            command = 'echo 1 > ' + path + 'gpio7/value'
        else if ( data === 'DOWN')
            command = 'echo 1 > ' + path + 'gpio8/value'
        else if ( data === 'LEFT')
            command = 'echo 1 > ' + path + 'gpio9/value'
        else if ( data === 'RIGHT')
            command = 'echo 1 > ' + path + 'gpio11/value'
        else if ( data === 'STOPUP')
            command = 'echo 0 > ' + path + 'gpio7/value'
        else if ( data === 'STOPDOWN')
            command = 'echo 0 > ' + path + 'gpio8/value'
        else if ( data === 'STOPLEFT')
            command = 'echo 0 > ' + path + 'gpio9/value'
        else if ( data === 'STOPRIGHT')
            command = 'echo 0 > ' + path + 'gpio11/value'

        // Execute the command to send the signal to the car's remote.
        exec(command, function (error, stdout, stderr) 
        {
            if (error !== null)
                console.log('exec error: ' + error)
        })
    })

})

The full source code is available in our Evothings Gallery github repository.

In upcoming projects we will take the challenge further and try to find more ways like this to have great fun while gaining useful experience tinkering with hardware like this. Maybe we will control an industrial robot next time, how about that? Let us know what you would be interested in seeing. Keep a watch on our blog at evothings.com for more videos, code and upcoming tutorials.

Photos:


App developed using Evothings Studio, simply composed of a thumbstick.


All equipment used in the project.