Most recent activity
-
-
@CriscoCrusader, that is strange as I tested it with a Pico-WiFi and Mosquitto. What board are you using and how are you connecting to the broker? I had a local http server to serve my MQTT.js
-
-
I have added some QoS handshake and some basic caching, need to get a replay after reconnect and drop old undelivered. See github https://github.com/srhart/MQTT
-
Hi Gordon,
It seems that the clinet.on('data') is sending more than one message in the data item.
Here is a fix I have hacked for MQTT.js, couldn't find it on github for a pull request.
MQTT.prototype.connect = function(client) { var mqo = this; var onConnect = function() { console.log('Client connected'); client.write(mqo.mqttConnect(mqo.client_id)); mqo.partData = ''; // Disconnect if no CONNACK is received mqo.ctimo = setTimeout(function() { mqo.ctimo = undefined; mqo.disconnect(); }, mqo.C.CONNECT_TIMEOUT); // Set up regular keep_alive ping mqo.pintr = setInterval(function() { // console.log("Pinging MQTT server"); mqo.ping(); }, mqo.ping_interval*1000); // Incoming data client.on('data', function(data) { if (mqo.partData) { //console.log('** Adding PART DATA **'); data = mqo.partData + data; mqo.partData = ''; } var type = data.charCodeAt(0) >> 4; var rLen = data.charCodeAt(1); var pLen = rLen + 2; if (data.length < pLen) { mqo.partData = data; return; } var pData = data.substr(0,pLen); if(type === TYPE.PUBLISH) { var parsedData = parsePublish(pData); if (parsedData!==undefined) { mqo.emit('publish', parsedData); mqo.emit('message', parsedData.topic, parsedData.message); } } else if(type === TYPE.PUBACK) { // implement puback } else if(type === TYPE.SUBACK) { // implement suback } else if(type === TYPE.UNSUBACK) { // implement unsuback } else if(type === TYPE.PINGREQ) { // silently reply to pings client.write(TYPE.PINGRESP+"\x00"); // reply to PINGREQ } else if(type === TYPE.PINGRESP) { mqo.emit('ping_reply'); } else if(type === TYPE.CONNACK) { if (mqo.ctimo) clearTimeout(mqo.ctimo); mqo.ctimo = undefined; var returnCode = pData.charCodeAt(3); if(returnCode === RETURN_CODES.ACCEPTED) { mqo.connected = true; console.log("MQTT connection accepted"); mqo.emit('connected'); mqo.emit('connect'); } else { var mqttError = "Connection refused, "; switch(returnCode) { case RETURN_CODES.UNACCEPTABLE_PROTOCOL_VERSION: mqttError += "unacceptable protocol version."; break; case RETURN_CODES.IDENTIFIER_REJECTED: mqttError += "identifier rejected."; break; case RETURN_CODES.SERVER_UNAVAILABLE: mqttError += "server unavailable."; break; case RETURN_CODES.BAD_USER_NAME_OR_PASSWORD: mqttError += "bad user name or password."; break; case RETURN_CODES.NOT_AUTHORIZED: mqttError += "not authorized."; break; default: mqttError += "unknown return code: " + returnCode + "."; } console.log(mqttError); mqo.emit('error', mqttError); } } else { console.log("MQTT unsupported packet type: " + type); console.log("[MQTT]" + data.split("").map(function (c) { return c.charCodeAt(0); })); } if (data.length > pLen) { client.emit('data', data.substr(pLen)); } });
-
Update. testing with QoS
mqtt.publish("test/espruino", msg, 1);
1v91 Copyright 2016 G.Williams > =undefined Connected! 192.168.1.226 Client connected Response: httpCRs { "headers": { "Date": "Tue, 04 Apr 2017 19:34:40 GMT", "Server": "Apache/2.4.18 (Ubuntu)", "Last-Modified": "Fri, 15 Nov 2013 15:42:26 GMT", "ETag": "\"d-4eb390b887c80\"", "Accept-Ranges": "bytes", "Content-Length": "13", "Connection": "close", "Content-Type": "text/plain" }, "httpVersion": "1.1", "statusCode": "200", "statusMessage": "OK" } --->Hello World! MQTT connection accepted makeid: swv0V MQTT unsupported packet type: 5 [MQTT]86 makeid: 1HPH2 makeid: 8h85j MQTT unsupported packet type: 5 [MQTT]83,71,58,32,56,104,56,53,106 makeid: wCyVx makeid: l2Chh makeid: gbk2L makeid: 5HdG8 makeid: wWLvZ makeid: 6PHch MQTT unsupported packet type: 5 [MQTT]80,72,99,104 makeid: 1fuue makeid: sctqL MQTT unsupported packet type: 6 [MQTT]99,116,113,76 makeid: Rkpqp makeid: FqKuv MQTT unsupported packet type: 7 [MQTT]117,118 makeid: G6wsU makeid: j3g6z MQTT unsupported packet type: 6 [MQTT]106,51,103,54,122 makeid: 5RuIk makeid: hJLhd >
eMQ Console subscribed test/# QoS 1
2017-04-04 20:36:33 --(Q1, R1, D0, Topic=test/espruino, Payload=from server) 2017-04-04 20:36:40 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: gbk2L) 2017-04-04 20:37:00 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: 5HdG8) 2017-04-04 20:37:20 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: wWLvZ) 2017-04-04 20:37:40 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: 6PHch) 2017-04-04 20:38:00 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: 1fuue) 2017-04-04 20:38:20 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: sctqL) 2017-04-04 20:38:40 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: Rkpqp) 2017-04-04 20:39:00 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: FqKuv) 2017-04-04 20:39:20 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: G6wsU) 2017-04-04 20:39:40 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: j3g6z) 2017-04-04 20:40:00 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: 5RuIk) 2017-04-04 20:40:20 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: hJLhd) 2017-04-04 20:40:40 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: sF75w) 2017-04-04 20:41:00 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: x87O6)
mqtt.publish("test/espruino", msg, 2);
1v91 Copyright 2016 G.Williams > =undefined Connected! 192.168.1.226 Client connected Response: httpCRs { "headers": { "Date": "Tue, 04 Apr 2017 19:41:48 GMT", "Server": "Apache/2.4.18 (Ubuntu)", "Last-Modified": "Fri, 15 Nov 2013 15:42:26 GMT", "ETag": "\"d-4eb390b887c80\"", "Accept-Ranges": "bytes", "Content-Length": "13", "Connection": "close", "Content-Type": "text/plain" }, "httpVersion": "1.1", "statusCode": "200", "statusMessage": "OK" } --->Hello World! MQTT connection accepted {"topic":"test/espruino","message":"from server","dup":0,"qos":0,"retain":1} topic: test/espruino message: from server makeid: M95lA MQTT unsupported packet type: 5 [MQTT]80,2,0,1 makeid: vuVhs MQTT unsupported packet type: 5 [MQTT]80,2,0,1,208,0 makeid: X3K5K MQTT unsupported packet type: 5 [MQTT]80,2,0,1 makeid: 2ZZN3 MQTT unsupported packet type: 5 [MQTT]80,2,0,1,208,0 makeid: yG6HR MQTT unsupported packet type: 5 [MQTT]80,2,0,1 makeid: SSwc0 MQTT unsupported packet type: 5 [MQTT]80,2,0,1 makeid: UKyIs MQTT unsupported packet type: 5 [MQTT]80,2,0,1 makeid: NFBwM MQTT unsupported packet type: 5 [MQTT]80,2,0,1,208,0 makeid: SnxMM MQTT unsupported packet type: 5 [MQTT]80,2,0,1 makeid: aB2tu MQTT unsupported packet type: 5 [MQTT]80,2,0,1,208,0 >
Nothing in eMQ Console subscribed to test/#
-
Just as a follow up I am testing with a pico wifi, eMQ as the broker, I see random errors with this code on the device:
var server = "ubuntu.home"; // the ip of your MQTT broker var options = { // all optional - the defaults are below client_id : getSerial(), // the client ID sent to MQTT - it's a good idea to define your own static one based on `getSerial()` keep_alive: 60, // keep alive time in seconds port: 1883, // port number clean_session: true, username: "stephen", // default is undefined password: "*****", // default is undefined protocol_name: "MQTT", // or MQIsdp, etc.. protocol_level: 4, // protocol level }; var mqtt = require("MQTT").create(server, options /*optional*/); mqtt.on('connected', function() { mqtt.subscribe("test/espruino"); }); mqtt.on('publish', function (pub) { console.log(JSON.stringify(pub)); console.log("topic: "+pub.topic); console.log("message: "+pub.message); }); var WIFI_NAME = "****"; var WIFI_OPTIONS = { password : "****" }; var wifi = require("EspruinoWiFi"); function getPage() { require("http").get("http://www.pur3.co.uk/hello.txt", function(res) { console.log("Response: ",res); res.on('data', function(d) { console.log("--->"+d); mqtt.publish("test/espruino", d); }); }); } function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for( var i=0; i < 5; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); console.log('makeid: '+text); return text; } function postIt() { var msg = "MSG: " + makeid(); mqtt.publish("test/espruino", msg); } wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) { if (err) { console.log("Connection error: "+err); return; } console.log("Connected!"); wifi.getIP(function (err, res) { if (!err) { console.log(res.ip); mqtt.connect(); } else { console.log(err); } }); getPage(); ts = setInterval(function() {postIt();}, 20000); });
It seems to randomly add \xD0\x00 to the message.
Console.log
1v91 Copyright 2016 G.Williams > =undefined Connected! 192.168.1.226 Client connected Response: httpCRs { "headers": { "Date": "Tue, 04 Apr 2017 19:19:23 GMT", "Server": "Apache/2.4.18 (Ubuntu)", "Last-Modified": "Fri, 15 Nov 2013 15:42:26 GMT", "ETag": "\"d-4eb390b887c80\"", "Accept-Ranges": "bytes", "Content-Length": "13", "Connection": "close", "Content-Type": "text/plain" }, "httpVersion": "1.1", "statusCode": "200", "statusMessage": "OK" } --->Hello World! MQTT connection accepted {"topic":"test/espruino","message":"from server","dup":0,"qos":0,"retain":1} topic: test/espruino message: from server makeid: gRQRa undefined Uncaught Error: Field or method "topic" does not already exist, and can't create it on undefined at line 1 col 88 ...lish",c),a.emit("message",c.topic,c.message);else if(d!==f.P... ^ in function called from system at line 2 col 28 console.log("topic: "+pub.topic); ^ in function called from system MQTT unsupported packet type: 0 [MQTT]0,13,116,101,115,116,47,101,115,112,114,117,105,110,111,77,83,71,58,32,103,82,81,82,97 makeid: sJZwi {"topic":"test/espruino","message":"MSG: sJZwi\xD0\x00","dup":0,"qos":0,"retain":0} topic: test/espruino message: MSG: sJZwiÐ makeid: kL0LJ {"topic":"test","message":"","dup":0,"qos":0,"retain":0} topic: test message: Uncaught Error: Unknown Timeout at line 2 col 149 ...ACK)if(clearTimeout(a.ctimo),c=c.charCodeAt(3),0===c)a.conne... ^ in function called from system makeid: GJTbH {"topic":"test/espruino","message":"MSG: GJTbH\xD0\x00","dup":0,"qos":0,"retain":0} topic: test/espruino message: MSG: GJTbHÐ makeid: pUTU1 undefined Uncaught Error: Field or method "topic" does not already exist, and can't create it on undefined at line 1 col 88 ...lish",c),a.emit("message",c.topic,c.message);else if(d!==f.P... ^ in function called from system at line 2 col 28 console.log("topic: "+pub.topic); ^ in function called from system MQTT unsupported packet type: 6 [MQTT]101,115,116,47,101,115,112,114,117,105,110,111,77,83,71,58,32,112,85,84,85,49 makeid: 6QAbL {"topic":"test/espruino","message":"MSG: 6QAbL\xD0\x00","dup":0,"qos":0,"retain":0} topic: test/espruino message: MSG: 6QAbLÐ makeid: demXD {"topic":"tes","message":"","dup":0,"qos":0,"retain":0} topic: tes message: MQTT unsupported packet type: 7 [MQTT]116,47,101,115,112,114,117,105,110,111,77,83,71,58,32,100,101,109,88,68 >
Something like the onion power dock?
Just let me know when you are ready to do something.