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
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
My self-hosting infrastructure, fully automated from empty disk to operating services

Khue's Homelab Current status: ALPHA This project utilizes Infrastructure as Code to automate provisioning, operating, and updating self-hosted servic

Khue Doan 6.4k Dec 31, 2022
Brogrammer-keyboard - FIrmware for the Brogrammer Keyboard v1.0

Brogrammer Keyboard Firmware The package contains the firmware that runs on the Brogrammer Keyboard v1.0 See https://imgur.com/a/oY5QZ14 This keyboard

Devin Hartleben 1 Apr 21, 2022
Using a raspberry pi, we listen to the coffee machine and count the number of coffee consumption

A typical datarootsian consumes high-quality fresh coffee in their office environment. The board of dataroots had a very critical decision by the end of 2021-Q2 regarding coffee consumption.

dataroots 51 Nov 21, 2022
Simples Keylogger para Windows com um autoboot implementado no sistema

MKW Keylogger Keylogger simples para Windos com um autoboot implementado no sistema, o malware irá capturar pressionamentos de tecla e armazená-lo em

3 Jul 03, 2021
Run this code to blink your ThinkPad LED with a hidden mysterious Morse code! ;)

TMorse Run this code to blink your ThinkPad LED with a hidden mysterious Morse code! ;) Compatible with python3.9+. No third-party library is required

Mahyar 2 Jul 11, 2022
Micropython-wifimanager-esp8266 - Simple Wifi Manager for ESP8266 using MicroPython

micropython-wifimanager-esp8266 Simple Wifi Manager for ESP8266 using MicroPytho

Abhinuv Nitin Pitale 1 Jan 04, 2022
🐱 Petkit feeder components for HomeAssistant

Petkit for HomeAssistant Installing Download and copy custom_components/xiaomi_miot folder to custom_components folder in your HomeAssistant config fo

62 Dec 29, 2022
Open-Source board for converting RaspberryPI to Brain-computer interface

The easiest way to the neuroscience world with the shield for RaspberryPi - PIEEG (website). Open-source. Crowdsupply This project is the result of se

Ildaron 436 Jan 01, 2023
This repository contains all the code and files needed to simulate the notspot quadrupedal robot using Gazebo and ROS.

Notspot robot simulation - Python version This repository contains all the files and code needed to simulate the notspot quadrupedal robot using Gazeb

50 Sep 26, 2022
Automatic CPU speed & power optimizer for Linux

Automatic CPU speed & power optimizer for Linux based on active monitoring of laptop's battery state, CPU usage, CPU temperature and system load. Ultimately allowing you to improve battery life witho

Adnan Hodzic 3.4k Jan 07, 2023
Simple Microservice to control 433Mhz wireless sockets over HTTP, e.g. on a RaspberryPi

REST-light is a simple microservice to control 433Mhz wireless sockets over HTTP, e.g. on a RaspberryPi. The main usage is an easy integration of 433M

Pascal Höhnel 1 Jan 09, 2022
A modular sequencer based on Pi Pico & EuroPi

PicoSequencer A modular sequencer based on Pi Pico & EuroPi by Zeno Van Moerkerke / Keurslager Kurt For now it is 'only' a trigger sequencer, but I si

5 Oct 27, 2022
Example Python code for building RPi-controlled robotic systems

RPi Example Code Example Python code for building RPi-controlled robotic systems These python files have been compiled / developed by the Neurobionics

Elliott Rouse 2 Feb 04, 2022
A global contest to grow and monitor your own food with Raspberry Pi

growlab A global contest to grow and monitor your own food with Raspberry Pi A capture from phototimer of my seed tray with a wide-angle camera positi

Alex Ellis 442 Dec 23, 2022
Интеграция Home Assistant с ЛК "Интер РАО"

ЕЛК ЖКХ «Интер РАО» для Home Assistant Предоставление информации о текущем состоянии ваших аккаунтов в ЕЛК ЖКХ. Введение @ TODO @ Установка Посредство

Alexander Ryazanov 27 Nov 05, 2022
Real-time Coastal Monitoring at the University of Hawaii at Manoa

Coastal Monitoring at the University of Manoa Source code for Beaglebone/RPi-based data loggers, shore internet gateways, and web server. Software dev

Stanley Lio 7 Dec 07, 2021
gdsfactory is an EDA (electronics design automation) tool to Layout Integrated Circuits.

gdsfactory 3.5.5 gdsfactory is an EDA (electronics design automation) tool to Layout Integrated Circuits. It is build on top of phidl gdspy and klayou

147 Jan 04, 2023
Python library for the Phomemo m02s bluetooth thermal printer

Phomemo M02S Python library This is a basic Python library for controlling the Phomemo M02S bluetooth thermal printer. It probably only works on Mac &

Stargirl Flowers 28 Nov 07, 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