#!/usr/bin/env python # coding: Latin-1 # Creates a web-page interface for UltraBorg # Import library functions we need import UltraBorg import time import sys import threading import SocketServer # Settings for the web-page webPort = 80 # Port number for the web-page, 80 is what web-pages normally use displayRate = 2 # Number of times to read the ultrasonic readings per second sliderHeight = 800 # The number of pixels high to make the sliders # Setup the UltraBorg global UB UB = UltraBorg.UltraBorg() # Create a new UltraBorg object UB.Init() # Set the board up (checks the board is connected) # Class used to implement the web server class WebServer(SocketServer.BaseRequestHandler): def handle(self): global UB # Get the HTTP request data reqData = self.request.recv(1024).strip() reqData = reqData.split('\n') # Get the URL requested getPath = '' for line in reqData: if line.startswith('GET'): parts = line.split(' ') getPath = parts[1] break if getPath.startswith('/distances-once'): # Ultrasonic distance readings # Get the readings distance1 = int(UB.GetDistance1()) distance2 = int(UB.GetDistance2()) distance3 = int(UB.GetDistance3()) distance4 = int(UB.GetDistance4()) # Build a table for the values httpText = '' if distance1 == 0: httpText += '' else: httpText += '' % (distance1) if distance2 == 0: httpText += '' else: httpText += '' % (distance2) if distance3 == 0: httpText += '' else: httpText += '' % (distance3) if distance4 == 0: httpText += '' else: httpText += '' % (distance4) httpText += '

None

%04d

None

%04d

None

%04d

None

%04d

' self.send(httpText) elif getPath.startswith('/set/'): # Servo position setting: /set/servo/position parts = getPath.split('/') # Get the power levels if len(parts) >= 4: try: servo = int(parts[2]) position = float(parts[3]) except: # Bad values servo = 0 position = 0.0 else: # Bad request servo = 0 position = 0.0 # Ensure settings are within limits if position < -1: position = -1 elif position > 1: position = 1 # Set the new servo position if servo == 1: UB.SetServoPosition1(position) elif servo == 2: UB.SetServoPosition2(position) elif servo == 3: UB.SetServoPosition3(position) elif servo == 4: UB.SetServoPosition4(position) # Read the current servo positions position1 = UB.GetServoPosition1() * 100.0 position2 = UB.GetServoPosition2() * 100.0 position3 = UB.GetServoPosition3() * 100.0 position4 = UB.GetServoPosition4() * 100.0 # Build a table for the values httpText = '' httpText += '' % (position1) httpText += '' % (position2) httpText += '' % (position3) httpText += '' % (position4) httpText += '

%.0f %%

%.0f %%

%.0f %%

%.0f %%

' self.send(httpText) elif getPath == '/': # Main page, move sliders to change the servo positions httpText = '\n' httpText += '\n' httpText += '\n' httpText += '\n' httpText += '\n' httpText += '\n' httpText += '' httpText += ' ' httpText += ' ' httpText += ' ' httpText += ' ' httpText += '
' httpText += ' \n' % (sliderHeight) httpText += '
' httpText += ' \n' % (sliderHeight) httpText += '
' httpText += ' \n' % (sliderHeight) httpText += '
' httpText += ' \n' % (sliderHeight) httpText += '
' httpText += '\n' httpText += '

Distances (mm)


\n' httpText += '\n' httpText += '\n' httpText += '\n' self.send(httpText) elif getPath == '/servo': # Alternative page with only servo control, move sliders to change the servo positions httpText = '\n' httpText += '\n' httpText += '\n' httpText += '\n' httpText += '\n' httpText += '\n' httpText += '' httpText += ' ' httpText += ' ' httpText += ' ' httpText += ' ' httpText += '
' httpText += ' \n' % (sliderHeight) httpText += '
' httpText += ' \n' % (sliderHeight) httpText += '
' httpText += ' \n' % (sliderHeight) httpText += '
' httpText += ' \n' % (sliderHeight) httpText += '
' httpText += '\n' httpText += '\n' httpText += '\n' self.send(httpText) elif getPath == '/distances': # Repeated reading of the ultrasonic distances, set a delayed refresh # We use AJAX to avoid screen refreshes caused by refreshing a frame displayDelay = int(1000 / displayRate) httpText = '\n' httpText += '\n' httpText += '\n' httpText += '\n' httpText += '\n' httpText += '\n' % (displayDelay) httpText += '

Waiting for first distance reading...

\n' httpText += '\n' httpText += '\n' self.send(httpText) else: # Unexpected page self.send('Path : "%s"' % (getPath)) def send(self, content): self.request.sendall('HTTP/1.0 200 OK\n\n%s' % (content)) # Run the web server until we are told to close httpServer = SocketServer.TCPServer(("0.0.0.0", webPort), WebServer) try: print 'Press CTRL+C to terminate the web-server' while True: httpServer.handle_request() except KeyboardInterrupt: # CTRL+C exit print '\nUser shutdown' print 'Web-server terminated.'