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:
me-no-dev
2017-02-23 01:11:57 +02:00
parent b5ac95b274
commit 1d759380a6
72 changed files with 1079 additions and 308 deletions

View File

@ -0,0 +1,128 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Sketch shows how to switch between WiFi and BlueTooth or use both
// Button is attached between GPIO 0 and GND and modes are switched with each press
#include "WiFi.h"
#define STA_SSID "your-ssid"
#define STA_PASS "your-pass"
#define AP_SSID "esp32"
enum { STEP_BTON, STEP_BTOFF, STEP_STA, STEP_AP, STEP_AP_STA, STEP_OFF, STEP_BT_STA, STEP_END };
void onButton(){
static uint32_t step = STEP_BTON;
switch(step){
case STEP_BTON://BT Only
Serial.println("** Starting BT");
btStart();
break;
case STEP_BTOFF://All Off
Serial.println("** Stopping BT");
btStop();
break;
case STEP_STA://STA Only
Serial.println("** Starting STA");
WiFi.begin(STA_SSID, STA_PASS);
break;
case STEP_AP://AP Only
Serial.println("** Stopping STA");
WiFi.mode(WIFI_AP);
Serial.println("** Starting AP");
WiFi.softAP(AP_SSID);
break;
case STEP_AP_STA://AP+STA
Serial.println("** Starting STA");
WiFi.begin(STA_SSID, STA_PASS);
break;
case STEP_OFF://All Off
Serial.println("** Stopping WiFi");
WiFi.mode(WIFI_OFF);
break;
case STEP_BT_STA://BT+STA
Serial.println("** Starting STA+BT");
WiFi.begin(STA_SSID, STA_PASS);
btStart();
break;
case STEP_END://All Off
Serial.println("** Stopping WiFi+BT");
WiFi.mode(WIFI_OFF);
btStop();
break;
default:
break;
}
if(step == STEP_END){
step = STEP_BTON;
} else {
step++;
}
//little debounce
delay(100);
}
void WiFiEvent(WiFiEvent_t event){
switch(event) {
case SYSTEM_EVENT_AP_START:
Serial.println("AP Started");
WiFi.softAPsetHostname(AP_SSID);
break;
case SYSTEM_EVENT_AP_STOP:
Serial.println("AP Stopped");
break;
case SYSTEM_EVENT_STA_START:
Serial.println("STA Started");
WiFi.setHostname(AP_SSID);
break;
case SYSTEM_EVENT_STA_CONNECTED:
Serial.println("STA Connected");
WiFi.enableIpV6();
break;
case SYSTEM_EVENT_AP_STA_GOT_IP6:
Serial.print("STA IPv6: ");
Serial.println(WiFi.localIPv6());
break;
case SYSTEM_EVENT_STA_GOT_IP:
Serial.print("STA IPv4: ");
Serial.println(WiFi.localIP());
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("STA Disconnected");
break;
case SYSTEM_EVENT_STA_STOP:
Serial.println("STA Stopped");
break;
default:
break;
}
}
void setup() {
Serial.begin(115200);
pinMode(0, INPUT_PULLUP);
WiFi.onEvent(WiFiEvent);
Serial.print("ESP32 SDK: ");
Serial.println(ESP.getSdkVersion());
Serial.println("Press the button to select the next mode");
}
void loop() {
static uint8_t lastPinState = 1;
uint8_t pinState = digitalRead(0);
if(!pinState && lastPinState){
onButton();
}
lastPinState = pinState;
}

View File

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