• @gordon yes correct.

    Not sure what it returns on its second call but it errors.

    How do you dump the contents of the variable to see what it is set to and I can let you know.

    If I do console.log("" + wlan) all I get is [object Object] both times.

    This code works:

    var wlan = require("CC3000").connect();
    
    function getTime() {
      // if we don't get DHCP or a complete request in 30 seconds,
      // disconnect
      var failTimeout = setTimeout(function() {
        console.log("Timed out, disconnecting...");
        wlan.disconnect();
      }, 30000);
      wlan.connect( "AccessPointName", "WPA2key", function (s) {
        if (s=="dhcp") {
          require("http").get("http://www.pur3.co.uk/time.php",
                              function(res) {
            // store the result
            var result = "";
            res.on('data', function(data) {
              result += data;
              console.log(data);
            });
            /* When the connection closes, print what we
            got, and then disconnect from WiFi */
            res.on('close', function() {
              console.log("Got: "+result);
              // finished, disconnect anyway
              setTimeout(function() {
                clearTimeout(failTimeout);
                console.log("Complete!, disconnecting...");
                wlan.disconnect();
              },1000);
            });
          });
        }
      });
    }
    
    // Every 24 hours, get the time again
    setInterval(getTime, 24*60*60*1000);
    

    This doesnt work:

    function getTime() {
      // if we don't get DHCP or a complete request in 30 seconds,
      // disconnect
      var failTimeout = setTimeout(function() {
        console.log("Timed out, disconnecting...");
        wlan.disconnect();
      }, 30000);
      var wlan = require("CC3000").connect();
      wlan.connect( "AccessPointName", "WPA2key", function (s) {
        if (s=="dhcp") {
          require("http").get("http://www.pur3.co.uk/time.php",
                              function(res) {
            // store the result
            var result = "";
            res.on('data', function(data) {
              result += data;
              console.log(data);
            });
            /* When the connection closes, print what we
            got, and then disconnect from WiFi */
            res.on('close', function() {
              console.log("Got: "+result);
              // finished, disconnect anyway
              setTimeout(function() {
                clearTimeout(failTimeout);
                console.log("Complete!, disconnecting...");
                wlan.disconnect();
              },1000);
            });
          });
        }
      });
    }
    
    // Every 24 hours, get the time again
    setInterval(getTime, 24*60*60*1000);
    
About

Avatar for user47955 @user47955 started