-
• #2
You need to upload the code in the right side of the ide, it appears you are entering the code on the left.
-
• #3
Can't connect to https://api.cloudmqtt.com. What's wrong? Is this even possible?
-
• #4
This is my code:
var server = "m14.cloudmqtt.com"; var options = { client_id : "random", keep_alive: 60, port: 35577, clean_session: true, username: "ciugi**", password: "eYV1i*****", protocol_name: "MQTT", protocol_level: 4, }; var mqtt = require("MQTT").create(server, options); mqtt.on('connected', function() { mqtt.subscribe("test"); }); mqtt.on('publish', function (pub) { console.log("topic: "+pub.topic); console.log("message: "+pub.message); }); var wifi = require("Wifi"); function onInit() { wifi.stopAP(); wifi.connect("INGRIFAB", {password: "B73vFeX6se2"}, function(err) { if(err)console.log(err);else console.log("connected!"); console.log(wifi.getIP().ip); }); } var server = "m14.cloudmqtt.com"; var options = { client_id : "random", keep_alive: 60, port: 35577, clean_session: true, username: "ciugi**", password: "eYV1i*****", protocol_name: "MQTT", protocol_level: 4, }; var mqtt = require("MQTT").create(server, options); mqtt.on('connected', function() { mqtt.subscribe("test"); }); mqtt.on('publish', function (pub) { console.log("topic: "+pub.topic); console.log("message: "+pub.message); }); var wifi = require("Wifi"); function onInit() { wifi.stopAP(); wifi.connect("INGRIFAB", {password: "B73vFeX6se2"}, function(err) { if(err)console.log(err);else console.log("connected!"); console.log(wifi.getIP().ip); }); } wifi.save(); save();
-
• #5
Maybe try and connect with an MQTT client on a local computer first to check it works?
It's also worth noting that Espruino on ESP8266 won't do TLS (needed for secure MQTT), which I guess could be causing you problems?
-
• #6
Understood! It works!!! Just the ports were closed
-
• #7
Here is the working code! Whom it is necessary.
var server = "m14.cloudmqtt.com"; var options = { keep_alive: 60, port: 15577, username: "ciug****", password: "eYV1ivU****", protocol_name: "MQTT", protocol_level: 4 }; var mqtt = require("MQTT").create(server, options); var wifi = require("Wifi"); function onInit() { wifi.stopAP(); wifi.connect("AndroidAP", {password: "tssu7350"}, function(err) { if(err)console.log(err);else console.log("connected!"); console.log(wifi.getIP().ip); wifi.getStatus(function(ap) { if(ap.station === 'connected'){ print("START..."); mqtt.on('connected', function() { print("connect!!!"); mqtt.subscribe("test"); }); mqtt.on('publish', function (pub) { console.log("topic: "+pub.topic); console.log("message: "+pub.message); }); mqtt.on('disconnected', function() { print("disconnected!!!"); console.log("MQTT disconnected... reconnecting."); setTimeout(function() { mqtt.connect(); print("mqtt connect"); }, 2000); }); mqtt.connect(); } }); }); } wifi.save(); save();
-
• #8
Are only outgoing messages. Why? The function mqtt.on('publish', mqttMessage); - not working
-
• #9
Have you subscribed to what you are interested in? Usually topics start with a
/
, so you might needmqtt.subscribe("/test");
-
• #10
PS... on a side note: @Alexander, when you include code in posts, place it between two line that have only three (3) 'back-ticks' at the beginning and make sure there is an empty line before and after the lines with the back-ticks. This way all your code is furnished with line numbers, shows in one block, and is much more legible.
-
• #11
Is that better? :)
-
• #12
Perfect! ;-)
Btw, now I noticed, that
save()
is part of your code you upload... is it?I'm not sure if it is bad, but it kind of tickles my guts the wrong way... @Gordon will for sure know. I keep the
save()
out of upload business, and even thedirect onInit()
or any other heavy activity in level 0... -
• #13
Yes, as @allObjects says I'd avoid using
save()
right at the end of code you upload, and would do it manually.If you want to save immediately when you send, you're probably better off using
save on send
in the IDE options. -
• #14
Thank you! Very useful tips.
Did not see the MQTT module. More can that where to disable that would work MQTT.