/** * Setup IP settings */ LOCAL void ICACHE_FLASH_ATTR esp_wifi_setup_apmode_ip( void ) { struct ip_info admin_ip_config; uint8 ap_mac[ 6 ] = { WIFI_AP_MAC_ADDRESS }; uint8 ap_ip[ 4 ] = { WIFI_AP_IP_SELF }; uint8 ap_gw[ 4 ] = { WIFI_AP_IP_GATEWAY }; uint8 ap_mk[ 4 ] = { WIFI_AP_IP_MASK }; // check for MAC address if( esp_flash_read_parameter( ESP_WIFI_APMODE_MAC_ID, ap_mac ) == 0x00 ) { // save default MAC into flash esp_flash_save_parameter( ESP_WIFI_APMODE_MAC_ID, ap_mac, 0x06 ); } // set MAC address of access point wifi_set_macaddr( SOFTAP_IF, ap_mac ); // check for stored IP settings // if( esp_flash_read_parameter( ESP_WIFI_APMODE_IP_SELF_ID, ap_ip ) == 0x00 ) // { // save parameters //esp_flash_save_parameter( ESP_WIFI_APMODE_IP_SELF_ID, ap_ip, 0x04 ); //esp_flash_save_parameter( ESP_WIFI_APMODE_IP_GATEWAY_ID, ap_gw, 0x04 ); //esp_flash_save_parameter( ESP_WIFI_APMODE_IP_MASK_ID, ap_mk, 0x04 ); // } else { // load parameters //esp_flash_read_parameter( ESP_WIFI_APMODE_IP_GATEWAY_ID, ap_gw ); //esp_flash_read_parameter( ESP_WIFI_APMODE_IP_MASK_ID, ap_mk ); // } // set ip settings IP4_ADDR( &admin_ip_config.ip, ap_ip[ 0 ], ap_ip[ 1 ], ap_ip[ 2 ], ap_ip[ 3 ] ); IP4_ADDR( &admin_ip_config.gw, ap_gw[ 0 ], ap_gw[ 1 ], ap_gw[ 2 ], ap_gw[ 3 ] ); IP4_ADDR( &admin_ip_config.netmask, ap_mk[ 0 ], ap_mk[ 1 ], ap_mk[ 2 ], ap_mk[ 3 ] ); // set ip configuration for AP mode wifi_set_ip_info( SOFTAP_IF, &admin_ip_config ); }
void user_init(void) { uart_init(115200, 115200); wifi_set_opmode(SOFTAP_MODE); wifi_set_macaddr(SOFTAP_IF, slave_mac); node_group_init(); }
void Settings::syncMacInterface() { if ( debug ) { Serial.println ( "[+] Trying to sync the MAC addr with settings" ); } if ( isSettingsLoaded ) { Mac macToSync; if ( isMacAPRand ) { macToSync.randomize (); wifi_set_macaddr ( SOFTAP_IF, macToSync._get () ); if ( debug ) { Serial.println ( "[+] Synced with a random mac addr : " + macToSync.toString () ); } } else if ( macAP.valid () ) { macToSync = macAP; wifi_set_macaddr ( SOFTAP_IF, macToSync._get () ); if ( debug ) { Serial.println ( "[+] Synced with saved mac addr : " + macToSync.toString () ); } } else { if ( debug ) { Serial.println ( "[+] Could not sync because of invalid settings !" ); } } } else { if ( debug ) { Serial.println ( "Could not sync because settings are not loaded !" ); } } }
// Lua: mac = wifi.xx.setmac() static int wifi_setmac( lua_State* L, uint8_t mode ) { unsigned len = 0; const char *mac = luaL_checklstring( L, 1, &len ); if(len!=6) return luaL_error( L, "wrong arg type" ); lua_pushboolean(L,wifi_set_macaddr(mode, (uint8 *)mac)); return 1; }
void softap_start() { char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab}; char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab}; struct ip_info info; wifi_set_macaddr(SOFTAP_IF, sofap_mac); wifi_set_macaddr(STATION_IF, sta_mac); IP4_ADDR(&info.ip, 192, 168, 3, 200); IP4_ADDR(&info.gw, 192, 168, 3, 1); IP4_ADDR(&info.netmask, 255, 255, 255, 0); wifi_set_ip_info(STATION_IF, &info); IP4_ADDR(&info.ip, 10, 10, 10, 1); IP4_ADDR(&info.gw, 10, 10, 10, 1); IP4_ADDR(&info.netmask, 255, 255, 255, 0); wifi_set_ip_info(SOFTAP_IF, &info); ets_uart_printf("Starting SoftAP\n"); }
void user_init(void) { uart_init(115200, 115200); wifi_set_opmode(STATION_MODE); wifi_set_macaddr(STATION_IF, ctrl_mac); node_group_init(); // send data through espnow every 5s os_timer_setfn(&client_timer, (os_timer_func_t *)node_group_send_cb, NULL); os_timer_arm(&client_timer, 5000, 1); }
/****************************************************************************** * FunctionName : user_esp_platform_init * Description : device parame init based on espressif platform * Parameters : none * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR user_esp_platform_init(void) { os_sprintf(iot_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\ IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG); os_printf("IOT VERSION = %s\n",iot_version); system_param_load(ESP_PARAM_START_SEC, 0, &esp_param, sizeof(esp_param)); struct rst_info *rtc_info = system_get_rst_info(); os_printf("reset reason: %x\n", rtc_info->reason); if (rtc_info->reason == REASON_WDT_RST || rtc_info->reason == REASON_EXCEPTION_RST || rtc_info->reason == REASON_SOFT_WDT_RST) { if (rtc_info->reason == REASON_EXCEPTION_RST) { os_printf("Fatal exception (%d):\n", rtc_info->exccause); } os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n", rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, rtc_info->depc); } /***add by tzx for saving ip_info to avoid dhcp_client start****/ struct dhcp_client_info dhcp_info; struct ip_info sta_info; system_rtc_mem_read(64,&dhcp_info,sizeof(struct dhcp_client_info)); if(dhcp_info.flag == 0x01 ) { if (true == wifi_station_dhcpc_status()) { wifi_station_dhcpc_stop(); } sta_info.ip = dhcp_info.ip_addr; sta_info.gw = dhcp_info.gw; sta_info.netmask = dhcp_info.netmask; if ( true != wifi_set_ip_info(STATION_IF,&sta_info)) { os_printf("set default ip wrong\n"); } } os_memset(&dhcp_info,0,sizeof(struct dhcp_client_info)); system_rtc_mem_write(64,&dhcp_info,sizeof(struct rst_info)); #if AP_CACHE wifi_station_ap_number_set(AP_CACHE_NUMBER); #endif #if 0 { char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab}; char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab}; struct ip_info info; wifi_set_macaddr(SOFTAP_IF, sofap_mac); wifi_set_macaddr(STATION_IF, sta_mac); IP4_ADDR(&info.ip, 192, 168, 3, 200); IP4_ADDR(&info.gw, 192, 168, 3, 1); IP4_ADDR(&info.netmask, 255, 255, 255, 0); wifi_set_ip_info(STATION_IF, &info); IP4_ADDR(&info.ip, 10, 10, 10, 1); IP4_ADDR(&info.gw, 10, 10, 10, 1); IP4_ADDR(&info.netmask, 255, 255, 255, 0); wifi_set_ip_info(SOFTAP_IF, &info); } #endif if (esp_param.activeflag != 1) { #ifdef SOFTAP_ENCRYPT struct softap_config config; char password[33]; char macaddr[6]; wifi_softap_get_config(&config); wifi_get_macaddr(SOFTAP_IF, macaddr); os_memset(config.password, 0, sizeof(config.password)); os_sprintf(password, MACSTR "_%s", MAC2STR(macaddr), PASSWORD); os_memcpy(config.password, password, os_strlen(password)); config.authmode = AUTH_WPA_WPA2_PSK; wifi_softap_set_config(&config); #endif wifi_station_set_hostname( HOST_NAME ); wifi_set_opmode(STATION_MODE); } #if SENSOR_DEVICE user_sensor_init(esp_param.activeflag); #endif #if 0 if (wifi_get_opmode() != SOFTAP_MODE) { os_timer_disarm(&client_timer); os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, 1); os_timer_arm(&client_timer, 100, 0); } // 2015-12-27 single_key[0] = key_init_single( 12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, NULL, GPIO_INTER); keys.key_num = 1; keys.single_key = single_key; key_init(&keys); #endif }
static void esp_now_setup (esp_now_netdev_t* dev) { DEBUG("%s: %p\n", __func__, dev); /* * Init the WiFi driver. TODO It is not only required before ESP_NOW is * initialized but also before other WiFi functions are used. Once other * WiFi functions are realized it has to be moved to a more common place. */ extern portMUX_TYPE g_intr_lock_mux; mutex_init(&g_intr_lock_mux); esp_system_event_add_handler (_esp_system_event_handler, NULL); esp_err_t result; #if CONFIG_ESP32_WIFI_NVS_ENABLED result = nvs_flash_init(); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "nfs_flash_init failed with return value %d\n", result); return; } #endif wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); result = esp_wifi_init(&cfg); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_wifi_init failed with return value %d\n", result); return; } #ifdef CONFIG_WIFI_COUNTRY /* TODO */ #endif /* we use predefined station configuration */ wifi_config_t wifi_config_sta = { .sta = { .ssid = CONFIG_WIFI_STA_SSID, .password = CONFIG_WIFI_STA_PASSWORD, .channel = CONFIG_WIFI_STA_CHANNEL, .scan_method = CONFIG_WIFI_STA_SCAN_METHOD, .sort_method = CONFIG_WIFI_STA_SORT_METHOD, .threshold.rssi = CONFIG_WIFI_STA_RSSI, .threshold.authmode = CONFIG_WIFI_STA_AUTHMODE } }; /* get SoftAP interface mac address and store it as device addresss */ esp_read_mac(dev->addr, ESP_MAC_WIFI_SOFTAP); /* prepare the ESP_NOW configuration for SoftAP */ wifi_config_t wifi_config_ap = {}; strcpy ((char*)wifi_config_ap.ap.password, esp_now_params.softap_pass); sprintf((char*)wifi_config_ap.ap.ssid, "%s%02x%02x%02x%02x%02x%02x", ESP_NOW_AP_PREFIX, dev->addr[0], dev->addr[1], dev->addr[2], dev->addr[3], dev->addr[4], dev->addr[5]); wifi_config_ap.ap.ssid_len = strlen((char*)wifi_config_ap.ap.ssid); wifi_config_ap.ap.channel = esp_now_params.channel; wifi_config_ap.ap.authmode = CONFIG_WIFI_AP_AUTH; wifi_config_ap.ap.ssid_hidden = CONFIG_WIFI_AP_HIDDEN; wifi_config_ap.ap.max_connection = CONFIG_WIFI_AP_MAX_CONN; wifi_config_ap.ap.beacon_interval = CONFIG_WIFI_AP_BEACON; /* set the WiFi interface to Station + SoftAP mode without DHCP */ result = esp_wifi_set_mode(WIFI_MODE_STA | WIFI_MODE_AP); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_wifi_set_mode failed with return value %d\n", result); return; } /* set the Station and SoftAP configuration */ result = esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config_sta); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_wifi_set_config station failed with " "return value %d\n", result); return; } result = esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config_ap); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_wifi_set_mode softap failed with return value %d\n", result); return; } /* start the WiFi driver */ result = esp_wifi_start(); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_wifi_start failed with return value %d\n", result); return; } #if ESP_NOW_UNICAST==0 /* TODO */ /* all ESP-NOW nodes get the shared mac address on their station interface */ wifi_set_macaddr(STATION_IF, (uint8_t*)_esp_now_mac); #endif /* set the netdev driver */ dev->netdev.driver = &_esp_now_driver; /* initialize netdev data structure */ dev->peers_all = 0; dev->peers_enc = 0; mutex_init(&dev->dev_lock); /* initialize ESP-NOW and register callback functions */ result = esp_now_init(); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_now_init failed with return value %d\n", result); return; } esp_now_register_send_cb (esp_now_send_cb); esp_now_register_recv_cb (esp_now_recv_cb); #if ESP_NOW_UNICAST /* create the ESP_NOW event handler thread */ esp_now_event_handler_pid = thread_create(esp_now_event_handler_stack, sizeof(esp_now_event_handler_stack), ESP_NOW_PRIO + 1, THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST, esp_now_event_handler, NULL, "net-esp-now-event"); /* timer for peer scan initialization */ _esp_now_scan_peers_done = false; _esp_now_scan_peers_timer.callback = &esp_now_scan_peers_timer_cb; _esp_now_scan_peers_timer.arg = dev; /* execute the first scan */ esp_now_scan_peers_done(); #else /* ESP_NOW_UNICAST */ #if 0 int res = esp_now_add_peer((uint8_t*)_esp_now_mac, ESP_NOW_ROLE_COMBO, esp_now_params.channel, NULL, 0); DEBUG("%s: multicast node added %d\n", __func__, res); #endif #endif /* ESP_NOW_UNICAST */ }