In a recent Tutorial, I explained how to address the GPIO pins on a Raspberry from within Openhab, but that was when Openhab is actually on the same RaspberryPi whose pins you wanted to address.
So what if it is another raspberry, that is not the one that has OpenHAB installed on it?
In that case MQTT is a good solution.
I wrote a Python program (download) that will do exactly that. Python most likely will already be on your Raspberry Pi.
Instaling the libraries
You will need two libraries:
The lightweight MQTT client: Here I describe how to install it
The proces comes down to this:
pip install paho-mqtt
if you donāt have pip, first install pip with:
apt install python-pip #python 2
if for whatever reason this does not work for you, try:
git clone https://github.com/eclipse/paho.mqtt.python.git
(=> download the client)
cd paho.mqtt.python
(=> go to proper directory)
python setup.py install
(=> install the client)
depending on how you are logged in you may need to add āsudoā before the install commands
The second one you need to install is the Rpi.GPIO library to address the GPIO pins.
LadyAda gives a good explanation how to do that, but it basically comes down to issuing the command:
sudo apt-get install python-dev python-rpi.gpio
The actual program
The Python program (download) itself is fairly simple, but there are a few things to point out
The Rpi.GPIO library has two modes to set up the pins. it has āBCM modeā and āBOARD modeā
These two modes refer to the numbering of the pin.
The GPIO.BOARD option specifies that you are referring to the pins by the number of the pin the the plug - i.e the numbers printed on the board.
The GPIO.BCM option means that you are referring to the pins by the āBroadcom SOC channelā number, these are the numbers after āGPIOā that can be seen in the various pinout diagrams
In my program I am using pin 11 according to BCM numbering. that is pin23 in BOARD numbering.
If that is OK with you, you do not need to alter anything. If you want another pin, you just need to change the number after āLED1ā, but in its current state the program expects a a BCM number.
If you are still a bit puzzled, you can try and issue the gpio readall
command on your terminal and depending on the type of Raspberry you have, you may get outputs like this for the Rpi3:
or this for the B+:
In my program I used the BCM option but you can easily change that into āBOARDā. I use GPIO11 as output, but ofcourse that can also be changed easily.
You will also need to set the IP for your particular MQTT broker. and need to change the topic according to your wishes.
The program currently switches only one pin ON or OFF. it is not particularly hard to expand that for more than one pin. If you venture into that and you are not a crack in Python yet, let me draw your attention to statements like if msg.topic.startswith(ātopic1/ā):
The workings can be controlled by the gpio readal
l command (check in the column āVā what the state of the pin is)
In my program I used BCM pin 11 which is equal (on my raspi model) to BOARD pin 23.
The below picture shows the readout of the gpio readall
command after issuing an OFF and an ON MQTT payload.
I saved the program in my home/pi directory. It can be started by
python mqttpublish2.py (or any other name you want to give it)
If you open the program in an ssh window, it will stop once you close that window. Therefore you could best start the program on boot.
In an earlier Tutorial I explained how to do that.
Make sure you end your file commands in rc.local with a " &" (space and ampersand), otherwise only the first may be executed
OpenHAB
Controlling from OpenHAB is through the familiair MQTT channel:
Itemsfile:
Switch RaspiLight "Raspberry Light [%s]" <light> (GF_Living, Lights) {mqtt=">[mosquitto:raspiB/pin11:command:*:default],<[mosquitto:raspiB/pin11:state:default]"}
To avoid any confusion for people who might just be starting with OpenHAB or MQTT: the āraspiB/pin11ā topic is completely random, you may call it anything you want. you donāt have to call it āpin11ā just because you use pin 11, you may call it say āhome/lightā