• @Gordon

    That's interesting - what happens if you switch back to the original
    MQTT?

    I executed reset(true) changed the mqtt require to mqtt = require("MQTT").connect then uploaded my code.
    after about 3 mqtt messages I get: MQTT disconnected... reconnecting. Then mqtt reconnects with message MQTT connected

    You could try replacing:

    I changed the code with your suggestion, executed reset(true) then uploaded the code.
    After about 3 messages I receive: MQTT disconnected... reconnecting. Then mqtt reconnects with message MQTT connected

    code:

    var MQTT_HOST = "192.168.0.17";
    var PATH = "/mydevice/";
    var mqtt;
    var eth;
    
    var inProcess =  false;
    var shortPress = 1000; // ms 1 sec
    var longPress = 5000; // ms 5 secs
    var pressPin = B3;
    
    // output like 'open collector' see reference
    pinMode(pressPin,"opendrain"); 
    pressPin.set();
    
    function mqttMessage(pub) {
      console.log(
         "MQTT=> ",pub.topic,pub.message);
      if (pub.topic==PATH+"computer") {
        if (pub.message == "shutdown") {
          press(shortPress);
        }
      }
    }
    
    function mqttConnect() {
      console.log("mqttConnect");
      mqtt = require("MQTT").connect({
        host: MQTT_HOST,
      });
      mqtt.on('connected', function() {
        console.log("MQTT connected");
        // subscribe to wildcard for our name
        mqtt.subscribe(PATH+"#");
      });
      mqtt.on('publish', mqttMessage);
      var thisMQTT = mqtt;
      mqtt.on('disconnected', function() {
        if (mqtt!=thisMQTT) {
          console.log("MQTT disconnected - but we already have a new MQTT");
          return;
        }
        console.log("MQTT disconnected... reconnecting.");
        setTimeout(function() {
          mqtt.connect();
        }, 1000);
      });
    }
    
    function press(short) {
      // ignore press function when one is still going on
      if (inProcess) return; 
      inProcess = true;
      // Relays (board) act in an inverse manner
      // reset() will latch the relay
      pressPin.reset();
      setTimeout(function(){
          // Relays (board) act in an inverse manner
          // set() will unlatch the relay
          pressPin.set();
          inProcess = false;
        },(short) ? shortPress : longPress);
    }
    
    setWatch(function(e) {
      digitalWrite(LED1, e.state);
      press(shortPress);
    }, BTN, { repeat: true });
    
    function onInit() {
      console.log("Connecting to Ethernet");
    
      SPI2.setup({ mosi:B15, miso:B14, sck:B13 });
      eth = require("WIZnet").connect(SPI2, B10);
    
      isConnected = eth.setIP();
    
      if (isConnected == true) {
        mqttConnect();
      }
    }
    

    Is it good practice to execute reset(true) before I upload any new code in the future?

About

Avatar for d0773d @d0773d started