bridge from sta to ap
Posted: Fri Dec 02, 2022 10:48 pm
Hello all! This is my first post on this forum. Super happy you exists!
So have writed something with idea to share internet from wifi_sta to wifi_ap.. i know that exists in esp_iot_bridge and works, have tested. but i like to achive so this works. will paste code and output of program. i think all is writed correctly just ip addresses are not set as they need to be, i guess.
specialy for "br" interface. and when i try to set them manualy ip, netmask and gw stays 0.0.0.0 ... ok here is output of program and then code of program...:
output:
[0;32mI (219731) wifi kosAP: task_status() in the loop..[0m
[0;32mI (219741) wifi kosAP: esp_netif_get_dns_info() STA, err: ESP_OK, sta_dns: 80.58.61.254
[0m
[0;32mI (219751) wifi kosAP: esp_netif_get_dns_info() AP, err: ESP_OK, ap_dns: 0.0.0.0
[0m
[0;32mI (219761) wifi kosAP: esp_netif_get_dns_info() PORT, err: ESP_OK, port_dns: 80.58.61.254
[0m
[0;32mI (219761) wifi kosAP: esp_netif_get_dns_info() BR, err: ESP_OK, br_dns: 80.58.61.254
[0m
[0;32mI (219761) wifi kosAP: task_status() ap.ip: 192.168.4.1[0m
[0;32mI (219771) wifi kosAP: task_status() ap.netmask: 255.255.255.0[0m
[0;32mI (219781) wifi kosAP: task_status() ap.gw: 192.168.4.1[0m
[0;32mI (219781) wifi kosAP: task_status() ap.mac: 94:e6:86:02:8e:d9
[0m
[0;32mI (219791) wifi kosAP: task_status() sta.ip: 192.168.1.47[0m
[0;32mI (219801) wifi kosAP: task_status() sta.netmask: 255.255.255.0[0m
[0;32mI (219801) wifi kosAP: task_status() sta.gw: 192.168.1.1[0m
[0;32mI (219811) wifi kosAP: task_status() sta.mac: 94:e6:86:02:8e:d8
[0m
[0;32mI (219811) wifi kosAP: task_status() port.ip: 0.0.0.0[0m
[0;32mI (219821) wifi kosAP: task_status() port.netmask: 0.0.0.0[0m
[0;32mI (219831) wifi kosAP: task_status() port.gw: 0.0.0.0[0m
[0;32mI (219831) wifi kosAP: task_status() port.mac: 00:00:00:00:00:00
[0m
[0;32mI (219841) wifi kosAP: task_status() br.ip: 0.0.0.0[0m
[0;32mI (219841) wifi kosAP: task_status() br.netmask: 0.0.0.0[0m
[0;32mI (219851) wifi kosAP: task_status() br.gw: 0.0.0.0[0m
[0;32mI (219861) wifi kosAP: task_status() br.mac: 94:e6:86:02:8e:d8
program code:
/* soft APSTA by grandekos aka t3ch */
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_mac.h"
#include "esp_wifi.h"
#include "esp_wifi_netif.h"
//#include "esp_event.h"
#include "esp_log.h"
//
//#include "tcpip_adapter.h" replaced for esp_netif...
#include "esp_netif.h"
#include "esp_netif_br_glue.h"
//#include "esp_netif_lwip.h"
#include "esp_netif_net_stack.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
//
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/lwip_napt.h"
//
#include "dhcpserver/dhcpserver.h"
#include "dhcpserver/dhcpserver_options.h"
/* The examples use WiFi configuration that you can set via project configuration menu.
If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
#define EXAMPLE_ESP_WIFI_CHANNEL CONFIG_ESP_WIFI_CHANNEL
#define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
#define EXAMPLE_ESP_MAXIMUM_RETRY 10
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
esp_netif_t *ap = NULL, *sta = NULL, *port=NULL, *br=NULL;
esp_netif_ip_info_t sta_info, ap_info, port_info, br_info;
esp_netif_dns_info_t sta_dns, ap_dns, port_dns, br_dns;
TaskHandle_t task_handle = NULL;
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
/*
#if CONFIG_ESP_WIFI_AUTH_OPEN
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
#elif CONFIG_ESP_WIFI_AUTH_WEP
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
#elif CONFIG_ESP_WIFI_AUTH_WPA_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA2_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA_WPA2_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA3_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA2_WPA3_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
#elif CONFIG_ESP_WIFI_AUTH_WAPI_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
#endif
*/
static const char *TAG = "wifi kosAP";
static int s_retry_num=0;
//
void task_status() {
ESP_LOGI(TAG,"task_status() starting");
esp_err_t err;
uint8_t tmpmac[6];
while(1) {
ESP_LOGI(TAG,"task_status() in the loop..");
//
err = esp_netif_get_dns_info(sta, ESP_NETIF_DNS_MAIN, &sta_dns);
ESP_LOGI(TAG,"esp_netif_get_dns_info() STA, err: %s, sta_dns: "IPSTR"\n", esp_err_to_name(err), IP2STR(&sta_dns.ip.u_addr.ip4));
//
err = esp_netif_get_dns_info(ap, ESP_NETIF_DNS_MAIN, &ap_dns);
ESP_LOGI(TAG,"esp_netif_get_dns_info() AP, err: %s, ap_dns: "IPSTR"\n", esp_err_to_name(err), IP2STR(&ap_dns.ip.u_addr.ip4));
//
err = esp_netif_get_dns_info(port, ESP_NETIF_DNS_MAIN, &port_dns);
ESP_LOGI(TAG,"esp_netif_get_dns_info() PORT, err: %s, port_dns: "IPSTR"\n", esp_err_to_name(err), IP2STR(&port_dns.ip.u_addr.ip4));
//
err = esp_netif_get_dns_info(br, ESP_NETIF_DNS_MAIN, &br_dns);
ESP_LOGI(TAG,"esp_netif_get_dns_info() BR, err: %s, br_dns: "IPSTR"\n", esp_err_to_name(err), IP2STR(&br_dns.ip.u_addr.ip4));
//
esp_netif_get_ip_info(ap,&ap_info);
ESP_LOGI(TAG,"task_status() ap.ip: "IPSTR,IP2STR(&ap_info.ip));
ESP_LOGI(TAG,"task_status() ap.netmask: "IPSTR,IP2STR(&ap_info.netmask));
ESP_LOGI(TAG,"task_status() ap.gw: "IPSTR,IP2STR(&ap_info.gw));
esp_netif_get_mac(ap,&tmpmac);
ESP_LOGI(TAG,"task_status() ap.mac: "MACSTR"\n",MAC2STR(tmpmac));
//
esp_netif_get_ip_info(sta,&sta_info);
ESP_LOGI(TAG,"task_status() sta.ip: "IPSTR,IP2STR(&sta_info.ip));
ESP_LOGI(TAG,"task_status() sta.netmask: "IPSTR,IP2STR(&sta_info.netmask));
ESP_LOGI(TAG,"task_status() sta.gw: "IPSTR,IP2STR(&sta_info.gw));
esp_netif_get_mac(sta,&tmpmac);
ESP_LOGI(TAG,"task_status() sta.mac: "MACSTR"\n",MAC2STR(tmpmac));
//
esp_netif_get_ip_info(port,&port_info);
ESP_LOGI(TAG,"task_status() port.ip: "IPSTR,IP2STR(&port_info.ip));
ESP_LOGI(TAG,"task_status() port.netmask: "IPSTR,IP2STR(&port_info.netmask));
ESP_LOGI(TAG,"task_status() port.gw: "IPSTR,IP2STR(&port_info.gw));
esp_netif_get_mac(port,&tmpmac);
ESP_LOGI(TAG,"task_status() port.mac: "MACSTR"\n",MAC2STR(tmpmac));
//
esp_netif_get_ip_info(br,&br_info);
ESP_LOGI(TAG,"task_status() br.ip: "IPSTR,IP2STR(&br_info.ip));
ESP_LOGI(TAG,"task_status() br.netmask: "IPSTR,IP2STR(&br_info.netmask));
ESP_LOGI(TAG,"task_status() br.gw: "IPSTR,IP2STR(&br_info.gw));
esp_netif_get_mac(br,&tmpmac);
ESP_LOGI(TAG,"task_status() br.mac: "MACSTR"\n",MAC2STR(tmpmac));
vTaskDelay(3000/10);
}
}
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data){
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", MAC2STR(event->mac), event->aid);
esp_netif_dhcps_stop( ap );
esp_netif_dhcps_start( ap );
//esp_netif_dhcps_start( ap );
//esp_netif_dhcps_start( sta );
ESP_LOGI(TAG, "station "MACSTR" join -> dhcp restarted!, AID=%d", MAC2STR(event->mac), event->aid);
}
else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
MAC2STR(event->mac), event->aid);
}
else if (event_id == WIFI_EVENT_STA_START) {
ESP_LOGI(TAG,"sta started, wifi connecting...");
esp_wifi_connect();
}
else if (event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"connect to the AP fail");
}
else if (event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
/*else if(event_id==SYSTEM_EVENT_AP_START) {
ESP_LOGI(TAG,"system event ap START!");
}
else if(event_id==SYSTEM_EVENT_STA_START) {
ESP_LOGI(TAG,"system event ap START!");
}*/
else {
ESP_LOGI(TAG,"Unknown event...%ld",event_id);
}
}
void wifi_init_softap(void)
{
esp_err_t err;
//esp_netif_dns_info_t ip_dns;
esp_netif_dhcp_status_t status;
//
s_wifi_event_group = xEventGroupCreate();
esp_event_loop_create_default();
ESP_ERROR_CHECK(esp_netif_init());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
//
sta = esp_netif_create_default_wifi_sta();
//esp_netif_create_default_wifi_mesh_netifs(&sta, &ap);
/*-- Set static ip for station
*------------------------------
esp_netif_ip_info_t ip_info;
IP4_ADDR(&ip_info.ip, 192, 168, 15, 22);
IP4_ADDR(&ip_info.gw, 192, 168, 15, 1);
IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
esp_netif_set_ip_info(sta, &ip_info);
*/
//
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL/*&instance_got_ip*/));
//
wifi_config_t wifi_config_sta = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
.threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
//.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
.pmf_cfg = {
.capable = true,
.required = false
}
},
};
//
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config_sta));
ESP_ERROR_CHECK(esp_wifi_start());
//--
//
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
//
esp_netif_get_ip_info(sta,&sta_info);
ESP_LOGI(TAG,"esp_netif_get_ip_info() sta done...");
printf("esp_netif_get_ip_info() sta ip: "IPSTR"\n",IP2STR(&sta_info.ip));
vTaskDelay( 3000/10 );
//
ESP_LOGI(TAG,"%s.) Test retrive sta iodriver... sta: %p", __func__, sta);
wifi_netif_driver_t sta_drv = esp_netif_get_io_driver(sta);
//wifi_netif_driver_t ap_drv = esp_netif_get_io_driver(ap);
uint8_t sta_mac[6], ap_mac[6];
err = esp_wifi_get_if_mac(sta_drv,sta_mac);
ESP_LOGI(TAG,"Test iodriver... sta_mac: "MACSTR"\n",MAC2STR(sta_mac));
//err = esp_wifi_get_if_mac(ap_drv,ap_mac);
//ESP_LOGI(TAG,"Test iodriver... ap_mac: "MACSTR"\n",MAC2STR(ap_mac));
//-------------------
//-- CREATE BRIDGE
// mac example:
// uint8_t new_mac[8] = {0x01,0x02,0x03,0x04,0x05,0x06};
// Create instances of esp-netif for Ethernet ports
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
esp_netif_config_t netif_cfg = {
.base = &esp_netif_config,
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA
};
//
esp_netif_config.if_key = "WLAN_0";
esp_netif_config.if_desc = "wlan0";
esp_netif_config.route_prio = 50;
esp_netif_config.flags = 0; // esp-netif flags need to be zero when port's to be bridged
port = esp_netif_new(&netif_cfg);
// Attach Ethernet driver to TCP/IP stack
ESP_ERROR_CHECK(esp_netif_attach(port, sta_drv));
// Create instance of esp-netif for bridge interface
esp_netif_inherent_config_t esp_netif_br_config = ESP_NETIF_INHERENT_DEFAULT_BR();
esp_netif_config_t netif_br_cfg = {
.base = &esp_netif_br_config,
.stack = ESP_NETIF_NETSTACK_DEFAULT_BR,
};
// Bridge configuration
bridgeif_config_t bridgeif_config = {
.max_fdb_dyn_entries = 10, // maximum number of address entries in dynamic forwarding database
.max_fdb_sta_entries = 2, // maximum number of address entries in static forwarding database
.max_ports = 1 // maximum number of ports the bridge can consist of
};
esp_netif_br_config.bridge_info = &bridgeif_config;
// Set MAC address of bridge interface the same as the Ethernet interface
//memcpy(esp_netif_br_config.mac, sta_mac, 6);
br = esp_netif_new(&netif_br_cfg);
//
/*esp_netif_ip_info_t br_info;
IP4_ADDR(&br_info.ip, 192, 168, 5, 1);
IP4_ADDR(&br_info.gw, 192, 168, 5, 1);
IP4_ADDR(&br_info.netmask, 255, 255, 255, 0);
esp_netif_set_ip_info(br, &br_info);*/
//
esp_netif_set_mac(br,sta_mac);
// Create new esp netif bridge glue instance
esp_netif_br_glue_handle_t netif_br_glue = esp_netif_br_glue_new();
// Add Ethernet port interfaces to that esp netif bridge glue instance
//for (int i = 0; i < eth_port_cnt; i++) {
// ESP_ERROR_CHECK(esp_netif_br_glue_add_port(netif_br_glue, eth_netifs));
//}
ESP_ERROR_CHECK(esp_netif_br_glue_add_port(netif_br_glue, port));
// Attach esp netif bridge glue instance with added ports to bridge netif
ESP_ERROR_CHECK(esp_netif_attach(br, netif_br_glue));
//
esp_netif_dhcps_stop( br );
esp_netif_dhcps_start( br );
//---------------------------
//-- CREATE & START WIFI AP
//
ap = esp_netif_create_default_wifi_ap();
ESP_LOGI(TAG,"esp_netif_create_default_wifi_ap(), sleeping 3s...");
vTaskDelay( 3000/10 );
//
esp_netif_get_ip_info(ap,&ap_info);
ESP_LOGI(TAG,"esp_netif_get_ip_info() ap done...");
printf("esp_netif_get_ip_info() ap ip: "IPSTR"\n",IP2STR(&ap_info.ip));
/*dhcps_lease_t dhcps_poll = {
.enable = true
};
uint32_t len = sizeof(dhcps_poll);
IP4_ADDR(&dhcps_poll.start_ip, 192, 168, 4, 2);
IP4_ADDR(&dhcps_poll.end_ip, 192, 168, 4, 10);
esp_netif_dhcps_option(ap, ESP_NETIF_OP_SET, REQUESTED_IP_ADDRESS, &dhcps_poll, len);*/
vTaskDelay( 3000/10 );
// init two mesh network interfaces
//esp_netif_create_default_wifi_mesh_netifs(&sta, &ap);
//
/*esp_netif_dhcpc_get_status(sta, &state);
esp_netif_dhcps_get_status(ap, &state);
// test both server and client are still *not* STARTED after start
esp_netif_action_start(ap, NULL, 0, NULL);
esp_netif_action_start(sta, NULL, 0, NULL);
esp_netif_dhcpc_get_status(sta, &state);
esp_netif_dhcps_get_status(ap, &state);*/
wifi_config_t wifi_config_ap = {
.ap = {
.ssid = "t3ch_ap",
.ssid_len = strlen("t3ch_ap"),
.channel = 6,
.password = "t3ch_ap1",
.max_connection = 4,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.pmf_cfg = {
.required = false,
},
},
};
//if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
// wifi_config.ap.authmode = WIFI_AUTH_OPEN;
//}
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config_ap));
ESP_ERROR_CHECK(esp_wifi_start());
//
/*err = esp_netif_dhcps_stop( ap );
ESP_LOGI(TAG,"esp_netif_dhcps_stop() ap, err: %s", esp_err_to_name(err));
//
esp_netif_ip_info_t ap_info;
IP4_ADDR(&ap_info.ip, 192, 168, 4, 1);
IP4_ADDR(&ap_info.gw, 192, 168, 1, 1);
IP4_ADDR(&ap_info.netmask, 255, 255, 255, 0);
esp_netif_set_ip_info(ap, &ap_info);
*/
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
/*err = esp_netif_dhcps_stop( ap );
ESP_LOGI(TAG,"esp_netif_dhcps_stop() ap, err: %s", esp_err_to_name(err));
err = esp_netif_dhcps_stop( sta );
ESP_LOGI(TAG,"esp_netif_dhcps_stop() sta, err: %s", esp_err_to_name(err));
err = esp_netif_dhcps_start( ap );
ESP_LOGI(TAG,"esp_netif_dhcps_start() ap, err: %s", esp_err_to_name(err));
err = esp_netif_dhcps_start( sta );
ESP_LOGI(TAG,"esp_netif_dhcps_start() sta, err: %s", esp_err_to_name(err));
* */
//
ESP_LOGI(TAG, "Starting task_status!!!");
xTaskCreate( task_status, "NAME", 5000, NULL, tskIDLE_PRIORITY, &task_handle );
vTaskDelay( 3000/10 );
}
void app_main(void)
{
//Initialize NVS
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);
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
wifi_init_softap();
}
A lot thanks!!!
t3ch
So have writed something with idea to share internet from wifi_sta to wifi_ap.. i know that exists in esp_iot_bridge and works, have tested. but i like to achive so this works. will paste code and output of program. i think all is writed correctly just ip addresses are not set as they need to be, i guess.
specialy for "br" interface. and when i try to set them manualy ip, netmask and gw stays 0.0.0.0 ... ok here is output of program and then code of program...:
output:
[0;32mI (219731) wifi kosAP: task_status() in the loop..[0m
[0;32mI (219741) wifi kosAP: esp_netif_get_dns_info() STA, err: ESP_OK, sta_dns: 80.58.61.254
[0m
[0;32mI (219751) wifi kosAP: esp_netif_get_dns_info() AP, err: ESP_OK, ap_dns: 0.0.0.0
[0m
[0;32mI (219761) wifi kosAP: esp_netif_get_dns_info() PORT, err: ESP_OK, port_dns: 80.58.61.254
[0m
[0;32mI (219761) wifi kosAP: esp_netif_get_dns_info() BR, err: ESP_OK, br_dns: 80.58.61.254
[0m
[0;32mI (219761) wifi kosAP: task_status() ap.ip: 192.168.4.1[0m
[0;32mI (219771) wifi kosAP: task_status() ap.netmask: 255.255.255.0[0m
[0;32mI (219781) wifi kosAP: task_status() ap.gw: 192.168.4.1[0m
[0;32mI (219781) wifi kosAP: task_status() ap.mac: 94:e6:86:02:8e:d9
[0m
[0;32mI (219791) wifi kosAP: task_status() sta.ip: 192.168.1.47[0m
[0;32mI (219801) wifi kosAP: task_status() sta.netmask: 255.255.255.0[0m
[0;32mI (219801) wifi kosAP: task_status() sta.gw: 192.168.1.1[0m
[0;32mI (219811) wifi kosAP: task_status() sta.mac: 94:e6:86:02:8e:d8
[0m
[0;32mI (219811) wifi kosAP: task_status() port.ip: 0.0.0.0[0m
[0;32mI (219821) wifi kosAP: task_status() port.netmask: 0.0.0.0[0m
[0;32mI (219831) wifi kosAP: task_status() port.gw: 0.0.0.0[0m
[0;32mI (219831) wifi kosAP: task_status() port.mac: 00:00:00:00:00:00
[0m
[0;32mI (219841) wifi kosAP: task_status() br.ip: 0.0.0.0[0m
[0;32mI (219841) wifi kosAP: task_status() br.netmask: 0.0.0.0[0m
[0;32mI (219851) wifi kosAP: task_status() br.gw: 0.0.0.0[0m
[0;32mI (219861) wifi kosAP: task_status() br.mac: 94:e6:86:02:8e:d8
program code:
/* soft APSTA by grandekos aka t3ch */
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_mac.h"
#include "esp_wifi.h"
#include "esp_wifi_netif.h"
//#include "esp_event.h"
#include "esp_log.h"
//
//#include "tcpip_adapter.h" replaced for esp_netif...
#include "esp_netif.h"
#include "esp_netif_br_glue.h"
//#include "esp_netif_lwip.h"
#include "esp_netif_net_stack.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
//
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/lwip_napt.h"
//
#include "dhcpserver/dhcpserver.h"
#include "dhcpserver/dhcpserver_options.h"
/* The examples use WiFi configuration that you can set via project configuration menu.
If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
#define EXAMPLE_ESP_WIFI_CHANNEL CONFIG_ESP_WIFI_CHANNEL
#define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
#define EXAMPLE_ESP_MAXIMUM_RETRY 10
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
esp_netif_t *ap = NULL, *sta = NULL, *port=NULL, *br=NULL;
esp_netif_ip_info_t sta_info, ap_info, port_info, br_info;
esp_netif_dns_info_t sta_dns, ap_dns, port_dns, br_dns;
TaskHandle_t task_handle = NULL;
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
/*
#if CONFIG_ESP_WIFI_AUTH_OPEN
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
#elif CONFIG_ESP_WIFI_AUTH_WEP
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
#elif CONFIG_ESP_WIFI_AUTH_WPA_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA2_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA_WPA2_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA3_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA2_WPA3_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
#elif CONFIG_ESP_WIFI_AUTH_WAPI_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
#endif
*/
static const char *TAG = "wifi kosAP";
static int s_retry_num=0;
//
void task_status() {
ESP_LOGI(TAG,"task_status() starting");
esp_err_t err;
uint8_t tmpmac[6];
while(1) {
ESP_LOGI(TAG,"task_status() in the loop..");
//
err = esp_netif_get_dns_info(sta, ESP_NETIF_DNS_MAIN, &sta_dns);
ESP_LOGI(TAG,"esp_netif_get_dns_info() STA, err: %s, sta_dns: "IPSTR"\n", esp_err_to_name(err), IP2STR(&sta_dns.ip.u_addr.ip4));
//
err = esp_netif_get_dns_info(ap, ESP_NETIF_DNS_MAIN, &ap_dns);
ESP_LOGI(TAG,"esp_netif_get_dns_info() AP, err: %s, ap_dns: "IPSTR"\n", esp_err_to_name(err), IP2STR(&ap_dns.ip.u_addr.ip4));
//
err = esp_netif_get_dns_info(port, ESP_NETIF_DNS_MAIN, &port_dns);
ESP_LOGI(TAG,"esp_netif_get_dns_info() PORT, err: %s, port_dns: "IPSTR"\n", esp_err_to_name(err), IP2STR(&port_dns.ip.u_addr.ip4));
//
err = esp_netif_get_dns_info(br, ESP_NETIF_DNS_MAIN, &br_dns);
ESP_LOGI(TAG,"esp_netif_get_dns_info() BR, err: %s, br_dns: "IPSTR"\n", esp_err_to_name(err), IP2STR(&br_dns.ip.u_addr.ip4));
//
esp_netif_get_ip_info(ap,&ap_info);
ESP_LOGI(TAG,"task_status() ap.ip: "IPSTR,IP2STR(&ap_info.ip));
ESP_LOGI(TAG,"task_status() ap.netmask: "IPSTR,IP2STR(&ap_info.netmask));
ESP_LOGI(TAG,"task_status() ap.gw: "IPSTR,IP2STR(&ap_info.gw));
esp_netif_get_mac(ap,&tmpmac);
ESP_LOGI(TAG,"task_status() ap.mac: "MACSTR"\n",MAC2STR(tmpmac));
//
esp_netif_get_ip_info(sta,&sta_info);
ESP_LOGI(TAG,"task_status() sta.ip: "IPSTR,IP2STR(&sta_info.ip));
ESP_LOGI(TAG,"task_status() sta.netmask: "IPSTR,IP2STR(&sta_info.netmask));
ESP_LOGI(TAG,"task_status() sta.gw: "IPSTR,IP2STR(&sta_info.gw));
esp_netif_get_mac(sta,&tmpmac);
ESP_LOGI(TAG,"task_status() sta.mac: "MACSTR"\n",MAC2STR(tmpmac));
//
esp_netif_get_ip_info(port,&port_info);
ESP_LOGI(TAG,"task_status() port.ip: "IPSTR,IP2STR(&port_info.ip));
ESP_LOGI(TAG,"task_status() port.netmask: "IPSTR,IP2STR(&port_info.netmask));
ESP_LOGI(TAG,"task_status() port.gw: "IPSTR,IP2STR(&port_info.gw));
esp_netif_get_mac(port,&tmpmac);
ESP_LOGI(TAG,"task_status() port.mac: "MACSTR"\n",MAC2STR(tmpmac));
//
esp_netif_get_ip_info(br,&br_info);
ESP_LOGI(TAG,"task_status() br.ip: "IPSTR,IP2STR(&br_info.ip));
ESP_LOGI(TAG,"task_status() br.netmask: "IPSTR,IP2STR(&br_info.netmask));
ESP_LOGI(TAG,"task_status() br.gw: "IPSTR,IP2STR(&br_info.gw));
esp_netif_get_mac(br,&tmpmac);
ESP_LOGI(TAG,"task_status() br.mac: "MACSTR"\n",MAC2STR(tmpmac));
vTaskDelay(3000/10);
}
}
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data){
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", MAC2STR(event->mac), event->aid);
esp_netif_dhcps_stop( ap );
esp_netif_dhcps_start( ap );
//esp_netif_dhcps_start( ap );
//esp_netif_dhcps_start( sta );
ESP_LOGI(TAG, "station "MACSTR" join -> dhcp restarted!, AID=%d", MAC2STR(event->mac), event->aid);
}
else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
MAC2STR(event->mac), event->aid);
}
else if (event_id == WIFI_EVENT_STA_START) {
ESP_LOGI(TAG,"sta started, wifi connecting...");
esp_wifi_connect();
}
else if (event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"connect to the AP fail");
}
else if (event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
/*else if(event_id==SYSTEM_EVENT_AP_START) {
ESP_LOGI(TAG,"system event ap START!");
}
else if(event_id==SYSTEM_EVENT_STA_START) {
ESP_LOGI(TAG,"system event ap START!");
}*/
else {
ESP_LOGI(TAG,"Unknown event...%ld",event_id);
}
}
void wifi_init_softap(void)
{
esp_err_t err;
//esp_netif_dns_info_t ip_dns;
esp_netif_dhcp_status_t status;
//
s_wifi_event_group = xEventGroupCreate();
esp_event_loop_create_default();
ESP_ERROR_CHECK(esp_netif_init());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
//
sta = esp_netif_create_default_wifi_sta();
//esp_netif_create_default_wifi_mesh_netifs(&sta, &ap);
/*-- Set static ip for station
*------------------------------
esp_netif_ip_info_t ip_info;
IP4_ADDR(&ip_info.ip, 192, 168, 15, 22);
IP4_ADDR(&ip_info.gw, 192, 168, 15, 1);
IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
esp_netif_set_ip_info(sta, &ip_info);
*/
//
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL/*&instance_got_ip*/));
//
wifi_config_t wifi_config_sta = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
.threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
//.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
.pmf_cfg = {
.capable = true,
.required = false
}
},
};
//
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config_sta));
ESP_ERROR_CHECK(esp_wifi_start());
//--
//
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
//
esp_netif_get_ip_info(sta,&sta_info);
ESP_LOGI(TAG,"esp_netif_get_ip_info() sta done...");
printf("esp_netif_get_ip_info() sta ip: "IPSTR"\n",IP2STR(&sta_info.ip));
vTaskDelay( 3000/10 );
//
ESP_LOGI(TAG,"%s.) Test retrive sta iodriver... sta: %p", __func__, sta);
wifi_netif_driver_t sta_drv = esp_netif_get_io_driver(sta);
//wifi_netif_driver_t ap_drv = esp_netif_get_io_driver(ap);
uint8_t sta_mac[6], ap_mac[6];
err = esp_wifi_get_if_mac(sta_drv,sta_mac);
ESP_LOGI(TAG,"Test iodriver... sta_mac: "MACSTR"\n",MAC2STR(sta_mac));
//err = esp_wifi_get_if_mac(ap_drv,ap_mac);
//ESP_LOGI(TAG,"Test iodriver... ap_mac: "MACSTR"\n",MAC2STR(ap_mac));
//-------------------
//-- CREATE BRIDGE
// mac example:
// uint8_t new_mac[8] = {0x01,0x02,0x03,0x04,0x05,0x06};
// Create instances of esp-netif for Ethernet ports
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
esp_netif_config_t netif_cfg = {
.base = &esp_netif_config,
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA
};
//
esp_netif_config.if_key = "WLAN_0";
esp_netif_config.if_desc = "wlan0";
esp_netif_config.route_prio = 50;
esp_netif_config.flags = 0; // esp-netif flags need to be zero when port's to be bridged
port = esp_netif_new(&netif_cfg);
// Attach Ethernet driver to TCP/IP stack
ESP_ERROR_CHECK(esp_netif_attach(port, sta_drv));
// Create instance of esp-netif for bridge interface
esp_netif_inherent_config_t esp_netif_br_config = ESP_NETIF_INHERENT_DEFAULT_BR();
esp_netif_config_t netif_br_cfg = {
.base = &esp_netif_br_config,
.stack = ESP_NETIF_NETSTACK_DEFAULT_BR,
};
// Bridge configuration
bridgeif_config_t bridgeif_config = {
.max_fdb_dyn_entries = 10, // maximum number of address entries in dynamic forwarding database
.max_fdb_sta_entries = 2, // maximum number of address entries in static forwarding database
.max_ports = 1 // maximum number of ports the bridge can consist of
};
esp_netif_br_config.bridge_info = &bridgeif_config;
// Set MAC address of bridge interface the same as the Ethernet interface
//memcpy(esp_netif_br_config.mac, sta_mac, 6);
br = esp_netif_new(&netif_br_cfg);
//
/*esp_netif_ip_info_t br_info;
IP4_ADDR(&br_info.ip, 192, 168, 5, 1);
IP4_ADDR(&br_info.gw, 192, 168, 5, 1);
IP4_ADDR(&br_info.netmask, 255, 255, 255, 0);
esp_netif_set_ip_info(br, &br_info);*/
//
esp_netif_set_mac(br,sta_mac);
// Create new esp netif bridge glue instance
esp_netif_br_glue_handle_t netif_br_glue = esp_netif_br_glue_new();
// Add Ethernet port interfaces to that esp netif bridge glue instance
//for (int i = 0; i < eth_port_cnt; i++) {
// ESP_ERROR_CHECK(esp_netif_br_glue_add_port(netif_br_glue, eth_netifs));
//}
ESP_ERROR_CHECK(esp_netif_br_glue_add_port(netif_br_glue, port));
// Attach esp netif bridge glue instance with added ports to bridge netif
ESP_ERROR_CHECK(esp_netif_attach(br, netif_br_glue));
//
esp_netif_dhcps_stop( br );
esp_netif_dhcps_start( br );
//---------------------------
//-- CREATE & START WIFI AP
//
ap = esp_netif_create_default_wifi_ap();
ESP_LOGI(TAG,"esp_netif_create_default_wifi_ap(), sleeping 3s...");
vTaskDelay( 3000/10 );
//
esp_netif_get_ip_info(ap,&ap_info);
ESP_LOGI(TAG,"esp_netif_get_ip_info() ap done...");
printf("esp_netif_get_ip_info() ap ip: "IPSTR"\n",IP2STR(&ap_info.ip));
/*dhcps_lease_t dhcps_poll = {
.enable = true
};
uint32_t len = sizeof(dhcps_poll);
IP4_ADDR(&dhcps_poll.start_ip, 192, 168, 4, 2);
IP4_ADDR(&dhcps_poll.end_ip, 192, 168, 4, 10);
esp_netif_dhcps_option(ap, ESP_NETIF_OP_SET, REQUESTED_IP_ADDRESS, &dhcps_poll, len);*/
vTaskDelay( 3000/10 );
// init two mesh network interfaces
//esp_netif_create_default_wifi_mesh_netifs(&sta, &ap);
//
/*esp_netif_dhcpc_get_status(sta, &state);
esp_netif_dhcps_get_status(ap, &state);
// test both server and client are still *not* STARTED after start
esp_netif_action_start(ap, NULL, 0, NULL);
esp_netif_action_start(sta, NULL, 0, NULL);
esp_netif_dhcpc_get_status(sta, &state);
esp_netif_dhcps_get_status(ap, &state);*/
wifi_config_t wifi_config_ap = {
.ap = {
.ssid = "t3ch_ap",
.ssid_len = strlen("t3ch_ap"),
.channel = 6,
.password = "t3ch_ap1",
.max_connection = 4,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.pmf_cfg = {
.required = false,
},
},
};
//if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
// wifi_config.ap.authmode = WIFI_AUTH_OPEN;
//}
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config_ap));
ESP_ERROR_CHECK(esp_wifi_start());
//
/*err = esp_netif_dhcps_stop( ap );
ESP_LOGI(TAG,"esp_netif_dhcps_stop() ap, err: %s", esp_err_to_name(err));
//
esp_netif_ip_info_t ap_info;
IP4_ADDR(&ap_info.ip, 192, 168, 4, 1);
IP4_ADDR(&ap_info.gw, 192, 168, 1, 1);
IP4_ADDR(&ap_info.netmask, 255, 255, 255, 0);
esp_netif_set_ip_info(ap, &ap_info);
*/
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
/*err = esp_netif_dhcps_stop( ap );
ESP_LOGI(TAG,"esp_netif_dhcps_stop() ap, err: %s", esp_err_to_name(err));
err = esp_netif_dhcps_stop( sta );
ESP_LOGI(TAG,"esp_netif_dhcps_stop() sta, err: %s", esp_err_to_name(err));
err = esp_netif_dhcps_start( ap );
ESP_LOGI(TAG,"esp_netif_dhcps_start() ap, err: %s", esp_err_to_name(err));
err = esp_netif_dhcps_start( sta );
ESP_LOGI(TAG,"esp_netif_dhcps_start() sta, err: %s", esp_err_to_name(err));
* */
//
ESP_LOGI(TAG, "Starting task_status!!!");
xTaskCreate( task_status, "NAME", 5000, NULL, tskIDLE_PRIORITY, &task_handle );
vTaskDelay( 3000/10 );
}
void app_main(void)
{
//Initialize NVS
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);
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
wifi_init_softap();
}
A lot thanks!!!
t3ch