• Hardware setup: Pico, wiznet 5500io, Dual relay module

    I executed save() in the IDE to save the code.

    Functionality:
    Pico connects to my mqtt broker and sends commands to latch/unlatch my relay.

    Odd behavior:
    Code will execute even though there is an Error thrown; however, when I disconnect the Pico from the IDE the code doesn't execute. To debug this issue I disconnected the Pico from my computer and plugged it back in to reboot, but the code still doesn't execute.

    I then connected the Pico to the IDE again. Once Console.log and the Error dumps out to the screen the code executes correctly again.

    Error:

    Uncaught Error: This socket is closed. at line 1 col 54
    this.client&&this.client.write(g(d.PINGR­EQ<<4)+"\x00") ^ in function
    "ping" called from line 1 col 8 a.ping() ^ in function called from
    system

    Code:

    var MQTT_HOST = "192.168.0.22";
    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;
    
    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);
      mqtt.on('disconnected', function() {
        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 act in an inverse manner
      //reset() will latch the relay
      pressPin.reset();
      mqtt.publish(PATH+"shutdown/status", 1);
    
      setTimeout(function(){
          //Relays 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();
      }
    }
    
About

Avatar for d0773d @d0773d started