You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.3 KiB

import paho.mqtt.client as mqtt
import logging
import pifacedigitalio
logging.basicConfig(
level=10,
format="%(asctime)s %(filename)s:%(lineno)s %(levelname)s %(message)s"
)
pifacedigital = pifacedigitalio.PiFaceDigital()
def on_connect(client, userdata, flags, rc):
"""Called when connected to MQTT broker."""
client.subscribe("cmnd/totoro/power")
client.subscribe("cmnd/totoro/#")
logging.info("Connected. Waiting for intents.")
def on_disconnect(client, userdata, flags, rc):
"""Called when disconnected from MQTT broker."""
client.reconnect()
def on_message(client, userdata, msg):
"""Called each time a message is received on a subscribed topic."""
logging.info('get message on ' + msg.topic)
if msg.topic == "cmnd/totoro/power":
client.publish("sensor/totoro/state",relay(not pifacedigital.output_pins[1].value))
def relay(v):
if v:
logging.info('on...')
pifacedigital.output_pins[1].turn_on()
return 'on'
else:
logging.info('off...')
pifacedigital.output_pins[1].turn_off()
return 'off'
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.connect("10.10.10.10", 1883)
client.loop_forever()