Raspberry pi GPIO control via TCP

1,185 views
Skip to first unread message

bele...@gmail.com

unread,
Aug 30, 2014, 7:27:07 PM8/30/14
to ope...@googlegroups.com
Hello all,

I would like to control an 8-relay board attached to GPIO port of a brand new raspberry pi via tcp.

I looked at the GPIO binding but I think it only works when openhab is running on the pi. My openhab setup runs ona dedicated pc on the same network as the pi.

I found some information about webiopi but don't know how or if it is possible to use that witb openhab. I do not have a piface board either, all i need is to control the gpio pins directly.

The opensprinkler pi implementation looks like a good start but i do not plan to use a shift register on the outputs so this can not be used directly.

I would like to find out:

1. Which image should I start with I write to my sd card?
2. Which script should I install on the pi?
3. Which binding to use on the openhab side? (Gpio, tcp, opensprinker?)

I have very good knowledge of programming microcontrollers and application development in C and C++. I am also learning Python at the same time. Hope this may help :-)

Thank you

Eric

Eugene Schava

unread,
Aug 31, 2014, 7:26:08 AM8/31/14
to ope...@googlegroups.com, bele...@gmail.com
You can install OpenHAB on PI as well and use MQTT for communication between instances


Неділя, 31 серпня 2014 р. 02:27:07 UTC+3 користувач bele...@gmail.com написав:

Henrik Hausner

unread,
Aug 31, 2014, 2:49:32 PM8/31/14
to ope...@googlegroups.com, bele...@gmail.com
Another approche could be REST: https://github.com/openhab/openhab/wiki/REST-API

bele...@gmail.com

unread,
Sep 1, 2014, 5:06:16 PM9/1/14
to ope...@googlegroups.com
Made some progress... I think ;-)

I installed Mosquitto MQTT Broker on my windows machines that runs openHAB. On the windows machine, I am able to start mosquitto-pub and mosquitto-req to publish and received topics. I setup MQTT in openhab.cfg to broadcast the event bus on MQTT. I see the event in mosquitto-req running on the same PC as openhab or even on another PC where I installed mosquitto and running mosquitto-req. Events on openhab bus are now broadcasted to another PC via TCP.

On on raspberry pi, I used mqtt-gpio-monitor.py program as discussed here:
https://github.com/sumnerboy12/mqtt-gpio-monitor

My windows machine running openhab and mosquitto is on 192.168.2.39
The mqtt-gpio-monitor program cannot connect to 192.168.2.39. The log file shows : ERROR Error connecting to 192.168.2.39:1833: [Errno 111] Connection refused

I made sure the pi sees the windows machine, ping 192.168.2.39 works. I am also able to connect to mosquitto from another pc so the problem seems to be with the pi.

Below is the content of my mqtt-gpio-monitor.ini file:

[global]
MODULE = <gpio>
DEBUG = True

MQTT_HOST = 192.168.2.39
MQTT_PORT = 1833
MQTT_USERNAME =
MQTT_PASSWORD =
MQTT_CLIENT_ID = mqtt-gpio-monitor
MQTT_QOS = 2
MQTT_RETAIN = False
MQTT_CLEAN_SESSION = True

MQTT_TOPIC = /openHAB
MQTT_LWT = /clients/mqtt-gpio-monitor

MONITOR_PINS = 1, 2, 3, 4
MONITOR_POLL = 0.1
MONITOR_REFRESH = /mqtt-gpio-monitor/refresh

Any idea what could be wrong?

Eric

Ben Jones

unread,
Sep 1, 2014, 5:21:11 PM9/1/14
to ope...@googlegroups.com, bele...@gmail.com
Without knowing too much about your setup I would suggest checking the mosquitto configuration to ensure it is permitting connections from machines other than localhost. Can you install the mosquitto client on the Raspberry Pi and check if you can connect to your broker using that?

bele...@gmail.com

unread,
Sep 1, 2014, 5:28:38 PM9/1/14
to ope...@googlegroups.com
I already tried on another PC and it works.

