1technophile / OpenMQTTGateway

MQTT gateway for ESP8266 or ESP32 with bidirectional 433mhz/315mhz/868mhz, Infrared communications, BLE, Bluetooth, beacons detection, mi flora, mi jia, LYWSD02, LYWSD03MMC, Mi Scale, TPMS, BBQ thermometer compatibility & LoRa.
https://docs.openmqttgateway.com
GNU General Public License v3.0
3.6k stars 793 forks source link

Reduce power consumption for BLE devices by defaulting Active Scanning to off #1395

Closed ilgrank closed 1 year ago

ilgrank commented 1 year ago

According to @pvvx, BLE active scanning does greatly reduce BLE battery life:

Active scanning increases battery consumption by many percent. The BLE device responds to active requests, which consumes additional power.

(original commend here) So wouldn't it be 'wiser' to default to passive scanning?

1technophile commented 1 year ago

I agree on this fact, nevertheless having passive per default could generate more support with users not seeing their devices. I have another idea in mind that should make everybody happy, give me some time to implement it.

ilgrank commented 1 year ago

Thanks! You mean something like an MQTT command to enable/disable it?

Also, while I'm at it, and as I don't want to flood you with issues, since this is just another suggestion.. IMHO it would be great to allow to retain a (password-protected) configuration webpage after the initial wifi config one.

I spent most of the time configuring OpenMQTT with an issue (my fault) I had with my MQTT broker, and so every time I had to re-set, re-configure and hope for the best, until I found what my problem was, dozens of resets later.

Having a webpage (tasmota-like) for configuration would have made changing config way easier (and since I had issue connecting with my broker, using MQTT to reconfigure was not an option)

Again, thanks!

1technophile commented 1 year ago

Thanks! You mean something like an MQTT command to enable/disable it?

We have already this for info https://docs.openmqttgateway.com/use/ble.html#setting-if-the-gateway-use-active-or-passive-scanning

For the web page, this is something that I'm considering

ilgrank commented 1 year ago

Double thumbs up for the Web page, it would make things a lot more practical 👍

Whoops, sorry, completely missed that page :( edit: after disabling Active scanning, I do still receive passive beacons, but they don't get decoded. , eg:

{
  "id": "A4:C1:38:36:20:D6",
  "mac_type": 0,
  "rssi": -55,
  "servicedata": "d6203638c1a44708a013180c641705",
  "servicedatauuid": "0x181a"
}

If I get it right, the servicedatashould be carrying the sensor readings... i'm missing something else?

1technophile commented 1 year ago

And they are decoded when you turn active scan ON ?

If it is an LYWSD03MMC with PVVX, you may have to try to change the name to have ATC_ in it. Or alternatively, you can flash this patch which is less restrictive than the production one: https://docs.openmqttgateway.com/dev/upload/web-install.html

ilgrank commented 1 year ago

made all the tests with retained messages as you suggested

had the sensor data -> turned active scan off -> no more data turned active scan back on -> still no sensor data power cycled the ESP32 (still with active scan retained message) : now I have data

edit: been able to reproduce by setting active scan off again -> no sensor data. (running pvvx firmware with pvvx data format)

edit2: installing https://docs.openmqttgateway.com/dev/upload/web-install.html right now

1technophile commented 1 year ago

did you try this different binary: https://docs.openmqttgateway.com/dev/upload/web-install.html

ilgrank commented 1 year ago

sorry, edited while you were replying, I'm already flashing it, will take some minute to reconfigure

1technophile commented 1 year ago

If you don't erase you don't have to reconfigure for info

ilgrank commented 1 year ago

I can confirm that with this version it works perfectly! I also can confirm that the active scan is still off as I see way less devices (actually, just the Xiaomi sensors)

If you don't erase you don't have to reconfigure for info I had fear that keeping the 'old' config with the new build could mess things up

1technophile commented 1 year ago

Great, we will integrate it into the development branch for the next release

ilgrank commented 1 year ago

again, my thanks! 👍 🥇

ilgrank commented 1 year ago

btw: I've checked the "Contribute" page, and I would want to make a donation, but apart from contributing code (alas, I'm not able to) is there a way to tip the project (or, as the page says, its contributors?) Edit: I found your PP address in a 5 year old issue : https://github.com/1technophile/OpenMQTTGateway/issues/35 is that one still valid? :)

1technophile commented 1 year ago

Thanks for asking, to tip the project you can purchase the Theengs app https://app.theengs.io/ Or a Theengs Plug if you are in the US or Canada https://shop.theengs.io/

ilgrank commented 1 year ago

Alas, not in USA/CA, and will surely try Theengs in the future, hope that that PP address is still fine :)

1technophile commented 1 year ago

Send me an email to florian@theengs.io I can share you a link

1technophile commented 1 year ago

Received on the previous address thanks

jacol-code commented 7 months ago

Naive question, how do I setup passive mode? (so that it reacts on every BLE advertisement)

No matter how I'm trying to do it (BTConfig.forcePassiveScan = true in ZgatewayBT.ino or sending command messages: { "forcepscn": true } or { "forcePassiveScan": true }) it does not really work for me. The gateway still seems to scan and connect to individual devices (at least based on serial messages).

I'm using ESP32 with LYWSD03MMC thermometers running ATC firmware sending ads every 10s, but MQTT messages I receive come every minute or so.

DigiH commented 7 months ago

Hi @jacol-code

There are two different scanning intervals for passive and active scanning

TimeBtwRead - interval between passive scans https://github.com/1technophile/OpenMQTTGateway/blob/development/main/config_BT.h#L103

and TimeBtwActive - interval between active scans https://github.com/1technophile/OpenMQTTGateway/blob/development/main/config_BT.h#L84

hence your

… come every minute or so

as the default for both is 55 seconds.

So you can adjust these separately to your requirements.

Hope this clarifies things.