Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: RTC calibration #72

Open
Deoptim opened this issue Oct 24, 2024 · 7 comments
Open

Feature Request: RTC calibration #72

Deoptim opened this issue Oct 24, 2024 · 7 comments

Comments

@Deoptim
Copy link

Deoptim commented Oct 24, 2024

Здравствуйте.
У меня два датчика BTH01, один Outside (на улице) диапазон +40 до -10 , другой в Freezer chamber (в морозилке) от -19 до -28.
Вроде бы кварц или сама микросхема должны показывать разное время отставания, но в принципе они примерно на одном уровне.
На скриншотах ниже оно показывает отставание за месяц.

Screenshot_2024-10-24-13-53-55-933_com android chrome
Screenshot_2024-10-24-13-55-43-711_com android chrome

Хотелось бы чтобы появилась функция ввода коэффициента калибровки RTC (даже желательно сделать чтобы можно было ввести три коэффициента калибровки RTC в зависимости от текущей температуры датчика).

@pvvx
Copy link
Owner

pvvx commented Oct 24, 2024

В программе применен плавающий коэффициент поправки счета времени.
У чипов PHY622x2 существует проблема - сверх большая нестабильность счета RC генератора от напряжения питания.
Некоторые партии PHY62222 невозможно заставить работать от напряжения питания более 2.5В. Возникает уход счета RC генератора несовместимый с параметрами спецификаций BLE для соединения. Стандартный SDK от производителя не справлялся с коррекцией - не работало соединение.
По этим причинам пришлось патчить стандартный SDK от производителя и вписывать другой алгоритм динамической поправки счета RC генератора.
Этот алгоритм плавающий - поправка меняется каждый цикл по мере набора счетчика и в таком варианте говорить о каких-то статических коэффициентах нет смысла.

У меня более десятка устройств на PHY622x2 - работают для теста прошивок с момента первых публикаций альтернативных прошивок. Везде уход часов разный. На некоторых меняется со временем то в плюс, то в минус.

Единственный метод коррекции - периодическое соединение и ввод текущего времени в датчик.
Для этого необходимо написать какой скрипт или изменить https://esphome.io/components/display/pvvx_mithermometer.html#only-synchronize-the-time-once-a-day
на соединение с другим UUID.

@universam1
Copy link
Contributor

made an automation script for #125

@pvvx
Copy link
Owner

pvvx commented Jan 25, 2025

@universam1