Running:
mosquitto_sub -h 192.168.2.39 -t /# -v

I am able to see openhab event bus ON and OFF commands on items.

I did not change any settings in mosquitto.conf. In fact I can not make any changes to it as windows is telling me the file is in use, even after I stop the mosquitto broken from the SERVICES in Control Panel...

Ben Jones

unread,
Sep 1, 2014, 5:39:29 PM9/1/14
to ope...@googlegroups.com, bele...@gmail.com
Yep - but can you try on the RPi?

bele...@gmail.com

unread,
Sep 1, 2014, 5:41:33 PM9/1/14
to ope...@googlegroups.com
How do I install the mosquitto client on the pi?

Sorry, Noob here ;-)

Ben Jones

unread,
Sep 1, 2014, 5:42:13 PM9/1/14
to ope...@googlegroups.com
Google is your friend ;).

Tuesday, 2 September 2014 9:41 a.m.
How do I install the mosquitto client on the pi?

Sorry, Noob here ;-)

Tuesday, 2 September 2014 9:39 a.m.
Yep - but can you try on the RPi?
--
You received this message because you are subscribed to a topic in the Google Groups "openhab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openhab/Y65IYe7WMI4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.
Tuesday, 2 September 2014 9:28 a.m.
Tuesday, 2 September 2014 9:21 a.m.
Without knowing too much about your setup I would suggest checking the mosquitto configuration to ensure it is permitting connections from machines other than localhost. Can you install the mosquitto client on the Raspberry Pi and check if you can connect to your broker using that?
--
You received this message because you are subscribed to a topic in the Google Groups "openhab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openhab/Y65IYe7WMI4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.

Dirk Boon

unread,
Dec 8, 2014, 5:57:22 PM12/8/14
to ope...@googlegroups.com
Hi,

I have the same problem as described.
Mosquitto is installed on the Raspberry here.
with "localhost" I got an error "address family not supported by protocol"
with RPI's IP address I get "connection refused"
I did not make any conf for Mosquitto
Who can help ?
Thx,
DirkB

Op maandag 1 september 2014 23:42:13 UTC+2 schreef Ben Jones:

Dirk Boon

unread,
Dec 8, 2014, 6:12:26 PM12/8/14
to ope...@googlegroups.com
OK, i found that the port was set wrong in mqtt-gpio-monitor.ini, now from the  mqtt-gpio-monitor.log I see it is connected to localhost:1883
On my android phone, I have MQTT installed and I am also connected to the Mosquitto broker on the RPI
however when I do :
sudo python mqtt-gpio-monitor.py
I get this :
Traceback (most recent call last):
  File "mqtt-gpio-monitor.py", line 330, in <module>
    poll()
  File "mqtt-gpio-monitor.py", line 310, in poll
    if newstate != oldstate:
UnboundLocalError: local variable 'newstate' referenced before assignment
Exception in thread Thread-1 (most likely raised during interpreter shutdown):
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
  File "/usr/lib/python2.7/threading.py", line 505, in run
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2180, in _thread_main
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1212, in loop_forever
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 808, in loop

When I change the state of a monitored pin, I see no message in MQTT (android)
Also when publishing a message "mqtt-gpio-monitor/in/23  0" to the broker, nothing happens on GPIO pin 23.

What am I missing here ?
Thx,
Dirk

Op maandag 8 december 2014 23:57:22 UTC+1 schreef Dirk Boon:

Ben Jones

unread,
Dec 8, 2014, 6:18:49 PM12/8/14
to ope...@googlegroups.com
What module have you enabled in your mqtt-gpio-monitor.ini file (as per the instructions at https://github.com/sumnerboy12/mqtt-gpio-monitor#mqtt-gpio-monitor)?

Dirk Boon

unread,
Dec 9, 2014, 7:52:26 AM12/9/14
to ope...@googlegroups.com
only GPIO if this is your question ?

Op dinsdag 9 december 2014 00:18:49 UTC+1 schreef Ben Jones:

Dirk Boon

unread,
Dec 9, 2014, 7:54:28 AM12/9/14
to ope...@googlegroups.com
MODULE             = <gpio>
is this syntax correct, or should it be
MODULE             = gpio

Op dinsdag 9 december 2014 13:52:26 UTC+1 schreef Dirk Boon:

Ben Jones

unread,
Dec 9, 2014, 2:10:42 PM12/9/14
to ope...@googlegroups.com
MODULE = gpio

Dirk Boon

unread,
Dec 9, 2014, 2:30:59 PM12/9/14
to ope...@googlegroups.com
Ok, I changed it, now i get :
root@PIMATIC:/home/pi/MQTT# sudo python mqtt-gpio-monitor.py
Traceback (most recent call last):
  File "mqtt-gpio-monitor.py", line 326, in <module>
    init_gpio()
  File "mqtt-gpio-monitor.py", line 272, in init_gpio
    GPIO.setup(pin, GPIO.IN)
ValueError: The channel sent is invalid on a Raspberry Pi
r

And here is my ini file :
[global]
MODULE             = gpio
DEBUG              = True

MQTT_HOST          = localhost
MQTT_PORT          = 1883
MQTT_USERNAME      =
MQTT_PASSWORD      =
MQTT_CLIENT_ID     = mqtt-gpio-monitor
MQTT_QOS           = 2
MQTT_RETAIN        = False
MQTT_CLEAN_SESSION = True

MQTT_TOPIC         = /mqtt-gpio-monitor
MQTT_LWT           = /clients/mqtt-gpio-monitor

MONITOR_PINS       = 25
MONITOR_POLL       = 0.1
MONITOR_REFRESH    = /mqtt-gpio-monitor/refresh


Op dinsdag 9 december 2014 20:10:42 UTC+1 schreef Ben Jones:

Ben Jones

unread,
Dec 9, 2014, 2:34:16 PM12/9/14
to ope...@googlegroups.com
So you might have to do a little thinking here for yourself. What does that error (The channel sent is invalid on a Raspberry Pi) sound like to you? I have never seen that error myself but it is pretty obvious what the problem is...


Dirk Boon

unread,
Dec 9, 2014, 3:29:25 PM12/9/14
to ope...@googlegroups.com
Hi Ben,

Your support is much appreciated, thank you.
I did a little thinking indeed and deducted that the actual physical pin nr's are required and not the GPIO pin nrs.

For newbies (like myself) who never done python, I must say that getting this stuff going is really quite frustrating mostly because of lack or (incomplete) documentation.
All my posts would have been unnecessary if there had been some kind of "mqtt-gpio-monitor for dummy's"-explanation.
But no worries, I'm getting there ... step by step.
Thanks again !
Grtz,
DirkB



Op dinsdag 9 december 2014 20:34:16 UTC+1 schreef Ben Jones:

Ben Jones

unread,
Dec 9, 2014, 3:37:04 PM12/9/14
to ope...@googlegroups.com
Hi Dirk,

I do understand your frustration, I have been thru it all myself ;). But in my experience if someone spoon feeds everything you don't end up understanding it and then when you have problems later on you are back to square one.

The issue is something like mqtt-gpio-monitor was written as a tool (by me) to solve a specific task. I released it to group in the hope someone else would find it useful, but it is by no means a 'supported' tool. It is not part of openHAB but just something I knocked together to help with integrating MQTT enabled RPis.

If you want to contribute a 'mqtt-gpio-monitor for dummies' document then I am sure future users would be very appreciative!

Cheers,
Ben

Dirk Boon

unread,
Dec 9, 2014, 4:55:20 PM12/9/14
to ope...@googlegroups.com
Ben,

No worries and thanks again.

Now for my next raspbian-newbie problem. Getting your scipt ro run at reboot of the pi.
Seriously, I've been googling and trialing it now for another hour and nothing seems to work.... grrrr
I tried :
rc.local
/etc/init
crontab
to no avail so far.
Cheers,
DirkB

Op dinsdag 9 december 2014 21:37:04 UTC+1 schreef Ben Jones:

Ben Jones

unread,
Dec 9, 2014, 4:59:04 PM12/9/14
to ope...@googlegroups.com
I am using supervisor on my Pis. Definitely worth a look.

I can browse to the RPi (on port 9003) and start/stop services and check the status.

Here is my supervisor config to help out;

[unix_http_server]
file = /var/run/supervisor.sock

[inet_http_server]         ; inet (TCP) server disabled by default
port=*:9003                ; (ip_address:port specifier, *:port for all iface)

[supervisord]
logfile = /var/log/supervisord.log
logfile_maxbytes = 200KB
logfile_backups = 1
pidfile = /var/run/supervisord.pid
childlogdir = /var/log

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl = unix:///var/run/supervisor.sock

[program:mqtt-gpio-monitor]
environment = LOGFILE="/tmp/mqtt-gpio-monitor.log"
command = /home/pi/mqtt-gpio-monitor/mqtt-gpio-monitor.py
directory = /home/pi/mqtt-gpio-monitor
user = root


Wednesday, 10 December 2014 10:55 a.m.
Ben,

No worries and thanks again.

Now for my next raspbian-newbie problem. Getting your scipt ro run at reboot of the pi.
Seriously, I've been googling and trialing it now for another hour and nothing seems to work.... grrrr
I tried :
rc.local
/etc/init
crontab
to no avail so far.
Cheers,
DirkB

Op dinsdag 9 december 2014 21:37:04 UTC+1 schreef Ben Jones:
--
You received this message because you are subscribed to a topic in the Google Groups "openhab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openhab/Y65IYe7WMI4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.
Wednesday, 10 December 2014 9:37 a.m.
Hi Dirk,

I do understand your frustration, I have been thru it all myself ;). But in my experience if someone spoon feeds everything you don't end up understanding it and then when you have problems later on you are back to square one.

The issue is something like mqtt-gpio-monitor was written as a tool (by me) to solve a specific task. I released it to group in the hope someone else would find it useful, but it is by no means a 'supported' tool. It is not part of openHAB but just something I knocked together to help with integrating MQTT enabled RPis.

If you want to contribute a 'mqtt-gpio-monitor for dummies' document then I am sure future users would be very appreciative!

Cheers,
Ben
--
You received this message because you are subscribed to a topic in the Google Groups "openhab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openhab/Y65IYe7WMI4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.
Wednesday, 10 December 2014 9:29 a.m.
Hi Ben,

Your support is much appreciated, thank you.
I did a little thinking indeed and deducted that the actual physical pin nr's are required and not the GPIO pin nrs.

For newbies (like myself) who never done python, I must say that getting this stuff going is really quite frustrating mostly because of lack or (incomplete) documentation.
All my posts would have been unnecessary if there had been some kind of "mqtt-gpio-monitor for dummy's"-explanation.
But no worries, I'm getting there ... step by step.
Thanks again !
Grtz,
DirkB



Op dinsdag 9 december 2014 20:34:16 UTC+1 schreef Ben Jones:
--
You received this message because you are subscribed to a topic in the Google Groups "openhab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openhab/Y65IYe7WMI4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.
Wednesday, 10 December 2014 8:30 a.m.
--
You received this message because you are subscribed to a topic in the Google Groups "openhab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openhab/Y65IYe7WMI4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.
Wednesday, 10 December 2014 8:10 a.m.
MODULE = gpio

On Wednesday, 10 December 2014 01:54:28 UTC+13, Dirk Boon wrote:

Dirk Boon

unread,
Dec 10, 2014, 4:28:02 PM12/10/14
to ope...@googlegroups.com
Thanks again, Ben, supervisor does the job.

Op dinsdag 9 december 2014 22:59:04 UTC+1 schreef Ben Jones:
Reply all
Reply to author
Forward
0 new messages