Alle Programme können von hier heruntergeladen werden. |
Schnappschuss erstellen und Bild speichern |
Programm:[►] # Camera1.py import camera print "Capturing image..." img = camera.captureJPEG(300, 200) print "size", len(img) camera.saveData(img, "/home/pi/test.jpg") print "JPEG saved" Bemerkungen: Ziel: Programm:[►] # Camera1.py import camera print "Capturing image..." img = camera.captureJPEG(300, 200) print "size", len(img) camera.saveData(img, "/home/pi/test.jpg") print "JPEG saved" Bemerkungen: |
Schnappschuss erstellen und auf das Remote-System transferieren |
Wir nutzen die Gelegenheit um zu zeigen, wie man Daten vom Raspberry Pi zum einem Remote-Client unter der Verwendung des TCP-Client/Server-Verfahrens übertragen kann. Der Programmcode wird nicht im Detail erklärt, aber mit der Hilfe des vorangehenden Kapitels über TCP/IP und von zusätzlichen Informationen, die Sie im Internet finden, sollten Sie in der Lage sein, ihn vollständig zu verstehen. Ziel: Ein eigener Thread wäre in unserer Situation nicht unbedingt notwendig, da nur ein Client vorhanden ist. Wir wählen trotzdem dieses Standardvorgehen, welches in vielen anderen Client-Server-Anwendungen eingesetzt wird. Programm:[►] # CameraServer.py from threading import Thread import socket import time import camera VERBOSE = False IP_PORT = 22000 def debug(text): if VERBOSE: print "Debug:---", text # ---------------------- class SocketHandler ------------------------ class SocketHandler(Thread): def __init__(self, conn): Thread.__init__(self) self.conn = conn def run(self): global isConnected debug("SocketHandler started") isRunning = True while isRunning: cmd = "" try: debug("Calling blocking conn.recv()") cmd = self.conn.recv(1024) except: debug("exception in conn.recv()") # happens when connection is reset from the peer break debug("Received cmd: " + cmd + " len: " + str(len(cmd))) if len(cmd) == 0: break rc = self.executeCommand(cmd) if not rc: isRunning = False conn.close() print "Client disconnected. Waiting for next client..." isConnected = False debug("SocketHandler terminated") def executeCommand(self, cmd): debug("Calling executeCommand() with cmd: " + cmd) if cmd == "go": print "Taking snapshot and transfer to client..." jpg = camera.captureJPEG(640, 480) return self.sendData(jpg) if cmd == "disconnect": debug("Client request disconnection") return False def sendData(self, data): debug("sendData() with len: " + str(len(data))) try: self.conn.sendall(data) except: return False return True # ----------------- End of SocketHandler ----------------------- serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # close port when process exits: serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) debug("Socket created") HOSTNAME = "" # Symbolic name meaning all available interfaces try: serverSocket.bind((HOSTNAME, IP_PORT)) except socket.error as msg: print "Bind failed", msg[0], msg[1] sys.exit() serverSocket.listen(10) print "Waiting for a connecting client..." isConnected = False while True: debug("Calling blocking accept()...") conn, addr = serverSocket.accept() print "Connected with client at " + addr[0] isConnected = True socketHandler = SocketHandler(conn) # necessary to terminate it at program termination: socketHandler.setDaemon(True) socketHandler.start() t = 0 while isConnected: print "Camera ready at", t, "s" time.sleep(10) t += 10 Bemerkungen: Programm:[►] # CameraClient1.py # Display image in TigerJython's GPanel graphics window from threading import Thread import socket, time from gpanel import * VERBOSE = False IP_ADDRESS = "192.168.0.12" IP_PORT = 22000 def debug(text): if VERBOSE: print "Debug:---", text # --------------------- class Receiver --------------------------- class Receiver(Thread): def run(self): debug("Receiver thread started") while True: try: rxData = self.readServerData() except: debug("Exception in Receiver.run()") isReceiverRunning = False closeConnection() break debug("Receiver thread terminated") def readServerData(self): global isJPEG debug("Calling readResponse") bufSize = 4096 data = "" while data[-2:] != "\xff\xd9": # eof tag for jpeg files (both chars must be in same block) # We are not sure 100% that this sequence is never embedded in image # but it is improbable to happen at the end of the data block try: blk = sock.recv(bufSize) if blk != None: debug("Received data block, len: " + str(len(blk))) else: debug("sock.recv() returned with None") except: raise Exception("Exception from blocking sock.recv()") data += blk print "JPEG received. Displaying it..." display(data) # -------------------- End of Receiver --------------------- def startReceiver(): debug("Starting Receiver thread") receiver = Receiver() receiver.start() def sendCommand(cmd): debug("sendCommand() with cmd = " + cmd) try: sock.sendall(cmd) except: debug("Exception in sendCommand()") closeConnection() def closeConnection(): debug("Closing socket") sock.close() def connect(): global sock sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) debug("Connecting...") try: sock.connect((IP_ADDRESS, IP_PORT)) except: debug("Connection failed.") return False startReceiver() return True def display(data): img = readImage(data) if img != None: image(img, 0, 0) def onExit(): global isRunning isRunning = False dispose() width = 640 height = 480 makeGPanel(Size(width, height)) addExitListener(onExit) sock = None isRunning = True if connect(): print "Connection established" time.sleep(1) while isRunning: print "Sending command 'go'..." sendCommand("go") time.sleep(2) print "Disconnecting now..." # sendCommand("disconnect") closeConnection() else: print "Connection to %s:%d failed" %(IP_ADDRESS, IP_PORT) print "done" Bemerkungen: Programm:[►] # CameraClient1.py import webbrowser def saveData(data, filename): file = open(filename, "wb") file.write(data) file.close() def display(data): jpgFile = "c:/scratch/test.jpg" saveData(data, jpgFile) webbrowser.open(jpgFile) |