Advertisement
Midge

Windows Phone to Arduino via Touchdevelop

Oct 31st, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. # Proof of concept Python script to receive POST data (over network) from a Touchdevelop app to send to an Arduino
  2. # Tested/developed on Python version 2.7.3
  3.  
  4. """ NOTE: I'm a newbie Python programmer, the server bit's mostly cut n paste from elsewhere and the POST data processing to Arduino bit
  5.    was learnt on the fly. This script is supplied as is, feel free to chop, change point and laugh etc but I cant't supply much
  6.    in the way of support.
  7.    Midge
  8. """
  9.  
  10. from BaseHTTPServer import BaseHTTPRequestHandler
  11. # Set serial Arduino port
  12. # You'll need the PYSerial module if it's not already installed
  13. import serial
  14. ser = serial.Serial('COM3', 9600,) #Note COM3,
  15. ser.write(chr(0)) #reset Arduino LED's on startup
  16.  
  17. #Set Port to listen on
  18. my_port = 90
  19.  
  20. buttonstatus = 0 #declare buttonstatus
  21.  
  22. # datasend fnc receives POST data and sends it to the Arduino oveer Serial
  23. def datasend(input):
  24.        
  25.         global buttonstatus
  26.         decimal_val=int(input) # turn POST data to a number (int)
  27.         if decimal_val==255: #shake to reset
  28.          print("Phone Shook (reset)") # Debug info
  29.          blank=chr(0)
  30.          ser.write(blank) # Write 0 to Arduino, turn off LED's
  31.          buttonstatus=0 # reset values
  32.          
  33.        
  34.  
  35.         else:
  36.          
  37.          buttonstatus ^= (decimal_val) # Take incoming data and XOR it with current (to toggle LED value)
  38.          print 'Data sent (binary) ' + (bin(buttonstatus)) # Debug info
  39.          send_val=chr(buttonstatus) # Convert to char**
  40.          ser.write(send_val) # Send data over serial to Arduino
  41.  
  42. """ ** This was a bloody pain to get working!, it turned out that Pyserial sends data as strings so my lovely binary numbers
  43.     were being mangled before being sent. The workaround is to convert the number into the corresponding character and send that,
  44.     The Arduino is set to read a (one byte) char from serial.
  45. """
  46.  
  47. class PostHandler(BaseHTTPRequestHandler):
  48.    
  49.     def do_POST(self):
  50.         content_len = int(self.headers.getheader('content-length'))
  51.         post_body = self.rfile.read(content_len) # Read Post data
  52.         datasend(post_body) # Call datasend function and pass POST data
  53.        
  54.        
  55.         # Below commented out, no response set to phone
  56.         # Begin the response
  57.         #self.send_response(200)
  58.         #self.end_headers()
  59.         #self.wfile.write('Client: %s\n' % str(self.client_address))
  60.         #self.wfile.write('Path: %s\n' % self.path)
  61.         #self.wfile.write('Form data:\n')
  62.  
  63.        
  64.  
  65.        
  66.         return
  67.    
  68.    
  69.  
  70.  
  71. if __name__ == '__main__':
  72.     from BaseHTTPServer import HTTPServer
  73.     server = HTTPServer(('', my_port), PostHandler)
  74.     print 'Starting server, use <Ctrl-C> to stop'
  75.     server.serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement