A python script to poll RPi GPIO pins and subscribe and publish their state via MQTT

Related tags

HardwareMQTT-GPIO
Overview

MQTT-GPIO

A python script to poll RPi GPIO pins and subscribe and publish their state via MQTT using TLS.

This script is short and meant to be edited to the specific setup.

Configuration

The script is edited directly, there is no separate config file.

Polling intervall

The alternative to polling pins is to use interrupts.

Interrupts are good for very short signals and they prevent the CPU to query the level again and again, basically keeping the CPU busy. However, this can also be a downside if for instance, while switching the outputs, the inputs might briefly detect a level change, causing false or unwanted signals. Stronger pull-up or pull-down resistors than the RPIs internal ones, could be used to overcome such issues. Alternatively, just poll the inputs when the outputs are not changing state. The CPU consumption for "wastefully" polling four inputs every 100 ms is 1-3% on a RPi 1B, which is acceptable for many use cases.

The variable POLL_INTERVALL = 0.1 configures the main loop to pause 100 ms before it checks the states again. If this is too slow or too quick for your use case, simply adjust it. The value is specified in seconds, float values are working as well. Higher values reduce CPU consumption, but increase reaction time; smaller values increase the CPU load and decrease reaction time.

Debugging messages

To see a verbose output of what the script is doing, set the variable PRINT_MESSAGES = True.

MQTT Server parameters

This script considers encryption to be mandatory.

  • The variable HOST = "server.lan" configures the IP or FQDN (e.g. the server name).
  • With PORT = 8883 you set the port of the MQTT server. Default for encrypted MQTT connections is 8883.
  • With USERNAME = "username" the username is set. This script expects it to be mandatory.
  • With PASSWORD = "password" the password is set. This script expects it to be mandatory.
  • With TOPIC_PREFIX = "living_room" the initial string of the MQTT topic is set. It really depends on your setup what is used.
  • With PAYLOAD = {True: 'ON', False: 'OFF'} the values for True/High level of a pin and False/Low level of a pin are configured. If an input is read as "High" it corresponds to True. This mapping/dictionary will be used to convert it to the string "ON" and later publish that value. If you need to change it, adjust this dictionary. Some homeautomation-systems expect lower-case values: that would be PAYLOAD = {True: 'on', False: 'off'} for example.
  • With CA_CERTS = "/root/your_ca.crt" the script gets the Root-CA-certificate. If you use self signed-certificates this is the file containing the public certificate of your Root-CA.
  • The variable CERTFILE = "/root/your_cert.crt" is the public certificate of this server/computer the script is running on, signed by the Root-CA.
  • The variable KEYFILE = "/root/your_key.pem" is the private, confidential key of this server/computer the script is running on. This file is not to be shared.

Pins / GPIOs

GPIOs as output

To configure the outputs fill the array Outputs with objects of class OUTPUT. For example you just have one GPIO-output named "Output_01" at pin 23, the array is configured: Outputs = [OUTPUT("Output_01", 23)]. If you have more outputs, simply add those to the array. Outputs are set from MQTT by publishing the value "ON" to the topic "living_room/Output_01/set". Note the appended "/set" in the full topic to set the value. Outputs are read from MQTT by subscribing to the topic "living_room/Output_01". The state is published when it changes.

GPIOs as inputs

To configure the inputs fill the array Inputs with objects of class INPUT. For example you just have one GPIO-input named "Schalter_01" at pin 8, the array is configured: Inputs = [INPUT("Schalter_01", 8)]. If you have more inputs, simply add those to the array. Inputs are read from MQTT by subscribing to the topic "living_room/Schalter_01". The state is published when it changes.

Dependencies

  • RPi.GPIO
  • paho-mqtt
Python Keylogger for Linux

A keylogger is a program that records your keystrokes, this program saves them in a .txt file on your local computer and, after 30 seconds (or as long as you want), it will close the .txt file and se

Darío Mazzitelli 4 Jul 31, 2021
OpenStickFirmware is open source software designed to handle any and all tasks required in a custom Fight Stick

OpenStickFirmware is open source software designed to handle any and all tasks required in a custom Fight Stick. It can handle being the brains of your entire stick, or just handling the bells and wh

