-->
Page 2 of 2

Re: Static IP

PostPosted: Sun Mar 13, 2016 11:41 am
by Piotr K
Sure, this is it:

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);
}

LOCAL void ICACHE_FLASH_ATTR connect_wifi()
{
   wifi_station_disconnect();

   struct station_config stationConf;

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

   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();
}

LOCAL 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");
}

LOCAL void ICACHE_FLASH_ATTR my_wifi_event_handler(System_Event_t *evt)
{
   os_printf("event %x\n", evt->event);
   switch (evt->event) {
   case EVENT_STAMODE_CONNECTED:
      os_printf("connect to ssid %s, channel %d\n",
            evt->event_info.connected.ssid,
            evt->event_info.connected.channel);
      break;
   case EVENT_STAMODE_DISCONNECTED:
      os_printf("disconnect from ssid %s, reason %d\n",
            evt->event_info.disconnected.ssid,
            evt->event_info.disconnected.reason);
      break;
   case EVENT_STAMODE_AUTHMODE_CHANGE:
      os_printf("mode: %d -> %d\n",
            evt->event_info.auth_change.old_mode,
            evt->event_info.auth_change.new_mode);
      break;
   case EVENT_STAMODE_GOT_IP:
      os_printf("ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR,
            IP2STR(&evt->event_info.got_ip.ip),
            IP2STR(&evt->event_info.got_ip.mask),
            IP2STR(&evt->event_info.got_ip.gw));
      os_printf("\n");
      init_httpd();
      break;
   case EVENT_SOFTAPMODE_STACONNECTED:
      os_printf("station: " MACSTR "join, AID = %d\n",
            MAC2STR(evt->event_info.sta_connected.mac),
            evt->event_info.sta_connected.aid);
      break;
   case EVENT_SOFTAPMODE_STADISCONNECTED:
      os_printf("station: " MACSTR "leave, AID = %d\n",
            MAC2STR(evt->event_info.sta_disconnected.mac),
            evt->event_info.sta_disconnected.aid);
      break;
   default:
      break;
   }
}

LOCAL void my_post_init_fn(void *arg)
{
  connect_wifi();

   os_timer_disarm(&hello_timer);
   os_timer_setfn(&hello_timer, (os_timer_func_t *)hello_cb, (void *)0);
   os_timer_arm(&hello_timer, 100, 1);
}

void user_init(void) {
   wifi_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);

   stdoutInit();
   ioInit();
}


Re: Static IP

PostPosted: Thu Mar 23, 2017 1:36 pm
by Marc Aston
piersfinlayson wrote: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


Hallo piersfinlayson,
may I ask for a working example, I'm not a professional,
but I have always an issue when reconnecting with the IP adress.
I would really appreciate your helping hand and also reward it wherever I would meet
you on the globe with liters of beer ;)

Marc / Croatia :geek: