# Controlling a RaspberryPi GPIO pin with MQTT

**URL:** <https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912>\
**Category:** Tutorials & Examples\
**Tags:** raspberry, mqtt, gpio\
**Created:** [February 28, 2018, 6:09pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912 "2018-02-28T18:09:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kees\_van\_Gelder](https://community.openhab.org/user_avatar/community.openhab.org/kees_van_gelder/32/17497_2.png) [@Kees\_van\_Gelder](https://community.openhab.org/u/Kees_van_Gelder)\
**Post date:** [February 28, 2018, 6:09pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/1 "2018-02-28T18:09:30Z")

</div>

In a recent Tutorial, I [explained how to address the GPIO pins](https://community.openhab.org/t/simple-gpio-example/36062) 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](https://drive.google.com/open?id=1lwpqZwVPpJIgoT7q71d4pfz7gk1oBy-0)) 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](https://community.openhab.org/t/reading-a-dht11-22-on-a-raspberry-and-send-the-results-by-mqtt/40582)

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](https://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi/install-python-module-rpi-dot-gpio) 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](https://drive.google.com/open?id=1lwpqZwVPpJIgoT7q71d4pfz7gk1oBy-0)) 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

![Screenshot_2018-03-01_10-39-38](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/c/d/cd4bca291e6efbc11e5c532e17fa31886ffebcf4.png)

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:

 ![Gpi5](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/7/c/7ceb1a4d645d7b536720657fc2baaaeadb3bd96e.jpeg)  
or this for the B+:  
 ![gpiob%20](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/6/e/6e0491c84ee84aa17869c840f9de5ab65fa502ae.png)  
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.

 ![mqttpyconfig](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/8/e/8e984e65d3ec0c13824bedcf60ef8c2b55d6ac26.png)

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.  
 ![Screenshot_2018-03-01_10-21-36](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/5/1/51025482f7a7f4adaef891c7087c48007ba4abab.png)

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)

 ![mqttpublish](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/6/f/6f8c7b589b07961fac690193cea06a3e3dfc7045.png)

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](https://community.openhab.org/t/reading-a-dht11-22-on-a-raspberry-and-send-the-results-by-mqtt/40582) 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_”

---

<div class="post-metadata">

**Author:** ![allen](https://community.openhab.org/user_avatar/community.openhab.org/allen/32/20309_2.png) [@allen](https://community.openhab.org/u/allen)\
**Post date:** [August 24, 2018, 9:15am UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/2 "2018-08-24T09:15:36Z")

</div>

Hi. say I have more then one topic that I want to subscribe to how do I do that ?

---

<div class="post-metadata">

**Author:** ![Kees\_van\_Gelder](https://community.openhab.org/user_avatar/community.openhab.org/kees_van_gelder/32/17497_2.png) [@Kees\_van\_Gelder](https://community.openhab.org/u/Kees_van_Gelder)\
**Post date:** [August 25, 2018, 3:58pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/3 "2018-08-25T15:58:11Z")

</div>

usually done with a wildcard: topic/#  
I haven’t done that with the paho-mqtt library yet as I didn’t have the need, but that’s how I do it on Arduino/ESP, so I presume it is the same here

---

<div class="post-metadata">

**Author:** ![allen](https://community.openhab.org/user_avatar/community.openhab.org/allen/32/20309_2.png) [@allen](https://community.openhab.org/u/allen)\
**Post date:** [August 25, 2018, 7:45pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/4 "2018-08-25T19:45:14Z")

</div>

Ok. But if I want to send to multipels

Example I have 8 Temp sensors

Bedroom 1 - 3 and so on.

Bathroom

How would that work ?

---

<div class="post-metadata">

**Author:** ![H102](https://community.openhab.org/user_avatar/community.openhab.org/h102/32/25595_2.png) [@H102](https://community.openhab.org/u/H102)\
**Post date:** [August 25, 2018, 8:48pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/5 "2018-08-25T20:48:19Z")

</div>

> [@allen](#):
>
> Ok. But if I want to send to multipels
> 
> Example I have 8 Temp sensors
> 
> Bedroom 1 - 3 and so on.
> 
> Bathroom
> 
> How would that work ?

All connected to RPI gpio pins or just multiple MQTT messages?

---

<div class="post-metadata">

**Author:** ![allen](https://community.openhab.org/user_avatar/community.openhab.org/allen/32/20309_2.png) [@allen](https://community.openhab.org/u/allen)\
**Post date:** [August 25, 2018, 9:37pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/6 "2018-08-25T21:37:10Z")

</div>

Yes all on my rpi

And need to go to mqtt ?

---

<div class="post-metadata">

**Author:** ![allen](https://community.openhab.org/user_avatar/community.openhab.org/allen/32/20309_2.png) [@allen](https://community.openhab.org/u/allen)\
**Post date:** [August 26, 2018, 12:30pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/7 "2018-08-26T12:30:17Z")

</div>

I want most of my sensors that do my enviro monitoring running of my rpi. And comunicate via mqtt. That i have in center of my house. My ligits runs off of sonnof t1  
And my other power controls i use esp8266

---

<div class="post-metadata">

**Author:** ![allen](https://community.openhab.org/user_avatar/community.openhab.org/allen/32/20309_2.png) [@allen](https://community.openhab.org/u/allen)\
**Post date:** [August 27, 2018, 9:46am UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/8 "2018-08-27T09:46:37Z")

</div>

Or Is there other suggestions on how to do this as the rip that the sensors are connected to is my openhab server aswell.

---

<div class="post-metadata">

**Author:** ![H102](https://community.openhab.org/user_avatar/community.openhab.org/h102/32/25595_2.png) [@H102](https://community.openhab.org/u/H102)\
**Post date:** [August 27, 2018, 11:58am UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/9 "2018-08-27T11:58:46Z")

</div>

Check out this link:

[https://github.com/rkoshak/sensorReporter](https://github.com/rkoshak/sensorReporter)

---

<div class="post-metadata">

**Author:** ![Kees\_van\_Gelder](https://community.openhab.org/user_avatar/community.openhab.org/kees_van_gelder/32/17497_2.png) [@Kees\_van\_Gelder](https://community.openhab.org/u/Kees_van_Gelder)\
**Post date:** [August 27, 2018, 1:00pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/10 "2018-08-27T13:00:28Z")

</div>

perhaps I misunderstand the question, but it is just as always with mqtt, check for the incoming message and then take action on that basis.

The program I provided only checks on Payload ON/OF as I only control one pin and am subscribed to one topic, but if you want to control more pins, you also need to check on ‘Topic’’

So in my program, before the check:  
`if (msg.payload=='ON'):`

you would first check which topic, like so:  
`if (msg.topic=='mytopic1'):`

and then that is followed by the ON/OFF check

You repeat that for the amount of topics you are subscribed to.

so it would be:

```
check for topic 1
   if payload ==ON ->take action
   if payload==OFF -> take action

check for topic 2
   if payload ==ON ->take action
   if payload==OFF -> take action

etcetera

```

Now ofcourse if it is all on the same raspi as your openhab install, you do not need mqtt, then you just let openhab set the desired pins directly, via the GPIO binding, as I explained here:[https://community.openhab.org/t/simple-gpio-example/36062?u=kees\_van\_gelder](https://community.openhab.org/t/simple-gpio-example/36062)

---

<div class="post-metadata">

**Author:** ![allen](https://community.openhab.org/user_avatar/community.openhab.org/allen/32/20309_2.png) [@allen](https://community.openhab.org/u/allen)\
**Post date:** [August 27, 2018, 1:08pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/11 "2018-08-27T13:08:53Z")

</div>

i was thinking if that would not be better doing it direct.  
my other question is how would I do analog ?  
I ill be using a map 3008 for to mq-05 gas sensors  
and 7 x dht 22, and then only one relay that turns a extractor on and off in the bath room.

will draw out a diagram showing my idea and setup.

---

<div class="post-metadata">

**Author:** ![Kees\_van\_Gelder](https://community.openhab.org/user_avatar/community.openhab.org/kees_van_gelder/32/17497_2.png) [@Kees\_van\_Gelder](https://community.openhab.org/u/Kees_van_Gelder)\
**Post date:** [August 27, 2018, 1:14pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/12 "2018-08-27T13:14:53Z")

</div>

the raspi is not really great at doing analog signals, as it has no ADC, although it is possible to use an analog signal that charges a capacitor and then measure the time it takes to reach a certain voltage. But I wouldn’t advise to go that way.

I presume you mean the MCP3008, that is certainly an option. I think that one uses SPI. My preference would be an I2C module and there are plenty of those as well  
the 7xdht22 can all be handled with the digital pins of the raspberry.Plenty of totorials available and I did one as well here on the forum [Reading a DHT11/22 on a raspberry and send the results by MQTT](https://community.openhab.org/t/reading-a-dht11-22-on-a-raspberry-and-send-the-results-by-mqtt/40582)

---

<div class="post-metadata">

**Author:** ![allen](https://community.openhab.org/user_avatar/community.openhab.org/allen/32/20309_2.png) [@allen](https://community.openhab.org/u/allen)\
**Post date:** [August 27, 2018, 1:19pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/13 "2018-08-27T13:19:39Z")

</div>

sorry about the typo.

I would also rather look at using the i2c.  
the mQ-x range comes mostly with AQ. and only a few with DQ. the one I am looking at using is analog. for measuring LPG an Co2.in the kitchen and in the entertainment area .

---

<div class="post-metadata">

**Author:** ![allen](https://community.openhab.org/user_avatar/community.openhab.org/allen/32/20309_2.png) [@allen](https://community.openhab.org/u/allen)\
**Post date:** [August 27, 2018, 1:20pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/14 "2018-08-27T13:20:33Z")

</div>

but I can also read the dht22 directly in openhab no need to use mqtt

---

<div class="post-metadata">

**Author:** ![Kees\_van\_Gelder](https://community.openhab.org/user_avatar/community.openhab.org/kees_van_gelder/32/17497_2.png) [@Kees\_van\_Gelder](https://community.openhab.org/u/Kees_van_Gelder)\
**Post date:** [August 27, 2018, 1:21pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/15 "2018-08-27T13:21:07Z")

</div>

consider he ADS1115

---

<div class="post-metadata">

**Author:** ![H102](https://community.openhab.org/user_avatar/community.openhab.org/h102/32/25595_2.png) [@H102](https://community.openhab.org/u/H102)\
**Post date:** [August 27, 2018, 1:22pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/16 "2018-08-27T13:22:01Z")

</div>

You could also use your esp8266, depending where you need the sensors, it has ADC ability. Heres example/guide on how to use:[https://www.instructables.com/id/ESP8266-with-Multiple-Analog-Sensors/](https://www.instructables.com/id/ESP8266-with-Multiple-Analog-Sensors/)

---

<div class="post-metadata">

**Author:** ![Kees\_van\_Gelder](https://community.openhab.org/user_avatar/community.openhab.org/kees_van_gelder/32/17497_2.png) [@Kees\_van\_Gelder](https://community.openhab.org/u/Kees_van_Gelder)\
**Post date:** [August 27, 2018, 1:25pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/17 "2018-08-27T13:25:42Z")

</div>

certainly a possibility  
@allen  
keep in mind the MQ-x sensors need preheating, though some are OK to just remain connected to Vcc, some others need a certain cycling of the applied voltage.Certainly the CO sensor needs that. Not sure about the MQ-5

---

<div class="post-metadata">

**Author:** ![allen](https://community.openhab.org/user_avatar/community.openhab.org/allen/32/20309_2.png) [@allen](https://community.openhab.org/u/allen)\
**Post date:** [August 27, 2018, 1:29pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/18 "2018-08-27T13:29:53Z")

</div>

what other sensors would you recommend for the application ?

---

<div class="post-metadata">

**Author:** ![Kees\_van\_Gelder](https://community.openhab.org/user_avatar/community.openhab.org/kees_van_gelder/32/17497_2.png) [@Kees\_van\_Gelder](https://community.openhab.org/u/Kees_van_Gelder)\
**Post date:** [August 27, 2018, 1:37pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/19 "2018-08-27T13:37:20Z")

</div>

well that all depends on what you like to measure.  
depending on the purpose (like CO2 for plant growth) I would generally be more interested in CO levels.  
LPG, I am not sure. I guess it will also react to the gas you might be cooking on in yr kitchen,

MQ-2 does methane and Butane, LPG and smoke  
MQ-4 does methane and CNG  
MQ-6 does butane and LPG  
MQ-7 does CO  
seems only the MQ7 needs to cycle between 5V and 1.4V.the others just need 5 or 6 v  
Full summary here [Arduino Playground - HomePage](http://playground.arduino.cc/Main/MQGasSensors)

---

<div class="post-metadata">

**Author:** ![allen](https://community.openhab.org/user_avatar/community.openhab.org/allen/32/20309_2.png) [@allen](https://community.openhab.org/u/allen)\
**Post date:** [August 27, 2018, 1:39pm UTC](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912/20 "2018-08-27T13:39:53Z")

</div>

I had a look at this now.

will get one and test. and see what happens. the co I might just leave  
as long as I can read lpg and say smoke. like the mq-2

> **[MQ-2 Flammable Gas & Smoke Sensor - Micro Robotics](https://www.robotics.org.za/1480?search=mq)**
>
> Flammable Gas & Smoke Sensor, Gas, SensorMQ-2 Flammable Gas & Smoke Sensor - MQ-2 Flammable Gas and Smoke Sensor + Interface BoardThis semiconductor gas sensor detects the presence of combustible gas and smoke at concentrations from 300 to 10,000

[Next page](https://community.openhab.org/t/controlling-a-raspberrypi-gpio-pin-with-mqtt/40912.md?page=2)
