Page 2 of 3

Re: WIFI_MODE_APSTA Sample

Posted: Wed Mar 01, 2017 6:57 pm
by squonk11
@imtiaz:
nice that You found a solution for this problem!
Would You be so nice to share it so that also other people are delighted?

kind regards

Squonk

Re: WIFI_MODE_APSTA Sample

Posted: Thu Mar 02, 2017 1:36 am
by imtiaz
its not the most ideal solution but works well:

Code: Select all

/************************************************************
@Inputs:
@Outputs:
@Comments:
*************************************************************/
void Start_wifi(wifi_config_t* wifi_config , wifi_mode_t Mode )
{
	wifi_mode_t currentMode;
	if(esp_wifi_get_mode(&currentMode) == ESP_ERR_WIFI_NOT_INIT)
	{
		wifiSetup_Init();
	}
	else if(currentMode == WIFI_MODE_APSTA)
	{
		ESP_LOGE(TAG,"wifi APSTA already running\n");
		return;
	}
	else if(currentMode == Mode)
	{
		ESP_LOGE(TAG,"that mode is already running\n");
		return;
	}
	else
	{
		esp_wifi_stop();
	}

   	if(currentMode == WIFI_MODE_AP && Mode == WIFI_MODE_STA)
   	{
   		ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_APSTA) );
   	}
   	else if(currentMode == WIFI_MODE_STA && Mode == WIFI_MODE_AP)
   	{
   		ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_APSTA) );
   	}
   	else
   	{
   		ESP_ERROR_CHECK( esp_wifi_set_mode(Mode) );
   	}
	if(Mode == WIFI_MODE_AP)
	{
		ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_AP, wifi_config) );
	}
	else if(Mode == WIFI_MODE_STA)
	{
		ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_config) );
	}
	ESP_ERROR_CHECK( esp_wifi_start());
}
/************************************************************
@Inputs:
@Outputs:
@Comments:
*************************************************************/
void wifiSetup_StopSTA(void)
{
	wifi_mode_t currentMode;
	esp_wifi_get_mode(&currentMode);
	if(currentMode == WIFI_MODE_STA )
	{
		esp_wifi_stop();
		ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
	}
	else if (currentMode == WIFI_MODE_APSTA)
	{
		ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );
	}
}
/************************************************************
@Inputs:
@Outputs:
@Comments:
*************************************************************/
void wifiSetup_StopAP(void)
{
	wifi_mode_t currentMode;
	esp_wifi_get_mode(&currentMode);
	if(currentMode == WIFI_MODE_AP )
	{
		esp_wifi_stop();
		ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
	}
	else if (currentMode == WIFI_MODE_APSTA)
	{
		ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
	}
}
/************************************************************
@Inputs:
@Outputs:
@Comments:
*************************************************************/
void Kill_wifi(void)
{

	esp_wifi_stop();
	//vEventGroupDelete(wifi_event_group);
	esp_wifi_deinit();
}

/************************************************************
@Inputs:
@Outputs:
@Comments:
*************************************************************/
void wifiSetup_Init(void)
{
	ESP_LOGI(TAG,"Initilising TCP and wifi\n");
	tcpip_adapter_init();
	wifi_event_group = xEventGroupCreate();
	if(esp_event_loop_init(event_handler, NULL) == ESP_FAIL)
	{
			ESP_LOGI(TAG,"Event Loop not created , may have been created before \n");
	}
	wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
	ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
	ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
	esp_wifi_set_mode(0);
}

Re: WIFI_MODE_APSTA Sample

Posted: Thu Mar 02, 2017 3:28 pm
by squonk11
Thank You for Your fast answer. But unfortunately the most importent thing for me is missing:
How do You populate the union "wifi_config_t"?

Re: WIFI_MODE_APSTA Sample

Posted: Thu Mar 02, 2017 11:02 pm
by imtiaz
Ummm - well there is no magic there , just look at some of the example code. Important thing is that if you are configuring an AP , then populate wifiConfig.ap , if you are configuring as a STA then populate wifiConfig.sta , then call

Code: Select all

Start_wifi(&wifiConfig , WIFI_MODE_AP);
or

Code: Select all

