-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathIoT_MqttPy_Drone.py
171 lines (160 loc) · 7.22 KB
/
IoT_MqttPy_Drone.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import paho.mqtt.client as mqtt
import os.path
import time
import json
# Key strings
COMMAND_KEY = "COMMAND"
SUCCESFULLY_PROCESSED_COMMAND_KEY = "SUCCESSFULLY_PROCESSED_COMMAND"
# Command strings
CMD_TAKE_OFF = "TAKE_OFF"
CMD_LAND = "LAND"
CMD_LAND_IN_SAFE_PLACE = "LAND_IN_SAFE_PLACE"
CMD_MOVE_UP = "MOVE_UP"
CMD_MOVE_DOWN = "MOVE_DOWN"
CMD_MOVE_FORWARD = "MOVE_FORWARD"
CMD_MOVE_BACK = "MOVE_BACK"
CMD_MOVE_LEFT = "MOVE_LEFT"
CMD_MOVE_RIGHT = "MOVE_RIGHT"
CMD_ROTATE_RIGHT = "ROTATE_RIGHT"
CMD_ROTATE_LEFT = "ROTATE_LEFT"
CMD_SET_MAX_ALTITUDE = "SET_MAX_ALTITUDE"
CMD_SET_MIN_ALTITUDE = "SET_MIN_ALTITUDE"
KEY_DEGREES = "DEGREES"
KEY_FEET = "FEET"
certificates_path = "/user/share/certificates"
ca_certificate = os.path.join(certificates_path, "ca.crt")
client_certificate = os.path.join(certificates_path, "device001.crt")
client_key = os.path.join(certificates_path, "device001.key")
mqtt_server_host = "localhost"
mqtt_server_port = 8883
mqtt_keepalive = 60
class Drone:
def __init__(self, name):
self.name = name
self.min_altitude = 0
self.max_altitude = 30
def print_with_name_prefix(self, message):
print("{}: {}".format(self.name, message))
def take_off(self):
self.print_with_name_prefix("Taking off")
def land(self):
self.print_with_name_prefix("Landing")
def land_in_safe_place(self):
self.print_with_name_prefix("Landing in a safe place")
def move_up(self):
self.print_with_name_prefix("Moving up")
def move_down(self):
self.print_with_name_prefix("Moving down")
def move_forward(self):
self.print_with_name_prefix("Moving forward")
def move_back(self):
self.print_with_name_prefix("Moving back")
def move_left(self):
self.print_with_name_prefix("Moving left")
def move_right(self):
self.print_with_name_prefix("Moving right")
def rotate_right(self, degrees):
self.print_with_name_prefix("Rotating right {} degrees".format(degrees))
def rotate_left(self, degrees):
self.print_with_name_prefix("Rotating left {} degrees".format(degrees))
def set_max_altitude(self, feet):
self.max_altitude = feet
self.print_with_name_prefix("Setting maximum altitude to {} feet".format(feet))
def set_min_altitude(self, feet):
self.min_altitude = feet
self.print_with_name_prefix("Setting minimum altitude to {} feet".format(feet))
class DroneCommandProcessor:
commands_topic = ""
processed_commands_topic = ""
active_instance = None
def __init__(self, name, drone):
self.name = name
self.drone = drone
DroneCommandProcessor.commands_topic = "commands/{}".format(self.name)
DroneCommandProcessor.processed_commands_topic = "processedcommands/{}".format(self.name)
self.client = mqtt.Client(protocol=mqtt.MQTTv311)
DroneCommandProcessor.active_instance = self
self.client.on_connect = DroneCommandProcessor.on_connect
self.client.on_message = DroneCommandProcessor.on_message
self.client.tls_set(ca_certs = ca_certificate, certfile=client_certificate, keyfile=client_key)
self.client.connect(host=mqtt_server_host, port=mqtt_server_port, keepalive=mqtt_keepalive)
@staticmethod
def on_connect(client, userdata, flags, rc):
print("Connected to the MQTT server")
client.subscribe(DroneCommandProcessor.commands_topic, qos=2)
client.publish(topic=DroneCommandProcessor.processed_commands_topic,
payload="{} is listening to messages".format(DroneCommandProcessor.active_instance.name))
@staticmethod
def on_message(client, userdata, msg):
payload_string = msg.payload.decode('utf-8')
if msg.topic == DroneCommandProcessor.commands_topic:
print("I've received the following message:{0} ".format(str(msg.payload_string)))
try:
message_dictionary = json.loads(msg.payload_string)
if COMMAND_KEY in message_dictionary:
command = message_dictionary[COMMAND_KEY]
drone = DroneCommandProcessor.active_instance.drone
is_command_processed = False
if command == CMD_TAKE_OFF:
drone.take_off()
is_command_processed = True
elif command == CMD_LAND:
drone.land()
is_command_processed = True
elif command == CMD_LAND_IN_SAFE_PLACE:
drone.land_in_safe_place()
is_command_processed = True
elif command == CMD_MOVE_UP:
drone.move_up()
is_command_processed = True
elif command == CMD_MOVE_DOWN:
drone.move_down()
is_command_processed = True
elif command == CMD_MOVE_FORWARD:
drone.move_forward()
is_command_processed = True
elif command == CMD_MOVE_BACK:
drone.move_back()
is_command_processed = True
elif command == CMD_MOVE_LEFT:
drone.move_left()
is_command_processed = True
elif command == CMD_MOVE_RIGHT:
drone.move_right()
is_command_processed = True
elif command == CMD_ROTATE_RIGHT:
degrees = message_dictionary[KEY_DEGREES]
drone.rotate_right(degrees)
is_command_processed = True
elif command == CMD_ROTATE_LEFT:
degrees = message_dictionary[KEY_DEGREES]
drone.rotate_left(degrees)
is_command_processed = True
elif command == CMD_SET_MAX_ALTITUDE:
feet = message_dictionary[KEY_FEET]
drone.set_max_altitude(feet)
is_command_processed = True
elif command == CMD_SET_MIN_ALTITUDE:
feet = message_dictionary[KEY_FEET]
drone.set_min_altitude(feet)
is_command_processed = True
if is_command_processed:
DroneCommandProcessor.active_instance.publish_response_message( message_dictionary )
else:
print("The message includes an unknown command.")
except ValueError:
# msg is not a dictionary
print("The message doesn't include a valid command.")
def publish_response_message(self, message):
response_message = json.dumps({ SUCCESFULLY_PROCESSED_COMMAND_KEY:message[COMMAND_KEY]})
result = self.client.publish(topic=self.__class__.processed_commands_topic, payload=response_message)
return result
def process_commands(self):
self.client.loop()
if __name__ == "__main__":
drone = Drone("drone01")
drone_command_processor = DroneCommandProcessor("drone01", drone)
while True:
# Process messages and the commands every 1 second
drone_command_processor.process_commands()
time.sleep(1)