29
Using WebSockets and Android to control robots in real-time Sudar Muthu (@sudarmuthu) http://hardwarefun.com http://github.com/sudar

Using websockets and android to control robots in realtime

Embed Size (px)

Citation preview

Using WebSockets and Android to control robots

in real-timeSudar Muthu (@sudarmuthu)

http://hardwarefun.comhttp://github.com/sudar

http://hardwarefun.com

Research Engineer by profession Create robots as hobby Uses an Android phone Why not combine both of them? Has spoken in 6+ hasgeek events

Who am I?

http://hardwarefun.com

There are at least half dozen ways to do it and I am not going to talk about each of them.

http://sudarmuthu.com/blog/building-robots-using-arduino-and-android-at-droidcon

Android and Arduino

http://hardwarefun.com

WebSockets in AndroidHow to use it to control robots

So today…

http://hardwarefun.com

Why hardware for a software hacker?

Fun!

http://hardwarefun.com

Demohttp://hardwarefun.com/projects/asimijs

Let’s start with a demo

http://hardwarefun.com

Participate in the demo

Visithttp://hardwarefun.com:3000

http://hardwarefun.com

http://hardwarefun.com/projects/asimijs

Control the bot at stage

#asimijs @hardwarefun

http://hardwarefun.com

Now let’s see how I built it

http://hardwarefun.com

Full-duplex over a single TCP connection Provides push support Designed to work inside browsers Works on port 80

What is WebSockets?

http://hardwarefun.com

WebSockets vs HTTP

http://hardwarefun.com

http://hardwarefun.com

Server Client

Is there an update?

Is there an update?

Is there an update?

No

No

Yes

HTTP (Pull)

http://hardwarefun.com

Server Client

Tell me when you have an update

Here is the update

Ok

WebSockets (Push)

http://hardwarefun.com

Firewall friendly No need to reinvent protocol Real-time communication Easy to setup server

Why use WebSockets in Android

http://hardwarefun.com

Remember, I am not talking about using WebSockets inside an Android

browser

http://hardwarefun.com

There are lot of options, but I choose Socket.IO implementation by Kaushik Dutta (Rom

Manager fame)

https://github.com/koush/android-websockets

WebSocket implementation for Android

http://hardwarefun.com

You should have attended jsFoo ;)

Why Socket.IO?

http://hardwarefun.com

Additional features on top of WebSockets Node.js based server Has support for maximum no: of browsers Has clients in lot of languages

Why Socket.IO

http://hardwarefun.com

I had the server already written

and ….

http://hardwarefun.com

Let’s see some code

http://hardwarefun.com

client = new SocketIOClient(URI.create(”http://hardwarefun.com:3000"),new SocketIOClient.Handler() { @Override public void onConnect() { Log.d(TAG, "Connected!"); }

@Override public void on(String event, JSONArray arguments) { Log.d(TAG, "Got event); }

@Override public void onDisconnect(int code, String reason) { Log.d(TAG, "Disconnected!); }});

In Android, create a socket

http://hardwarefun.com

client.connect();

JSONArray arguments = new JSONArray();arguments.put("first argument");JSONObject second = new JSONObject();second.put("dictionary", true);arguments.put(second)client.emit("hello", arguments);

client.disconnect();

Send data through socket

http://hardwarefun.com

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on(’my other event', function (data) { console.log(data); });});

In node.js side

http://hardwarefun.com

Checkout the full source code at http://hardwarefun.com/projects/asimijs

http://hardwarefun.com

How the demo worked?So…

Node.js Server

Admin page

User pageUser page

User page

Android Client

Asimi (Bot)Web Socket

ADK

http://hardwarefun.com

http://hardwarefun.com

AsimiJS – The demo that I showed initially http://hardwarefun.com/projects/asimijs

Android websockets - https://github.com/koush/android-websockets

Getting started with hardware programming http://hardwarefun.com/tutorials/getting-started-with-hardware-programming

Getting started with Arduino http://hardwarefun.com/tutorials/getting-started-with-arduino-and-avr

Android and Arduino - http://sudarmuthu.com/blog/building-robots-using-arduino-and-android-at-droidcon

Arduino Workshop - http://hardwarefun.com/arduino-workshop

Links

Thank youSudar Muthu

http://hardwarefun.comhttp://github.com/sudar