CC3000 WiFi Connect after no DHCP failing

Posted on
  • Trying to work around the CC3000 when it does not get an address with DHCP.

    What I am doing is doing a disconnect after 30 seconds when it fails to get a DHCP address, then wait another 10 seconds and try to connect again.

    However I am getting:

    WLAN Connecting...
    Uncaught Error: Function "connect" not found!
     at line 1 col 274
    ...e("CC3000").connect();WLAN.connect(WL­AN_SSID,WLAN_WPA2_KEY,f...
    

    Anybody else have a good work around for when it fails to get a DHCP address or any ideas why it is saying the connect function does not exist?

  • OK this also occurs after successfully getting a DHCP address, doing what I want with it then doing a disconnect.

    When I then try to connect the WiFi a minute later it fails with the same error.

  • It'd be nice if we could figure out which function called connect it's having a hard time with - since there are two there (the require("CC3000").connect() and the WLAN.connect()).

  • It is using this code as per the tutorial, hence the two connect() in the error:

        WLAN = require("CC3000").connect();
        WLAN.connect( WLAN_SSID, WLAN_WPA2_KEY, function (s) {
    
  • And the little error arrow is under this connect():

    WLAN.connect(WLAN_SSID,WLAN_WPA2_KEY,f..­.
    
  • what does the WLAN object look like? It sounds like the call to require("CC3000").connect(); is returning something bogus the second and subsequent times it's called.

    Actually, are you even supposed to be using it like that?
    The documentation shows doing 'wlan.disconnect()' and then later, wlan.connect() - but never a second call to require("CC3000").connect() - so maybe the second time you call it, it's returning a bogus object, because it's only meant to be called once, and isn't smart enough to throw an exception?

  • WLAN is just a global variable, nothing special, so that I can run the WLAN.disconnect() in another function.

    Trying to have one WiFi connection function to avoid code duplication for all the functions that will need WiFi.

    According to the documentation here: http://www.espruino.com/Reference#l_WLAN­_disconnect it says:

    function WLAN.disconnect()
    Completely uninitialise and power down the CC3000. After this you'll have to use require("CC3000").connect() again.

    I will try it using the sample code where everything is in one function, disconnect and then connect again and see if I get the same issue.

  • OK, not sure how to output the contents of WLAN to see what it is the second time.

    If I do console.log("" + WLAN);

    I get [object Object]

    And get the same the second time when it fails.

  • OK, seems you are correct and the documentation wrong.

    As you say in the DISCONNECTING example here: http://www.espruino.com/CC3000

    var wlan = require("CC3000").connect();
    

    Is only called once.

    Many thanks for your help.

  • Just to make sure I understand here, we're saying that if you call require("CC3000").connect() a second time, it returns undefined or something that's incorrect? That should work, so it could be some kind of bug.

    Looks like the documentation is incorrect too though - looking at the code it's designed such that you should be able to call either connect function after it.

  • @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);
    
  • You can dump it with console.log(JSON.stringify(wlan)) or maybe even console.log(wlan)... I'll try it out here and will fix it when I get time though.

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

CC3000 WiFi Connect after no DHCP failing

Posted by Avatar for user47955 @user47955

Actions