TAGS :Viewed: 3 - Published at: a few seconds ago

[ HTML variables to Python file ]

I am building a remote control car using the Raspberry Pi, and it runs off of a Python script perfectly from an ssh keyboard input. I want it to be able to run off of a phone's gyroscope. I created an Apache web server and used the source code from this: http://www.html5rocks.com/en/tutorials/device/orientation/deviceorientationsample.html to try and get the gyroscope information. I am correctly receiving the input on the web page and it is displaying/ moving the image properly. I now need to export the 3 variables to a python script that I am running on the same Raspberry Pi. I don't know where to begin as to what to use to export these variables across the two platforms. Also the variables need to be able to update every 10ms. I don't know if this is the best way to set this up or if using something like Node.js would be easier? Any help would be greatly appreciated!

Answer 1


There are lots of ways to skin this, although many might have more latency than you'd like (10ms should be a reasonable goal though i don't know enough about the phone side to know if it generates data at that rate) but the challenge will be getting the data off the phone, presumably over a wireless link to the RPi, and then processed in a timely fashion. An advanced method with lower latencies (which I'm not going to try to write here) would use something like node.js with web sockets listen for updates from the web page (which then requires modifying the webpage code to use websockets) and also do whatever your python script is doing. A quicker, dirtier, but pure python solution (plus a little javascript) would look something like:

  1. Configure apache to use cgi
  2. Drop the following code in your cgi-bin directory
import socket
import cgi

form = cgi.FieldStorage
x = form["x"]
y = form["y"]
z = form["z"]
s = socket.socket()
s.connect(("localhost",5000))
s.send(",".join([x,y,z]))
print "Content-Type: text/text"     
print                               
print "Debug: sent",x,y,z
  1. In your javascript code, use jquery to, every 10 milliseconds make a request to http:///cgi-bin/dumpdata.cgi?x=&y=&z= (or whatever values you want to pass through.
    EDIT to add ajax example: Assuming you used the sample code you linked to, somewhere inside the tag, add <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> (or download jquery and run on your webserver, which might be preferable if you're going to run everything over wifi)

    In your <body>, optionally create a <div id="debug">Waiting for response</div>

    And then in the deviceOrientationHandler function, you should be able to do something like

  url = "/cgi-bin/dumpdata.cgi?x="+tiltLR+"&y="+tiltFB+"&z="+dir;
$.ajax({url: url, success: function(result){
        //result will hold the results passed back by the CGI
        $("#debug").html(result);
    }});
  1. In your python script (which I'm assuming has an event loop), have it do something like:
   import socket
   s = socket.socket()
   s.bind(("",5000))
   s.listen(5)
   while True:
      cli,addr = s.accept()
      x,y,z = cli.recv(1000).split(",")
      print "DEBUG",x,y,z
      cli.close()
      # do the rest of your loop