-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverLaunchMiniRocket.py
68 lines (56 loc) · 1.76 KB
/
serverLaunchMiniRocket.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
import sys
import time
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import RPi.GPIO as GPIO
print('Imported Packages and Starting Launch Server')
HOST = "localhost"
TOPIC_1 = "control/client"
TOPIC_2 = "control/server"
print (("Please connect client software to: %s at port: %d \n") % (HOST, 1883))
print ("Waiting to establish connection........ \n")
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
print ("Connection established.")
#print ('Connection address: ',addr)
#logger.debug("Connection established at {}".format(time.asctime())) #find what the ip is
print ("Awaiting commands... \n")
error = rc
client.subscribe(TOPIC_1)
return error
def on_disconnect(client, userdata,rc=0):
print("Connection Lost.")
client.loop_stop()
def on_message(client, userdata, msg):
calldata(str(msg.payload))
print(str(msg.payload))
#print('On Message')
client = mqtt.Client("server",True)
client.on_connect = on_connect
client.on_message = on_message
#client.on_publish = on_publish
client.on_disconnect = on_disconnect
client.connect(HOST, 1883, 60)
#Setup GPIO on PI
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
launchPin = 18
GPIO.setup(launchPin,GPIO.OUT)
GPIO.output(launchPin, GPIO.input(launchPin))
def calldata(data):
if 'launch' in data:
print('Sending Launch Codes!')
launch()
else:
print('Call Data Did Not Work')
def launch():
GPIO.output(launchPin, True)
client.publish(TOPIC_2, "Launch Code Sent")
time.sleep(1)
closeMainValve()
#print('launch function called')
def closeMainValve():
GPIO.output(launchPin, False)
print('Closed Main Valve')
client.publish(TOPIC_2, "Closed Main Valve")
client.loop_forever()