Chat freely about anything...

User avatar
By metalphreak
#15987 I was testing out setting up my own SoftAP with my required SSID and password, as well as using it in Station mode to connect to my wifi AP.

My wifi AP runs on channel 6.

I configured the SoftAP to run on channel 1.

SDK is v1.0.1

What ends up happening, is it connects to my wifi AP on ch6, and the softAP is forced onto ch6 as well. The DHCP server on the softAP stops responding to requests for IP leases.

If I set my softAP to ch6 to match, DHCP server works fine. However, how are you supposed to know the channel in advance if a user sets their own wifi AP to connect to?

What I did was register a callback function for Station Connection Event, which then reinitialised the SoftAP on the correct channel, and DHCP server works ok again.

Might be helpful to know if others are having similar issues?

Code: Select allvoid ICACHE_FLASH_ATTR station_init(void)
{
   uint8 ssid[32] = SSID;
    uint8 password[64] = SSID_PASSWORD;
    struct station_config stationConf;
    os_memcpy(&stationConf.ssid, ssid, 32);
    os_memcpy(&stationConf.password, password, 64);
   wifi_station_set_config(&stationConf);
   //wifi_station_set_reconnect_policy(1);
   wifi_set_event_handler_cb(wifi_connected_cb);
}

void ICACHE_FLASH_ATTR wifi_connected_cb(System_Event_t *evt)
{
   uint8 newchannel = 0;
   switch (evt->event) {
      case EVENT_STAMODE_CONNECTED:
      
      newchannel = evt->event_info.connected.channel;
      os_printf("New Channel: %d\n", newchannel)
      softap_init(newchannel);
      break;
   }

}

void ICACHE_FLASH_ATTR softap_init(uint8 channel)
{

   uint8 ap_ssid[32];
    uint8 ap_password[64];
    uint8 ap_channel = channel;
    uint8 ap_auth = AP_AUTH;
    uint8 ap_mac[6];
    wifi_get_macaddr(SOFTAP_IF, ap_mac);
    os_sprintf(ap_ssid, "%s%x%x%x%x%x%x", AP_SSID, ap_mac[0], ap_mac[1], ap_mac[2], ap_mac[3], ap_mac[4], ap_mac[5]);
   
    struct softap_config softapConf;
    os_memcpy(&softapConf.ssid, ap_ssid, 32);
    os_sprintf(ap_password, "%s", AP_SSID_PASSWORD);
    os_memcpy(&softapConf.password, ap_password, strlen(ap_password));
    os_printf("MAC: %x:%x:%x:%x:%x:%x\n", ap_mac[0],ap_mac[1],ap_mac[2],ap_mac[3],ap_mac[4],ap_mac[5]);
    softapConf.channel = ap_channel;
    softapConf.authmode = ap_auth;
    softapConf.max_connection = 4;
    softapConf.beacon_interval = 100;
    os_printf("APSSID PASS: %s\n", softapConf.password);
   wifi_softap_set_config(&softapConf);

   
   uint8 dhcpstatus = 0;

   dhcpstatus = wifi_softap_dhcps_stop();
   os_printf("DHCP Stop: %d\n", dhcpstatus);
   struct dhcps_lease dhcp_lease;
   IP4_ADDR(&dhcp_lease.start_ip, 192, 168, 5, 2);
   IP4_ADDR(&dhcp_lease.end_ip, 192, 168, 5, 100);
   wifi_softap_set_dhcps_lease(&dhcp_lease);
    os_printf("DHCP Set Lease: %d\n", dhcpstatus);
    struct ip_info info;
    IP4_ADDR(&info.ip, 192, 168, 5, 1);
    IP4_ADDR(&info.gw, 192, 168, 5, 1);
    IP4_ADDR(&info.netmask, 255, 255, 255, 0);
    dhcpstatus = wifi_set_ip_info(SOFTAP_IF, &info);
    os_printf("Set SoftAP IP: %d\n", dhcpstatus);
   dhcpstatus = wifi_softap_dhcps_start();
   os_printf("DHCP Start: %d\n", dhcpstatus);
   
}

void user_init(void)
{
   wifi_set_opmode(0x03); //0x01 Station mode, 0x02 Soft-AP, 0x03 Combined Mode
   
   softap_init(AP_CHANNEL);
   station_init();
}