Permalink
Browse files

Initial commit of RemoteSensors

  • Loading branch information...
0 parents commit 821c056b2e858e80f07f4e329339e863cd000bf5 @Jonty committed Nov 17, 2009
@@ -0,0 +1,27 @@
+RemoteSensors is a small python daemon that runs under the Android Scripting Environment.
+It's purpose is to explore the options that become available for device integration when the sensors
+available on Android devices are exported for use in external applications.
+
+The daemon can run in several modes and is fairly simple, I suggest reading the code is the best
+documentation for this - it's very small.
+
+It ships with one example app, a javascript bookmarklet to be used on Google Maps pages that
+interactively scrolls the map by tilting the Android device.
+
+
+Instructions for demo use:
+
+Download remoteSensors.pl and place it in the Android Scripting Environments scripts directory on
+the SD card of the Android device. Connect the device to the same wifi network as the computer
+running google maps.
+
+Start remoteSensors.pl from within the Android Scripting Environment, after a few seconds it will
+give you an IP address and port.
+
+Save the following line as a bookmarklet in your browser:
+javascript:var%20e=document.createElement('script');e.setAttribute('language','javascript');e.setAttribute('src','http://www.jonty.co.uk/bits/GoogleMapSensors.js');document.body.appendChild(e);void(0);
+
+Browse to a google map, hit the bookmarklet, and when prompted enter the IP:PORT given to you
+by remoteSensors.pl
+
+Tilt your device and scroll about!
@@ -0,0 +1,36 @@
+function RemoteSensors (params) {
+
+ this.fetch = function () {
+ while (script = document.getElementById('JSONP')) {
+ script.parentNode.removeChild(script);
+ for (var prop in script) {
+ delete script[prop];
+ }
+ }
+
+ var node = document.createElement('script');
+ node.setAttribute('language','javascript');
+ node.setAttribute('id','JSONP');
+ node.setAttribute('src', 'http://' + this.host + '/?jsonp=' + this.callback + '&rand=' + Math.random());
+ document.body.appendChild(node);
+ };
+
+ this.getHost = function () {
+ host = prompt("Please enter the host:port of the sensor server", "");
+ if (host) {
+ this.host = host;
+ } else {
+ this.getHost();
+ }
+ };
+
+ // Initial
+ this.callback = params.callback;
+ if (params.host) {
+ this.host = params.host;
+ } else {
+ this.getHost();
+ }
+
+ this.fetch();
+}
Binary file not shown.
@@ -0,0 +1,48 @@
+function RemoteSensors (params) {
+
+ this.fetch = function () {
+ while (script = document.getElementById('JSONP')) {
+ script.parentNode.removeChild(script);
+ for (var prop in script) {
+ delete script[prop];
+ }
+ }
+
+ var node = document.createElement('script');
+ node.setAttribute('language','javascript');
+ node.setAttribute('id','JSONP');
+ node.setAttribute('src', 'http://' + this.host + '/?jsonp=' + this.callback + '&rand=' + Math.random());
+ document.body.appendChild(node);
+ };
+
+ this.getHost = function () {
+ host = prompt("Please enter the host:port of the sensor server", "");
+ if (host) {
+ this.host = host;
+ } else {
+ this.getHost();
+ }
+ };
+
+ // Initial
+ this.callback = params.callback;
+ if (params.host) {
+ this.host = params.host;
+ } else {
+ this.getHost();
+ }
+
+ this.fetch();
+}
+
+var rs = new RemoteSensors({callback: 'gotData'});
+function gotData(data) {
+
+ var url = gApplication.getPageUrl();
+ var zoom = url.match('z=([0-9]+)');
+
+ var map = gApplication.getMap().getCenter();
+ gApplication.getMap().panTo(new GLatLng(map.lat() + (data['pitch'] / (zoom[1] * zoom[1] * 100)), map.lng() - (data['roll'] / (zoom[1] * zoom[1] * 100))));
+
+ rs.fetch();
+}
@@ -0,0 +1 @@
+<a href="javascript:var%20e=document.createElement('script');e.setAttribute('language','javascript');e.setAttribute('src','http://www.jonty.co.uk/bits/GoogleMapSensors.js');document.body.appendChild(e);void(0);">Google Maps Scroll Bookmarklet</a>
@@ -0,0 +1,65 @@
+import android, time, BaseHTTPServer, urlparse
+
+print "\nStarting sensors...\n"
+droid = android.Android()
+droid.startSensing()
+time.sleep(2)
+
+class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
+
+ # Disable logging DNS lookups
+ def address_string(self):
+ return str(self.client_address[0])
+
+ def do_GET(self):
+
+ url = urlparse.urlparse(self.path)
+ params = urlparse.parse_qs(url.query)
+
+ delay = 1
+ if 'delay' in params:
+ delay = float(params['delay'][0])
+
+ self.send_response(200)
+ self.send_header("Content-type", "application/x-javascript; charset=utf-8")
+ self.end_headers()
+
+ try:
+ while True:
+ sensors = droid.readSensors()
+ data = sensors['result']
+
+ if 'jsonp' in params:
+ self.wfile.write("%s(" % (params['jsonp'][0]))
+
+ output = []
+ for type in data:
+ output.append("\"%s\": \"%s\"" % (type, data[type]))
+
+ self.wfile.write("{%s}" % (','.join(output)))
+
+ if 'jsonp' in params:
+ self.wfile.write(');')
+
+ self.wfile.write("\n")
+ self.wfile.flush()
+
+ if 'continuous' not in params:
+ break
+
+ time.sleep(delay)
+
+ except socket.error, e:
+ print "Client disconnected.\n"
+
+
+PORT = 8000
+httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
+
+import socket
+s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+s.connect(('google.co.uk', 80))
+sData = s.getsockname()
+
+print "Serving at '%s:%s'" % (sData[0], PORT)
+httpd.serve_forever()

0 comments on commit 821c056

Please sign in to comment.