kylegordon / mqtt-republisher

A simple Python republisher to help organize topics
MIT License
26 stars 7 forks source link

Question #4

Open ghost opened 11 years ago

ghost commented 11 years ago

Hi Kyle, when using this code on my machine, with mosquitto installed (MAC OS X), it seems to connect with you test IP address 10.8.0.1, but when I try to connect to test.mosquitto.org (85.119.83.194) it simply fails to connect and throws me an error message from on_connect method. Any idea why ? Florian

kylegordon commented 11 years ago

Hi Florian,

Apologies for the delay. I've just tested it, and it appears to connect.

Can you set debug = True in mqtt-republisher.cfg, and try again as root. Attach the output of /var/log/mqtt-republisher.log as well please. With debug = True it should say "Looping" repeatedly in the log.

Can you also copy and paste all the output from running sudo ./mqtt-republisher.py please?

Regards

Kyle

ghost commented 11 years ago

Hello Kyle, thank you for your answer. When I ran your code 'out of the box' with mosquitto installed, it was throwing the following:

Traceback (most recent call last): File "/Users/florianpuget/my_code/MQTT_tests/mqtt-republisher.py", line 137, in connect() File "/Users/florianpuget/my_code/MQTT_tests/mqtt-republisher.py", line 63, in connect result = mqttc.connect(MQTT_HOST, MQTT_PORT, 60, True) TypeError: connect() takes at most 4 arguments (5 given)

I have fixed that by removing the 'True' in result = mqttc.connect(MQTT_HOST, MQTT_PORT, 60, True) line 60.

Then I was facing another error:

Traceback (most recent call last): File "/Users/florianpuget/my_code/MQTT_tests/mqtt-republisher.py", line 138, in main_loop() File "/Users/florianpuget/my_code/MQTT_tests/mqtt-republisher.py", line 128, in main_loop while mqttc.loop() == 0: File "/Library/Python/2.7/site-packages/mosquitto.py", line 670, in loop rc = self.loop_read(max_packets) File "/Library/Python/2.7/site-packages/mosquitto.py", line 840, in loop_read rc = self._packet_read() File "/Library/Python/2.7/site-packages/mosquitto.py", line 1151, in _packet_read rc = self._packet_handle() File "/Library/Python/2.7/site-packages/mosquitto.py", line 1533, in _packet_handle return self._handle_connack() File "/Library/Python/2.7/site-packages/mosquitto.py", line 1574, in _handle_connack self.on_connect(self, self._userdata, result) TypeError: on_connect() takes exactly 1 argument (3 given)

That I have fixed adding 2 more parameters to the methods on_connect(), on_disconnect() and on_message()

I am now questioning if we are using the same library.. :) Or I might simply be missing something..

Obviously I changed the map.csv file to match some test topics i could control but didn't get it to work..

But I still have to say a big thank you, because I am in the process of learning MQTT and python and your code was a great starting point for me, I especially like how you keep it neat linking to external files (.csv and .cfg).

If I can take advantage of your experience a little more and ask you another question :) I would like to set some logic for mqtt in python and publish it on a google app engine website, so it will do the parsing job from this server rather than from a locally hosted one. Any idea how I could get to run such a thing ? I am missing knowledge on how I can run a python script on a distant server basically.. I had a look on internet but was pretty unsuccessful this time. Any suggestion ?

Thanks a lot,

Florian

ghost commented 11 years ago

Update: I have found what was wrong eventually (once the two issues described above were resolved). My map.csv file was containing spaces, therefore it was publishing with a slightly wrong topic name ... :) Easy one, but it took me a while to find out..

antex22 commented 10 years ago

..I am runninginto the same exact problem. Can you kindly share the script you've used or the changes you've made?

Here is the error I get Connecting... /dev/ttyACM0 Traceback (most recent call last): File "./serilScrape.py", line 71, in while mqttc.loop() == 0: File "/usr/lib/python2.7/dist-packages/mosquitto.py", line 720, in loop rc = self.loop_read(max_packets) File "/usr/lib/python2.7/dist-packages/mosquitto.py", line 961, in loop_read rc = self._packet_read() File "/usr/lib/python2.7/dist-packages/mosquitto.py", line 1360, in _packet_read rc = self._packet_handle() File "/usr/lib/python2.7/dist-packages/mosquitto.py", line 1781, in _packet_handle return self._handle_connack() File "/usr/lib/python2.7/dist-packages/mosquitto.py", line 1822, in _handle_connack self.on_connect(self, self._userdata, result) TypeError: on_connect() takes exactly 1 argument (3 given)

Here is the script Used

Andy Piper http://andypiper.co.uk

2011/09/15

import serial import mosquitto import os

serialdev = '/dev/ttyUSB0' broker = "127.0.0.1" port = 1883

MQTT callbacks

def on_connect(rc): if rc == 0:

rc 0 successful connect

