Advertisement
MnMWizard

PLAB 2 paste 2

Oct 11th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.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. print("hello")
  6.  
  7. import time
  8. import board
  9. import digitalio
  10. import os
  11. from gpiozero import Servo
  12.  
  13. from paho.mqtt import client as mqtt
  14. from paho.mqtt import publish
  15. from paho.mqtt import subscribe
  16.  
  17.  
  18. IR = digitalio.DigitalInOut(board.D26)
  19. IR.direction = digitalio.Direction.INPUT
  20.  
  21. servo = Servo(16)
  22.  
  23. ThermalCamOff = 1 #temp variable at the moment
  24.  
  25. servoPos = 0
  26.  
  27.  
  28. broker = '192.168.1.15'
  29. port = 1883
  30. topic = "peripheralSensor/rightSensor"
  31. clientID = "rightPumpkin"
  32.  
  33. print("hello")
  34.  
  35. def connect_mqtt():
  36. def on_connect(client, userdata, flags, rc):
  37. if rc==0:
  38. print("connected to MQTT Broker!")
  39. else:
  40. print("failed to connect")
  41. client = mqtt.Client(clied=nt_id)
  42. client.on_connect = on_connect
  43. client.connect(broker, port)
  44. return client
  45.  
  46.  
  47. def publish(client, msg)
  48. result = client.publish(topic, msg)
  49. status = result[0]
  50. if status == 0:
  51. print(f"sent `{msg}` to topic `{topic}`")
  52. else:
  53. print(f"failed to send message to topic {topic}")
  54.  
  55.  
  56. def run():
  57. client = connect_mqtt()
  58. client.loop_start()
  59. #sensorCodeFunction here
  60.  
  61. if __name__ == '__main__':
  62. run()
  63.  
  64. #os.system("your terminal command here")
  65.  
  66.  
  67.  
  68. while(True):
  69.  
  70. # here should check status of the thermal camera, assign to ThermalCamState
  71.  
  72.  
  73. if(ThermalCamState==0):
  74. if(IR.value==1):
  75. #publish left IR trigger, need to change localhost when we get the broker set up.
  76. #os.system("mosquitto_pub -h localhost -t 'mqtt/leftsensortrig' -m '1'")
  77.  
  78. client.publish("rightSensorTrig", "1")
  79.  
  80. # colbi says bigger val = more counter clockwise, -1 to 1 range
  81. servo.value=-1
  82.  
  83. time.sleep(0.8) #however long the length of time of the IR trigger is
  84.  
  85. else: #if thermal cam is on
  86.  
  87. # here should check status of servo position from mid pumpkin, assign to servo pos
  88.  
  89. servo.value = servoPos
  90.  
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement