MQTT library returning error connecting to mosquitto

Posted on
  • I am using my Espruino Wifi updated to 2v21. I have a strange problem, although I have used the MQTT library before without any problems, it seem to error for me now. I am able to connect and use tinyMQTT but I want to use QoS > 0 which is only available in MQTT.

    I am running mosquitto in a container on my raspberrypi. If I try to connect using the MQTT library I get the following output:

    Log from the mosquitto container:
    1708970867: New connection from 192.168.1.100:44538 on port 8883.
    1708970867: New client connected from 192.168.1.100:44538 as 29004900-0c513532-39333638 (p2, c1, k60, u'username').

    Log from Espruino IDE:
    Connection refused, unknown return code: NaN.

    Here is the code for the MQTT example:

    const WIFI_NAME = "BT-redacted";
    const WIFI_OPTIONS = { password : "redacted" };
    const wifi = require("Wifi");
    let mqtt;
    
    const server = "192.168.1.77"; // the ip of your MQTT broker
    const options = {
      client_id: getSerial(),
      keep_alive: 60,
      port: 8883,
      clean_session: true,
      username: "username",
      password: "password",
      protocol_name: "MQTT",
      protocol_level: 4,
    };
    
    const onInit = () => {
      connect();
    };
    
    const connect = () => {
      console.log("wifi connecting...");
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, (err) => {
        if (err) {
          console.log("wifi error", err);
          return;
        }
        console.log("wifi connected");
        connectMQTT();
      });
    };
    
    const connectMQTT = () => {
      console.log("connecting to mqtt");
      mqtt = require("MQTT").create(server, options);
    
      mqtt.on("connected", () => {
        console.log("mqtt connected");
        mqtt.subscribe("espruino/rgb");
        mqtt.subscribe("espruino/speed");
      });
    
      mqtt.on("publish", (pub) => {
        console.log(pub.topic, pub.message);
      });
    
      mqtt.on("error", (err) => console.log("error", err));
      mqtt.connect();
    };
    

    Full output in Espruino IDE

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v21 (c) 2023 G.Williams
    >
    >onInit()
    wifi connecting...
    =undefined
    wifi connected
    connecting to mqtt
    error Connection refused, unknown return code: NaN.
    > 
    

    What could be different about MQTT v tinyMQTT for me now?

  • When you see:

    1708970867: New client connected from 192.168.1.100:44538 as 29004900-0c513532-39333638 (p2, c1, k60, u'username').
    

    Is the stuff in brackets the same for tinyMQTT and MQTT?

    And did tinyMQTT actually work to send/receive data, or did it just connect?

    I guess it's possible tinyMQTT/MQTT use different names for username/password/similar? So you could run wifi.dbg() (I think?) to turn on debug messages, and you could maybe see what's different about the first few messages sent/received from the MQTT server?

    I just checked and I'm pretty sure that to get Connection refused, unknown return code NaN the message it received from the server had to have been cut short - it'd be receiving a CONNACK message with less than the 4 bytes it's expecting.

    tinyMQTT never checks any of that so it's possible it's still receiving the unexpected data, it just doesn't care

  • After a little more digging, I noticed I was sending defaults to MQTT and nothing to tinyMQTT.

    wifi.dbg() throws unhandled exception

    tinyMQTT:

    mqtt = require("tinyMQTT").create(server);
    
    mosquitto logs:
    1709312245: New client connected from 192.168.1.100:15450 as 29004900-0c513532-39333638 (p2, c0, k65535).
    
    K = keep_alive
    

    MQTT:

    mqtt = require("MQTT").create(server, options);
    // tried different defaults in the options obj, nothing matters
    
    mosquitto logs:
    1709312100: New connection from 192.168.1.100:44568 on port 8883.
    1709312100: New client connected from 192.168.1.100:44568 as random (p2, c1, k10).
    

    Summary:

    tinyMQTT :
    Mosquitto says connected - YES
    Espruino says connected - YES
    Receive messages - YES

    MQTT
    Mosquitto says connected - YES
    Espruino says connected - NO (Connection Error)
    Receive messages - NO

    3rd part Android app
    Mosquitto says connected - YES
    Can publish messages - YES

    All using same server and port number

  • Interesting that the values are different - so keepalive is different which you might expect - do you know what c stands for in (p2, c1, k10)?

  • c stands for clean session apparently.

    c0: This indicates the clean session status. "c0" means that the clean session is set to false. When a client connects with a clean session set to true (c1), it means the broker should not store any subscriptions or undelivered messages for the client when it disconnects. With clean session set to false (c0), the broker will store subscriptions and undelivered messages for the client.

    p stand for QoS. So p2 would be QoS level 2.

    Take it with a pinch of salt until I've verified this as it's a quick ChatGPT search

  • Even though it says it's not connected, if you do mqtt.publish("test","hello") and you're looking at the server with mosquitto_sub -t "#" do you actually see the message coming out?

    Similarly if you call mqtt.subscribe("test"); does it work?

    I just tried here and it all seems ok except the connection callback isn't working at all for me (no error or anything)

  • Ahh good shout, seem I can use the standard MQTT library even if there's an error. So now instead of waiting for a successful connection (which never comes) I just run my subscribes in the on error handler instead.

    mqtt.on("error", (err) => {
        console.log("error", err);
        console.log("mqtt connected");
        lightsOff();
        lightsOn({r: 0, g: 128, b: 0}, speed);
        mqtt.subscribe("espruino/rgb");
        mqtt.subscribe("espruino/speed");
      });
    

    Doesn't matter that I receive an error NaN, everything works as expected afterwards. Thanks for supporting me on this Gordon its good enough for a home automation project I'm working on. Looking forward to getting the next Espruino you put out in the future.

  • Thanks! Obviously that's not ideal though!

    I'll take a look here and see if I can figure out what's up with the connect event - I'm pretty sure it did work in the past!

  • Ok, found and fixed it! If you upload again now it should work.

    We were using String.charCodeAt in a String, and expecting it would report back 0 if the string didn't include the character. A recent PR for Espruino changed it to report back NaN as per the spec, and that broke this MQTT lib.

  • I've been away in Mexico, just updated and its working great. Thanks again Gordon, any news on the next Espruino Wifi or other products coming out?

  • Great!

    I'm in the process of getting the first 1500 of https://www.espruino.com/Jolt.js ready - I'll have to wait a little for the cases to be manufactured, but hopefully they'll be ready in a month or so.

    After that we'll see how it goes - I have started selling the Espruino WiFi again for now (as the STM32 prices are down to sensible levels again) but I think probably a Pixl.js with WiFi (and maybe a motor driver again) will be coming, but probably not until the end of the year earliest

  • I'm in the process of getting the first 1500 of https://www.espruino.com/Jolt.js ready

    Is there any "TAKE MY MONEY" waiting list in which I can buy a seat?

  • :) Not yet - but soon I hope!

  • Why not open a pre order posibily in your shop? I would buy three even if I have to wait 6 month ;-)

  • Thanks! - that was the plan, but it's been feeling like they should be ready to ship so soon it was hardly worth doing a pre-order. I'll see how long the manufacturer says the silicone cases will take - that's really what I'm waiting for now. If it's more than 2 weeks I'll do a preorder.

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

MQTT library returning error connecting to mosquitto

Posted by Avatar for Coder2012 @Coder2012

Actions