esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
293 stars 36 forks source link

OTA Issues with BLE/WiFi sensors #4147

Open natbesh opened 1 year ago

natbesh commented 1 year ago

The problem

I use an ESP32 as an ESPHome Sensor - it uses BLE to get some data from a device & then pass it via WiFi to Home Assistant. It polls BLE every 5 sec & updates over WiFi every 10. WiFi signal is strong, throughput is great.

When trying to do OTA updates there's continual failures which I think are due to busy wifi/BLE circuits. It takes ~6 or so attempts to do a successful update, the same ESP32's in the same location without BLE/WiFi polling update perfectly every time.

The way I can get semi-reliability is to do a reboot of the ESP32 just before it starts the upload - then it works every time, as long as I get the reboot timing right.

Is it possible to update the code so that when an update is pushed to the ESP, it stops its normal loop processing (i.e. wont keep trying to poll BLE & update to HA) - waits 5sec to clear whatever & then starts the upload/update process?

Which version of ESPHome has the issue?

ESPHome version 2022.12.8

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

Home Assistant 2023.2.3

What platform are you using?

ESP32

Board

ESP32-WROVER-B

Component causing the issue

No response

Example YAML snippet

N/A - OTA Updates

Anything in the logs that might be useful for us?

No - OTA times out & ESP reboots

Additional information

As per this: https://github.com/syssi/esphome-jk-bms/discussions/235

Doing lots of BLE & WiFi seems to overload the board/RF Circuits & reliability seems to be an issue with lots of polling.

airy10 commented 1 year ago

It can be fixed by disabling bluetooth during the OTA. This simple change is fixing it for me (probably doesn't cover all configs but good enough for me for now...)

--- a/esphome/components/ota/ota_component.cpp
+++ b/esphome/components/ota/ota_component.cpp
@@ -15,6 +15,11 @@
 #include <cerrno>
 #include <cstdio>

+#ifdef ARDUINO_ARCH_ESP32
+#include <esp_gap_ble_api.h>
+#endif
+
+
 namespace esphome {
 namespace ota {

@@ -145,6 +150,10 @@ void OTAComponent::handle_() {
   }

   ESP_LOGD(TAG, "Starting OTA Update from %s...", this->client_->getpeername().c_str());
+#ifdef ARDUINO_ARCH_ESP32
+    esp_ble_gap_stop_scanning();
+#endif
+
   this->status_set_warning();
 #ifdef USE_OTA_STATE_CALLBACK
   this->state_callback_.call(OTA_STARTED, 0.0f, 0);
github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

oldjiberjaber commented 1 year ago

It can be fixed by disabling bluetooth during the OTA. This simple change is fixing it for me (probably doesn't cover all configs but good enough for me for now...)

--- a/esphome/components/ota/ota_component.cpp
+++ b/esphome/components/ota/ota_component.cpp
@@ -15,6 +15,11 @@
 #include <cerrno>
 #include <cstdio>

+#ifdef ARDUINO_ARCH_ESP32
+#include <esp_gap_ble_api.h>
+#endif
+
+
 namespace esphome {
 namespace ota {

@@ -145,6 +150,10 @@ void OTAComponent::handle_() {
   }

   ESP_LOGD(TAG, "Starting OTA Update from %s...", this->client_->getpeername().c_str());
+#ifdef ARDUINO_ARCH_ESP32
+    esp_ble_gap_stop_scanning();
+#endif
+
   this->status_set_warning();
 #ifdef USE_OTA_STATE_CALLBACK
   this->state_callback_.call(OTA_STARTED, 0.0f, 0);

Has this code been rolled in to the ESPHome code? I suffer from this on all my ESP32 devices which are using BLE and I'm not sure if there is a way to include this in to the build that doesn't get over written by the weekly updates in HA ?