Skip to content

Commit b35b26a

Browse files
author
omk
committed
Init
1 parent 5fe41ed commit b35b26a

37 files changed

+2485
-0
lines changed

.clang-format

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BasedOnStyle: LLVM
2+
ColumnLimit: 0 # Отключает ограничение на длину строки
3+
BreakStringLiterals: false # Не разбивать строки внутри макросов
4+
AlignEscapedNewlines: Left

.gitignore

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.pio
2+
.vscode
3+
.vscode/.browse.c_cpp.db*
4+
.vscode/c_cpp_properties.json
5+
.vscode/launch.json
6+
.vscode/ipch
7+
8+
# ESP-IDF default build directory name
9+
build
10+
build/
11+
build_*/
12+
sdkconfig
13+
sdkconfig.old
14+
sdkconfig.esp*
15+
dependencies.lock
16+
managed_components
17+
__pycache__/
18+
out/
19+
_build/

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "components/bl09xx"]
2+
path = components/bl09xx
3+
url = [email protected]:alex-omk/bl09xx.git

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.16.0)
2+
3+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
4+
5+
idf_build_set_property(MINIMAL_BUILD ON)
6+
project(airWick)

include/README

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

lib/README

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

links.txt

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
https://github.com/darkxst/silabs-firmware-builder/tree/main/firmware_builds/zbdonglee
2+
https://www.youtube.com/watch?v=gUtGoYhhkjE
3+
https://github.com/kvazis/training/tree/master/lessons/articles/Elelabs%20EFR32MG21%20update
4+
5+
6+
7+
https://github.com/espressif/esp-idf/issues/10662
8+
9+
https://github.com/espressif/esp-zigbee-sdk/issues/328
10+
11+
https://github.com/espressif/esp-idf/issues/11943
12+
13+
https://github.com/xmow49/ESP32H2-Zigbee-Demo/blob/main/main/esp_zb_light.c
14+
https://github.com/luar123/esphome_zb_sensor/blob/master/esp_zb_light.c
15+
16+
https://community.home-assistant.io/t/esp32-h2-having-2-zigbee-endpoints/614489
17+
18+
https://github.com/espressif/esp-zigbee-sdk/blob/796eb801a26f973f33a191d5aeb13966dabf10bf/examples/esp_zigbee_customized_devices/customized_server/main/esp_HA_customized_light.c
19+
20+
https://github.com/espressif/esp-zigbee-sdk/issues/407
21+
https://github.com/espressif/esp-zigbee-sdk/issues/295
22+
https://github.com/espressif/esp-zigbee-sdk/issues/34
23+
http://esp32.io/viewtopic.php?t=34688
24+
25+
# Zigbee OTA
26+
https://github.com/espressif/esp-zigbee-sdk/blob/main/examples/esp_zigbee_ota/ota_client/main/esp_ota_client.c
27+
https://github.com/lmahmutov/esp32_c6_co2_sensor/blob/main/main/esp_zigbee_co2.c#L474
28+
https://github.com/espressif/esp-zigbee-sdk/issues/146
29+
30+
#Z2M OTA img index
31+
https://github.com/Koenkk/zigbee-OTA/blob/master/index.json
32+
33+
#Z2M
34+
https://github.com/Koenkk/zigbee2mqtt.io/blob/master/docs/externalConvertersExample/switch.js
35+
https://gist.github.com/Koenkk/8e665bf0c40a5c39a9dda421e1ba4345
36+
https://github.com/Koenkk/zigbee2mqtt/issues/20841
37+
https://github.com/Koenkk/zigbee-herdsman-converters/blob/master/src/devices/tuya.ts
38+
https://github.com/Koenkk/zigbee2mqtt/issues/18750
39+
https://github.com/Koenkk/zigbee2mqtt/issues/22076
40+
https://github.com/Koenkk/zigbee2mqtt/issues/14369
41+
https://github.com/Koenkk/zigbee2mqtt/issues/5960
42+
https://gist.github.com/Koenkk/159904dbc760446e39091c4b14f10a9a
43+
44+
#ZB privilege_command
45+
https://github.com/espressif/esp-zigbee-sdk/issues/212
46+
47+
#ZB genPOWER
48+
https://github.com/espressif/esp-zigbee-sdk/issues/77

