MnMWizard

new_sensor_code

Oct 19th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. # rightSensor and broker file
  2.  
  3. #Preliminary code for an outer pi, definitely not finished just an idea
  4.  
  5. import time
  6. import board
  7. import os
  8. import multiprocessing
  9. import logging
  10. from gpiozero import MotionSensor
  11. from gpiozero import Servo
  12. from paho.mqtt import client as mqtt
  13. from paho.mqtt import publish
  14. from paho.mqtt import subscribe
  15.  
  16. IRSensor = MotionSensor(4)
  17.  
  18. servo = Servo(16)
  19.  
  20. ThermalCamOff = 1 #temp variable at the moment
  21.  
  22. broker = '192.168.1.15'
  23. port = 1883
  24. home_topic="sensors"
  25. aux_topic="rightSensor"
  26. clientID = "rightPumpkin"
  27. subscription = ("servoPosition", 2)
  28. RSensorRunning = True
  29. CLEAN_SESSION=True
  30. logging.basicConfig(level=logging.INFO)
  31. # def subscribe(client, subscription):
  32. # r = client.subscribe(subscription)
  33. # if r[0] == 0:
  34. # print(f"Successfully subscribed to {subscription}")
  35. # else:
  36. # print(f"Failed to subscribe to {subscription}")
  37. def on_subscribe(client, userdata, mid, granted_qos):
  38. time.sleep(1)
  39. logging.info("sub acknowledge message id="+str(mid))
  40. pass
  41.  
  42. def on_disconnect(client, userdata, rc=0):
  43. logging.info("Disconnected result code" +str(rc))
  44.  
  45. def on_connect(client, userdata, flags, rc):
  46. if rc==0:
  47. print("connected to MQTT Broker!")
  48. else:
  49. print("failed to connect")
  50. logging.info("Connected flags " +str(flags)+"result code " + str(rc))
  51.  
  52. # def connect_mqtt():
  53. # def on_connect(client, userdata, flags, rc):
  54. # if rc==0:
  55. # print("connected to MQTT Broker!")
  56. # else:
  57. # print("failed to connect")
  58. # client = mqtt.Client(clientID)
  59. # client.on_connect = on_connect
  60. # client.on_message = on_message
  61. # client.connect(broker, port)
  62. # subscribe(client, subscription)
  63. # return client
  64.  
  65.  
  66. # def publish(client, msg):
  67. # print("in publish")
  68. # result = client.publish(topic, msg)
  69. # status = result[0]
  70. # if status == 0:
  71. # print(f"sent `{msg}` to topic `{topic}`")
  72. # else:
  73. # print(f"failed to send message to topic {topic}")
  74. # time.sleep(5)
  75.  
  76. def on_message(client, userdata, message):
  77. print("in response to subscription")
  78. # msg = str(message.payload.decode("utf-8"))
  79. print("topic="+message.topic)
  80. topics = (message.topic).split("/")
  81. print(f"Message received: {message.payload} from {topics[2]}")
  82. if topics[1] == home_topic:
  83. reply_topic = "base/" + topics[2] + "/" + home_topic + "/" + aux_topic
  84. client.publish(reply_topic,"I got it dude!")
  85. print("replied")
  86. # if(RSensorRunning):
  87. # RSensorRunning = False
  88. # if (message.payload == 5):
  89. # servo.value = 0
  90. # print("0enmasse")
  91. # time.sleep(60)
  92. # RSensorRunning = True
  93. # else:
  94. # servo.value = int(message.payload)
  95. # if (rightSensorProcess.is_alive()):
  96. # print("still alive jackass")
  97. # print(f'received {int(message.payload)}')
  98.  
  99. def on_publish(client, userdata, mid):
  100. logging.info("message published " +str(mid))
  101.  
  102.  
  103. send_topic = "house/thermalPumpkin/sensors/rightSensor"
  104. client = mqtt.Client("rightSensor", False)
  105.  
  106. client.on_subscribe = on_subscribe
  107. client.on_disconnect = on_disconnect
  108. client.on_connect = on_connect
  109. client.on_message = on_message
  110. client.on_publish = on_publish
  111.  
  112. client.connect(broker, port)
  113. time.sleep(1)
  114. client.subscribe("house/sensors/thermalPumpkin/rightSensor")
  115. client.loop_start()
  116.  
  117. count = 1
  118.  
  119. def RSensor(client):
  120. global RSensorRunning
  121. print("in RSensor")
  122. while True:
  123. print("loop")
  124. IRSensor.wait_for_motion()
  125. print('seen')
  126. publish(client, "-1")
  127. return
  128.  
  129. # client = connect_mqtt()
  130. # rightSensorProcess = multiprocessing.Process(target=RSensor, args=(client,))
  131.  
  132.  
  133. def run():
  134. global count
  135. while True:
  136. msg="message " + str(count) + " from RSensor"
  137. count += 1
  138. time.sleep(5)
  139. client.disconnect()
  140. client.loop_stop()
  141. # client.loop_start()
  142. # # publish(client, "bruh moment")
  143. # rightSensorProcess.start()
  144.  
  145.  
  146. if __name__ == '__main__':
  147. run()
  148.  
Add Comment
Please, Sign In to add comment