-->
Page 1 of 2

Wifi connection state when user_init() returns

PostPosted: Wed Jul 22, 2015 11:29 am
by rith87
Hello folks,

What happens to the wifi connection state when user_init() returns? Also, is user_init() called in some while (true) loop in the actual main function?

The reason I ask this is some of Sprite_tm's code in his espeink project:

Code: Select allvoid user_init(void)
{
   int rtcmagic;
   int batEmptyShown;

   stdoutInit();
   ioInit();
   configLoad();

   system_rtc_mem_read(128, &rtcmagic, 4);
   system_rtc_mem_read(128+4, &batEmptyShown, 4);
   if (rtcmagic!=RTC_MAGIC) {
      //Make sure we're in STA+AP mode
      if (wifi_get_opmode()!=3) {
         wifi_set_opmode(3);
         system_restart();
         return;
      }
      //For the next time: start in normal mode
      rtcmagic=RTC_MAGIC;
      system_rtc_mem_write(128, &rtcmagic, 4);
      //Magic word has fallen out of the RTC. Probably means the battery has been changed or
      //taken out. Go into reconfig mode.

      einkFile=espFsOpen("apconnect.bm");
      einkDisplay(2048, fileEinkNeedData, fileEinkDoneCb);

      httpdInit(builtInUrls, 80);

      os_timer_disarm(&wdtTimer);
      os_timer_setfn(&wdtTimer, wdtTimerCb, NULL);
      os_timer_arm(&wdtTimer, 120000, 0); //shut down after 120 seconds
      return;
   }
   
   if (wifi_get_opmode()!=1) {
      wifi_set_opmode(1);
      system_restart();
      return;
   }

   ioEinkEna(1);
   os_delay_us(2000);
   //Battery voltage is divided by an 4.7K/1K resistor divider.
   //ADC: 1024 = 1V
   batteryMeasMv=(system_adc_read()*9765*5.7)/10000;
   if (batteryMeasMv<BAT_LOW_LVL_MV) {
      //Crap, battery is dying.
      if (batEmptyShown!=1) {
         os_printf("Battery empty! Showing bat empty icon...\n");
         batEmptyShown=1;
         system_rtc_mem_write(128+4, &batEmptyShown, 4);
         einkFile=espFsOpen("batempty.bm");
         einkDisplay(2048, fileEinkNeedData, fileEinkDoneCb);
         return;
      } else {
         os_printf("Battery empty! Sleeping...\n");
         sleepmode();
      }
   } else {
      batEmptyShown=0;
      system_rtc_mem_write(128+4, &batEmptyShown, 4);
   }

   os_printf("Datasource %s\n", myConfig.url);
   tcpConnState=TCPSTATE_HEADER;
   einkHeaderPos=0;
   einkHeaderIsValid=0;
   httpclientFetch(myConfig.url, httpclientCb, httpclientHdrCb);
   einkDisplay(24*1024, tcpEinkNeedData, einkDoneCb);

   os_timer_disarm(&wdtTimer);
   os_timer_setfn(&wdtTimer, wdtTimerCb, NULL);
   os_timer_arm(&wdtTimer, 20000, 0); //shut down after 15 seconds
}


How does httpclientFetch() work if the wifi connection is lost upon returning from the user_init() function? The only way I see this working is if the wifi connection persists even after user_init() returns and user_init() is called again by the actual main function.

PS: Regardless, I'll probably try it out myself tomorrow morning

Re: Wifi connection state when user_init() returns

PostPosted: Wed Jul 22, 2015 11:49 am
by eriksl
The init function only sets up some stuff. You're absolutely not supposed to keep running in there. Even if you do, you will be bitten by the watchdog.

Wlan connection takes place in the background and so does accepting connections. For both are callback functions that you should register (although the first may not be that interesting).

Programming the esp8266 using SDK is based on an even-driven model. There is no "loop", the SDK code calls your functions when something needs to get done.

Re: Wifi connection state when user_init() returns

PostPosted: Thu Jul 23, 2015 4:08 am
by rith87
Wlan connection takes place in the background

How does this work? If the WLAN SSID and password are stored at 0x7e000, then the ESP will automatically connect to the router? Does this happen before or after user_init()? So, what is the right way to handle the wifi connection? Just write some timered function that polls for the wifi connection state? I don't think there's an event driven way to handle this part, is there?

Re: Wifi connection state when user_init() returns

PostPosted: Thu Jul 23, 2015 11:09 am
by eriksl
Read the documentation.

If you do nothing, it will connect to whatever ssid is stored in nv storage. If you disconnect and connect explicitly you can choose your own. It's all in the docs.