sumnerboy12 / mqtt-gpio-monitor

Python script for sending/receiving commands to/from GPIO pins via MQTT messages
46 stars 23 forks source link

Debug relay action #13

Closed xbmcnut closed 8 years ago

xbmcnut commented 8 years ago

Hi Ben,

I have it partly working now and can control the PiFace module with Python using pfd.output_pins[7].value = 1. Below is my ini file and the log file. It is connected to my broker but when I publish a message to /piface/out/0 with a payload of 1, the broker reports the action on /piface/# but the relay does not change state. I'm using tail -F to track the log and it is not updating past the data below after running the script.

How do I debug this?

[global]
MODULE             = pfio
DEBUG              = True

MQTT_HOST          = [ip.of.my.nas.broker]
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         = /piface
MQTT_LWT           = /clients/piface

MONITOR_PINS       = 0, 1, 2, 3, 4, 5, 6, 7
MONITOR_POLL       = 0.1
MONITOR_REFRESH    = /piface/refresh
2016-08-01 16:40:59,598 INFO  Starting mqtt-gpio-monitor
2016-08-01 16:40:59,601 INFO  INFO MODE
2016-08-01 16:40:59,605 DEBUG DEBUG MODE
2016-08-01 16:40:59,608 DEBUG INIFILE = mqtt-gpio-monitor.ini
2016-08-01 16:40:59,611 DEBUG LOGFILE = mqtt-gpio-monitor.log
2016-08-01 16:40:59,706 INFO  PiFace.PFIO module detected...
2016-08-01 16:40:59,710 DEBUG Monitoring pins [0, 1, 2, 3, 4, 5, 6, 7]
2016-08-01 16:41:00,028 DEBUG Connecting to 10.0.2.8:1883...
2016-08-01 16:41:00,040 DEBUG Pin 0 changed from -1 to 0
2016-08-01 16:41:00,048 INFO  Connected to 10.0.2.8:1883
2016-08-01 16:41:00,052 DEBUG Pin 1 changed from -1 to 0
2016-08-01 16:41:00,069 DEBUG Pin 2 changed from -1 to 0
2016-08-01 16:41:00,080 DEBUG Pin 3 changed from -1 to 0
2016-08-01 16:41:00,086 DEBUG Refreshing the state of all monitored pins...
2016-08-01 16:41:00,091 DEBUG Pin 4 changed from -1 to 0
2016-08-01 16:41:00,095 DEBUG Refreshing pin 0 state -> 0
2016-08-01 16:41:00,103 DEBUG Pin 5 changed from -1 to 0
2016-08-01 16:41:00,107 DEBUG Refreshing pin 1 state -> 0
2016-08-01 16:41:00,116 DEBUG Pin 6 changed from -1 to 0
2016-08-01 16:41:00,120 DEBUG Refreshing pin 2 state -> 0
2016-08-01 16:41:00,129 DEBUG Pin 7 changed from -1 to 0
2016-08-01 16:41:00,133 DEBUG Refreshing pin 3 state -> 0
2016-08-01 16:41:00,141 DEBUG Refreshing pin 4 state -> 0
2016-08-01 16:41:00,148 DEBUG Refreshing pin 5 state -> 0
2016-08-01 16:41:00,153 DEBUG Refreshing pin 6 state -> 0
2016-08-01 16:41:00,165 DEBUG Refreshing pin 7 state -> 0
xbmcnut commented 8 years ago

P.S The buttons on the PiFace do update the log and I also receive a broker message using mqtt.fx. Sending /piface/in/0 payload 1 also does not update the log.

sumnerboy12 commented 8 years ago

The script is not receiving any message on /piface/in/+, as any thing arriving on that should be logged via logging.debug("Incoming message for pin %d -> %d" % (pin, value)). You need to be publishing to /piface/in/0 to change the relay state of pin 0. /piface/out/+ is where the pin states being monitored are published.

xbmcnut commented 8 years ago

Thanks for your help Ben. If I send /piface/in/0 with 1, I get the following in the log file but no output at /piface/out/+. Is the + supposed to be a pin #? If I subscribe to /piface/out/#, I see nothing either.

2016-08-04 08:06:02,260 DEBUG Incoming message for pin 0 -> 1

xbmcnut commented 8 years ago

Incidentally, pushing the buttons on the PiFace does update the log, but not /piface/out/+

2016-08-04 08:13:56,805 DEBUG Pin 1 changed from 0 to 1
2016-08-04 08:13:57,033 DEBUG Pin 1 changed from 1 to 0
2016-08-04 08:13:58,018 DEBUG Pin 2 changed from 0 to 1
2016-08-04 08:13:58,355 DEBUG Pin 2 changed from 1 to 0
2016-08-04 08:14:00,096 DEBUG Pin 3 changed from 0 to 1
2016-08-04 08:14:00,432 DEBUG Pin 3 changed from 1 to 0
xbmcnut commented 8 years ago

Further testing. /piface/out/+ is updated for all monitored pins when I start and stop the script so I know PAHO is capable of sending the messages now.

One output is working now but this is where I think the problem might be? The outputs on the V1 PiFace are 0 thru 7. When I monitor 0~7, the script will start. If I change the ini to monitor 1~8, the script will not start. However, when I publish to /piface/in/0, nothing is published, same for any of the other inputs except input 1. If I publish to /piface/in/1, then I DO get an output on /piface/out/+ but it reports /piface/out/0!

xbmcnut commented 8 years ago

Getting there Ben. All outputs are controllable using /piface/in/<pin#>, 0 or 1. Pushing the buttons 0~3 outputs MQTT messages now e.g. /piface/out/3 (0/1) when subscribed to /piface/out/+. So the only problem I have now is when I publish to /piface/in/0 or /piface/in/2~7, there is no MQTT message but as mentioned above, I do get a message albeit incorrect when I publish to /piface/in/1.

sumnerboy12 commented 8 years ago

The idea with this simple script was to allow swtiching of a relay (with no acknowledgement) and to monitor digital pins. But it was not designed to both for the same pin - i.e. if you are switching pin 3 then you would not be monitoring pin 3. since your calling system knows the state (since it is controlling it).

I think that is your problem, you are expecting that when you publish a message to switch a relay, that you will receive an ack on the /out topic. That was never designed to work that way unfortunately - it was a very simple implementation which I used with openHAB.

xbmcnut commented 8 years ago

Thanks Ben but that doesn't explain why I get an output (desirable) when I publish to /piface/in/1. In MQTT for HA, normally the system has a feedback loop so the HA system knows the device received the message. I know I can publish a message to turn a relay on, but did it actually flip? Looks like it is real close to working perfectly if it could output MQTT messages on the other 7 pins with the right statement.

sumnerboy12 commented 8 years ago

As I said before, this was never the intention so it is not going to do that for you. You are welcome to make some changes to do what you want, but I no longer use this script so won't be able to do it myself.

xbmcnut commented 8 years ago

Thanks Ben, I totally understand. If it's OK with you, I'll ask the Home Assistant community to see if someone can fork it and update it for output messages on relay activation. Thanks heaps for all of your assistance and time, very much appreciated.

sumnerboy12 commented 8 years ago

Of course - this is open source after all! I would suggest you also have a look at Homie - that might be a better match to what you are after and is a much more complete implementation than my hack!