Auto Time Sync
-------------
Program will run for 10 minutes
Status: Scanning for devices...
Status: Connecting to device A4:C1:38:05:4E:0A (attempt 1/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with A4:C1:38:05:4E:0A (attempt 1/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:50:38: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device A4:C1:38:05:4E:0A (attempt 2/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with A4:C1:38:05:4E:0A (attempt 2/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:50:48: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device CGG_11C3B2 (attempt 1/2)...
ERROR: Error with CGG_11C3B2 (attempt 1/2): Could not get GATT services: Unreachable
21:51:08: ERROR: Could not get GATT services: Unreachable
Status: Connecting to device CGG_11C3B2 (attempt 2/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with CGG_11C3B2 (attempt 2/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:51:23: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device CGG_11C3B2 (attempt 1/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with CGG_11C3B2 (attempt 1/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:51:42: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device CGG_11C3B2 (attempt 2/2)...
ERROR: Error with CGG_11C3B2 (attempt 2/2): Could not get GATT services: Unreachable
21:52:04: ERROR: Could not get GATT services: Unreachable
Status: Connecting to device TH5_F8CA3B (attempt 1/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with TH5_F8CA3B (attempt 1/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:52:15: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device TH5_F8CA3B (attempt 2/2)...
ERROR: Error with TH5_F8CA3B (attempt 2/2): Connection process timed out
21:52:52: ERROR: Connection process timed out
Status: Connecting to device TH5_F8CA3B (attempt 1/2)...
ERROR: Error with TH5_F8CA3B (attempt 1/2): Could not get GATT services: Unreachable
21:53:08: ERROR: Could not get GATT services: Unreachable
Status: Connecting to device TH5_F8CA3B (attempt 2/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with TH5_F8CA3B (attempt 2/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:53:18: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device TH05D-50B5B9 (attempt 1/2)...
ERROR: Error with TH05D-50B5B9 (attempt 1/2): Could not get GATT services: Unreachable
21:53:37: ERROR: Could not get GATT services: Unreachable
Status: Connecting to device TH05D-50B5B9 (attempt 2/2)...
ERROR: Error with TH05D-50B5B9 (attempt 2/2): Could not get GATT services: Unreachable
21:53:54: ERROR: Could not get GATT services: Unreachable
Status: Connecting to device TH05D-50B5B9 (attempt 1/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
Status: Connected to TH05D-50B5B9
21:54:05: Device time: 2025-01-26 01:22:45
21:54:05: Time difference: 1720 seconds
21:54:05: Time difference > 3s, updating device time...
21:54:05: Time set successfully
21:54:05: Time updated, disconnecting...
Status: Connecting to device TH5_14A68A (attempt 1/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with TH5_14A68A (attempt 1/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:54:13: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device TH5_14A68A (attempt 2/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with TH5_14A68A (attempt 2/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:54:37: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:54:41: Skipping TH5_14A68A - processed recently
Status: Connecting to device CGG_145087 (attempt 1/2)...
ERROR: Error with CGG_145087 (attempt 1/2): Could not get GATT services: Unreachable
21:54:56: ERROR: Could not get GATT services: Unreachable
Status: Connecting to device CGG_145087 (attempt 2/2)...
ERROR: Error with CGG_145087 (attempt 2/2): Could not get GATT services: Unreachable
21:55:14: ERROR: Could not get GATT services: Unreachable
Status: Connecting to device CGG_145087 (attempt 1/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with CGG_145087 (attempt 1/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:55:19: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device CGG_145087 (attempt 2/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with CGG_145087 (attempt 2/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:55:33: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device THSz36D56B (attempt 1/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with THSz36D56B (attempt 1/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:55:49: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device THSz36D56B (attempt 2/2)...
ERROR: Error with THSz36D56B (attempt 2/2): Could not get GATT services: Unreachable
21:56:10: ERROR: Could not get GATT services: Unreachable
Status: Connecting to device THSz36D56B (attempt 1/2)...
Status: Connected, discovering services...
Status: Setting up notifications...
ERROR: Error with THSz36D56B (attempt 1/2): Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
21:56:28: ERROR: Characteristic 0000fff4-0000-1000-8000-00805f9b34fb was not found!
Status: Connecting to device THSz36D56B (attempt 2/2)...

Shutting down due to user interrupt
21:56:44: Shutting down...
21:56:44: 10 minute runtime completed

OS Windows 10.
I can see more than 40 BLE with custom firmware through this adapter.
https://github.com/pvvx/ATC_MiThermometer have the same protocol but different UUID for commands.
PrimaryService: 00001f10-0000-1000-8000-00805f9b34fb
Characteristic: 00001f1f-0000-1000-8000-00805f9b34fb

And another ten BLE devices work in "LE Long Range" mode - Bluetooth core 5.0+.
Bluetooth core 5.0 Bleak does not support. Only BT 4.2 from 2014 :)

@universam1
Copy link
Contributor

go ahead and adjust as needed, I can only test the THB2 devices for now

@pvvx
Copy link
Owner

pvvx commented Jan 26, 2025

go ahead and adjust as needed, I can only test the THB2 devices for now

With this approach, this script will drain all batteries of other devices.
Such a connection with a BLE device creates a large peak in consumption at the very beginning of the connection.
This is caused by Bleak reading the entire UUID table from the device with a short polling interval.
Plus, if the device has a PIN code, then starting the connection causes even more battery consumption.

In this regard, I do not recommend using this script.

A complete rework of the algorithm and a departure from Bleak is required.
BT 5.0+ has a "fast connection". The script should also create a list of supported devices and, when connecting, not request the UUID table, but immediately access the index number of the stored UUID. Such a connection is processed in a few ms.
For these reasons, forget about Bleak as a nightmare example of API design.

An example of BLE scanning for Linux is given here - https://github.com/frawau/aioblescan.

@universam1
Copy link
Contributor

I don't see the need to save a few ms runtime when the script is run once a month or so, but if there is a portable pkg alternative available fine with me

@pvvx
Copy link
Owner

pvvx commented Jan 26, 2025

Firstly, not a few ms, but tens of seconds.
And secondly, it consumes energy from all devices with its active scanning and even more from those working in the BTHome format.
Thirdly, when the CR2032 battery is discharged, there may not be enough energy for such a connection. But the device can still transmit BLE advertising with data for BTHome for months.
Do you need to provide a consumption chart of the devices this script is trying to connect to?
And comparison with the correct connection?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants