ESP32-APSTA mode

Hi,

#include <WiFi.h>
#include <WiFiAP.h>

const char* ssid = "station";
const char* password = "password";

const char* assid = "Esp32";
const char* asecret = "123456789";

void setup(){
Serial.begin(115200);
WiFi.begin(ssid,password);
WiFi.mode(WIFI_AP_STA);

Serial.println("Creating Accesspoint");
WiFi.softAP(assid,asecret,7,0,5);
Serial.print("IP address:\t");
Serial.println(WiFi.softAPIP());

Serial.print("connecting to...");
Serial.println(ssid);

WiFi.begin(ssid,password);

while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

}

void loop(){

}

I am using above program. I would like to connect it with internet.In AP mode after connecting to the wifi it shows no internet access.How to get the internet access.

"I am using above program. I would like to connect it with internet.In AP mode after connecting to the wifi it shows no internet access.How to get the internet access."

Is the "wifi" that the board connects to an internet connected router?

Yes it connects to an internet connected router (or) my mobile hotspot.

In my serial monitor........It display like this

Creating Accesspoint
IP address: 192.168.4.1
connecting to...sat
..............................

after connected to my router

WiFi connected
IP address:
my ip

after connect my laptop to the esp32 wifi

dhcps: send_nak>>udp_sendto result 0
dhcps: send_offer>>udp_sendto result 0 (or) sometimes these two message is not there.

Hi,

Is it possible to get internet connection from Esp32 without using ethernet.

What is it exactly that you trying to do?

Cheers,
Kari

I need to act my esp32 as AP and my mobile device as Station. i need to access internet on my mobile using esp32 AP network, is this possible.

Help me out to solve this issue

Thank you,

How is your esp32 connected to internet, sound liske there's something missing from the formula???

You trying to build some sort of gateway? You need another interface for your system, right?

Cheers,
Kari

hi
thanks for the reply....
Simply i need to access my esp32 as a internet connection produced router,i am getting the internet connection via ethernet or wifi for the esp32,can i routing the network to other devices,

application:
In examples i am using eth2wifi,i am getting ip from the ethernet and also can access the wifi as AP,but the esp32 does not give a any internet connection.

Sorry, but it is hard to follow your writings.

Have you already done something, or are you just planning?

Sound to me that you are still doing some kind of pseudo designing, but at the same time I got the feeling that you have tried "eth2wifi".

Show us your circuit and code now.

Cheers,
Kari

hi,

thanks for the replay...

I have Esp32 ...I set the Esp32 in AP mode ...In my mobile connect the wifi and it shows it has no internet connection...

My target is to connect the internet in my mobile...for that i am searching ...simply i want to connect internet on my mobile.by using esp32..for that i am trying with above program and eth2wifi program...

i am checking for the possible way to achieve my requirement.

Code and circuit...

You can't get to to internet that way. How do you think your esp32 is connected to internet if you don't have another interface on it?

This is getting a bit frustrating. Maybe I just don't understand what you are saying.

Code and circuit you have tested, let's start with that.

Kari

#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "esp_event_loop.h"
#include "esp_event.h"
#include "esp_log.h"

#include "esp_eth.h"

#include "esp_wifi.h"
#include "esp_wifi_internal.h"

#include "nvs_flash.h"

#ifdef CONFIG_PHY_LAN8720
#include "eth_phy/phy_lan8720.h"
#define DEFAULT_ETHERNET_PHY_CONFIG phy_lan8720_default_ethernet_config
#endif
#ifdef CONFIG_PHY_TLK110
#include "eth_phy/phy_tlk110.h"
#define DEFAULT_ETHERNET_PHY_CONFIG phy_tlk110_default_ethernet_config
#endif
#ifdef CONFIG_PHY_IP101
#include "eth_phy/phy_ip101.h"
#define DEFAULT_ETHERNET_PHY_CONFIG phy_ip101_default_ethernet_config
#endif

static const char* TAG = "eth2wifi_demo";

#define PIN_PHY_POWER CONFIG_PHY_POWER_PIN
#define PIN_SMI_MDC CONFIG_PHY_SMI_MDC_PIN
#define PIN_SMI_MDIO CONFIG_PHY_SMI_MDIO_PIN

typedef struct {
void* buffer;
uint16_t len;
void* eb;
} tcpip_adapter_eth_input_t;

static bool mac_is_set = false;
static xQueueHandle eth_queue_handle;
static bool wifi_is_connected = false;
static bool ethernet_is_connected = false;
static uint8_t eth_mac[6];

