-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraspi_2.py
126 lines (97 loc) · 3.02 KB
/
raspi_2.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
125
126
#!/usr/bin/python
from __future__ import division
from time import sleep
import paho.mqtt.client as mqtt
import logging
import threading
import json
import RPi.GPIO as GPIO
import atexit
# topics
topic_control = "topic_control_jku_20"
topic_data = "topic_data_jku_20"
# times
ttg = [10, 7, 7, 5]
gd = [15, 10, 20, 20]
#pins
green_pin = 24
red_pin = 25
light_input_pin = 23
# setup logging
logger = logging.getLogger(__name__)
logging.basicConfig(level="INFO")
brokers_out = {"broker1": "tcp://broker.hivemq.com:1883"}
data_out = json.dumps(brokers_out)
data_in = data_out
brokers_in = json.loads(data_in)
# LED setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(red_pin, GPIO.OUT)
GPIO.setup(green_pin, GPIO.OUT)
GPIO.setup(light_input_pin, GPIO.IN)
GPIO.output(red_pin, True)
GPIO.output(green_pin, False)
# mqtt connect callback function
def on_connect(client, userdata, flags, rc):
logger.info('Connected with result code %s', str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(topic_data, qos=0)
client.subscribe(topic_control, qos=0)
# mqtt message received callback function
def on_message(client, userdata, msg):
topic = msg.topic
if topic == topic_control:
m_decode = str(msg.payload.decode("utf-8", "ignore"))
state_dict = json.loads(m_decode)
if "state" in state_dict:
state = state_dict['state']
threading.Thread(target=handle_leds, args=(state,)).start()
def publish_light(client):
light = get_light()
msg = "{\"light\": \"" + str(light) + "\"}"
result = client.publish(topic_data, msg)
print(f"Sent `{msg}` to topic `{topic_data}`")
def publish_data(client):
try:
while True:
publish_light(client)
sleep(10)
except (KeyboardInterrupt, SystemExit):
logger.info("disconnecting...")
client.disconnect()
sleep(1)
logger.info("succesfully disconnected.")
def listen(client):
client.loop_forever()
# gets as input one of the four states (0, 1, 2, 3)
def handle_leds(state):
global ttg, gd
state = int(state)
if state < 4:
sleep(ttg[state])
GPIO.output(red_pin, False)
GPIO.output(green_pin, True)
sleep(gd[state])
GPIO.output(red_pin, True)
GPIO.output(green_pin, False)
else:
logger.info(f"Invalid control signal: {state}")
def get_light():
curLvl = 1 - GPIO.input(light_input_pin) # consistent naming, we send back if it is dark -> invert
logger.info(f"Light level measured: {curLvl}")
return curLvl
def exit_handler():
GPIO.cleanup()
# =======================
atexit.register(exit_handler)
# setup mqtt
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
# connect mqtt
client.connect("broker.hivemq.com", 1883, 60)
listener = threading.Thread(target=listen, args=(client,))
publisher = threading.Thread(target=publish_data, args=(client,))
listener.start()
publisher.start()