main/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This file was automatically generated for projects
2+
# without default 'CMakeLists.txt' file.
3+
4+
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/main/*.*)
5+
6+
idf_component_register(SRCS ${app_sources}
7+
PRIV_REQUIRES nvs_flash app_update esp_timer freertos ieee802154 esp_adc
8+
)

main/airWick.c

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "common.h"
2+
3+
#ifdef USE_AIR_WICK
4+
5+
6+
#endif

main/airWick.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#pragma once

main/battery.c

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#include "common.h"
2+
3+
#ifdef USE_BATTERY_MOD
4+
5+
#include "battery.h"
6+
#include "ha/esp_zigbee_ha_standard.h"
7+
#include "tools.h"
8+
#include "zcl/esp_zigbee_zcl_power_config.h"
9+
10+
static const char *TAG = "ESP_ZB_BATT";
11+
12+
#include "esp_adc/adc_cali.h"
13+
#include "esp_adc/adc_cali_scheme.h"
14+
#include "esp_adc/adc_oneshot.h"
15+
#include "soc/soc_caps.h"
16+
17+
#define ADC_CHANNEL ADC_CHANNEL_0
18+
#define ADC_ATTEN ADC_ATTEN_DB_12
19+
20+
static bool adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, adc_cali_handle_t *out_handle);
21+
static void adc_calibration_deinit(adc_cali_handle_t handle);
22+
23+
adc_cali_handle_t adc1_cali_chan0_handle = NULL;
24+
25+
adc_oneshot_unit_handle_t adc1_handle;
26+
27+
int adc_raw;
28+
float battery_voltage;
29+
int battery_millivolts;
30+
31+
int64_t last_battery_measurement_time = 0;
32+
33+
bool do_calibration1_chan0;
34+
35+
void batteryUpdate(void) {
36+
batterySetup();
37+
batteryReadVolts();
38+
batteryPercentage();
39+
}
40+
41+
void batterySetup(void) {
42+
ESP_LOGI(TAG, "Setup ADC");
43+
44+
adc_oneshot_unit_init_cfg_t init_config1 = {
45+
.unit_id = ADC_UNIT_1,
46+
};
47+
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
48+
49+
adc_oneshot_chan_cfg_t config = {
50+
.atten = ADC_ATTEN,
51+
.bitwidth = ADC_BITWIDTH_DEFAULT,
52+
};
53+
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL, &config));
54+
55+
do_calibration1_chan0 = adc_calibration_init(ADC_UNIT_1, ADC_CHANNEL, ADC_ATTEN, &adc1_cali_chan0_handle);
56+
}
57+
58+
void batteryReadVolts() {
59+
int adc = 0, sample = 0, count = 10;
60+
61+
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL, &adc_raw)); //one blank measurement for stabilization
62+
63+
while (sample < count) {
64+
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL, &adc_raw));
65+
// ESP_LOGI(TAG, "ADC%d Channel[%d] Raw Data: %d", ADC_UNIT_1 + 1, ADC_CHANNEL, adc_raw);
66+
adc += adc_raw;
67+
sample++;
68+
}
69+
70+
int adc_avg = adc / count;
71+
72+
ESP_LOGI(TAG, "ADC%d Channel[%d] Raw Data: %d", ADC_UNIT_1 + 1, ADC_CHANNEL, adc_avg);
73+
74+
if (do_calibration1_chan0) {
75+
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_chan0_handle, adc_avg, &battery_millivolts));
76+
// ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_chan0_handle, adc_raw, &battery_millivolts));
77+
ESP_LOGI(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", ADC_UNIT_1 + 1, ADC_CHANNEL, battery_millivolts);
78+
battery_millivolts = battery_millivolts * 2; // divider
79+
battery_voltage = battery_millivolts / 1000.0;
80+
81+
last_battery_measurement_time = millis();
82+
ESP_LOGW(TAG, "LAST READ BATT %d millis", (int)last_battery_measurement_time);
83+
}
84+
85+
ESP_ERROR_CHECK(adc_oneshot_del_unit(adc1_handle));
86+
if (do_calibration1_chan0) {
87+
adc_calibration_deinit(adc1_cali_chan0_handle);
88+
}
89+
}
90+
91+
void batteryPercentage(void) {
92+
93+
// 0% 3.2v
94+
// 100% 4.2v
95+
96+
uint8_t percentage = 0;
97+
if (battery_voltage < 3.2) {
98+
percentage = 0;
99+
} else if (battery_voltage > 4.1) {
100+
percentage = 100;
101+
} else {
102+
percentage = (uint8_t)(((battery_voltage - 3.2f) / (4.2f - 3.2f)) * 100);
103+
// percentage = (uint8_t)((battery_voltage - 3.2) * 100 / 2.5);
104+
}
105+
106+
ESP_LOGI(TAG, "vIN: %.3f, percentage: %d", battery_voltage, percentage);
107+
percentage = percentage * 2; // zigbee scale
108+
uint8_t r_state = 0;
109+
esp_zb_zcl_status_t status = esp_zb_zcl_set_attribute_val(HA_ENDPOINT, ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_PERCENTAGE_REMAINING_ID, &percentage, false);
110+
if (status != ESP_ZB_ZCL_STATUS_SUCCESS) {
111+
ESP_LOGE(TAG, "Setting ON_OFF for CH%d attribute failed!", 0);
112+
}
113+
}
114+
115+
static void adc_calibration_deinit(adc_cali_handle_t handle) {
116+
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
117+
ESP_LOGI(TAG, "deregister %s calibration scheme", "Curve Fitting");
118+
ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));
119+
120+
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
121+
ESP_LOGI(TAG, "deregister %s calibration scheme", "Line Fitting");
122+
ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle));
123+
#endif
124+
}
125+
126+
static bool adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, adc_cali_handle_t *out_handle) {
127+
128+
adc_cali_handle_t handle = NULL;
129+
esp_err_t ret = ESP_FAIL;
130+
bool calibrated = false;
131+
132+
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
133+
if (!calibrated) {
134+
ESP_LOGI(TAG, "calibration scheme version is %s", "Curve Fitting");
135+
adc_cali_curve_fitting_config_t cali_config = {
136+
.unit_id = unit,
137+
.chan = channel,
138+
.atten = atten,
139+
.bitwidth = ADC_BITWIDTH_DEFAULT,
140+
};
141+
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
142+
if (ret == ESP_OK) {
143+
calibrated = true;
144+
}
145+
}
146+
#endif
147+
148+
#if ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
149+
if (!calibrated) {
150+
ESP_LOGI(TAG, "calibration scheme version is %s", "Line Fitting");
151+
adc_cali_line_fitting_config_t cali_config = {
152+
.unit_id = unit,
153+
.atten = atten,
154+
.bitwidth = ADC_BITWIDTH_DEFAULT,
155+
};
156+
ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle);
157+
if (ret == ESP_OK) {
158+
calibrated = true;
159+
}
160+
}
161+
#endif
162+
163+
*out_handle = handle;
164+
if (ret == ESP_OK) {
165+
ESP_LOGI(TAG, "Calibration Success");
166+
} else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) {
167+
ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
168+
} else {
169+
ESP_LOGE(TAG, "Invalid arg or no memory");
170+
}
171+
172+
return calibrated;
173+
}
174+
175+
#endif

main/battery.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include "common.h"
4+
5+
#ifdef USE_BATTERY_MOD
6+
7+
extern int adc_raw;
8+
extern int voltage;
9+
extern int64_t last_battery_measurement_time;
10+
11+
void batteryUpdate(void);
12+
void batterySetup(void);
13+
void batteryReadVolts();
14+
void batteryPercentage(void);
15+
16+
// void adc_calibration_deinit(adc_cali_handle_t handle);
17+
18+
// void battery_task(void *pvParameters);
19+
20+
#endif

0 commit comments

Comments
 (0)