Sleep Unit 23 Nov 24, 2022
MPY tool - manage files on devices running MicroPython

mpytool MPY tool - manage files on devices running MicroPython It is an alternative to ampy Target of this project is to make more clean code, faster,

Pavel Revak 5 Aug 17, 2022
emhass: Energy Management for Home Assistant

emhass EMHASS: Energy Management for Home Assistant Context This module was conceived as an energy management optimization tool for residential electr

David 70 Dec 24, 2022
Tool to create 3D printable terrain with integrated path/road part files (Single material 3d printer)

BACKGROUND This has been an ongoing project of mine for a few months now. I run trails a lot and original the goal was to create a function to combine

9 Apr 26, 2022
PyTorch implementation of paper "MT-ORL: Multi-Task Occlusion Relationship Learning" (ICCV 2021)

MT-ORL: Multi-Task Occlusion Relationship Learning Official implementation of paper "MT-ORL: Multi-Task Occlusion Relationship Learning" (ICCV 2021) P

Panhe Feng 12 Oct 11, 2022
A simple Python script for toggling Philips Hue Lights by clapping

LightsClap A simple Python script for toggling Philips Hue Lights by clapping Usage pip3 install -r requirements.txt python3 main.py and press the Ent

Flux Industries 2 Nov 16, 2021
Vvim - Keyboardless Vim interactions

This is done via a hardware glove that the user wears. The glove detects the finger's positions and translates them into key presses. It's currently a work in progress.

Boyd Kane 8 Nov 17, 2022
Provide Unifi device info via api to Home Assistant that will give ap sensors

Unifi AP Device info Provide Unifi device info via api to Home Assistant that will give ap sensors

12 Jan 07, 2023
Turn your Raspberry Pi Pico into a USB Rubber Ducky

pico-ducky Turn your Raspberry Pi Pico into a USB Rubber Ducky Install Requirements CircuitPython for the Raspberry Pi Pico adafruit-circuitpython-bun

Konstantinos 5 Nov 08, 2022
Kwcpu - An unobtrusive CPU meter that fits in the default Windows 11 taskbar. Supports up to 32 cores.

kwcpu An unobtrusive CPU meter that fits in the default Windows 11 taskbar. Supports up to 32 cores. kwcpu is provided as a Rainmeter skin. By default

Jay Oster 2 Nov 07, 2022
Ansible tools for operating and managing fleets of Blinksticks in harmony using the Blinkstick Python library.

Ansible tools for operating and managing fleets of Blinksticks in harmony using the Blinkstick Python library.

Greg Robinson 3 Aug 10, 2022
An arduino/ESP project that can play back G-Force data previously recorded

An arduino/ESP project that can play back G-Force data previously recorded

7 Apr 12, 2022
View your medication from Medisafe Cloud in Home Assistant

Medisafe View your medication from Medisafe Cloud in Home Assistant. This integration adds sensors for today's upcoming, taken, skipped, and missed do

Sam Steele 12 Dec 27, 2022
Robo Arm :: Rigging is a rigging addon for Blender that helps animating industrial robotic arms.

Robo Arm :: Rigging Robo Arm :: Rigging is a rigging addon for Blender that helps animating industrial robotic arms. It construct serial links(a kind

2 Nov 18, 2021
DongshanPI Seven for STM32MP157DAC.

STM32MP1 Buildroot External Tree

DongshanPI 14 May 06, 2022
A versatile program that uses the raspberry pi camera and provides it as a service

PiCameleon Is a daemon program meant to provide the RaspberryPi Camera as a service while running according to a configuration.

André Esser 52 Oct 16, 2022
Pure micropython ESP32 SPI driver for sdcard and screen at the same SPI bus

micropython-esp32-spi-sdcard-and-screen-driver Proof of concept of Pure micropython espidf SPI driver for sdcard with screen at the same SPI bus (exam

Thomas Favennec 7 Mar 14, 2022
a weather application for the raspberry pi and the Pimorioni Inky pHAT.

raspi-weather a weather application for the raspberry pi and the Inky pHAT

Derek Caelin 59 Oct 24, 2022
Event-based hardware simulation framework

An event-based multi-device simulation framework providing configuration and orchestration of complex multi-device simulations.

Diamond Light Source Controls Group 3 Feb 01, 2022