[SOLVED] Connect to MQTT problems

Posted on
  • Hi,

    I'm trying to use MQTT on a ESP8266 (Flash map 1MB:512/512, manuf 0xc8 chip 0x4014) and always returns same error:

    1526108440: New connection from 192.168.10.44 on port 1883.
    1526108440: New client connected from 192.168.10.44 as 4e8422275dd0 (c1, k60).
    1526108440: Socket error on client 4e8422275dd0, disconnecting.
    

    It's only a simple connection test code like this:

    var wifi = require("Wifi");
    var mqtt = require("MQTT").create("192.168.10.43", {
      keep_alive: 60,
      clean_session: true
    });
    
    wifi.connect("MyWifi", {password: "MyPassword"},function(err){
      if (!err) {
        const info = wifi.getIP();
        console.log('connected? err=', err, 'info=', info);
        mqtt.connect();
      } else {
        console.log('error: ',err);
      }
    });
    wifi.stopAP();
    
    mqtt.on("connected", function(){
      console.log("connected");
    });
    
    mqtt.on("disconnected", function(){
      console.log("disconnected");
    });
    

    The output is always the same:

    >
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     1v97 (c) 2018 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 1MB:512/512, manuf 0xc8 chip 0x4014
    >connected? err= null info= {
      "ip": "192.168.10.44",
      "netmask": "255.255.255.0",
      "gw": "192.168.10.1",
      "mac": "5c:cf:7f:f1:bd:25"
     }
    connected
    >
    =undefined
    disconnected
    disconnected
    > 
    

    I try a lot of configurations and I don't know how to continue:

    1. Mosquitto server's: 1.4.14, 1.4.15, "test.mosquitto.org",...etc
    2. Test on different ESP8266 boards.
    3. Using module "MQTT" and "TinyMQTT".
    4. Move "mqtt.connect()" line inside "wifi.connect" callback
    5. Try using connect directly:

      var mqtt = require("MQTT").connect({
      host: "192.168.10.43",
      });
      
    6. Run mosquitto server in verbose mode "mosquitto -v":

      1526109123: New connection from 192.168.10.44 on port 1883.
      1526109123: New client connected from 192.168.10.44 as 3fefc4344328 (c1, k60).
      1526109123: Sending CONNACK to 3fefc4344328 (0, 0)
      1526109123: Socket error on client 3fefc4344328, disconnecting.
      

    Mosquitto server is up and runnig with anonymous access acepted. I'm using this server with other esp8266 boards running native C code and goes good, so I think that is no problem with the server.

    If someone can help me with this problem, I would be enormously grateful.

    Thank you

  • I first start my mqtt in:

    wlan.on ('connected', function (s) {
      mqttstart ()
    };
    

    as it sometimes takes some time before my esp8266 catches the network.

  • You could try using one of the very recent builds from http://www.espruino.com/binaries/travis/­master/ - it looks like 1v97 had an issue that could have broken TCPIP sockets in some cases.

  • Thank you very much @Gordon. I delete flash and upgrade to 1v97.98 and works great!!!

    I saw new versions at http://www.espruino.com/binaries/travis/­master/ for example release 1v97.100. This versions are the last development versions?. Are the same as github repository "master" branch?

    Thank you.

    Best regards,
    Juanillo

  • This versions are the last development versions?. Are the same as github repository "master" branch?

    Yes, that's right - they're built automatically with each new commit. You can actually look up a specific commit in http://www.espruino.com/binaries/travis/­

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

[SOLVED] Connect to MQTT problems

Posted by Avatar for Juanillo @Juanillo

Actions