pico + WIZ550io + mqtt connection issue

Posted on
  • I've been attempting to connect to mqtt using the WIZ550io instead of wifi without success. My goal is to use a pico + WIZ550io + mqtt to trigger relays. I doubled checked if my pico has an IP address by typing eth.getIP() into the IDE console and I get the following result:

    ={
      "ip": "192.168.0.21",
      "subnet": "255.255.255.0",
      "gateway": "192.168.0.1",
      "dns": "************",
      "mac": "00:08:dc:1e:b9:73"
     }
    

    I'm following this guide: http://www.espruino.com/Home+Automation

    I think my issue is with in my onInit function. I'm trying to call mqttConnect(); on a successful

    eth.getIP

    , but I don't think

    mqttConnect()

    is getting called:

    eth.getIP(function(f,ip) {
        console.log("IP: ",ip);
        mqttConnect();
      });
    

    code:

    var turnedOFF = 1;
    
    var MQTT_HOST = "192.168.0.22";
    var PATH = "/mydevice/";
    var mqtt;
    var eth;
    
    function mqttMessage(pub) {
      console.log(
         "MQTT=> ",pub.topic,pub.message);
      if (pub.topic==PATH+"onoff") {
        var v = pub.message!=0;
        digitalWrite(B3, !v);
        mqtt.publish(PATH+"1/status", v?1:0);
      }
    }
    
    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);
      });
    }
    
    setInterval(function() {
      if (!mqtt) return;
      mqtt.publish(
        PATH+"cputemp",
        E.getTemperature());
    }, 2*60*1000);
    
    function trigger() {
      turnedOFF = 1;
      digitalPulse(B3,0,1000);
      digitalWrite(LED1, 0);
      console.log("digitalWrite");
    }
    
    setWatch(function(e) {
      digitalWrite(LED1, e.state);
      trigger();
    }, BTN, { repeat: true });
    
    function onInit() {
      console.log("Connecting to Ethernet");
    
      SPI2.setup({ mosi:B15, miso:B14, sck:B13 });
      eth = require("WIZnet").connect(SPI2, B10);
    
      eth.setIP();
    
      //eth.getIP();
      eth.getIP(function(f,ip) {
        console.log("IP: ",ip);
        mqttConnect();
      });
    
    }
    
  • I figured this out. setIP() returns true when it successfully finishes.

    Updated onInit code:

    function onInit() {
      console.log("Connecting to Ethernet");
    
      SPI2.setup({ mosi:B15, miso:B14, sck:B13 });
      eth = require("WIZnet").connect(SPI2, B10);
    
      console.log("Connect?: " + isConnected);
    
      isConnected = eth.setIP();
      console.log("Connect?: " + isConnected);
    
      if (isConnected == true) {
        mqttConnect();
      }
    
      //eth.getIP();
      eth.getIP(function(f,ip) {
        console.log("IP: ",ip);
        mqttConnect();
      });
    
    }
    
  • Great! I think one issue you have might be that Ethernet.getIP doesn't use a callback so never calls your function - it just returns the IP as-is.

    That's definitely an issue, as it should use the same form as other networking APIs to avoid this kind of confusion. I just filed an issue to get it fixed.

  • @Gordon I think I meant to say setIP instead of getIP in my original post. I would like to execute mqttConnect(); after espruino successfully connects to the network via a callback. Implementing a callback on both setIP and getIP would be great. Thank you for submitting an issue.

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

pico + WIZ550io + mqtt connection issue

Posted by Avatar for d0773d @d0773d

Actions