1
votes

REVISION: Please note I am now using IP address 10.0.0.15, my device publishing to the MQTT broker is 10.0.0.122. This is still all working through terminal.

I think I am getting somewhere with an MQTT connector. I have moved forward after having issues as describe in the post below

Can't connect to localhost Mosquitto Broker with Javascript?

I am now seeing the following error.

         mqttws31.js:1585 Uncaught Error: AMQJS0013E Invalid argument 
         169.254.118.199 for host.
         at new M (mqttws31.js:1585)
         at startConnect (n.js:29)
         at HTMLInputElement.onclick ((index):107)

which according to the js file indicates a match error. I have tried prefixing the ip address to be wss://169.254.118.199 but this doesn't resolve the issue. Do you know what could be causing this?

I have tried the following

 wss://169.254.118.199
 ws://169.254.118.199
 wss://localhost
 tcp://169.254.118.199
 tcp://localhost

They all produce the same error

This is the bit of code in mqttws31.js that the error points to.

          if (arguments.length == 2) {
          // host: must be full ws:// uri
          // port: clientId
          clientId = port;
          uri = host;
          var match = uri.match(/^(wss?):\/\/((\[(.+)\])|([^\/]+?))(:(\d+))? 
          (\/.*)$/);
          if (match) {
          host = match[4]||match[2];
          port = parseInt(match[7]);
          path = match[8];
          } else {
          --> this is where error is pointing throw new Error(format(ERROR.INVALID_ARGUMENT,[host,"host"]));
          }
          } else {
          if (arguments.length == 3) {
            clientId = path;
            path = "/mqtt";
          }
          if (typeof port !== "number" || port < 0)
            throw new Error(format(ERROR.INVALID_TYPE, [typeof port, "port"]));
          if (typeof path !== "string")
            throw new Error(format(ERROR.INVALID_TYPE, [typeof path, "path"]));

          var ipv6AddSBracket = (host.indexOf(":") != -1 && host.slice(0,1) != "[" && 
          host.slice(-1) != "]");
          uri = "ws://"+(ipv6AddSBracket?"["+host+"]":host)+":"+port+path;
          }

EDIT: in the print out I am seeing the following being sent to the Paho client on my webpage:

    Connecting to: 10.0.0.122 on port: 8083
    Using the following client value: clientID-64

I am hoping to sucessfully connect with the IP address and get the MQTT payload

1
There seems to be an issue with the eth0, when I run Mosquitto throughthe terminal I can connect, when I run in this code it will connect to my device. The device IP is actually 169.254.84.122, which really doesn't make any sense. Also when I try to set a static ip on eth0 the pi just wont take it. im stumped at the minute.PrimitiveSource
Edit the code to print to the console exactly what you are feeding into the Paho.MQTT.Client(host, Number(port), clientID); and add this to the question so we can see EXACTLY what you are passing to the code.hardillb
@hardlib I have added an *EDIT near the bottom of the original question showing what is being sent to Paho.MQTT.Client(host,Number(port), clientID);PrimitiveSource
I have tested again, I am now getting past this host name error and I now keep having a connection time out? mqttws31.js:977 WebSocket connection to 'ws://10.0.0.15:8083/mqtt' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUTPrimitiveSource

1 Answers

1
votes

To get the error you are seeing, you can not be using the code you have posted in the other questions:

clientID = "clientID-" + parseInt(Math.random() * 100);

// Fetch the hostname/IP address and port number from the form
host = document.getElementById("host").value;
port = document.getElementById("port").value;
// Print output for the user in the messages div

// Initialize new Paho client connection
 client = new Paho.MQTT.Client(host, Number(port), clientID);

That error can only happen if you only pass 2 arguments to the Paho.MQTT.Client() constructor not 3. In which case the first argument is interpreted as a full URI (e.g. ws://10.0.0.122:8083/mqtt), the second as the ClientID.