Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- import paho.mqtt.client as mqtt
- import urllib
- from time import sleep
- import RPi.GPIO as GPIO
- #Conf GPIO Number for relays
- out_1 = 6
- #Conf MQTT broker
- broker_ip = "192.168.1.34"
- broker_port = 1883
- broker_timeout = 60
- topic_sub = "/printer/#"
- topic_out1 = "/printer/onoff"
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(out_1, GPIO.OUT)
- GPIO.output(out_1, GPIO.HIGH)
- def main():
- # This is the issue part where I wanted to make looped check for actual value
- def check_state(astate):
- f= open("/sys/class/gpio/gpio6/value","r")
- if f.mode == "r":
- state = f.read(1)
- if astate == state :
- return
- else:
- print("CHANGE")
- def on_connect(client, userdata, flags, rc):
- client.subscribe(topic_sub)
- def on_message(client, userdata, msg):
- if msg.topic == topic_out1 :
- if msg.payload == "1" :
- GPIO.output(out_1, GPIO.LOW)
- state = "1"
- sleep(.1)
- print("OUT 1 ON")
- if msg.payload == "0" :
- GPIO.output(out_1, GPIO.HIGH)
- state = "0"
- sleep(.1)
- print("OUT 1 OFF")
- client = mqtt.Client()
- client.on_connect = on_connect
- client.on_message = on_message
- client.connect(broker_ip, broker_port, broker_timeout)
- client.loop_forever()
- if __name__ == "__main__":
- try:
- main()
- except KeyboardInterrupt:
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement