MQTT newbie troubles

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

    function mqttRun(){
      var server = 'my_broker';
      var options = {
              keep_alive: 60,
              port: my_port,
              clean_session: my_port,
              username: "my_user",
              password: "my_pass",
              protocol_name: "MQTT",
              protocol_level: 4,
      };
      var mqtt = require("MQTT").create(server);
      mqtt.connect(options);
      mqtt.on('connected', function() {
        mqtt.subscribe("test");
      });
    }
    

    and as result got:

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

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

    mqttRun()
    ["AT+CIPSTART=0,\"TCP\",\"iot.eclipse.or­g\",1883\r\n"
    =undefined
    Client connected
    ] "\r\n" <--- "\r\n"
    ] "OK\r\n" <--- "OK\r\n"
    ] "\r\n" <--- "\r\n"
    ] "0, CO" <--- "0, CO"
    ] "0, CONNEC" <--- "NNEC"
    ] "0, CONNECT OK\r" <--- "T OK\r"
    ] "\n" <--- "\n"
    ] "\r" <--- "\r"
    ] "\n> " <--- "\n> "
    ] "\r\n" <--- "\r\n"
    ] "0, SE" <--- "0, SE"
    ] "0, SEND O" <--- "ND O"
    ] "0, SEND OK\r\n" <--- "K\r\n"
    ] "\r\n+RECEI" <--- "\r\n+RECEI"
    ["AT+CIPCLOSE=0,1\r\n"
    MQTT client disconnected
    MQTT client disconnected
    Uncaught Error: Unknown Interval
     at line 1 col 62
    ...ted");clearInterval(b.pintr);b.emit("­disconnected");b.emit("...
                                  ^
    in function called from system
    ] "+RECEIVE,0,4:\r\n " <--- "VE,0,4:\r\n "
    ] "+D,0,3:\x02\x00\x00" <--- "\x02\x00\x00"
    ] "\r" <--- "\r"
    ] "\n> " <--- "\n> "
    ] "\r" <--- "\r"
    ] "\n0, " <--- "\n0, "
    ] "0, SEN" <--- "SEN"
    ] "0, SEND O" <--- "D O"
    ] "0, SEND OK\r\n" <--- "K\r\n"
    ] "\r" <--- "\r"
    ] "\n0, " <--- "\n0, "
    ] "0, CLO" <--- "CLO"
    ] "0, CLOSE" <--- "SE"
    ] "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 espruino.com/MQTT

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

    function mqttRun(){
      var server = 'my_broker';
      var options = {
              keep_alive: 60,
              port: my_port,
              clean_session: my_port,
              username: "my_user",
              password: "my_pass",
              protocol_name: "MQTT",
              protocol_level: 4,
      };
      var mqtt = require("MQTT").create(server);
        mqtt.on('connected', function() {
        mqtt.subscribe("test");
      });
    
      
      mqtt.connect(options);
    }
    

    I tried now - same error

    >mqttRun()
    Client connected
    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

    function mqttRun(){
      var server = 'my_broker';
      var options = {
              keep_alive: 60,
              port: my_port,
              clean_session: my_port,
              client_id: "my_id",
              username: "my_user",
              password: "my_pass",
              protocol_name: "MQTT",
              protocol_level: 4,
      };
      var mqtt = require("MQTT").create(server, options); // changes here
        mqtt.on('connected', function() {
        mqtt.subscribe("/test");
      });
       mqtt.on('publish', function (pub) {  // this place throw error
        console.log("topic: "+pub.topic);
        console.log("message: "+pub.message);
      });
    
      mqtt.connect();
    
    }
    
    

    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:

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

    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:

    JSON TEST {"topic":"3865","message":"","dup":1,"qo­s":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:

    Uncaught Error: Unknown Timeout
     at line 2 col 149
    ...ACK)if(clearTimeout(b.ctimo),c=c.char­CodeAt(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 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.

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

    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: github.com/olliephillips/tinyMQT­T. 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.

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

MQTT newbie troubles

Posted by Avatar for Ickis @Ickis

Actions