mirror of
https://github.com/0xFEEDC0DE64/arduino-esp32.git
synced 2025-07-03 13:56:32 +02:00
Major IDF and Arduino Update
WiFi and BlueTooth can now be started and stopped at will. basic functions added to esp32-hal to start and stop the BT radio SimpleBLE class added to show the most basic functionality Example to show how to switch between BT, WiFi or Both
This commit is contained in:
@ -42,76 +42,72 @@ extern "C" {
|
||||
#include "lwip/dns.h"
|
||||
#include "esp_ipc.h"
|
||||
|
||||
#include "esp32-hal-log.h"
|
||||
|
||||
/**
|
||||
* Boot and start WiFi
|
||||
* This method get's called on boot if you use any of the WiFi methods.
|
||||
* If you do not link to this library, WiFi will not be started.
|
||||
* */
|
||||
static bool _esp_wifi_initalized = false;
|
||||
extern void initWiFi()
|
||||
{
|
||||
#if !CONFIG_ESP32_PHY_AUTO_INIT
|
||||
arduino_phy_init();
|
||||
#endif
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
tcpip_adapter_init();
|
||||
esp_event_loop_init(&WiFiGenericClass::_eventCallback, NULL);
|
||||
esp_wifi_init(&cfg);
|
||||
esp_wifi_set_storage(WIFI_STORAGE_FLASH);
|
||||
_esp_wifi_initalized = true;
|
||||
}
|
||||
|
||||
} //extern "C"
|
||||
|
||||
#include "esp32-hal-log.h"
|
||||
|
||||
#undef min
|
||||
#undef max
|
||||
#include <vector>
|
||||
|
||||
static bool _esp_wifi_start()
|
||||
{
|
||||
static bool started = false;
|
||||
esp_err_t err;
|
||||
|
||||
if(!_esp_wifi_initalized){
|
||||
initWiFi();
|
||||
if(!_esp_wifi_initalized){
|
||||
log_w("not initialized");
|
||||
static bool wifiLowLevelInit(){
|
||||
static bool lowLevelInitDone = false;
|
||||
if(!lowLevelInitDone){
|
||||
tcpip_adapter_init();
|
||||
esp_event_loop_init(&WiFiGenericClass::_eventCallback, NULL);
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_err_t err = esp_wifi_init(&cfg);
|
||||
if(err){
|
||||
log_e("esp_wifi_init %d", err);
|
||||
return false;
|
||||
}
|
||||
esp_wifi_set_storage(WIFI_STORAGE_FLASH);
|
||||
esp_wifi_set_mode(WIFI_MODE_NULL);
|
||||
lowLevelInitDone = true;
|
||||
}
|
||||
if(started){
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool wifiLowLevelDeinit(){
|
||||
//deinit not working yet!
|
||||
//esp_wifi_deinit();
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool _esp_wifi_started = false;
|
||||
|
||||
static bool espWiFiStart(){
|
||||
if(_esp_wifi_started){
|
||||
return true;
|
||||
}
|
||||
started = true;
|
||||
err = esp_wifi_start();
|
||||
if (err != ESP_OK) {
|
||||
log_e("%d", err);
|
||||
if(!wifiLowLevelInit()){
|
||||
return false;
|
||||
}
|
||||
#if CONFIG_AUTOCONNECT_WIFI
|
||||
wifi_mode_t mode = WIFI_MODE_NULL;
|
||||
bool auto_connect = false;
|
||||
|
||||
err = esp_wifi_get_mode(&mode);
|
||||
esp_err_t err = esp_wifi_start();
|
||||
if (err != ESP_OK) {
|
||||
log_e("esp_wifi_get_mode: %d", err);
|
||||
log_e("esp_wifi_start %d", err);
|
||||
wifiLowLevelDeinit();
|
||||
return false;
|
||||
}
|
||||
|
||||
err = esp_wifi_get_auto_connect(&auto_connect);
|
||||
if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && auto_connect) {
|
||||
err = esp_wifi_connect();
|
||||
if (err != ESP_OK) {
|
||||
log_e("esp_wifi_connect: %d", err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
_esp_wifi_started = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool espWiFiStop(){
|
||||
esp_err_t err;
|
||||
if(!_esp_wifi_started){
|
||||
return true;
|
||||
}
|
||||
err = esp_wifi_stop();
|
||||
if(err){
|
||||
log_e("Could not stop WiFi! %u", err);
|
||||
return false;
|
||||
}
|
||||
_esp_wifi_started = false;
|
||||
return wifiLowLevelDeinit();
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
|
||||
// -----------------------------------------------------------------------------------------------------------------------
|
||||
@ -246,7 +242,16 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
|
||||
if(cm == m) {
|
||||
return true;
|
||||
}
|
||||
return esp_wifi_set_mode(m) == ESP_OK;
|
||||
esp_err_t err;
|
||||
err = esp_wifi_set_mode(m);
|
||||
if(err){
|
||||
log_e("Could not set mode! %u", err);
|
||||
return false;
|
||||
}
|
||||
if(m){
|
||||
return espWiFiStart();
|
||||
}
|
||||
return espWiFiStop();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -255,10 +260,10 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
|
||||
*/
|
||||
wifi_mode_t WiFiGenericClass::getMode()
|
||||
{
|
||||
uint8_t mode;
|
||||
if(!_esp_wifi_start()){
|
||||
if(!wifiLowLevelInit()){
|
||||
return WIFI_MODE_MAX;
|
||||
}
|
||||
uint8_t mode;
|
||||
esp_wifi_get_mode((wifi_mode_t*)&mode);
|
||||
return (wifi_mode_t)mode;
|
||||
}
|
||||
|
Reference in New Issue
Block a user