Skip to content

Instantly share code, notes, and snippets.

@lakewik
Created December 25, 2017 20:26
Show Gist options
  • Save lakewik/8cba63a0195132effdf22262c1eb838e to your computer and use it in GitHub Desktop.
Save lakewik/8cba63a0195132effdf22262c1eb838e to your computer and use it in GitHub Desktop.
Sterowanie24 python :D
import websocket
import thread
import time
import threading
import RPi.GPIO as GPIO
import json
def set_interval(func, sec, ws):
def func_wrapper():
set_interval(func, sec, ws)
func(ws)
t = threading.Timer(sec, func_wrapper)
t.start()
return t
def ping (ws):
ts = int(time.time())
ws.send(json.dumps([json.dumps({'event': 'ping', 'data': ts})]))
print "Sending ping"
def on_message(ws, message):
j = json.loads(str(message))
if j["event"] == "steruj" and j["card_id"] == "6728934":
if j['button'] == 0:
if j['state'] == 0:
GPIO.output(26, GPIO.HIGH)
else:
GPIO.output(26, GPIO.LOW)
if j['button'] == 1:
if j['state'] == 0:
GPIO.output(6, GPIO.HIGH)
else:
GPIO.output(6, GPIO.LOW)
if j['button'] == 2:
if j['state'] == 0:
GPIO.output(13, GPIO.HIGH)
else:
GPIO.output(13, GPIO.LOW)
if j['button'] == 3:
if j['state'] == 0:
GPIO.output(19, GPIO.HIGH)
else:
GPIO.output(19, GPIO.LOW)
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
startWS()
def on_open(ws):
#def run(*args):
#for i in range(3):
#time.sleep(1)
#ws.send("Hello %d" % i)
# time.sleep(1)
# ws.close()
# print("thread terminating...")
#thread.start_new_thread(run, ())
#set_interval(ping, 5, ws)
print "Opened connection"
def startWS():
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://socket.sterowanie24.pl:8000//?c=6728934",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
if __name__ == "__main__":
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
startWS()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment