#!/usr/bin/env python # coding: Latin-1 # Creates a web-page interface for DiddyBorg 2 # Import library functions we need import ThunderBorg import time import sys import threading import SocketServer #import picamera #import picamera.array #import cv2 import datetime # Settings for the web-page webPort = 2000 # Port number for the web-page, 80 is what web-pages normally use #imageWidth = 240 # Width of the captured image in pixels #imageHeight = 192 # Height of the captured image in pixels #frameRate = 30 # Number of images to capture per second #displayRate = 10 # Number of images to request per second #photoDirectory = '/home/pi' # Directory to save photos to #flippedCamera = True # Swap between True and False if the camera image is rotated by 180 #jpegQuality = 80 # JPEG quality level, smaller is faster, higher looks better (0 to 100) # Global values global TB #global lastFrame #global lockFrame #global camera #global processor global running global watchdog running = True TB = ThunderBorg.ThunderBorg() #TB.i2cAddress = 0x15 # Uncomment and change the value if you have changed the board address TB.Init() if not TB.foundChip: boards = ThunderBorg.ScanForThunderBorg() if len(boards) == 0: print 'No ThunderBorg found, check you are attached :)' else: print 'No ThunderBorg at address %02X, but we did find boards:' % (TB.i2cAddress) for board in boards: print ' %02X (%d)' % (board, board) print 'If you need to change the I²C address change the setup line so it is correct, e.g.' print 'TB.i2cAddress = 0x%02X' % (boards[0]) sys.exit() #TB.SetCommsFailsafe(False) TB.SetLedShowBattery(False) TB.SetLeds(0,0,1) # Power settings voltageIn = 1.2 * 10 # Total battery voltage to the ThunderBorg voltageOut = 12.0 * 0.95 # Maximum motor voltage, we limit it to 95% to allow the RPi to get uninterrupted power # Setup the power limits if voltageOut > voltageIn: maxPower = 1.0 else: maxPower = voltageOut / float(voltageIn) # Timeout thread class Watchdog(threading.Thread): def __init__(self): super(Watchdog, self).__init__() self.event = threading.Event() self.terminated = False self.start() self.timestamp = time.time() def run(self): timedOut = True # This method runs in a separate thread while not self.terminated: # Wait for a network event to be flagged for up to one second if timedOut: if self.event.wait(1): # Connection print 'Reconnected...' TB.SetLedShowBattery(True) timedOut = False self.event.clear() else: if self.event.wait(1): self.event.clear() else: # Timed out print 'Timed out...' TB.SetLedShowBattery(False) TB.SetLeds(0,0,1) timedOut = True TB.MotorsOff() # Class used to implement the web server class WebServer(SocketServer.BaseRequestHandler): def handle(self): global TB #global lastFrame global watchdog # 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 watchdog.event.set() #if getPath.startswith('/cam.jpg'): # Camera snapshot #lockFrame.acquire() #sendFrame = lastFrame #lockFrame.release() #if sendFrame != None: #self.send(sendFrame.tostring()) # Turn the drives off elif getPath.startswith('/off'): httpText = '