Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. import paho.mqtt.client as mqtt
  4. import urllib
  5. from time import sleep
  6. import RPi.GPIO as GPIO
  7.  
  8.  
  9. #Conf GPIO Number for relays
  10. out_1 = 6
  11.  
  12. #Conf MQTT broker
  13. broker_ip = "192.168.1.34"
  14. broker_port = 1883
  15. broker_timeout = 60
  16. topic_sub = "/printer/#"
  17. topic_out1 = "/printer/onoff"
  18.  
  19. GPIO.setmode(GPIO.BCM)
  20. GPIO.setwarnings(False)
  21. GPIO.setup(out_1, GPIO.OUT)
  22. GPIO.output(out_1, GPIO.HIGH)
  23.  
  24. def main():
  25. # This is the issue part where I wanted to make looped check for actual value
  26. def check_state(astate):
  27. f= open("/sys/class/gpio/gpio6/value","r")
  28. if f.mode == "r":
  29. state = f.read(1)
  30. if astate == state :
  31. return
  32. else:
  33. print("CHANGE")
  34.  
  35. def on_connect(client, userdata, flags, rc):
  36. client.subscribe(topic_sub)
  37.  
  38. def on_message(client, userdata, msg):
  39. if msg.topic == topic_out1 :
  40. if msg.payload == "1" :
  41. GPIO.output(out_1, GPIO.LOW)
  42. state = "1"
  43. sleep(.1)
  44. print("OUT 1 ON")
  45. if msg.payload == "0" :
  46. GPIO.output(out_1, GPIO.HIGH)
  47. state = "0"
  48. sleep(.1)
  49. print("OUT 1 OFF")
  50.  
  51. client = mqtt.Client()
  52. client.on_connect = on_connect
  53. client.on_message = on_message
  54.  
  55. client.connect(broker_ip, broker_port, broker_timeout)
  56.  
  57. client.loop_forever()
  58.  
  59. if __name__ == "__main__":
  60. try:
  61. main()
  62. except KeyboardInterrupt:
  63. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement