MQTT Restart on Connection Loss

Posted on
  • Using the ESP8266 I've managed a connection - no problem - and an MQTT (espruino.com/MQTT) connect - with subscribe. All works swimmingly well.

    But, if you disconnect and reconnect the router, you've lost your MQTT connection and subscribes - although the WIFI does seem to reconnect automatically.

    Has anyone already written something to check for broken MQTT connect - and reconnect automatically? (without which of course it isn't a lot of use :-) )

  • you need to watch for wifi.on("connected") events - and in this function do your MQTT setup.

    So if the connection is dropped, or the board restarts, it will then run this code.

    There is some discussion on gitter about this - you might ask them to post their code so you can see how they have set it up...

    gitter.im/espruino/Espruino?at=5­8ee1f738e4b63533dbfad9e

    Something like:

    function Start() {
    console.log("starting servers");
    var http = require('http').createServer(onPageReque­st);
    http.listen(80);
    }
    
    var wifi=require("Wifi");
    
    wifi.on('connected', Start );
    
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

MQTT Restart on Connection Loss

Posted by Avatar for Scargill @Scargill

Actions