Start_wifi(&wifiConfig , WIFI_MODE_STA);
You only need to do one or the other. If you want to stop

Code: Select all

wifiSetup_StopAP();
wifiSetup_StopSTA();

Hope that helps.... In my case these commands are coming in externally through the uart so I cant really paste any code.. suffice to say its straight forward.

Re: WIFI_MODE_APSTA Sample

Posted: Sat Mar 04, 2017 8:52 am
by squonk11
o.k. thank you. Now it's clear and it works. Thanks a lot.
Now I am trying to obtain infos (e.g. ssid(s) or IPs) of the stations connected to my AP. I found an example on that using "esp_wifi_get_station_list". But this does not seem to work because this function does not seem to be available in ESP-IDF (any more)?
Do you have another suggestion how to do this?

Re: WIFI_MODE_APSTA Sample

Posted: Sat Mar 04, 2017 6:17 pm
by squonk11
I found a solution how to find the IP:

Code: Select all

			wifi_sta_list_t stations;
				ESP_ERROR_CHECK(esp_wifi_ap_get_sta_list(&stations));
				tcpip_adapter_sta_list_t infoList;
				ESP_ERROR_CHECK(tcpip_adapter_get_sta_list(&stations, &infoList));
				int i = 0;
				while(i < infoList.num) {
					 printf("mac: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x " IPSTR " %d\n",
								 infoList.sta[i].mac[0],infoList.sta[i].mac[1],infoList.sta[i].mac[2],
								 infoList.sta[i].mac[3],infoList.sta[i].mac[4],infoList.sta[i].mac[5],
								 IP2STR(&(infoList.sta[i].ip)),
								 (uint32_t)(infoList.sta[i].ip.addr));
					i++;
				}

Re: WIFI_MODE_APSTA Sample

Posted: Mon Dec 25, 2017 9:18 pm
by forkus2000
can you publish entire code for master and slave working at the same time accesspoint for (esp_now) and station mode to connect to a wifi ? many people talks but there are no WORKING SAMPLE !!!!

Thanks guys

Re: WIFI_MODE_APSTA Sample

Posted: Tue Sep 04, 2018 2:47 pm
by zhivko
I am looking into arduino sample for esp32. There is:
#define WiFiMode_t wifi_mode_t
#define WIFI_OFF WIFI_MODE_NULL
#define WIFI_STA WIFI_MODE_STA
#define WIFI_AP WIFI_MODE_AP
#define WIFI_AP_STA WIFI_MODE_APSTA

So I can assume there is
AccessPoint mode
Station mode
and
AcccessPoint and Station mode

Is my assumption correct?

Also I want to know if it is possible to have sample for WIFI_MODE_APSTA.

Re: WIFI_MODE_APSTA Sample

Posted: Thu Dec 20, 2018 7:46 am
by osmpradeep
Hi,
I also facing the same issue with esp32, I tried to configure both STA and AP and same but Station is connecting but when I tired to connect with AP mode it is not connecting. im using Arduino IDE here is my code please help me

#include <WiFiClient.h>
#include <ESP32WebServer.h>
#include <WiFi.h>
#include <ESPmDNS.h>
const char* ssid = "TEST1234";
const char* password = "test1234";
const char* ssidAP = "TIMER_SWITCH";
const char* passwordAP = "timer_test";
ESP32WebServer server(80);
const int led = 13;
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "hello from ESP32!");
digitalWrite(led, 0);
}

void handleNotFound(){
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup(void){
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.mode(WIFI_MODE_APSTA);
WiFi.begin(ssid, password,6);
WiFi.softAP(ssidAP, passwordAP,6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/inline", [](){
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}
Yeah, I understand the reasoning - however, the API should have better documentation so you don't have to figure these things out the hard way or by trial and error.

On a positive note I've got it to work in APSTA mode - finally

Re: WIFI_MODE_APSTA Sample

Posted: Mon May 06, 2019 4:56 am
by bhakti
Yeah I understand the reasoning - however the API should have better documentation so you dont have to figure these things out the hard way or by trial and error.

On a positive note I've got it to work in APSTA mode - finally
Can you please share the code? I am confused with event_handler function. which cases should we use in event handler?