Last active
December 11, 2015 17:38
-
-
Save pironic/4635381 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import SocketServer, subprocess, sys | |
from threading import Thread | |
import RPi.GPIO as GPIO, time, os | |
GPIO.setmode(GPIO.BCM) | |
HOST = '10.1.1.45' | |
PORT = 2000 | |
def RCtime (RCpin): | |
reading = 0 | |
GPIO.setup(RCpin, GPIO.OUT) | |
GPIO.output(RCpin, GPIO.LOW) | |
time.sleep(.1) | |
GPIO.setup(RCpin, GPIO.IN) | |
# This takes about 1 millisecond per loop cycle | |
while (GPIO.input(RCpin) == GPIO.LOW & reading < 100000): | |
reading += 1 | |
return reading | |
class SingleTCPHandler(SocketServer.BaseRequestHandler): | |
"One instance per connection. Override handle(self) to customize action." | |
def handle(self): | |
# self.request is the client connection | |
data = self.request.recv(1024) # clip input at 1Kb | |
print data | |
reply = RCtime(int(data)) | |
if reply is not None: | |
self.request.send(str(reply)) | |
self.request.close() | |
class SimpleServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): | |
# Ctrl-C will cleanly kill all spawned threads | |
daemon_threads = True | |
# much faster rebinding | |
allow_reuse_address = True | |
def __init__(self, server_address, RequestHandlerClass): | |
SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass) | |
if __name__ == "__main__": | |
server = SimpleServer((HOST, PORT), SingleTCPHandler) | |
# terminate with Ctrl-C | |
try: | |
server.serve_forever() | |
except KeyboardInterrupt: | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment