WIFI_MODE_APSTA Sample

squonk11
Posts: 69
Joined: Wed Mar 01, 2017 6:53 pm
Location: Germany

Re: WIFI_MODE_APSTA Sample

Postby squonk11 » Wed Mar 01, 2017 6:57 pm

@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
"Whoever believes to be someone has stopped becoming someone"
Sokrates

imtiaz
Posts: 106
Joined: Wed Oct 26, 2016 1:34 am

Re: WIFI_MODE_APSTA Sample

Postby imtiaz » Thu Mar 02, 2017 1:36 am

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

squonk11
Posts: 69
Joined: Wed Mar 01, 2017 6:53 pm
Location: Germany

Re: WIFI_MODE_APSTA Sample

Postby squonk11 » Thu Mar 02, 2017 3:28 pm

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"?
"Whoever believes to be someone has stopped becoming someone"
Sokrates

imtiaz
Posts: 106
Joined: Wed Oct 26, 2016 1:34 am

Re: WIFI_MODE_APSTA Sample

Postby imtiaz » Thu Mar 02, 2017 11:02 pm

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.

squonk11
Posts: 69
Joined: Wed Mar 01, 2017 6:53 pm
Location: Germany

Re: WIFI_MODE_APSTA Sample

Postby squonk11 » Sat Mar 04, 2017 8:52 am

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?
"Whoever believes to be someone has stopped becoming someone"
Sokrates

squonk11
Posts: 69
Joined: Wed Mar 01, 2017 6:53 pm
Location: Germany

Re: WIFI_MODE_APSTA Sample

Postby squonk11 » Sat Mar 04, 2017 6:17 pm

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++;
				}
"Whoever believes to be someone has stopped becoming someone"
Sokrates

forkus2000
Posts: 5
Joined: Wed Nov 08, 2017 6:50 pm

Re: WIFI_MODE_APSTA Sample

Postby forkus2000 » Mon Dec 25, 2017 9:18 pm

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

zhivko
Posts: 23
Joined: Tue Dec 01, 2015 12:02 pm

Re: WIFI_MODE_APSTA Sample

Postby zhivko » Tue Sep 04, 2018 2:47 pm

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.

osmpradeep
Posts: 2
Joined: Thu Dec 20, 2018 7:37 am

Re: WIFI_MODE_APSTA Sample

Postby osmpradeep » Thu Dec 20, 2018 7:46 am

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();
}
imtiaz wrote:
Thu Jan 26, 2017 6:25 pm
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

bhakti
Posts: 12
Joined: Fri Oct 05, 2018 6:52 am

Re: WIFI_MODE_APSTA Sample

Postby bhakti » Mon May 06, 2019 4:56 am

imtiaz wrote:
Thu Jan 26, 2017 6:25 pm
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?
------------------------------

Thanks and Best regards,
Bhakti Deshpande.

Who is online

Users browsing this forum: Bing [Bot] and 141 guests