-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiot_client_camera.py
50 lines (43 loc) · 1.45 KB
/
iot_client_camera.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
'''
This module represents camera iot client, which
subscribe the messagae from controller and
take a picture when ordered.
'''
from pyaml_env import parse_config, BaseConfig
from module.camera import take_photo
from module.mqtt_connector import connect_mqtt, print_connect, print_message
config = BaseConfig(parse_config('./config/config.yml'))
def on_connect(client, userdata, flags, rc):
'''
when connected to the MQTT client,
subscribe controller message.
'''
print_connect(client, userdata, flags, rc)
# Camera subscribe controller topic
client.subscribe(
config.client_camera.subscribe.controller)
print("Subscribed to: " + config.client_camera.subscribe.controller)
def on_message(client, userdata, msg):
'''
when message delivered on MQTT,
Show client, userdata and messages.
And if the message is "Capture", then
capture a picture and return the message.
'''
print_message(client, userdata, msg)
req = str(msg.payload.decode("utf-8"))
# When receive camera message, take a photo and publish a message to controller
if req == "Capture":
take_photo()
client.publish(
config.client_camera.publish.controller,
"Captured",
config.mqtt.qos)
if __name__ == "__main__":
# Main program to connect MQTT broker
connect_mqtt(
config.client_camera.user,
config.client_camera.password,
on_connect,
on_message
)