Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # leftSensor and broker file
- #Preliminary code for an outer pi, definitely not finished just an idea
- import time
- import board
- import os
- import threading
- from gpiozero import MotionSensor
- from gpiozero import Servo
- from paho.mqtt import client as mqtt
- from paho.mqtt import publish
- from paho.mqtt import subscribe
- IRSensor = MotionSensor(4)
- servo = Servo(16)
- ThermalCamOff = 1 #temp variable at the moment
- stopLSensorReading = False
- broker = '192.168.1.15'
- port = 1883
- topic = "peripheralSensor/leftSensor"
- clientID = "leftPumpkin"
- subscription = ("servoPosition", 2)
- def subscribe(client, subscription):
- r = client.subscribe(subscription)
- if r[0] == 0:
- print(f"Successfully subscribed to {subscription}")
- else:
- print(f"Failed to subscribe to {subscription}")
- def connect_mqtt():
- def on_connect(client, userdata, flags, rc):
- if rc==0:
- print("connected to MQTT Broker!")
- else:
- print("failed to connect")
- client = mqtt.Client(clientID)
- client.on_connect = on_connect
- client.on_message = on_message
- client.connect(broker, port)
- subscribe(client, subscription)
- return client
- def publish(client, msg):
- print("in publish")
- result = client.publish(topic, msg)
- status = result[0]
- if status == 0:
- print(f"sent `{msg}` to topic `{topic}`")
- else:
- print(f"failed to send message to topic {topic}")
- time.sleep(5)
- def on_message(client, userdata, message):
- print("in response to subscription")
- global stopLSensorReading
- if (message.payload == "0enmasse"):
- stopLSensorReading = True
- servo.value = 0
- print("0enmasse")
- time.sleep(60)
- stopLSensorReading = False
- LSensor()
- else:
- servo.value = message.payload
- print(f'{message.payload}')
- def LSensor(client):
- print("in LSensor")
- global stopLSensorReading
- while(True):
- print("loop")
- if stopLSensorReading:
- break
- IRSensor.wait_for_motion()
- print('seen')
- publish(client, 1)
- def run():
- leftSensorThread = threading.Thread(target = LSensor)
- client = connect_mqtt()
- client.loop_start()
- LSensor(client)
- if __name__ == '__main__':
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement