how to connect the mqtt server #5872
Unanswered
espruino-discuss
asked this question in
ESP8266
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2017-09-28 by user81749
I use the mosca to run a mqtt server,and use the esp32 as a mqtt client ,but the client can not connect the server,the server and the client are in different network,so how can i set up the host ?var ssid="wangnuan";
var password="wang1234";
var wifi=require('Wifi');
var host='xxxxxxxx';
var options={
client_id : "random",
keep_alive: 60,
port: 1883,
clean_session: true,
username: "test",
password: "12345678",
protocol_name: "esp32",
protocol_level: 4,
};
wifi.connect(ssid, {password: password}, function() {
console.log('Connected to Wifi. IP address is:', wifi.getIP().ip);
console.log("Connected to access point");
var mqtt = require("MQTT").create(host);
mqtt.on("connected", function() {
console.log("MQTT connected");
mqtt.subscribe("test");
mqtt.on('publish', function (pub) {
console.log("topic: "+pub.topic);
console.log("message: "+pub.message);
});
});
console.log("Doing a connect");
mqtt.connect();
});
Beta Was this translation helpful? Give feedback.
All reactions