Remote Controlling a Car over the Web. Ingredients: Smartphone, WebSocket, and Raspberry Pi.

At Kaazing, we have been experimenting with using a smartphone as a remote control for quite some time now. Those familiar with our demos may have seen our Zing-Pong demo (which is a “Pong”-style game using smartphones to control the paddles over WebSocket) or our Racer demo (which is a 3D Formula One car rendered in WebGL as a Chrome Experiment, and remotely controlled with a smartphone). These demos, along with the other demos we’ve created with Kaazing’s platform, use no plug-ins. You simply point a browser on your computer to the address of the object you want to control (for example, the gorgeous Formula One car rendered in WebGL) and a browser from your smartphone to the address of the remote control (an ID we generate for you). Once connected, you just…go!

A couple of our Visionary Zingers, Prashant Khanal and David Witherspoon, then experimented with communicating with a Raspberry Pi using WebSocket–or, more specifically, using Kaazing’s JMS-over-WebSocket Gateway to tell Raspberry Pi to turn on and off a lightbulb. When the lightbulb turned on in this experiment, another lightbulb turned on: could we in fact turn a remote, radio-controlled car into a remote, WebSocket-controlled car? By controlling the car over the Web, could we control the car from another room? From another continent?

The Racer Demo

Let’s first take a closer look at the Racer demo. The Racer demo uses the reflector pattern: where we connect devices through a WebSocket server, thus simulating peer-to-peer connectivity, a very easy-to-achieve task using pub/sub. When we developed the Racer demo, we walked through the steps to build it in The Simplest Way to Use Your Smartphone as a Game Controller: A WebSocket Race Car Demo. By examining how to use pub/sub concepts to control a virtual car, it’s not so farfetched to consider doing the same with a radio-controlled car.

The Goal

After working with the Racer demo, we knew what we wanted to achieve: controlling a “real” car (or at least a good-sized monster truck model) with a simple web app on a smartphone. Something like this, for example:


And something that might move like this:

In just a few hours of hacking and experimenting, we turned this radio-controlled car into a WebSocket-controlled car. Don’t worry, no actual cars (or bunnies) were harmed in the making of this demo, as you’ll see as we walk through the hardware and software we used.

Ingredients: The Hardware

  • One RC car. We ended up using a black F150. The bigger the car, the easier it is to connect, position, and hide the building blocks. One thing to pay attention to is that the car you’re about to buy has to have two simple motors, one that drives the vehicle forward/backward, and another one that steers the car: front wheels left/right. The first thing you’ll get rid of is the radio control, so don’t worry about that too much.
  • One Raspberry Pi, model B. This piece of hardware is incredible. Runs Linux (and Java), has 512MB RAM, an Ethernet port, 2 USB ports (for keyboard, mouse, WiFi), takes an SD card, has an HDMI out, and has a handful of GPIO (General Purpose Input/Output) pins to drive external devices, like lights, motors, and alike.
  • One small breadboard. This simplifies connecting the building blocks.
  • Set of male to male and male to female jumper leads.
  • Edimax USB Wireless adapter. Follow simple setup instructions.
  • L298N Board: It is used to control the car motors. The car is equipped with two motors – one for steering and the other to drive wheels. Each motor is controlled  through two GPIO pins.
  • A couple of LEDs – optional. This RC car model didn’t come with any lights, but who has seen a serious RC car without lights – so we equipped ours with remote controllable LEDs.
  • One smartphone (can be substituted by a tablet, or any Web browser, really). Using a touch-based device makes the controlling more natural, but you can use any laptop/desktop browser with mouse/trackpad as well.
  • Battery. Anker Astro 5600 mAh external battery pack powering the Raspberry Pi and connected circuits: LEDs and relays.

The circuit diagram regarding the wiring among the Raspberry Pi GPIO pins, L298N board and the car battery can be found here. The pin numbers specified for the Raspberry Pi in the circuit diagram refers to the physical pin location.

Ingredients: The Software

  • WebSocket Server. We used Kaazing’s high performance enterprise grade WebSocket Gateway – JMS Edition. The JMS Edition provides an easy-to-use pub/sub abstraction, along with support for multiple WebSocket client technologies (of which we used the JavaScript and Java clients in the project). The WebSocket Gateway ships with Apache ActiveMQ, an open source message broker.
  • WebSocket JavaScript client code. The Kaazing JavaScript client code runs in the browser on the smartphone (or any other web browser), playing the role of the remote control. The user’s actions trigger events on the remote control and result in messages being sent to the WebSocket server.
  • WebSocket Java client code. The Kaazing Java client runs on the Raspberry Pi, receiving messages from the WebSocket server, and instructing the Raspberry Pi GPIO pins to control the car.  Control of the GPIO pins is achieved through the Pi4J Library.

Reviewing the Hardware

We decided to hide all the electronics in the body of the car, but wanted to have the Raspberry Pi accessible and more importantly, visible. It’s sitting on the truck’s plateau, lightly taped to it, so it doesn’t fall off as the truck accelerates and comes to sudden stops (read: bumps into things).


On the right you can see the WiFi plugged into the top USB port (blue LED). On the left, you see the blue SD card. The GPIO ports are on the top left, connected to drive the two motors and the lights of the truck. The Raspberry Pi is powered through a micro USB port on the bottom left, leveraging a smartphone quick charger battery.

IMG_6020Under the “hood”, you can see the relay, and the breadboard connecting to the Pi, and the motors of the truck, as well as the front LEDs.

Reviewing the Software

To enable the Raspberry Pi to receive WebSocket messages from the browser client, we had to write some logic that runs on the Pi.


The two clients, the smartphone remote control, and the code running on the Raspberry Pi, communicate with each other via WebSocket. In addition to JavaScript clients, Kaazing supports Java, .NET, and Flash/Flex clients. We chose the Java client for the Pi. The communication between these clients is seamless. The back-end message broker, which provides the messaging services is Apache ActiveMQ, but could be another JMS message broker like TIBCO’s, Informatica’s, or IBM’s, as well.


JavaScript Remote Control Code on the Smartphone

Since we plan to use this demo at trade shows, we wanted to make sure that we have a certain level of control over who controls the car and when. Therefore, we use a “secret” key that the remote control has to know to control the car. This key is sent to the code running on the Pi via a secured admin topic. Every message coming from the smartphone has to contain the same key. If it doesn’t, the message is thrown away.

The remote control client on the smartphone first asks for the key that identifies who can control the car.


Then, the HTML5 remote control code, which runs on the smartphone, first establishes a connection to the WebSocket server. It creates a JMS topic, and a producer (or publisher) for the topic. When done, the client is ready to start sending messages through the WebSocket gateway to the Java code running on the Pi. Every message contains the previously specified key as well, as a custom JMS message property.

The images rendered for the remote control UI in the browser have JavaScript event listeners defined. The listeners are invoked both when the images are touched (touchstart), as well as when the images as released (touchend). The JavaScript client code sends messages as frequently as it can while one or more buttons are pressed, and one message as a control icon is let go.


If you want to learn more and understand the JavaScript JMS APIs, try out the Step-by-Step Tutorial of Building a Simple Peer-to-Peer WebSocket Application tutorial or our API documentation.

Java Code Running on the Raspberry Pi

We chose to program the Raspberry Pi using Java.  Instructions to install Java on the Raspberry Pi can be found here.  The Java code on the raspberry requires both the JMS Kaazing Websocket Library to receive messages from the client, and the Pi4J library to interact with the car through the Rasbperry Pi’s GPIO .

The code is split into two main classes, CommandReceiver and Car.  CommandReceiver is responsible for connecting to the Kaazing JMS Gateway and listening to both on the command and the admin topic.  When admin messages are received the CommandReceiver will record which user has valid access to control the car.  When a command message is received, its properties will be checked to see if it is the current user in control of the car. If so, the message will be parsed and read, resulting in a corresponding method being called on the Car class.

The Car class is an abstraction of everything the car may do.  It has methods such as Steering, Thrust, and Lights that are called with Command parameters such as ON, OFF, LEFT, and RIGHT. When called, the GPIO inputs corresponding to each method and are switched on or off, thereby resulting on the motors of the car moving forward, backwards, or steering left or right.

To check out or download the full source code visit KaazingPi on Github.

The Result

Well, we cheated and already showed the video of the result at the beginning. But, if you want to see it in person, come see us at one of our upcoming events:

This entry was posted in Events, Gaming, html5, JMS, Kaazing, WebSocket and tagged , , , , , , , , , , . Bookmark the permalink.

53 Responses to Remote Controlling a Car over the Web. Ingredients: Smartphone, WebSocket, and Raspberry Pi.

  1. Pingback: Highlights fra HTML5 Developer Conference | VERTICA BLOG

  2. johngslater says:

    A great and very clear read. Some things that might be obvious to people who deal with hardware:

    To understand the java code running on the pi:

    it helps to know the mapping from the code’s pin numbers to the physical pins on the pi:

    I am not certain why the SPI MOSI and SPI MISO pins on the pi’s GPIO port were used to control the car’s motor.

    The SainSmart L298N Dual H Bridge that you use does not seem to have documentation
    You clearly show the RC car’s 9.6v battery pack hooks up to the VCC and GND terminals:×768/

    The Anker Astro 5600 mAh external battery pack connects to the pi’ micro-USB port and powers the pi.

  3. L Shaw-McMinn says:

    Thanks what an excellent project. I am thinking about doing something similar using this as a guide, but I will also be mounting a webcam.

  4. Cool integration. If you want to use a ready gamepad in the smartphone side you can integrate very easily with muzzley ( with gamepad widget (for example).

    Check this video where you can see the gamepad widget in action:

  5. Edu says:

    Cool integration. If you want to use a ready gamepad in the smartphone side you can integrate very easily with muzzley ( with gamepad widget (for example).

    Check this video where you can see the gamepad widget in action:

    • Hey Eduardo – hope you’re doing well… It was nice to see you in Colorado. What are you guys up to these days?

      • EduEduardo says:

        Hi Peter,

        I hope your are doing great.
        Let’s have chat one of these days. I will send you an email to grab a coffee in Bay Area.

        See you!!!

      • Edu says:

        Hi Peter, I hope you’re doing great :) Let’s grab a coffee one of these days in the Bay Area. I will send you an email. Continue your great work!!! Go Kaazing…! :)

  6. Pingback: Kaazing controls car with a smartphone. What comes next? | PandoDaily

  7. Pingback: Kaazing controls a car with a smartphone. What comes next? | Dumper

  8. Pingback: A Connected Car Project: Navigation Sharing with WebSocket | The Zinger

  9. Pingback: Raspberry Pi RC Car can be controlled from anywhere in the world — Tech & Gadgets

  10. Pingback: Web Design and Coding Tutorials – May 12th 2013 : Facebook Open Graph and Responsive Emails | DDMBOSS

  11. I want to buy Kaazing controls a car with a smartphone but need to run with blackberry z10.
    Can I control this with Blackberry ?
    if yse then what will prize for this product ?

  12. Pingback: Controlling Physical Devices on the Real Time Web: Kaazing IoT Talk at JavaOne 2013 | The Zinger

  13. Hiro says:

    excellent integration. Excuse my ignorance, is it necessary to run also What is the URL for the customer? Is it integrated javascript/HTML5 code (arrow keys) to control the car?

  14. Magdi El-Kelani says:

    nice work , but i’m a bit confused about powering the R pi and L298n h bridge .
    The Anker Astro 5600 mAh external battery pack connects to the pi’ micro-USB port and powers the pi. but how about the L298N H bridge and 2 motors ??

  15. Pingback: Useful Links | Özgür Aytekin's Personal Blog

  16. Riyaz says:

    hello Peter. great work. very helpful
    but im in search of a project where i need only the position of a simple remote car on an arena and im gonna use that position details to make a racing game using a simple GUI system. its like playing a racing game in a pc with the help of real car on the arena which we build. hope u understand my query. please help me regarding this

  17. Obin says:

    could this be done in Python?

    • The WebSocket standard defines the communication wire protocol, therefore it’s language independent; that is there’s nothing that would stop you from “speaking” WebSocket from Python.

  18. Old Alex says:

    Could you explain the hardware hookup for steering, looking at the code you just use the h bridge, many use servos with pulses to control the position. So how do you make that happen from what I see your system must auto center, “hard left” power one way, hard right power the other, power off returns to center. If not pleas let me know how you converted it.

  19. Pingback: Auto per Webanwendung fernsteuern | boblog

  20. Rupesh Bhochhibhoya says:

    Thank for the great explanation and presentation. It is the best page on integrating rpi with RC car. I am a beginner and I have a question. Do we really need L298N Board to control the RC car? Can’t we connect rpi GPIO directly to remote PCB or if not to PCB on car itself?

    • [posting answer on behalf of David Witherspoon]

      Hi Rupesh,

      We used the L298N because we wanted direct control of the motors and the GPIO did not have the power/voltage to drive the motors independently.

      The PCB that comes with the car/remote will be used to transfer RC signals into motor movements. So hypothetically you could connect it to the PCB if you had a voltage meter and you measured the voltages at the Vias/terminals when the remote control was engaged in forward / left right actions. You could then map the Vias to GPIO, this is all assuming Voltages match up and the Vias are exposed, and the PCB board is digital and not analog.

      • Rupesh Bhochhibhoya says:

        Thank you for clearing this out. I just wanted to build one with minimal addition of any devices. But I like the way you have that eliminates the rc controller.

        thank you again.

      • No problem, Rupesh. Feel free to reach out to us if you have further questions… We’re curious to hear how your project is going.

  21. Pingback: Remote Controlling a Car over the Web. Ingredients: Smartphone, WebSocket, and Raspberry Pi. | 링크잇 컬렉션 :: Link It Collection

  22. Al B says:

    Nice article! We built a similar project. However, we installed an Android device onto a RC car and used an IOIO board instead to control it autonomously. Here is the preliminary result:

  23. Arjen Van den Broeck says:

    Hello ,

    Very nice project. I’m interesting in something like that. I am a guy from Belgium. I’m 17years old and I have to make next year in my 6th year of secondary school an end project. I study Industrial science. I have search a lot on the internet for information but I’m not become smarter because there are so many possibilities. It’s not a easy project but I think I can surprised them with a project like this. I was thinking about a RC car with streaming or maybe controlling over the internet. I’m not sure how I want to control the car and which software. What is your experience with raspberry and Arduino or another possible one?

    Already thanks for the information.
    Arjen Van den Broeck

  24. Ed GeorgeEd says:

    Awesome project, but how did you connect the 9v Battery of the car to the L298n H bridge? I am trying to do that step currently and I am not sure how to isolate it and power the bridge. Did you literally just run a wire from the battery contacts to the bridge?

  25. gajettonl says:

    Sounds pretty dangerous to control a car with your phone

  26. Pingback: The top 10 realtime web apps | Gyankosh – Inspire !! Fun !! Innovate !!

  27. GP says:

    Hi Peter,

    I am having trouble with my L298H bridge, the front motor is working but turns to right irrespective of which GPIO is set to High. I also powered the board with the 5V from the Pi, not sure if that makes a difference because without this the LED on the board wasn’t lighting up.

    The thrust pins aren’t working for me at all.


  28. piscis says:

    Hi Peter,
    thanks for the excellent tutorial. I had trouble to find a New Bright RC car in germany, so I used a Carrera RC Raptor F-150 and was able to control the car via NodeJS / Websockets and a HTML frontend. There is a video of the testdrive:

    • Wow, this is impressive! Thanks for sharing it! You’re really good at controlling your car! Were you using a go pro to record it? And just out of curiosity, what’s the white circular thing on your right? Are you keeping your baby in there?

  29. Pedro says:

    I guess I must be a real noob…. how exactly do I link the files to get the pi to link to the web and have a running rc car?

  30. ziznzkak says:

    Hi Peter I’m very impressed with what you guys have done!
    I have a question to ask.
    I’m looking for how you guys connect all the building blocks to the breadboard.
    I can’t find any information about that.
    And is it possible to give me some links for an instruction how to connect pi between L298N board and the breadboard?
    And one more thing! I don’t need to set the ESC(Electronic Speend Controller)?
    Once again, Nice work!,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s