Cannot connect to Wifi after deep sleep.

Posted on
  • Hi,

    I'm trying to connect to wifi after deep sleep wakeup on ESP32. The initial run work, but after wake from deep sleep the code wifi.connect is not executed. It does not fall into error or success branch.

    I would like to ask how to do it in the right way:

    Firmware version:
    espruino_2v03_esp32
    device is connected throught the USB cable

    var WIFI_NAME = "";
    var WIFI_OPTIONS = { password : "" };
    var wifi = require("Wifi");
    
    
    function connectToWifi(){
      console.log("connectToWifi");
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
      if (err) {
        console.log("Connection error: "+err);
        return;
      } else {
        console.log("Connected!");
        getPage();
      }
        console.log("End");
      });
    };
    
    function getPage() {
      require("http").get("http://www.pur3.co.­uk/hello.txt", function(res) {
        console.log("Response: ",res);
        res.on('data', function(d) {
          console.log("--->"+d);
        });
      });
    }
    
    
    
    setInterval(function() {
      connectToWifi();
    }, 10000);
    setDeepSleep(1);
    
  • It depends how you saved the code but chances are you need to put the code you want to run at boot into a function called onInit

  • Thanks. I tried it in the following way but with the same result (even without deep sleep):

    var WIFI_NAME = "";
    var WIFI_OPTIONS = { password : "" };
    
    function onInit() {
      wifi = require("Wifi");
     // Call to an function that kicks off my code
      setInterval(function() {
      connectToWifi();
      //setDeepSleep(1);
      }, 10000);
    }
    
    function connectToWifi(){
      console.log("connectToWifi");
      var wifi = require("Wifi");
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
      if (err) {
        console.log("Connection error: "+err);
        return;
      } else {
        console.log("Connected!");
        getPage();
      }
        console.log("End");
      });
    }
    
    function getPage() {
      require("http").get("http://www.pur3.co.­uk/hello.txt", function(res) {
        console.log("Response: ",res);
        res.on('data', function(d) {
          console.log("--->"+d);
        });
      });
    }
    
    
    
    
    

    Calculating Size...
    Writing..
    Compressed 36800 bytes to 2742
    Running onInit()...
    connectToWifi
    connectToWifi
    connectToWifi
    connectToWifi
    connectToWifi
    connectToWifi
    connectToWifi
    connectToWifi
    connectToWifi
    connectToWifi
    save();

  • I do not know your IDE settings, but the idea here is to save() before running anything.

  • I'd say it takes longer than 10s to connect to the Wifi initially, for whatever reason.

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

Cannot connect to Wifi after deep sleep.

Posted by Avatar for user106328 @user106328

Actions