This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
124 lines (105 loc) · 2.81 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import network
import time
from umqtt.simple import MQTTClient
from lights import *
from traffic import *
# Config
wifi_ssid = "HaSi-Kein-Internet-Legacy"
wifi_psk = "bugsbunny"
mqtt_server = "mqtt.hasi"
mqtt_client_name = "traffic_light"
mqtt_topic = "hasi/lights/traffic_light"
# State
lights_on = True
mqtt_client = None
snmp_traffic = None
light = None
wlan = None
# Set everything up
def setup():
global wifi_ssid
global wifi_psk
global mqtt_topic
global mqtt_server
global mqtt_client
global mqtt_client_name
global snmp_traffic
global light
global wlan
# Setup Network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(wifi_ssid, wifi_psk)
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
# Setup MQTT
mqtt_client = MQTTClient(mqtt_client_name, mqtt_server)
mqtt_client.set_callback(mqtt_callback)
mqtt_client.connect()
mqtt_client.subscribe(bytes(mqtt_topic, "utf-8"))
# Setup remaining stuff
snmp_traffic = Traffic()
light = Lights()
# MQTT-Callback; Triggered by c.check_msg()
def mqtt_callback(topic, msg):
global lights_on
message = str(msg, "utf-8")
if message == "on":
lights_on = True
elif message == "off":
lights_on = False
# Test routine
def test_lights():
global light
print('testing...')
light.set_all_color((0, 0, 0, 0))
time.sleep(0.5)
light.set_low_load()
time.sleep(0.2)
light.set_middle_load()
time.sleep(0.2)
light.set_high_load()
time.sleep(0.2)
light.set_all_color((255, 255, 255, 31))
time.sleep(0.2)
light.set_all_color((0, 0, 0, 0))
time.sleep(0.2)
light.set_all_color((255, 255, 255, 31))
time.sleep(0.2)
light.set_all_color((0, 0, 0, 0))
light.set_low_load()
print('testing complete')
# Work work work...
def loop():
global mqtt_client
global lights_on
global light
global snmp_traffic
global wlan
while True:
if wlan.isconnected():
#check for mqtt-messages
time.sleep(1)
mqtt_client.check_msg()
if not lights_on:
light.set_all_color((0, 0, 0, 0))
else:
traffic = snmp_traffic.get_traffic()
if traffic != 0 and traffic != None:
if traffic < 2:
light.set_low_load()
elif traffic < 10:
light.set_middle_load()
elif traffic < 14:
light.set_high_load()
else:
light.set_chaos_load()
else:
setup()
setup()
test_lights()
loop()
mqtt_client.disconnect()