Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # rightSensor and broker file
- #Preliminary code for an outer pi, definitely not finished just an idea
- import time
- import board
- import os
- import multiprocessing
- import logging
- 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
- broker = '192.168.1.15'
- port = 1883
- home_topic="sensors"
- aux_topic="rightSensor"
- clientID = "rightPumpkin"
- subscription = ("servoPosition", 2)
- RSensorRunning = True
- CLEAN_SESSION=True
- logging.basicConfig(level=logging.INFO)
- # 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 on_subscribe(client, userdata, mid, granted_qos):
- time.sleep(1)
- logging.info("sub acknowledge message id="+str(mid))
- pass
- def on_disconnect(client, userdata, rc=0):
- logging.info("Disconnected result code" +str(rc))
- def on_connect(client, userdata, flags, rc):
- if rc==0:
- print("connected to MQTT Broker!")
- else:
- print("failed to connect")
- logging.info("Connected flags " +str(flags)+"result code " + str(rc))
- # 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")
- # msg = str(message.payload.decode("utf-8"))
- print("topic="+message.topic)
- topics = (message.topic).split("/")
- print(f"Message received: {message.payload} from {topics[2]}")
- if topics[1] == home_topic:
- reply_topic = "base/" + topics[2] + "/" + home_topic + "/" + aux_topic
- client.publish(reply_topic,"I got it dude!")
- print("replied")
- # if(RSensorRunning):
- # RSensorRunning = False
- # if (message.payload == 5):
- # servo.value = 0
- # print("0enmasse")
- # time.sleep(60)
- # RSensorRunning = True
- # else:
- # servo.value = int(message.payload)
- # if (rightSensorProcess.is_alive()):
- # print("still alive jackass")
- # print(f'received {int(message.payload)}')
- def on_publish(client, userdata, mid):
- logging.info("message published " +str(mid))
- send_topic = "house/thermalPumpkin/sensors/rightSensor"
- client = mqtt.Client("rightSensor", False)
- client.on_subscribe = on_subscribe
- client.on_disconnect = on_disconnect
- client.on_connect = on_connect
- client.on_message = on_message
- client.on_publish = on_publish
- client.connect(broker, port)
- time.sleep(1)
- client.subscribe("house/sensors/thermalPumpkin/rightSensor")
- client.loop_start()
- count = 1
- def RSensor(client):
- global RSensorRunning
- print("in RSensor")
- while True:
- print("loop")
- IRSensor.wait_for_motion()
- print('seen')
- publish(client, "-1")
- return
- # client = connect_mqtt()
- # rightSensorProcess = multiprocessing.Process(target=RSensor, args=(client,))
- def run():
- global count
- while True:
- msg="message " + str(count) + " from RSensor"
- count += 1
- time.sleep(5)
- client.disconnect()
- client.loop_stop()
- # client.loop_start()
- # # publish(client, "bruh moment")
- # rightSensorProcess.start()
- if __name__ == '__main__':
- run()
Add Comment
Please, Sign In to add comment