Switching GPIO on RPi via mqtt-message


state: running/testing

A simple mqtt subscriber that changes gpio state on mqtt messages (based on https://github.com/ncopa/mqtt-exec) using wiringpi, therefor working on RaspberryPi (BCM2835, BCM2836 and BCM2837 SoC devices)

 Build requriements

During usage:

  • libmosquitto
  • wiringpi
  • openssl

Only required during build, and can be removed again after succesfull build:

  • C compiler + make (build-essential)
  • libmosquitto-dev
  • wiringpi-dev
  • openssl-dev or libssl-dev


This example shows how to change gpio state on receiving messages:

mqtt-gpio -h $mqtt_host -t $topic -g $wiringpi-pin

pin numbering from wiringPi is used: http://wiringpi.com/pins/

Send a message to whatever is your configured $topic with "1" or "false" (without quotes) to turn of most relay boards (default state after gpios are initialized), and send "0" or "true" (without quotes) to turn relays on.


More command line options via mqtt-gpio -?

mqtt-exec - execute command on mqtt messages
libmosquitto version: 1.4.10

usage: mqtt-exec [ARGS...] -t TOPIC ...

 -c,--disable-clean-session  Disable the 'clean session' flag
 -d,--debug                  Enable debugging
 -g,--gpio PIN               Set GPIO pin to use. Default is GPIO 17 (=WiringPi Pin 0)
 -h,--host HOST              Connect to HOST. Default is localhost
 -i,--id ID                  The id to use for this client
 -k,--keepalive SEC          Set keepalive to SEC. Default is 60
 -p,--port PORT              Set TCP port to PORT. Default is 1883
 -P,--password PASSWORD      Set password for authentication
 -q,--qos QOS                Set Quality of Serive to level. Default is 0
 -t,--topic TOPIC            Set MQTT topic to TOPIC. May be repeated
 -u,--username USERNAME      Set username for authentication
 --will-topic TOPIC          Set the client Will topic to TOPIC
 --will-payload MSG          Set the client Will message to MSG
 --will-qos QOS              Set the QoS level for client Will message
 --will-retain               Make the client Will retained
 --cafile FILE               Path to file containing CA certificates
 --capath DIR                Path to directory containing CA certificates
 --cert FILE                 Client certificate for authentication
 --key FILE                  Client private key for authentication
 --ciphers LIST              OpenSSL compatible list of TLS ciphers
 --tls-version VERSION       TLS protocol version: tlsv1.2 tlsv1.1 tlsv1
 --psk KEY                   pre-shared-key in hexadecimal (no leading 0x)
 --psk-identity STRING       client identity string for TLS-PSK mode


mqtt-gpio -h your_mqtt_host.domain.local -t topicexample/switch -g 0

(WiringPi PIN 0 => Pin 11 on RPi Header)

Example command to switch

On (Logical 0):
mosquitto_pub -h your_mqtt_host.domain.local -t topicexample/switch -m "true"

Off (Logical 1):
mosquitto_pub -h your_mqtt_host.domain.local -t topicexample/switch -m "false"




- update documentation/description
- add config-file support
- create Dockerfile/container for easy usage