Skip to content

Instantly share code, notes, and snippets.

@nbuchwitz
Last active June 25, 2021 14:51
Show Gist options
  • Save nbuchwitz/08cc2d50b4eecd3d974115da3c65454e to your computer and use it in GitHub Desktop.
Save nbuchwitz/08cc2d50b4eecd3d974115da3c65454e to your computer and use it in GitHub Desktop.
RevPiModIO2 replace_io example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ts=4:sw=4:et
import revpimodio2
import paho.mqtt.client as mqtt
# connect to mqtt broker
client = mqtt.Client()
client.connect("your-broker.example.com", 1883)
def event_handler(io_name, io_value):
"""
event handler for sending io values via mqtt on value change
"""
client.publish(topic='yourtopic/{io}'.format(io=io_name), payload=io_value)
# instanciate revpimodio with automatic refresh of I/O
rpi = revpimodio2.RevPiModIO(autorefresh=True)
# handle CTRL+C for a clean exit of the programm
rpi.handlesignalend()
# define your I/O (pictory_name=Input_1, new_name=temperature, type=float, byteorder=big endian)
# adapt to your needs (esp. type and endianness)
# see python docs for other format characters than float:
# https://docs.python.org/3/library/struct.html#format-characters
rpi.io.Input_1.replace_io('temperature', 'f', byteorder='big')
# register our event handler to the newly defined io named 'temperature'
rpi.io.temperature.reg_event(event_handler)
# start mainloop (watching I/O)
rpi.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment