Using websockets and android to control robots in realtime
Nov. 2, 2012•0 likes
7 likes
Be the first to like this
Show More
•13,266 views
views
Total views
0
On Slideshare
0
From embeds
0
Number of embeds
0
Download to read offline
Report
Technology
Talk about using WebSockets in Android to control robots in real-time. More details at http://sudarmuthu.com/blog/using-websockets-with-android-to-control-arduino-robots
Using websockets and android to control robots in realtime
Using WebSockets and Android
to control robots in real-time
Sudar Muthu (@sudarmuthu)
http://hardwarefun.com
http://github.com/sudar
Who am I?
Research Engineer by profession
Create robots as hobby
Uses an Android phone
Why not combine both of them?
Has spoken in 6+ hasgeek events
http://hardwarefun.com
Android and Arduino
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
http://hardwarefun.com
So today…
WebSockets in Android
How to use it to control robots
http://hardwarefun.com
What is WebSockets?
Full-duplex over a single TCP connection
Provides push support
Designed to work inside browsers
Works on port 80
http://hardwarefun.com
HTTP (Pull)
Is there an update?
No
Is there an update?
No
Is there an update?
Yes
http://hardwarefun.com
WebSockets (Push)
Tell me when you have an update
Ok
Here is the update
http://hardwarefun.com
Why use WebSockets in Android
Firewall friendly
No need to reinvent protocol
Real-time communication
Easy to setup server
http://hardwarefun.com
Remember, I am not talking about
using WebSockets inside an
Android browser
http://hardwarefun.com
WebSocket implementation for
Android
There are lot of options, but I choose Socket.IO
implementation by Kaushik Dutta (Rom Manager fame)
https://github.com/koush/android-websockets
http://hardwarefun.com
Why Socket.IO?
You should have attended jsFoo
;)
http://hardwarefun.com
Why Socket.IO
Additional features on top of WebSockets
Node.js based server
Has support for maximum no: of browsers
Has clients in lot of languages
http://hardwarefun.com
and ….
I had the server already written
http://hardwarefun.com
In Android, create a socket
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!);
}
});
http://hardwarefun.com
Send data through socket
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();
http://hardwarefun.com
In node.js side
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);
});
});
http://hardwarefun.com
Checkout the full source code
at
http://hardwarefun.com/projects/asimijs
http://hardwarefun.com
Node.js Server
Android Client
Admin page
Web Socket User page Asimi (Bot)
User page
ADK User page
http://hardwarefun.com
Links
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
http://hardwarefun.com
Thank you
Sudar Muthu
http://hardwarefun.com
http://github.com/sudar