MQTT newbie troubles

Posted on
Page
of 2
/ 2
Next
  • Hello!
    I'm trying to run mqtt client on my espruino. And have errors all the time. I tried to do it with options:

    1. function mqttRun(){
    2. var server = 'my_broker';
    3. var options = {
    4. keep_alive: 60,
    5. port: my_port,
    6. clean_session: my_port,
    7. username: "my_user",
    8. password: "my_pass",
    9. protocol_name: "MQTT",
    10. protocol_level: 4,
    11. };
    12. var mqtt = require("MQTT").create(server);
    13. mqtt.connect(options);
    14. mqtt.on('connected', function() {
    15. mqtt.subscribe("test");
    16. });
    17. }

    and as result got:

    1. Client connected
    2. Uncaught Error: Function "write" not found! at line 1 col 35 ....log("Client connected");a.write(b.mqttConnect(b.client_id))...
    3. ^ in function "e" called from line 3 col 353 ..."close")});b.client=a};a?e():a=require("net").connect({host:...
    4. ^ in function "connect" called from line 27 col 23 mqtt.connect(options);
    5. ^ in function "mqttRun" called from line 1 col 9 mqttRun()

    Without options I tried to connect with eclipse sandbox, and it works different:

    1. mqttRun()
    2. ["AT+CIPSTART=0,\"TCP\",\"iot.eclipse.org\",1883\r\n"
    3. =undefined
    4. Client connected
    5. ] "\r\n" <--- "\r\n"
    6. ] "OK\r\n" <--- "OK\r\n"
    7. ] "\r\n" <--- "\r\n"
    8. ] "0, CO" <--- "0, CO"
    9. ] "0, CONNEC" <--- "NNEC"
    10. ] "0, CONNECT OK\r" <--- "T OK\r"
    11. ] "\n" <--- "\n"
    12. ] "\r" <--- "\r"
    13. ] "\n> " <--- "\n> "
    14. ] "\r\n" <--- "\r\n"
    15. ] "0, SE" <--- "0, SE"
    16. ] "0, SEND O" <--- "ND O"
    17. ] "0, SEND OK\r\n" <--- "K\r\n"
    18. ] "\r\n+RECEI" <--- "\r\n+RECEI"
    19. ["AT+CIPCLOSE=0,1\r\n"
    20. MQTT client disconnected
    21. MQTT client disconnected
    22. Uncaught Error: Unknown Interval
    23. at line 1 col 62
    24. ...ted");clearInterval(b.pintr);b.emit("disconnected");b.emit("...
    25. ^
    26. in function called from system
    27. ] "+RECEIVE,0,4:\r\n " <--- "VE,0,4:\r\n "
    28. ] "+D,0,3:\x02\x00\x00" <--- "\x02\x00\x00"
    29. ] "\r" <--- "\r"
    30. ] "\n> " <--- "\n> "
    31. ] "\r" <--- "\r"
    32. ] "\n0, " <--- "\n0, "
    33. ] "0, SEN" <--- "SEN"
    34. ] "0, SEND O" <--- "D O"
    35. ] "0, SEND OK\r\n" <--- "K\r\n"
    36. ] "\r" <--- "\r"
    37. ] "\n0, " <--- "\n0, "
    38. ] "0, CLO" <--- "CLO"
    39. ] "0, CLOSE" <--- "SE"
    40. ] "0, CLOSED\r\n" <--- "D\r\n"

    Could I fix it somehow?

  • You could try registering for the connected event before you call connect. It might help - it's definitely what gets done in the examples on http://www.espruino.com/MQTT

  • Thank you, Gordon.
    do you mean put it in order like this?

    1. function mqttRun(){
    2. var server = 'my_broker';
    3. var options = {
    4. keep_alive: 60,
    5. port: my_port,
    6. clean_session: my_port,
    7. username: "my_user",
    8. password: "my_pass",
    9. protocol_name: "MQTT",
    10. protocol_level: 4,
    11. };
    12. var mqtt = require("MQTT").create(server);
    13. mqtt.on('connected', function() {
    14. mqtt.subscribe("test");
    15. });
    16. mqtt.connect(options);
    17. }

    I tried now - same error

    1. >mqttRun()
    2. Client connected
    3. Uncaught Error: Function "write" not found!

    What I'm doing wrong?
    UPD. I saw this subject - http://forum.espruino.com/conversations/293870
    example in post #5 and mine shoud do the same, but they don't

  • ok. this problem solved by setting options when 'create'

    but i have next one. i can see messages in debug, but mqtt module give me error again

    1. function mqttRun(){
    2. var server = 'my_broker';
    3. var options = {
    4. keep_alive: 60,
    5. port: my_port,
    6. clean_session: my_port,
    7. client_id: "my_id",
    8. username: "my_user",
    9. password: "my_pass",
    10. protocol_name: "MQTT",
    11. protocol_level: 4,
    12. };
    13. var mqtt = require("MQTT").create(server, options); // changes here
    14. mqtt.on('connected', function() {
    15. mqtt.subscribe("/test");
    16. });
    17. mqtt.on('publish', function (pub) { // this place throw error
    18. console.log("topic: "+pub.topic);
    19. console.log("message: "+pub.message);
    20. });
    21. mqtt.connect();
    22. }

    Uncaught Error: Field or method "topic" does not already exist, and can't create it on undefined

  • Ok, glad you got that sorted - do you know what exactly the issue was? Missing client_id? Are you using up to date firmware, as it should really have reported a different error.

    For your new error, could you just try removing console.log("topic: "+pub.topic); - it could be that pub doesn't have a field in it called topic?

  • Actually can you try this instead:

    1. mqtt.on('publish', function (pub) {
    2. console.log(JSON.stringify(pub));
    3. });

    It looks like 'publish' can return undefined if it doesn't understand the MQTT packet that it was sent, which would produce your error

  • Gordon, thank you, i'll definitely try with json. I already looked through module code and and notice, when it can be undefined, so run my code without pub.topic and pub.message. mqtt.on('publish', ...) can catch the event, thats good. But still need to read messages for my project and still look for solution. Will try use JSON.stringify(pub) today and tell about result.

    And about first problem: it was solved when i set my options object as a parameter of 'create' function like this: mqtt.create (server, options);
    setting client_id didn't help, but i tried :)
    By the way, I still have issue in the connect (a.write dose not already exist, if i'm not mistaken), but this solution let mqtt run, so for now i just ignore the issue.

  • Thanks for letting me know - it looks like the last updated to the documentation wasn't tested at all. Looking through it I've spotted loads of bugs in the example code now :(

    I'll push an update soon.

  • Gordon and thank you for support! I really appreciate it.

    Ok, about 'json-solution'. I've been testing it for few hours.
    it's not working for the moment.
    best results look like this:

    1. JSON TEST {"topic":"3865","message":"","dup":1,"qos":0,"retain":0}

    topic was '/test', message contain a string, but not ':'
    most part I've got undefined and thats it. But in debug i can see whole message and topic as it was sent.
    As well I got one new error:

    1. Uncaught Error: Unknown Timeout
    2. at line 2 col 149
    3. ...ACK)if(clearTimeout(b.ctimo),c=c.charCodeAt(3),0===c)b.conne...

    it's not crash the script, but still.

    About MQTT packet: all the time i having 'MQTT unsupported packet type: 6' and 'MQTT unsupported packet type: 7'.
    It's the same when I send message either from espruino, websocket console or android client.
    Maybe I can change type somehow? How it works? Need a research this.

    and as soon as i can see topic and message in debug, probably i can get it to some buffer as well? I tried AT module, but not succeed with it so far.

  • For the unsupported packet type, see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.1_-

    Looks a lot like 6 and 7 are for assured delivery - I guess that's some kind of QoS - when the MQTT library only supports QoS 0.

    To be honest QoS>0 isn't really so important over TCP/IP since TCP/IP will already be doing error correction and packet management for you.

    I wonder whether you're connecting to an MQTT-SN server rather than normal MQTT, or something like that?

  • lI wonder whether you're connecting to an MQTT-SN server rather than normal MQTT, or something like that?

    I'm not sure that understand you right way, do you ask which broker I use? If so, I try cloudmqtt.com and iot.eclipse.org - same result.
    And today I have a new problem with my yesterday-somehow-worked code. Espruino can't publish messages now.

    1. Uncaught Error: Field or method "length" does not already exist, and can't create it on Number
    2. at line 1 col 88
    3. ...lish",c),b.emit("message",c.topic,c.message);else if(d!==f.P...
    4. ^
    5. in function called from system
    6. at line 1 col 24
    7. a=g(a);var c=b.length+e.length,d="";do{var f=c&127,c=c>>7;0<...
    8. ^
    9. in function "m" called from line 1 col 95
    10. ...?l(a):l(a)+d;return m(c,a,b)
    11. ^
    12. in function "q" called from line 1 col 42
    13. this.client.write(q(a,b,e||this.C.DEF_QOS))
    14. ^
    15. in function "publish" called from line 45 col 36
    16. mqtt.publish('/iskra', humidity);
    17. ^

    Could it be relevant to last updates? didn't look to changes so far.

  • I just tested here on an Espruino Wifi board with your code - publish and subscribe seem to work ok for me. Seems like your issue is that humidity isn't a string?

    I just saw '/iskra' in the .publish command - are you using this on an IskraJS board?

    If so, you should really be asking Amperka for help - or at least letting us know that you're not using an Espruino board. Amperka are using Espruino for free - I can't afford to spend hours of my time supporting their customers as well!

  • Oh. I'm really sorry for bothering you. I didn't know that it is working this way, thought that it's just a forum where people could ask questions.
    I didn't met anybody here who used MQTT on js controller and not much people working with internet connection on js contoller. Usually it's just kids plays and thats it. I think, js controllers can do really great things and try to prove it.
    I didn't tell about iskra just because don't think it could be important. So, please accept my I apologize. It certainly was not intentional.

  • @Ickis There is this module also: https://github.com/olliephillips/tinyMQTT. You may experience the same issues, but worth a shot. Note the API is slightly different but it's on the README

  • Ollie, thank you, didn't see that one. Will definitely try it and share the result.

  • No problem, it's fine. I'm in a difficult situation - I want the forum to be to help other people out, but because I put a lot of time (~3 hours a day) into trying to help everyone on the forum and email, it makes life difficult if I end up spending that time helping out another company's customers!

    @Ickis I believe if you try again, after i posted the last comment I think I fixed the issues you were having with the MQTT module?

  • @Gordon Sure, I can understand it. Actually, I was really surprised, when get answer from developer of module and such attention for my problem. It's of course takes time from you and your help is really appreciated. Sorry again for this misunderstanding.
    I didn't try MQTT module since friday's early morning. But when I do, let know what will get. I need to test Ollie suggestion as well.

  • 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:

    1. var server = "ubuntu.home"; // the ip of your MQTT broker
    2. var options = { // all optional - the defaults are below
    3. client_id : getSerial(), // the client ID sent to MQTT - it's a good idea to define your own static one based on `getSerial()`
    4. keep_alive: 60, // keep alive time in seconds
    5. port: 1883, // port number
    6. clean_session: true,
    7. username: "stephen", // default is undefined
    8. password: "*****", // default is undefined
    9. protocol_name: "MQTT", // or MQIsdp, etc..
    10. protocol_level: 4, // protocol level
    11. };
    12. var mqtt = require("MQTT").create(server, options /*optional*/);
    13. mqtt.on('connected', function() {
    14. mqtt.subscribe("test/espruino");
    15. });
    16. mqtt.on('publish', function (pub) {
    17. console.log(JSON.stringify(pub));
    18. console.log("topic: "+pub.topic);
    19. console.log("message: "+pub.message);
    20. });
    21. var WIFI_NAME = "****";
    22. var WIFI_OPTIONS = { password : "****" };
    23. var wifi = require("EspruinoWiFi");
    24. function getPage() {
    25. require("http").get("http://www.pur3.co.uk/hello.txt", function(res) {
    26. console.log("Response: ",res);
    27. res.on('data', function(d) {
    28. console.log("--->"+d);
    29. mqtt.publish("test/espruino", d);
    30. });
    31. });
    32. }
    33. function makeid()
    34. {
    35. var text = "";
    36. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    37. for( var i=0; i < 5; i++ )
    38. text += possible.charAt(Math.floor(Math.random() * possible.length));
    39. console.log('makeid: '+text);
    40. return text;
    41. }
    42. function postIt()
    43. {
    44. var msg = "MSG: " + makeid();
    45. mqtt.publish("test/espruino", msg);
    46. }
    47. wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
    48. if (err) {
    49. console.log("Connection error: "+err);
    50. return;
    51. }
    52. console.log("Connected!");
    53. wifi.getIP(function (err, res) {
    54. if (!err) {
    55. console.log(res.ip);
    56. mqtt.connect();
    57. } else {
    58. console.log(err);
    59. }
    60. });
    61. getPage();
    62. ts = setInterval(function() {postIt();}, 20000);
    63. });

    It seems to randomly add \xD0\x00 to the message.

    Console.log

    1. 1v91 Copyright 2016 G.Williams
    2. >
    3. =undefined
    4. Connected!
    5. 192.168.1.226
    6. Client connected
    7. Response: httpCRs {
    8. "headers": {
    9. "Date": "Tue, 04 Apr 2017 19:19:23 GMT",
    10. "Server": "Apache/2.4.18 (Ubuntu)",
    11. "Last-Modified": "Fri, 15 Nov 2013 15:42:26 GMT",
    12. "ETag": "\"d-4eb390b887c80\"",
    13. "Accept-Ranges": "bytes",
    14. "Content-Length": "13",
    15. "Connection": "close",
    16. "Content-Type": "text/plain"
    17. },
    18. "httpVersion": "1.1",
    19. "statusCode": "200",
    20. "statusMessage": "OK"
    21. }
    22. --->Hello World!
    23. MQTT connection accepted
    24. {"topic":"test/espruino","message":"from server","dup":0,"qos":0,"retain":1}
    25. topic: test/espruino
    26. message: from server
    27. makeid: gRQRa
    28. undefined
    29. Uncaught Error: Field or method "topic" does not already exist, and can't create it on undefined
    30. at line 1 col 88
    31. ...lish",c),a.emit("message",c.topic,c.message);else if(d!==f.P...
    32. ^
    33. in function called from system
    34. at line 2 col 28
    35. console.log("topic: "+pub.topic);
    36. ^
    37. in function called from system
    38. MQTT unsupported packet type: 0
    39. [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
    40. makeid: sJZwi
    41. {"topic":"test/espruino","message":"MSG: sJZwi\xD0\x00","dup":0,"qos":0,"retain":0}
    42. topic: test/espruino
    43. message: MSG: sJZwiÐ
    44. makeid: kL0LJ
    45. {"topic":"test","message":"","dup":0,"qos":0,"retain":0}
    46. topic: test
    47. message:
    48. Uncaught Error: Unknown Timeout
    49. at line 2 col 149
    50. ...ACK)if(clearTimeout(a.ctimo),c=c.charCodeAt(3),0===c)a.conne...
    51. ^
    52. in function called from system
    53. makeid: GJTbH
    54. {"topic":"test/espruino","message":"MSG: GJTbH\xD0\x00","dup":0,"qos":0,"retain":0}
    55. topic: test/espruino
    56. message: MSG: GJTbHÐ
    57. makeid: pUTU1
    58. undefined
    59. Uncaught Error: Field or method "topic" does not already exist, and can't create it on undefined
    60. at line 1 col 88
    61. ...lish",c),a.emit("message",c.topic,c.message);else if(d!==f.P...
    62. ^
    63. in function called from system
    64. at line 2 col 28
    65. console.log("topic: "+pub.topic);
    66. ^
    67. in function called from system
    68. MQTT unsupported packet type: 6
    69. [MQTT]101,115,116,47,101,115,112,114,117,105,110,111,77,83,71,58,32,112,85,84,85,49
    70. makeid: 6QAbL
    71. {"topic":"test/espruino","message":"MSG: 6QAbL\xD0\x00","dup":0,"qos":0,"retain":0}
    72. topic: test/espruino
    73. message: MSG: 6QAbLÐ
    74. makeid: demXD
    75. {"topic":"tes","message":"","dup":0,"qos":0,"retain":0}
    76. topic: tes
    77. message:
    78. MQTT unsupported packet type: 7
    79. [MQTT]116,47,101,115,112,114,117,105,110,111,77,83,71,58,32,100,101,109,88,68
    80. >
  • Update. testing with QoS

    mqtt.publish("test/espruino", msg, 1);

    1. 1v91 Copyright 2016 G.Williams
    2. >
    3. =undefined
    4. Connected!
    5. 192.168.1.226
    6. Client connected
    7. Response: httpCRs {
    8. "headers": {
    9. "Date": "Tue, 04 Apr 2017 19:34:40 GMT",
    10. "Server": "Apache/2.4.18 (Ubuntu)",
    11. "Last-Modified": "Fri, 15 Nov 2013 15:42:26 GMT",
    12. "ETag": "\"d-4eb390b887c80\"",
    13. "Accept-Ranges": "bytes",
    14. "Content-Length": "13",
    15. "Connection": "close",
    16. "Content-Type": "text/plain"
    17. },
    18. "httpVersion": "1.1",
    19. "statusCode": "200",
    20. "statusMessage": "OK"
    21. }
    22. --->Hello World!
    23. MQTT connection accepted
    24. makeid: swv0V
    25. MQTT unsupported packet type: 5
    26. [MQTT]86
    27. makeid: 1HPH2
    28. makeid: 8h85j
    29. MQTT unsupported packet type: 5
    30. [MQTT]83,71,58,32,56,104,56,53,106
    31. makeid: wCyVx
    32. makeid: l2Chh
    33. makeid: gbk2L
    34. makeid: 5HdG8
    35. makeid: wWLvZ
    36. makeid: 6PHch
    37. MQTT unsupported packet type: 5
    38. [MQTT]80,72,99,104
    39. makeid: 1fuue
    40. makeid: sctqL
    41. MQTT unsupported packet type: 6
    42. [MQTT]99,116,113,76
    43. makeid: Rkpqp
    44. makeid: FqKuv
    45. MQTT unsupported packet type: 7
    46. [MQTT]117,118
    47. makeid: G6wsU
    48. makeid: j3g6z
    49. MQTT unsupported packet type: 6
    50. [MQTT]106,51,103,54,122
    51. makeid: 5RuIk
    52. makeid: hJLhd
    53. >

    eMQ Console subscribed test/# QoS 1

    1. 2017-04-04 20:36:33 --(Q1, R1, D0, Topic=test/espruino, Payload=from server)
    2. 2017-04-04 20:36:40 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: gbk2L)
    3. 2017-04-04 20:37:00 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: 5HdG8)
    4. 2017-04-04 20:37:20 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: wWLvZ)
    5. 2017-04-04 20:37:40 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: 6PHch)
    6. 2017-04-04 20:38:00 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: 1fuue)
    7. 2017-04-04 20:38:20 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: sctqL)
    8. 2017-04-04 20:38:40 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: Rkpqp)
    9. 2017-04-04 20:39:00 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: FqKuv)
    10. 2017-04-04 20:39:20 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: G6wsU)
    11. 2017-04-04 20:39:40 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: j3g6z)
    12. 2017-04-04 20:40:00 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: 5RuIk)
    13. 2017-04-04 20:40:20 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: hJLhd)
    14. 2017-04-04 20:40:40 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: sF75w)
    15. 2017-04-04 20:41:00 --(Q1, R0, D0, Topic=test/espruino, Payload=MSG: x87O6)

    mqtt.publish("test/espruino", msg, 2);

    1. 1v91 Copyright 2016 G.Williams
    2. >
    3. =undefined
    4. Connected!
    5. 192.168.1.226
    6. Client connected
    7. Response: httpCRs {
    8. "headers": {
    9. "Date": "Tue, 04 Apr 2017 19:41:48 GMT",
    10. "Server": "Apache/2.4.18 (Ubuntu)",
    11. "Last-Modified": "Fri, 15 Nov 2013 15:42:26 GMT",
    12. "ETag": "\"d-4eb390b887c80\"",
    13. "Accept-Ranges": "bytes",
    14. "Content-Length": "13",
    15. "Connection": "close",
    16. "Content-Type": "text/plain"
    17. },
    18. "httpVersion": "1.1",
    19. "statusCode": "200",
    20. "statusMessage": "OK"
    21. }
    22. --->Hello World!
    23. MQTT connection accepted
    24. {"topic":"test/espruino","message":"from server","dup":0,"qos":0,"retain":1}
    25. topic: test/espruino
    26. message: from server
    27. makeid: M95lA
    28. MQTT unsupported packet type: 5
    29. [MQTT]80,2,0,1
    30. makeid: vuVhs
    31. MQTT unsupported packet type: 5
    32. [MQTT]80,2,0,1,208,0
    33. makeid: X3K5K
    34. MQTT unsupported packet type: 5
    35. [MQTT]80,2,0,1
    36. makeid: 2ZZN3
    37. MQTT unsupported packet type: 5
    38. [MQTT]80,2,0,1,208,0
    39. makeid: yG6HR
    40. MQTT unsupported packet type: 5
    41. [MQTT]80,2,0,1
    42. makeid: SSwc0
    43. MQTT unsupported packet type: 5
    44. [MQTT]80,2,0,1
    45. makeid: UKyIs
    46. MQTT unsupported packet type: 5
    47. [MQTT]80,2,0,1
    48. makeid: NFBwM
    49. MQTT unsupported packet type: 5
    50. [MQTT]80,2,0,1,208,0
    51. makeid: SnxMM
    52. MQTT unsupported packet type: 5
    53. [MQTT]80,2,0,1
    54. makeid: aB2tu
    55. MQTT unsupported packet type: 5
    56. [MQTT]80,2,0,1,208,0
    57. >

    Nothing in eMQ Console subscribed to test/#

  • There isn't QoS support in the MQTT client so that would be your problem in the latter tests. Because MQTT is running over a TCP/IP link which has QoS itself, there is no need to have it.

    I've just made some changes to the MQTT library that will hopefully fix the problems you're having, so could you re-upload with the initial code and see if it is better?

    Do you know what data you were sending initially? At least one of the problems seemed to stem from parsePublish being unable to parse the published data it was receiving - and about the only reason I could see for that was that the length of the topic and message together was just 1.

  • 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.

    1. MQTT.prototype.connect = function(client) {
    2. var mqo = this;
    3. var onConnect = function() {
    4. console.log('Client connected');
    5. client.write(mqo.mqttConnect(mqo.client_id));
    6. mqo.partData = '';
    7. // Disconnect if no CONNACK is received
    8. mqo.ctimo = setTimeout(function() {
    9. mqo.ctimo = undefined;
    10. mqo.disconnect();
    11. }, mqo.C.CONNECT_TIMEOUT);
    12. // Set up regular keep_alive ping
    13. mqo.pintr = setInterval(function() {
    14. // console.log("Pinging MQTT server");
    15. mqo.ping();
    16. }, mqo.ping_interval*1000);
    17. // Incoming data
    18. client.on('data', function(data) {
    19. if (mqo.partData) {
    20. //console.log('** Adding PART DATA **');
    21. data = mqo.partData + data;
    22. mqo.partData = '';
    23. }
    24. var type = data.charCodeAt(0) >> 4;
    25. var rLen = data.charCodeAt(1);
    26. var pLen = rLen + 2;
    27. if (data.length < pLen) {
    28. mqo.partData = data;
    29. return;
    30. }
    31. var pData = data.substr(0,pLen);
    32. if(type === TYPE.PUBLISH) {
    33. var parsedData = parsePublish(pData);
    34. if (parsedData!==undefined) {
    35. mqo.emit('publish', parsedData);
    36. mqo.emit('message', parsedData.topic, parsedData.message);
    37. }
    38. }
    39. else if(type === TYPE.PUBACK) {
    40. // implement puback
    41. }
    42. else if(type === TYPE.SUBACK) {
    43. // implement suback
    44. }
    45. else if(type === TYPE.UNSUBACK) {
    46. // implement unsuback
    47. }
    48. else if(type === TYPE.PINGREQ) {
    49. // silently reply to pings
    50. client.write(TYPE.PINGRESP+"\x00"); // reply to PINGREQ
    51. }
    52. else if(type === TYPE.PINGRESP) {
    53. mqo.emit('ping_reply');
    54. }
    55. else if(type === TYPE.CONNACK) {
    56. if (mqo.ctimo) clearTimeout(mqo.ctimo);
    57. mqo.ctimo = undefined;
    58. var returnCode = pData.charCodeAt(3);
    59. if(returnCode === RETURN_CODES.ACCEPTED) {
    60. mqo.connected = true;
    61. console.log("MQTT connection accepted");
    62. mqo.emit('connected');
    63. mqo.emit('connect');
    64. }
    65. else {
    66. var mqttError = "Connection refused, ";
    67. switch(returnCode) {
    68. case RETURN_CODES.UNACCEPTABLE_PROTOCOL_VERSION:
    69. mqttError += "unacceptable protocol version.";
    70. break;
    71. case RETURN_CODES.IDENTIFIER_REJECTED:
    72. mqttError += "identifier rejected.";
    73. break;
    74. case RETURN_CODES.SERVER_UNAVAILABLE:
    75. mqttError += "server unavailable.";
    76. break;
    77. case RETURN_CODES.BAD_USER_NAME_OR_PASSWORD:
    78. mqttError += "bad user name or password.";
    79. break;
    80. case RETURN_CODES.NOT_AUTHORIZED:
    81. mqttError += "not authorized.";
    82. break;
    83. default:
    84. mqttError += "unknown return code: " + returnCode + ".";
    85. }
    86. console.log(mqttError);
    87. mqo.emit('error', mqttError);
    88. }
    89. }
    90. else {
    91. console.log("MQTT unsupported packet type: " + type);
    92. console.log("[MQTT]" + data.split("").map(function (c) {
    93. return c.charCodeAt(0);
    94. }));
    95. }
    96. if (data.length > pLen) {
    97. client.emit('data', data.substr(pLen));
    98. }
    99. });
  • 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

  • I'm publishing the same topic over and over again, using the node mqtt module on my computer, and occasionally I'm getting incomplete data printed out in Espruino. I assume the Espruino MQTT code is to blame because the client matched on the topic, yet the output doesn't match.

    1. {
    2. "topic": "sb_platform/v1/tes_16002b00",
    3. "message": "100",
    4. "dup": 0, "qos": 0, "retain": 0 }
    5. 100
    6. Connection refused, unknown return code: 115.
    7. {
    8. "topic": "sb_platform/v1/tes_16002b00",
    9. "message": "100",
    10. "dup": 0, "qos": 0, "retain": 0 }
    11. 100
    12. {
    13. "topic": "sb_platform/v1/tes_16002b00",
    14. "message": "100",
    15. "dup": 0, "qos": 0, "retain": 0 }
    16. 100
    17. {
    18. "topic": "sb_platform/v1/tes_16002b00",
    19. "message": "100",
    20. "dup": 0, "qos": 0, "retain": 0 }
    21. 100
    22. MQTT unsupported packet type: 0
    23. [MQTT]0,27,115,98,95,112,108,97,116,102,111,114,109,47,118,49,47,116,101,115,95,49,54,48,48,50,98,48,48,49,48,48
    24. {
    25. "topic": "sb_platform/v1/tes_16002b00",
    26. "message": "10",
    27. "dup": 0, "qos": 0, "retain": 0 }
    28. 10
    29. {
    30. "topic": "sb_platform/v1/tes_16002b00",
    31. "message": "100",
    32. "dup": 0, "qos": 0, "retain": 0 }
    33. 100
    34. {
    35. "topic": "sb_platform/v1/tes_16002b00",
    36. "message": "100",
    37. "dup": 0, "qos": 0, "retain": 0 }
    38. 100
    39. {
    40. "topic": "sb_plat",
    41. "message": "",
    42. "dup": 0, "qos": 0, "retain": 0 }
    43. MQTT unsupported packet type: 6
    44. [MQTT]102,111,114,109,47,118,49,47,116,101,115,95,49,54,48,48,50,98,48,48,103,111
  • I had the same issue, it seems that the default MQTT module sometimes gets a ping reply and the message in a single packet and is not separating the two. Did you try the updated library I posted on Github?

  • @user75453, I did. I didn't have success. Are you sure you pasted valid JS? I recall a closing brace was missing, and some other issues which lead me to give up on that approach.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
    • $ Donate
About

MQTT newbie troubles

Posted by Avatar for Ickis @Ickis

Actions