def on_connect(client, userdata, flags, rc): global angle_thread, gndtime_thread, acc_err_thread, height_thread global ignore_angle client.subscribe("hoist") client.subscribe("time/fromground") client.subscribe("accelerometer/status") client.subscribe("height/status") print("connected") client.publish("status", "Upper Pi client connected") gndtime_thread = timer.setInterval(5, publish_gndtime) gndtime_thread.start() height_thread = timer.setInterval(.5, publish_height) height_thread.start() acc_err_thread = timer.setInterval(.5, accel_disconnect) if ACC.error == True: ignore_angle = True acc_err_thread.start() else: angle_thread = timer.setInterval(0.25, publish_angle) angle_thread.start() if ALT.error == True: height_thread.cancel() print("altimeter disconnected from start")
def __init__(self, r): self.dist = 0 # This should be in feet self.r = r self.rotary_count = 0 self.clk_prev = GPIO.input(clk) self.thread = timer.setInterval(0.002, self.height) self.thread.start()
def on_disconnect(client, userdata, rc): global broker, backup_listen, acc_err_thread print("Disconnected from broker") client.loop_stop() # Starts a new broker on backup when main loses power run_broker = subprocess.Popen( ["mosquitto", "-c", "/etc/mosquitto/mosquitto.conf"]) time.sleep(1) # Enables GPIO pins for hoist control GPIO.setmode(GPIO.BCM) GPIO.setup(UP_L, GPIO.OUT) GPIO.setup(DOWN_L, GPIO.OUT) GPIO.setup(UP_R, GPIO.OUT) GPIO.setup(DOWN_R, GPIO.OUT) # Reconnects client to backup broker backup_listen = True broker = backupBroker client.connect(broker, port) try: timefromground_thread.cancel() except: pass timefromground_thread = timer.setInterval(5, publish_timefromground) timefromground_thread.start() if ACC.error == True: ignore_angle = True try: angle_thread.cancel() except: pass acc_err_thread = timer.setInterval(.5, accel_disconnect) acc_err_thread.start() else: angle_thread = timer.setInterval(0.25, publish_angle) angle_thread.start()
def on_message(client, userdata, message): global backup_listen, msg, topic global angle_thread, acc_err_thread, timefromground_thread msg = message.payload.decode("utf-8") topic = message.topic if backup_listen: last_msg_timer.start() # Keeps track of time from ground and height while connected if topic == "time/fromground": print('time received ', msg) try: HOIST.set_time(float(msg)) except: pass # Deals with Scenario 2 - original broker stays on elif not backup_listen and (msg == "Switch to backup" or msg == 'Upper Pi client disconnected'): backup_listen = True # Try to start publishing angle if ACC.error == True: ignore_angle = True acc_err_thread = timer.setInterval(.5, accel_disconnect) acc_err_thread.start() else: angle_thread = timer.setInterval(0.25, publish_angle) angle_thread.start() # stops listening for height client.unsubscribe('height') # Starts publishing time from ground client.unsubscribe('time/fromground') timefromground_thread = timer.setInterval(5, publish_timefromground) timefromground_thread.start() client.publish("status", "Backup Pi client connected")
def on_connect(client, userdata, flags, rc): global altitude_thread client.subscribe("altimeter/sealevelpressure") altitude_thread = timer.setInterval(1.0, publish_all_alti) altitude_thread.start() print("connected")