[英]ESP32 using BLE and WiFi alternately
I have an ESP32 which should receive data over BLE and then send the data over WiFi to a webserver.我有一个 ESP32,它应该通过 BLE 接收数据,然后通过 WiFi 将数据发送到网络服务器。 If I code both tasks separately in Arduino, then everything works but as soon as I merge both tasks, sending over WiFi breaks down.
如果我在 Arduino 中分别对这两个任务进行编码,那么一切正常,但是一旦我合并这两个任务,通过 WiFi 发送就会中断。
From what I understand, BLE and WiFi are sharing the same radio on the ESP32, thus the tasks need to be done alternately to avoid interferences.据我了解,BLE 和 WiFi 在 ESP32 上共享同一个无线电,因此需要交替执行任务以避免干扰。 I've tried to implement this by adding delays between the two tasks but was unsuccessful.
我试图通过在两个任务之间添加延迟来实现这一点,但没有成功。
This is the code I have so far:这是我到目前为止的代码:
#include <HTTPClient.h>
#include <BLEDevice.h>
#include <BLEScan.h>
const char* ssid = "xx";
const char* password = "xx";
int scanTime = 2; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(1);
Serial.setDebugOutput(0); //turn off debut output
WiFi.begin(ssid, password);
int retrycon = 50;
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
if (--retrycon == 0)
{
Serial.println("RESTART");
ESP.restart();
}
Serial.print(".");
}
Serial.print("WiFi connected with IP: ");
Serial.println(WiFi.localIP());
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
}
void loop()
{
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
pBLEScan->clearResults();
delay(3000);
int tryconnect = 20;
while (--tryconnect != 0) {
if (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("...");
} else {
break;
}
}
if (WiFi.status() != WL_CONNECTED) {
WiFi.reconnect();
Serial.println("reconnect");
}
else {
Serial.println("connected to WiFi");
HTTPClient http;
http.begin("http://httpbin.org/ip");
int httpCode = http.GET();
if (httpCode > 0) {
Serial.print("HTTP code ");
Serial.println(httpCode);
} else {
Serial.println("Error on HTTP request");
}
http.end();
delay(10000);
}
}
Can anybody tell me how to implement the two tasks (receiving over BLE, sending over WiFi) to avoid interferences?谁能告诉我如何实现这两个任务(通过 BLE 接收,通过 WiFi 发送)以避免干扰?
I use commands like this to turn off BLE and WiFi:我使用这样的命令来关闭 BLE 和 WiFi:
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
btStop();
adc_power_off();
//esp_wifi_stop(); // Doesn't work for me!
esp_bt_controller_disable();
In my code I BLE advertize/scan, then do the stuff above, then connect to WiFi.在我的代码中,我进行 BLE 广告/扫描,然后执行上述操作,然后连接到 WiFi。 No problem.
没问题。 Oh and remember to
BLEDevice::deinit
when you finished with BLE, otherwise you can't get it to fit in a 4Mb ESP32.哦,记得在完成 BLE 时使用
BLEDevice::deinit
,否则你无法让它适合 4Mb ESP32。 I do BLE, WiFi, HTTPS/SSL, OTA and use the SPIFFS to store data, all on a standard 4Mb ESP32 (ESP-WROOM-32) without PSRAM.我使用 BLE、WiFi、HTTPS/SSL、OTA 并使用 SPIFFS 存储数据,所有这些都在没有 PSRAM 的标准 4Mb ESP32 (ESP-WROOM-32) 上。
All above is in a function to lower the power usage, so I call it all every time I change 'mode'.以上所有内容都在 function 中以降低功耗,所以我每次更改“模式”时都会调用它。 In deep sleep I only use 5uA.
在深度睡眠中,我只使用 5uA。
Also:还:
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
... don't work for me, so I commented them out, and scan for 3 sec. ...不适合我,所以我将它们注释掉,然后扫描 3 秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.