#ifdef CONFIG_PHY_USE_POWER_PIN
/* This replaces the default PHY power on/off function with one that
also uses a GPIO for power on/off.
If this GPIO is not connected on your device (and PHY is always powered), you can use the default PHY-specific power
on/off function rather than overriding with this one.
*/
static void phy_device_power_enable_via_gpio(bool enable)
{
assert(DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable);

if (!enable) {
/* Do the PHY-specific power_enable(false) function before powering down */
DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(false);
}

gpio_pad_select_gpio(PIN_PHY_POWER);
gpio_set_direction(PIN_PHY_POWER, GPIO_MODE_OUTPUT);

if (enable == true) {
gpio_set_level(PIN_PHY_POWER, 1);
ESP_LOGD(TAG, "phy_device_power_enable(TRUE)");
} else {
gpio_set_level(PIN_PHY_POWER, 0);
ESP_LOGD(TAG, "power_enable(FALSE)");
}

// Allow the power up/down to take effect, min 300us
vTaskDelay(1);

if (enable) {
/* Run the PHY-specific power on operations now the PHY has power */
DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(true);
}
}
#endif

static void ethernet2wifi_mac_status_set(bool status)
{
mac_is_set = status;
}

static bool ethernet2wifi_mac_status_get(void)
{
return mac_is_set;
}

static void eth_gpio_config_rmii(void)
{
// RMII data pins are fixed:
// TXD0 = GPIO19
// TXD1 = GPIO22
// TX_EN = GPIO21
// RXD0 = GPIO25
// RXD1 = GPIO26
// CLK == GPIO0
phy_rmii_configure_data_interface_pins();
// MDC is GPIO 23, MDIO is GPIO 18
phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO);
}

static esp_err_t tcpip_adapter_eth_input_sta_output(void* buffer, uint16_t len, void* eb)
{
tcpip_adapter_eth_input_t msg;

msg.buffer = buffer;
msg.len = len;
msg.eb = eb;

if (xQueueSend(eth_queue_handle, &msg, portMAX_DELAY) != pdTRUE) {
return ESP_FAIL;
}

return ESP_OK;
}

static void eth_task(void* pvParameter)
{
tcpip_adapter_eth_input_t msg;

for (;:wink: {
if (xQueueReceive(eth_queue_handle, &msg, (portTickType)portMAX_DELAY) == pdTRUE) {
if (msg.len > 0) {
if (!ethernet2wifi_mac_status_get()) {
memcpy(eth_mac, (uint8_t*)msg.buffer + 6, sizeof(eth_mac));
ESP_ERROR_CHECK(esp_wifi_start());
#ifdef CONFIG_ETH_TO_STATION_MODE
esp_wifi_set_mac(WIFI_IF_STA, eth_mac);
esp_wifi_connect();
#else
esp_wifi_set_mac(WIFI_IF_AP, eth_mac);
#endif
ethernet2wifi_mac_status_set(true);
}

if (wifi_is_connected) {
#ifdef CONFIG_ETH_TO_STATION_MODE
esp_wifi_internal_tx(ESP_IF_WIFI_STA, msg.buffer, msg.len - 4);
#else
esp_wifi_internal_tx(ESP_IF_WIFI_AP, msg.buffer, msg.len - 4);
#endif
}
}

esp_eth_free_rx_buf(msg.buffer);
}
}
}

static void initialise_wifi(void)
{
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init_internal(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
#ifdef CONFIG_ETH_TO_STATION_MODE
wifi_config_t wifi_config = {
.sta = {
.ssid = CONFIG_DEMO_SSID,
.password = CONFIG_DEMO_PASSWORD,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
#else
wifi_config_t wifi_config = {
.ap = {
.ssid = CONFIG_DEMO_SSID,
.password = CONFIG_DEMO_PASSWORD,
.ssid_len = strlen(CONFIG_DEMO_SSID),
.max_connection = 1,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
#endif
}

static void initialise_ethernet(void)
{
eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG;

/* Set the PHY address in the example configuration */
config.phy_addr = CONFIG_PHY_ADDRESS;
config.gpio_config = eth_gpio_config_rmii;
config.tcpip_input = tcpip_adapter_eth_input_sta_output;
config.promiscuous_enable = true;

#ifdef CONFIG_PHY_USE_POWER_PIN
/* Replace the default 'power enable' function with an example-specific
one that toggles a power GPIO. */
config.phy_power_enable = phy_device_power_enable_via_gpio;
#endif

esp_eth_init(&config);
esp_eth_enable();
}

static esp_err_t tcpip_adapter_sta_input_eth_output(void* buffer, uint16_t len, void* eb)
{
if (ethernet_is_connected) {
esp_eth_tx(buffer, len);
}

esp_wifi_internal_free_rx_buffer(eb);
return ESP_OK;
}

static esp_err_t tcpip_adapter_ap_input_eth_output(void* buffer, uint16_t len, void* eb)
{
if (ethernet_is_connected) {
esp_eth_tx(buffer, len);
}

esp_wifi_internal_free_rx_buffer(eb);
return ESP_OK;
}

static esp_err_t event_handler(void* ctx, system_event_t* event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
printf("SYSTEM_EVENT_STA_START\r\n");
break;

case SYSTEM_EVENT_STA_CONNECTED:
printf("SYSTEM_EVENT_STA_CONNECTED\r\n");
wifi_is_connected = true;

esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, (wifi_rxcb_t)tcpip_adapter_sta_input_eth_output);
break;

case SYSTEM_EVENT_STA_GOT_IP:
printf("SYSTEM_EVENT_STA_GOT_IP\r\n");
break;

case SYSTEM_EVENT_STA_DISCONNECTED:
printf("SlYSTEM_EVENT_STA_DISCONNECTED\r\n");
wifi_is_connected = false;
esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, NULL);
esp_wifi_connect();
break;

case SYSTEM_EVENT_AP_STACONNECTED:
printf("SYSTEM_EVENT_AP_STACONNECTED\r\n");
wifi_is_connected = true;

esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, (wifi_rxcb_t)tcpip_adapter_ap_input_eth_output);
break;

case SYSTEM_EVENT_AP_STADISCONNECTED:
printf("SYSTEM_EVENT_AP_STADISCONNECTED\r\n");
wifi_is_connected = false;
esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, NULL);
break;

case SYSTEM_EVENT_ETH_CONNECTED:
printf("SYSTEM_EVENT_ETH_CONNECTED\r\n");
ethernet_is_connected = true;
break;

case SYSTEM_EVENT_ETH_DISCONNECTED:
printf("SYSTEM_EVENT_ETH_DISCONNECTED\r\n");
ethernet2wifi_mac_status_set(false);
ethernet_is_connected = false;
break;

default:
break;
}

return ESP_OK;
}

void app_main()
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK( nvs_flash_erase() );
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );

tcpip_adapter_init();

eth_queue_handle = xQueueCreate(CONFIG_DMA_RX_BUF_NUM, sizeof(tcpip_adapter_eth_input_t));
xTaskCreate(eth_task, "eth_task", 2048, NULL, (tskIDLE_PRIORITY + 2), NULL);

esp_event_loop_init(event_handler, NULL);

initialise_ethernet();

initialise_wifi();
}

I am using the above program ...(or) first uploaded program....in my ESP32 first act as station and connected to my router (or) my ethernet, after connect it will display the connected ip address and then same esp32 act as AP mode ..in my mobile shows the sssid i will connect it...after connect it shows no internet connection....i need internet connection on my mobile

hi ,

just i want to connect internet on my mobile(or)laptop by using esp32 as router ..if you have any program (or)solution for this..please share with me

i didn't have any solution for my problem i just try the above two programs..I am new to esp32..

please help me out

Ok.

Now you have to ask yourself, why do you think that connecting laptop to esp32, could connect yout internet, esp32 can be only one interface at a time.

-You didn't show your harware yet, it is impossible to understand where you are going now.

-Do you understand the consept of router?

-Have you tried google?

-Format you code like this, only very few people are willing to read it the way you put it.


Code tags!!!!

Try to learn basics first, you are trying too complex first project.

Cheers,
Kari

"Simply i need to access my esp32 as a internet connection produced router,"

I'm new to the ESP wireless stuff, but I have not seen code yet that has both the ESP act as an AP and as a client connected to a router. It may be out there. You might be able to combine AP code and client code from the example code available in the arduino IDE.

zoomkat:
... the ESP act as an AP and as a client connected to a router...

I believe that is possible, but just wondering what would be the overall bitrate, overlap time while connecting back and forth would be ridiculous.

traceback,
What kind of data you are going to move on that network?

By the way, now that I understand your goal, YOU HAVE another interface? LAN8720???

You wasted our time when NOT telling about your harware.

https://www.google.com/search?q=esp32+lan8720+example&tbm=isch&source=iu&ictx=1&fir=reDz_jvjsEDUvM%253A%252C605Zey_9Uf_NNM%252C_&vet=1&usg=AI4_-kR0UsBxWc_HD1Qi0CG2vEWUkeUCvw&sa=X&ved=2ahUKEwiC7OO4vaDnAhVN_SoKHU-7B7AQ9QEwA3oECAoQCQ#imgrc=reDz_jvjsEDUvM:

And now I have to give up, I have no experience about that.

Cheers,
Kari