Esp32 and AP statio

mrdebug
Posts: 43
Joined: Sat Jul 27, 2019 8:26 am

Esp32 and AP statio

Postby mrdebug » Mon Aug 05, 2019 9:48 am

Hi, with these lines of code

Code: Select all

    tcpip_adapter_init();

    EspErrorCheck(esp_event_loop_create_default());
    wifi_init_config_t Config= WIFI_INIT_CONFIG_DEFAULT();
    EspErrorCheck(esp_wifi_init(&Config));

    EspErrorCheck(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifiEventHandle, NULL));

    wifi_config_t WifiConfig;
    strcpy((char*)WifiConfig.ap.ssid, (const char*)SSIDServer.c_str());
    WifiConfig.ap.ssid_len= SSIDServer.size();
	strcpy((char*)WifiConfig.ap.password, (const char*)PasswordServer.c_str());
	WifiConfig.ap.max_connection= 8;
	WifiConfig.ap.authmode= AuthenticationModeServer;
    if (PasswordServer.size()== 0) WifiConfig.ap.authmode= WIFI_AUTH_OPEN;

    EspErrorCheck(esp_wifi_set_mode(WIFI_MODE_AP));
    EspErrorCheck(esp_wifi_set_config(ESP_IF_WIFI_AP, &WifiConfig));

    EspErrorCheck(esp_wifi_start());
I can starts my esp32 as an access point.
How can I set its address and the range of dhcp addresses?

mikemoy
Posts: 626
Joined: Fri Jan 12, 2018 9:10 pm

Re: Esp32 and AP statio

Postby mikemoy » Mon Aug 05, 2019 3:01 pm

This is what I have to do that.
After " tcpip_adapter_init();" add this.

start_dhcp_server();

Code: Select all

void start_dhcp_server(void) 
{
	// stop DHCP server
	ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
	// assign a static IP to the network interface
	tcpip_adapter_ip_info_t info;
	memset(&info, 0, sizeof(info));
	
	IP4_ADDR(&info.ip, 192, 168, 0, 1);
	IP4_ADDR(&info.gw, 192, 168, 0, 1); //ESP acts as router, so gw addr will be its own addr
	IP4_ADDR(&info.netmask, 255, 255, 255, 0);
	ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
	// start the DHCP server   
	ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
	printf("DHCP server started \n");
}

mrdebug
Posts: 43
Joined: Sat Jul 27, 2019 8:26 am

Re: Esp32 and AP statio

Postby mrdebug » Wed Aug 07, 2019 7:40 am

Perfect!

Who is online

Users browsing this forum: No registered users and 100 guests