Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

GPIO Output update #31

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mqtt-gpio-monitor.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ MONITOR_PIN_NUMBERING = BOARD
MONITOR_OUT_INVERT = False
MONITOR_POLL = 0.1
MONITOR_REFRESH = mqtt-gpio-monitor/refresh
# specify output pins or leave blank for pfio
OUTPUT_PINS =
36 changes: 30 additions & 6 deletions mqtt-gpio-monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

PFIO_MODULE = False
GPIO_MODULE = False
GPIO_OUTPUT_PINS = []

# Script name (without extension) used for config/logfile names
APPNAME = os.path.splitext(os.path.basename(__file__))[0]
Expand Down Expand Up @@ -53,6 +52,8 @@
MONITOR_POLL = config.getfloat("global", "monitor_poll")
MONITOR_REFRESH = config.get("global", "monitor_refresh")

OUTPUT_PINS = config.get("global", "output_pins", raw=True)

# Initialise logging
LOGFORMAT = '%(asctime)-15s %(levelname)-5s %(message)s'

Expand Down Expand Up @@ -100,11 +101,27 @@
logging.debug("Not monitoring any pins")
else:
logging.debug("Monitoring pins %s" % PINS)

GPIO_OUTPUT_PINS = []
if OUTPUT_PINS:
GPIO_OUTPUT_PINS.extend(list(map(int, OUTPUT_PINS.split(","))))

if len(OUTPUT_PINS) == 0:
logging.debug("Not output pins configured")
else:
logging.debug("Output pins %s" % PINS)

# Append a column to the list of PINS. This will be used to store state.
for PIN in PINS:
PINS[PINS.index(PIN)] = [PIN, -1]

for PIN in GPIO_OUTPUT_PINS:
GPIO_OUTPUT_PINS[GPIO_OUTPUT_PINS.index(PIN)] = [PIN, -1]

MQTT_TOPIC_IN = MQTT_TOPIC + "/in/+"
MQTT_TOPIC_OUT = MQTT_TOPIC + "/out/%d"


MQTT_TOPIC_IN = MQTT_TOPIC + "/in/+"
MQTT_TOPIC_OUT = MQTT_TOPIC + "/out/%d"

Expand Down Expand Up @@ -196,7 +213,7 @@ def on_message(mosq, obj, msg):
GPIO.output(pin, GPIO.LOW)
else:
GPIO.output(pin, GPIO.HIGH)

# End of MQTT callbacks


Expand Down Expand Up @@ -246,17 +263,17 @@ def connect():
# Set TLS details
if MQTT_TLS_PROTOCOL:
if MQTT_TLS_PROTOCOL == 'tlsv1_2':
mqttc.tls_insecure_set(MQTT_TLS_INSECURE)
mqttc.tls_set(MQTT_CERT_PATH, tls_version=ssl.PROTOCOL_TLSv1_2)
if MQTT_TLS_PROTOCOL == 'tlsv1_1':
mqttc.tls_insecure_set(MQTT_TLS_INSECURE)
if MQTT_TLS_PROTOCOL == 'tlsv1_1':
mqttc.tls_set(MQTT_CERT_PATH, tls_version=ssl.PROTOCOL_TLSv1_1)
if MQTT_TLS_PROTOCOL == 'tlsv1':
mqttc.tls_insecure_set(MQTT_TLS_INSECURE)
if MQTT_TLS_PROTOCOL == 'tlsv1':
mqttc.tls_set(MQTT_CERT_PATH, tls_version=ssl.PROTOCOL_TLSv1)
if MQTT_TLS_PROTOCOL == 'sslv3':
mqttc.tls_insecure_set(MQTT_TLS_INSECURE)
if MQTT_TLS_PROTOCOL == 'sslv3':
mqttc.tls_set(MQTT_CERT_PATH, tls_version=ssl.PROTOCOL_SSLv3)
mqttc.tls_insecure_set(MQTT_TLS_INSECURE)

# Set the Last Will and Testament (LWT) *before* connecting
mqttc.will_set(MQTT_LWT, payload="0", qos=0, retain=True)
Expand Down Expand Up @@ -304,6 +321,13 @@ def init_gpio():
else:
GPIO.setup(pin, GPIO.IN)

for PIN in GPIO_OUTPUT_PINS:
index = [y[0] for y in GPIO_OUTPUT_PINS].index(PIN[0])
pin = GPIO_OUTPUT_PINS[index][0]

logging.debug("Initialising GPIO output pin %d..." % (pin))
GPIO.setup(pin, GPIO.IN)

def read_pin(pin):
state = -1
if PFIO_MODULE:
Expand Down