【问题标题】:why publish from paho raspberry pi to Mosca node js give me an error?为什么从 paho raspberry pi 发布到 Mosca node js 给我一个错误?
【发布时间】:2018-07-01 05:19:43
【问题描述】:

我正在尝试从运行在我的树莓派上的 Paho 库分包并发布到运行在 windows 机器上的 nodejs 服务器上的 MOSCA MQTT 代理,但没有任何事情发生 那么问题是什么 注意:当我从 nodejs 客户端连接到代理时,它可以工作 这是我的nodejs代码

var mosca = require('mosca');

var settings = {
  port: 1883
};

//here we start mosca
var server = new mosca.Server(settings);
server.on('ready', setup);

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running');
}

// fired whena  client is connected
server.on('clientConnected', function(client) {
  console.log('client connected', client.id);
});

// fired when a message is received
server.on('published', function(packet, client) {
  console.log('Published : ', packet.payload);
});

// fired when a client subscribes to a topic
server.on('subscribed', function(topic, client) {
  console.log('subscribed : ', topic);
});

// fired when a client subscribes to a topic
server.on('unsubscribed', function(topic, client) {
  console.log('unsubscribed : ', topic);
});

// fired when a client is disconnecting
server.on('clientDisconnecting', function(client) {
  console.log('clientDisconnecting : ', client.id);
});

// fired when a client is disconnected
server.on('clientDisconnected', function(client) {
  console.log('clientDisconnected : ', client.id);
});

// this is my client request

var mqtt = require('mqtt');

client = mqtt.connect('mqtt://192.168.10.99:1883');

client.subscribe('presence');

console.log('Client publishing.. ');
client.publish('presence', 'Client 1 is alive.. Test Ping! ' + Date());

client.end();

这是我在树莓派上运行的 python 代码

import time
import paho.mqtt.client as paho
broker="192.168.10.99"
#define callback
def on_message(client, userdata, message):
    time.sleep(1)
    print("received message =",str(message.payload.decode("utf-8")))

client= paho.Client("client-001") #create client object client1.on_publish = on_publish #assign function to callback client1.connect(broker,port) #establish connection client1.publish("house/bulb1","on")
######Bind function to callback
client.on_message=on_message
#####
print("connecting to broker ",broker)
client.connect(broker)#connect
client.loop_start() #start loop to process received messages
print("subscribing ")
client.subscribe("house/bulb1")#subscribe
time.sleep(2)
print("publishing ")
client.publish("house/bulb1","on")#publish
time.sleep(4)
client.disconnect() #disconnect
client.loop_stop() #stop loop

我的 python 错误是:

sudo python clientpaho.py
Traceback (most recent call last):
  File "clientpaho.py", line 40, in <module>
    mqttc.loop_forever()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1481, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1003, in loop
    rc = self.loop_read(max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1284, in loop_read
    rc = self._packet_read()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1849, in _packet_read
    rc = self._packet_handle()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2311, in _packet_handle
    return self._handle_connack()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2372, in _handle_connack
    self.on_connect(self, self._userdata, flags_dict, result)
TypeError: on_connect() takes exactly 3 arguments (4 given)

【问题讨论】:

  • 这与您的其他问题 (stackoverflow.com/questions/51076889/…) 中的两台机器是否相同?如果是这样,这确实意味着它是一些与网络/防火墙相关的问题。
  • 是的,树莓派或 Windows 机器上的网络/防火墙是什么意思
  • 不知道不能访问两者,但我猜 Windows 机器通常 Raspbian 没有启用任何防火墙
  • 我编辑了我的问题,@hardillb 错误呢

标签: python raspberry-pi mqtt paho


【解决方案1】:

最后这是我解决的问题:

Paho 客户端无法连接到 nodejs ==> 在 Windows 上禁用防火墙

on_connect 函数引起的错误它需要 4 个参数而不是像这样的 3 个参数on_connect(self, self._userdata, flags_dict, result)

谢谢你

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多