简体   繁体   English

如何在树莓派和Windows7之间连接mosquitto服务器?

[英]How can I connect mosquitto server between raspberry pi and windows7?

Hello I'm trying to use mosquitto server in Raspberry Pi using MQTT to send a json data from r-pi to window. 您好,我正在尝试使用MQTT在Raspberry Pi中使用mosquitto服务器将json数据从r-pi发送到窗口。

Before I use mosquitto server, I used "test.mosquitto.org" It worked well. 在使用mosquitto服务器之前,我使用了“ test.mosquitto.org”。它运行良好。

I mean It sended some json data to windows. 我的意思是它向Windows发送了一些json数据。

However, when I turned mosquitto server in r-pi on, the windows put some error message which is 但是,当我在r-pi上打开mosquitto服务器时,Windows放了一些错误消息,这是

opts.protocol = opts.protocol.replace, cannot read property 'replace' of null.

Would you mind telling me what it is going on and fix it? 您介意告诉我它正在发生什么并修复它吗?

this is javascript on windows code (I use python in raspberry pi) 这是Windows代码上的javascript(我在树莓派中使用python)

    console.log("start");
    var mqtt = require('mqtt');
    var client = mqtt.connect('mqtt://test.mosquitto.org');
    var client = mqtt.connect('192.168.1.2'); // IP of main-broker

    client.on('connect', function () {
        client.subscribe('sensor_A');
    });

    client.on('message', function (topic, message) {
        console.log("Topic: " + topic);
        var parsedData = JSON.parse(message);
        var dataLen = parsedData.length
        console.log('dataLen: ' + dataLen);

        for (var i = 0; i < dataLen; i++) {
            var data = JSON.parse(parsedData[i]);
            console.log('data ' + i + ': ' + data.time + ' ' + data.tem + ' ' + data.hum + ' ' + data.gas);
}
    });

I am using two r-pi which is sub-borker and main-broker. 我正在使用两个r-pi,即子经纪人和主经纪人。

sub-broker just send some sensor data as json and main-broker controls the json data and send again as json to windows. 子经纪人仅将一些传感器数据作为json发送,而主经纪人控制json数据,然后将其作为json再次发送到Windows。

I think my writing is quite complex to understand. 我认为我的写作很难理解。

In short, I don't want to use "test.mosquitto.org" in r-pi so I turn mosquitto server on in r-pi to send data to window, however, there a error in window. 简而言之,我不想在r-pi中使用“ test.mosquitto.org”,所以我在r-pi中打开mosquitto服务器将数据发送到窗口,但是,窗口中有错误。

First you need to remove the line connecting to test.mosquitto.org as that will just confuse things. 首先,您需要删除连接到test.mosquitto.org的行,因为这只会使事情变得混乱。

Secondly you have missed out the mqtt:// from the URL for the local instance of mosquitto. 其次,您已经从mqtt://本地实例的URL中错过了mqtt:// The error is points out it can not find the protocol from the url. 错误指出它无法从url中找到协议。

console.log("start");
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://192.168.1.2'); // IP of main-broker

client.on('connect', function () {
    client.subscribe('sensor_A');
});

client.on('message', function (topic, message) {
    console.log("Topic: " + topic);
    var parsedData = JSON.parse(message);
    var dataLen = parsedData.length
    console.log('dataLen: ' + dataLen);

    for (var i = 0; i < dataLen; i++) {
        var data = JSON.parse(parsedData[i]);
        console.log('data ' + i + ': ' + data.time + ' ' + data.tem + ' ' + data.hum + ' ' + data.gas);
    }
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM