I am trying to call the paho-mqtt library 
(https://pypi.python.org/pypi/paho-mqtt), here's the Python example code:

import paho.mqtt.client as mqtt


# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("$SYS/#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()


And here's my naïve translation to Julia:

using PyCall

@pyimport paho.mqtt.client as mqtt

function on_connect(client, userdata, flags, rc)
    println("Connected with result code "*string(rc))
    client[:subscribe]("$SYS/#")
end

function on_message(client, userdata, msg)
    println(msg[:topic]*" "*string(msg[:payload]))
end

client = mqtt.Client()
client[:on_connect] = on_connect
client[:on_message] = on_message

client[:connect]("iot.eclipse.org", 1883, 60)
client[:loop_forever]()


I get the following errors:

ERROR: LoadError: PyError (:PyObject_Call) <type 'exceptions.AttributeError'
>
AttributeError("'PyCall.jl_Function' object has no attribute 'func_code'",)
  File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py"
, line 1261, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py"
, line 811, in loop
    rc = self.loop_read(max_packets)
  File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py"
, line 1073, in loop_read
    rc = self._packet_read()
  File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py"
, line 1475, in _packet_read
    rc = self._packet_handle()
  File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py"
, line 1949, in _packet_handle
    return self._handle_connack()
  File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py"
, line 2001, in _handle_connack
    argcount = self.on_connect.func_code.co_argcount

 [inlined code] from /home/kjwiik/.julia/v0.4/PyCall/src/exception.jl:81
 in pycall at /home/kjwiik/.julia/v0.4/PyCall/src/PyCall.jl:356
 in call at /home/kjwiik/.julia/v0.4/PyCall/src/PyCall.jl:372
 in include at ./boot.jl:261
 in include_from_node1 at ./loading.jl:320
while loading /home/kjwiik/proj/telescopes/software/mqttclient.jl, in 
expression starting on line 19


It seems that I haven't been able to pass the callbacks correctly, how it 
should be done? The Python code works OK, of course.

Thanks,
Kaj


Reply via email to