ESP8266 Webserver Project

Moderator: Sprite_tm

User avatar
By Piotr K
#42577 I love this project, it is very smooth and powerful. OTA updates are great! But I have run into an obstacle. I need to get a static IP when connecting to WiFi. So far the only way to achieve it is something like this:

Code: Select allLOCAL void ICACHE_FLASH_ATTR set_ips()
{
   struct ip_info info;
   memset(&info, 0, sizeof(info));
   info.ip.addr = ipaddr_addr("192.168.0.24");
   info.netmask.addr = ipaddr_addr("255.255.255.0");
   info.gw.addr = ipaddr_addr("192.168.0.1");
   wifi_set_ip_info(STATION_IF, &info);

   //system_station_got_ip_set(&info.ip, &info.netmask, &info.gw);
}

[...]

   struct station_config stationConf;

   memset(&stationConf, 0, sizeof(stationConf));
   const char ssid[32] = "ssid";
   const char password[32] = "pswd";

   wifi_set_opmode( STATION_MODE );

   set_ips();
   wifi_station_dhcpc_stop();
   wifi_station_set_auto_connect(FALSE);

   os_memcpy(&stationConf.ssid, ssid, 32);
   os_memcpy(&stationConf.password, password, 32);

   wifi_station_set_config(&stationConf);
   set_ips();
   wifi_station_connect();

   os_printf("Wifi connected\n");

   set_ips();  // redo just in case, sometimes it does not work without it


The problem is that if I put this code in user_init, the IP does not get set. I need to start a timer and then call this couple of seconds after booting. But if I init http server from the timer later on, it does not work. I use the following code for that:

Code: Select allLOCAL void ICACHE_FLASH_ATTR init_httpd()
{
   captdnsInit();

   // 0x40200000 is the base address for spi flash memory mapping, ESPFS_POS is the position
   // where image is written in flash that is defined in Makefile.
#ifdef ESPFS_POS
   espFsInit((void*)(0x40200000 + ESPFS_POS));
#else
   espFsInit((void*)(webpages_espfs_start));
#endif
   httpdInit(builtInUrls, 80);
#ifdef SHOW_HEAP_USE
   os_timer_disarm(&prHeapTimer);
   os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL);
   os_timer_arm(&prHeapTimer, 3000, 1);
#endif
   os_timer_disarm(&websockTimer);
   os_timer_setfn(&websockTimer, websockTimerCb, NULL);
   os_timer_arm(&websockTimer, 1000, 1);
   os_printf("\nReady\n");
}


It also does not work if I start http server first and then I set IPs for the WiFi connection. If anyone has a working code that sets the IP to static value and start HTTP server, please share.

Thanks.
User avatar
By piersfinlayson
#42606 I haven't tried to use it to set a static IP, but there's a callback which is called by the system after the system has been initialized:

Code: Select allsystem_init_done_cb()


The only wifi stuff I do in user_init is:

Code: Select allwifi_station_set_auto_connect(0);
wifi_set_event_handler_cb(my_wifi_event_handler);
system_init_done_cb((init_done_cb_t)my_post_init_fn);


I think kick everything off when the system_init_done_cb is called
User avatar
By Piotr K
#42610 Thanks, that helped to get it working! Now I set wifi up in system_init_done_cb and then init httpd after I got IP. And it works every time.
User avatar
By vnarmy
#43013
Piotr K wrote:Thanks, that helped to get it working! Now I set wifi up in system_init_done_cb and then init httpd after I got IP. And it works every time.

Could you please share your working code?
Thanks!