-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
47 lines (34 loc) · 994 Bytes
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from flask import Flask, jsonify, request
import paho.mqtt.client as mqtt
app = Flask(__name__)
def get_empty_display():
return [
[[0, 0, 0] for y in range(32)] for x in range(8)
]
DISPLAY = get_empty_display()
MQTT_TOPIC = "ledtest/1"
MQTT_HOST = "broker.mqttdashboard.com"
MQTT_PORT = 1883
MQTT_TIMEOUT = 60
client = mqtt.Client()
client.connect(MQTT_HOST, MQTT_PORT, MQTT_TIMEOUT)
client.loop_start()
@app.route('/')
def index():
global DISPLAY
return jsonify(DISPLAY)
@app.route('/set', methods=['POST'])
def set():
global DISPLAY
req_data = request.get_json()
DISPLAY = get_empty_display() # Set display to all zero. If we want to keep the state, comment this line
if len(req_data):
for entry in req_data:
x = entry['x']
y = entry['y']
c = entry['c']
DISPLAY[y][x] = c
else:
DISPLAY = get_empty_display()
client.publish(MQTT_TOPIC, str(DISPLAY))
return "ok"