From 100eebf6d517885409f2ea55e364bea3b7871520 Mon Sep 17 00:00:00 2001 From: George Knowlden Date: Fri, 22 Mar 2019 05:58:06 +0000 Subject: [PATCH] Initial commit --- .gitignore | 2 + app.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 77 +++++++++++++++++++++++++++++++++++++ led.py | 35 +++++++++++++++++ requirements.txt | 4 ++ 5 files changed, 216 insertions(+) create mode 100644 .gitignore create mode 100755 app.py create mode 100644 index.html create mode 100644 led.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..93526df --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv/ +__pycache__/ diff --git a/app.py b/app.py new file mode 100755 index 0000000..f8e6043 --- /dev/null +++ b/app.py @@ -0,0 +1,98 @@ +import tornado.httpserver +import tornado.ioloop +import tornado.options +import tornado.web +import tornado.websocket + +# To handle requests +import json + +# Get the host IP +import netifaces as ni +ni.ifaddresses("wlan0") +ip = ni.ifaddresses("wlan0")[ni.AF_INET][0]["addr"] +print("Host IP address:", ip) + +# Get the host port +from tornado.options import define, options +define("port", default=8080, help="run on the given port", type=int) + +# Our program for reading/writing to our output +import led + +class IndexHandler(tornado.web.RequestHandler): + def get(self): + # TODO: render all the controls automatically depending on what we have connected + self.render("index.html", ip = ip, port = options.port) + +ws_clients = [] + +class WebSocketHandler(tornado.websocket.WebSocketHandler): + def open(self): + if self not in ws_clients: + print("New connection") + ws_clients.append(self) + #self.write_message("Connected") + + def on_message(self, message): + print("Message received:", message) + #self.write_message("Message received: " + message) + + message = json.loads(message) #load the json + + if message["message_type"] == "io_request": + # We have something that we need to do + + if message["io_type"] == "led": + colour = message["led_colour"] + operation = message["operation"] + + if operation == "toggle": + result = led.toggle(colour) + + response = {"message_type": "io_response", + "io_type": "led", + "led_colour": colour, + "led_state": result} + + send_to_all(json.dumps(response)) + + if operation == "read": + result = led.read(colour) + + response = {"message_type": "io_response", + "io_type": "led", + "led_colour": colour, + "led_state": result} + + send_to_all(json.dumps(response)) + + # if message == "BUTTON_PRESS": + # result = led.toggle() + # send_to_all("LED_STATE " + str(result)) + + # if message == "GET_STATES": + # states = led.read("R") + # send_to_all("LED_STATE" + str(states)) + + def on_close(self): + if self in ws_clients: + ws_clients.remove(self) + print("Connection closed") + +def send_to_all(message): + for c in ws_clients: + c.write_message(message) + +if __name__ == "__main__": + tornado.options.parse_command_line() + app = tornado.web.Application( + handlers=[ + (r"/", IndexHandler), + (r"/ws", WebSocketHandler) + ] + ) + httpServer = tornado.httpserver.HTTPServer(app) + httpServer.listen(options.port) + print("Listening on port:", options.port) + tornado.ioloop.IOLoop.instance().start() diff --git a/index.html b/index.html new file mode 100644 index 0000000..d6e4634 --- /dev/null +++ b/index.html @@ -0,0 +1,77 @@ + + + + + + + +
+

Controller

+ + + + + + + + + +

LED state: ?

LED state: ?

+ + diff --git a/led.py b/led.py new file mode 100644 index 0000000..495a5f0 --- /dev/null +++ b/led.py @@ -0,0 +1,35 @@ +import RPi.GPIO as GPIO +GPIO.setwarnings(False) +GPIO.setmode(GPIO.BCM) + +pins = [19, 26] + +GPIO.setup(pins, GPIO.OUT, initial=GPIO.LOW) + +def colour_to_pin(colour): + if colour == "R": + return 19 + if colour == "G": + return 26 + else: + return False + +def on(colour): + pin = colour_to_pin(colour) + GPIO.output(pin, GPIO.HIGH) + +def off(colour): + pin = colour_to_pin(colour) + GPIO.output(pin, GPIO.LOW) + +def read(colour): + pin = colour_to_pin(colour) + return GPIO.input(pin) + +def toggle(colour): + if read(colour): + off(colour) + return 0 + else: + on(colour) + return 1 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..39b4038 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +netifaces==0.10.9 +pkg-resources==0.0.0 +RPi.GPIO==0.6.5 +tornado==6.0.1