print "Connected" else: raise Exception

def on_publish(val): print "Published ", val

called on exit

close serial, disconnect MQTT

def cleanup(): print "Ending and cleaning up" ser.close() mqttc.disconnect()

try: print "Connecting... ", serialdev

connect to serial port

ser = serial.Serial(serialdev, 9600, timeout=20) except: print "Failed to connect serial"

unable to continue with no serial input

raise SystemExit

try: ser.flushInput()

create an mqtt client

mypid = os.getpid() client_uniq = "arduinopub"+str(mypid) mqttc = mosquitto.Mosquitto(client_uniq)

attach MQTT callbacks

mqttc.on_connect = on_connect mqttc.on_publish = on_publish

connect to broker

mqttc.connect(broker, port, 60, True)

remain connected to broker

read data from serial and publish

while mqttc.loop() == 0: line = ser.readline()

split line as it contains V,temp

list = line.split(",")

second list element is temp

temp = list[1].rstrip() mqttc.publish("arduino/temp", temp) pass

handle list index error (i.e. assume no data received)

except (IndexError): print "No data received within serial timeout period" cleanup()

handle app closure

except (KeyboardInterrupt): print "Interrupt received" cleanup() except (RuntimeError): print "uh-oh! time to die" cleanup()

antex22 commented 10 years ago

link to the script

http://mqtt.org/wiki/doku.php/python_examples

ghost commented 10 years ago

hello, it has been quite a while i looked into this and unfortunately didn't have much time to maintain this homework.. so i really can't tell you off the hook. but if by any chance i come across the code i used then I'll take a look. cheers, flo On 2 Jul 2014 22:33, "antex22" notifications@github.com wrote:

link to the script

http://mqtt.org/wiki/doku.php/python_examples

— Reply to this email directly or view it on GitHub https://github.com/kylegordon/mqtt-republisher/issues/4#issuecomment-47839844 .

kylegordon commented 10 years ago

Hey,

That one seems vaguely familiar. Are you using the stock Mosquitto libs from your distribution repository? If so, try using the repos from http://mosquitto.org/download/

It seems familiar, as I originally wrote it on Debian, and then got bitten by their out of date mosquitto libraries!

Kyle

antex22 commented 10 years ago

...You stated that "That I have fixed adding 2 more parameters to the methods on_connect(), on_disconnect() and on_message()"....can you elaborate on that. I think this is purely a python script issue.

kylegordon commented 10 years ago

I didn't state that - @flopupuj stated it, but is also short of time.

Did you do what I suggested, and use an up to date version of the Mosquitto libraries? What version are you on?

readserial.py by Andy Piper is coming up for 3 years old, and there have been significant changes since then. Specifically, the documentation at http://mosquitto.org/documentation/python/ states that 3 parameters can be passed to on_connect(), yet your library is reporting that only 1 can be passed.

I experienced this and fixed it with commit https://github.com/kylegordon/mqtt-republisher/commit/bb03171196e4190d4a2d7fb0e0c986f30128cad1 as it was indicative of an out of date library on my system.

antex22 commented 10 years ago

Thanks for your prompt response! So I used the 'sudo apt-get install python-mosquitto' lib. I am using python 2.7. on Ubuntu. And I went to mosquitto.org/download page and add the mosquitto-dev PPA repsoitory, and did the apt-get update, and still getting the same error.

antex22 commented 10 years ago

Thanks Kyle. You were right. The library is outdated. Installed the phao-mqtt lib

"Pip install paho-mqtt"

Used the paho lib instead of the "import osquitto"

"Import paho.mqtt.client as mqtt"

Edited the python script script some, and it works!

Matt

On Jul 3, 2014, at 5:55 AM, Kyle Gordon notifications@github.com wrote:

I didn't state that - @flopupuj stated it, but is also short of time.

Did you do what I suggested, and use an up to date version of the Mosquitto libraries? What version are you on?

readserial.py by Andy Piper is coming up for 3 years old, and there have been significant changes since then. Specifically, the documentation at http://mosquitto.org/documentation/python/ states that 3 parameters can be passed to on_connect(), yet your library is reporting that only 1 can be passed.

I experienced this and fixed it with commit bb03171 as it was indicative of an out of date library on my system.

— Reply to this email directly or view it on GitHub.

ghost commented 10 years ago

Hello, So I had a quick look but i am not sure if the file i found is the one error free.. I got the following headers (with arguments): on_message(self, topic, msg) on_connect(self, topic,result_code) on_disconnect(self, topic, result_code) hope this helps, i could try having a better look later ;)

kylegordon commented 10 years ago

Glad to hear you got it sorted!

For what it's worth, after you add a PPA in Ubuntu, you need to not only do an apt-get update, but also an apt-get upgrade. The update just updates your local list of available packages - the upgrade (or dist-upgrade) will actually install or upgrade things for you.

Nonetheless, you're working now :-)

Kyle