-
Notifications
You must be signed in to change notification settings - Fork 2
/
Raspberry_Pi_Client.py
121 lines (80 loc) · 2.89 KB
/
Raspberry_Pi_Client.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
'''
NORCO COLLEGE ROCKETRY
This is the code for Raspberry Pi 1 in the payload of the rocket.
'''
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
MQTT_SERVER = "192.168.4.1"
MQTT_PATH_COMMAND = "command_channel"
MQTT_PATH_REPLY = "reply_channel"
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(MQTT_PATH_COMMAND)
#client.subscribe(MQTT_PATH_REPLY)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client.publish(MQTT_PATH_REPLY, "Raspberry Pi 1 is ONLINE")
# more callbacks, etc
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)
client.loop_forever()
##################################################################################
'''
'''
NORCO COLLEGE ROCKETRY
This is the code for Raspberry Pi 1 in the payload of the rocket.
'''
import paho.mqtt.client as mqtt
import time
# the server is the static IP of the pi zero in the nosecone
MQTT_SERVER = "192.168.4.1"
MQTT_PATH = "test/channel"
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Raspberry Pi 1 is connected\nwith result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(MQTT_PATH)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(str(msg.payload)+" on channel: "+msg.topic)
# switch(msg.payload)
print("\n")
# print(msg.topic+" "+str(msg.payload))
'''
def message_1():
print("Raspberry Pi 1 is online")
def message_2():
print("Initiating startup sequence")
def message_3():
print("Altitude: ")
def message_4():
print("Raspberry Pi 1 entering idle state")
def message_5():
print("Powering off Raspberry Pi 1")
def switch(message):
swtch = {
1 : message_1(),
2 : message_2(),
3 : message_3(),
4 : message_4(),
5 : message_5()
}.get(message, 'default')
'''
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
# these are default parameters
client.connect(MQTT_SERVER, 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
'''