Version 1.0.0
Initial version.
This commit is contained in:
commit
db2a33d47f
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
.DS_Store
|
||||
.vscode
|
||||
build
|
||||
sdkconfig
|
||||
sdkconfig.old
|
17
CMakeLists.txt
Normal file
17
CMakeLists.txt
Normal file
@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
if(${IDF_TARGET} STREQUAL esp8266)
|
||||
project(zh_espnow_sensor_esp8266)
|
||||
elseif(${IDF_TARGET} STREQUAL esp32)
|
||||
project(zh_espnow_sensor_esp32)
|
||||
elseif(${IDF_TARGET} STREQUAL esp32c2)
|
||||
project(zh_espnow_sensor_esp32c2)
|
||||
elseif(${IDF_TARGET} STREQUAL esp32c3)
|
||||
project(zh_espnow_sensor_esp32c3)
|
||||
elseif(${IDF_TARGET} STREQUAL esp32c6)
|
||||
project(zh_espnow_sensor_esp32c6)
|
||||
elseif(${IDF_TARGET} STREQUAL esp32s2)
|
||||
project(zh_espnow_sensor_esp32s2)
|
||||
elseif(${IDF_TARGET} STREQUAL esp32s3)
|
||||
project(zh_espnow_sensor_esp32s3)
|
||||
endif()
|
73
LICENSE.txt
Normal file
73
LICENSE.txt
Normal file
@ -0,0 +1,73 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
2
Makefile
Normal file
2
Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
PROJECT_NAME := zh_espnow_sensor_esp8266
|
||||
include $(IDF_PATH)/make/project.mk
|
77
README.md
Normal file
77
README.md
Normal file
@ -0,0 +1,77 @@
|
||||
# ESP-NOW sensor
|
||||
|
||||
ESP-NOW based sensor for ESP32 ESP-IDF and ESP8266 RTOS SDK.
|
||||
|
||||
## Tested on
|
||||
|
||||
1. ESP8266 RTOS_SDK v3.4
|
||||
2. ESP32 ESP-IDF v5.2
|
||||
|
||||
## Features
|
||||
|
||||
1. Supported sensors:
|
||||
1. DS18B20
|
||||
2. DHT11/22
|
||||
2. Support sensor power management.
|
||||
3. Automatically adds sensor configuration to Home Assistan via MQTT discovery as a sensor.
|
||||
4. Update firmware from HTTPS server via ESP-NOW.
|
||||
5. Direct or mesh work mode.
|
||||
6. Support of external or battery powering.
|
||||
|
||||
## Notes
|
||||
|
||||
1. Work mode must be same with [gateway](https://github.com/aZholtikov/zh_gateway) work mode.
|
||||
2. ESP-NOW mesh network based on the [zh_network](https://github.com/aZholtikov/zh_network).
|
||||
3. For initial settings use "menuconfig -> ZH ESP-NOW Sensor Configuration". After first boot all settings will be stored in NVS memory for prevente change during OTA firmware update.
|
||||
4. Only one sensor can be used at a time.
|
||||
5. To restart the sensor, send the "restart" command to the root topic of the sensor (example - "homeassistant/espnow_sensor/24-62-AB-F9-1F-A8").
|
||||
6. To update the sensor firmware, send the "update" command to the root topic of the sensor (example - "homeassistant/espnow_sensor/70-03-9F-44-BE-F7"). The update path should be like as "https://your_server/zh_espnow_sensor_esp32.bin" (for ESP32) or "https://your_server/zh_espnow_sensor_esp8266.app1.bin + https://your_server/zh_espnow_sensor_esp8266.app2.bin" (for ESP8266). The time and success of the update depends on the load on WiFi channel 1. Average update time is up to five minutes. The online status of the update is displayed in the root sensor topic.
|
||||
7. To change initial settings of the sensor (except work mode and power selection GPIO), send the X1,X2,X3,X4,X5,X6 command to the hardware topic of the sensor (example - "homeassistant/espnow_sensor/70-03-9F-44-BE-F7/hardware"). The configuration will only be accepted if it does not cause errors. The current configuration status is displayed in the configuration topic of the sensor (example - "homeassistant/espnow_sensor/70-03-9F-44-BE-F7/config").
|
||||
|
||||
MQTT configuration message should filled according to the template "X1,X2,X3,X4,X5,X6". Where:
|
||||
|
||||
1. X1 - Sensor type. 1 for DS18B20, 2 for DHT11, 3 for DHT22.
|
||||
2. X2 - Sensor GPIO number 1 (main pin for 1-wire sensors, SDA pin for I2C sensors). 0 - 48 (according to the module used), 255 if not used.
|
||||
3. X3 - Sensor GPIO number 2 (SCL pin for I2C sensors). 0 - 48 (according to the module used), 255 if not used.
|
||||
4. X4 - Power GPIO number (if using sensor power control). 0 - 48 (according to the module used), 255 if not used.
|
||||
5. X5 - Measurement frequency (sleep time on battery power). 1 - 65536 seconds.
|
||||
6. X6 - Battery powered. 0 if false, 1 if true.
|
||||
|
||||
## Build and flash
|
||||
|
||||
Run the following command to firmware build and flash module:
|
||||
|
||||
```text
|
||||
cd your_projects_folder
|
||||
git clone https://github.com/aZholtikov/zh_espnow_sensor.git
|
||||
cd zh_espnow_sensor
|
||||
```
|
||||
|
||||
For ESP32 family:
|
||||
|
||||
```text
|
||||
idf.py set-target (TARGET) // Optional
|
||||
idf.py menuconfig
|
||||
idf.py build
|
||||
idf.py flash
|
||||
```
|
||||
|
||||
For ESP8266 family:
|
||||
|
||||
```text
|
||||
make menuconfig
|
||||
make
|
||||
make flash
|
||||
```
|
||||
|
||||
## Attention
|
||||
|
||||
1. A gateway is required. For details see [zh_gateway](https://github.com/aZholtikov/zh_gateway).
|
||||
2. Use the "make ota" command instead of "make" to prepare 2 bin files for OTA update (for ESP8266).
|
||||
3. For ESP8266 battery powering mode GPIO16 must be connected to RST.
|
||||
4. A battery powering mode sensor power management is highly recommended for greater stability.
|
||||
5. Updates and setting changes are only available in external power mode. To temporarily disable battery mode, enable "menuconfig -> ZH ESP-NOW Sensor Configuration -> Enable power mode selection at startup" and set the "Power selection GPIO number". Connecting this GPIO to GND during startup will temporarily disable the battery mode.
|
||||
|
||||
Thanks to [Rajeev Tandon](https://github.com/rajtan) for some good advice.
|
||||
|
||||
Any [feedback](mailto:github@azholtikov.ru) will be gladly accepted.
|
1
components/zh_config/CMakeLists.txt
Normal file
1
components/zh_config/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
idf_component_register(SRCS "zh_config.c" INCLUDE_DIRS "include")
|
73
components/zh_config/LICENSE
Normal file
73
components/zh_config/LICENSE
Normal file
@ -0,0 +1,73 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
18
components/zh_config/README.md
Normal file
18
components/zh_config/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for all my projects
|
||||
|
||||
## Using
|
||||
|
||||
In an existing project, run the following command to install the component:
|
||||
|
||||
```text
|
||||
cd ../your_project/components
|
||||
git clone http://git.zh.com.ru/alexey.zholtikov/zh_config.git
|
||||
```
|
||||
|
||||
In the application, add the component:
|
||||
|
||||
```c
|
||||
#include "zh_config.h"
|
||||
```
|
||||
|
||||
Any [feedback](mailto:github@azholtikov.ru) will be gladly accepted.
|
0
components/zh_config/component.mk
Normal file
0
components/zh_config/component.mk
Normal file
535
components/zh_config/include/zh_config.h
Normal file
535
components/zh_config/include/zh_config.h
Normal file
@ -0,0 +1,535 @@
|
||||
#pragma once
|
||||
|
||||
#include "stdio.h"
|
||||
#include "stdint.h"
|
||||
#include "stdbool.h"
|
||||
|
||||
#define ZH_HIGH true
|
||||
#define ZH_LOW false
|
||||
|
||||
#define ZH_ON true
|
||||
#define ZH_OFF false
|
||||
|
||||
#define ZH_ONLINE true
|
||||
#define ZH_OFFLINE false
|
||||
|
||||
#define ZH_NOT_USED 0xFF
|
||||
//***********************************************************************************//
|
||||
#define ZH_DEVICE_TYPE \
|
||||
DF(ZHDT_NONE, "") \
|
||||
DF(ZHDT_GATEWAY, "gateway") \
|
||||
DF(ZHDT_SWITCH, "espnow_switch") \
|
||||
DF(ZHDT_LED, "espnow_led") \
|
||||
DF(ZHDT_SENSOR, "espnow_sensor") \
|
||||
DF(ZHDT_RF_GATEWAY, "espnow_rf_gateway") \
|
||||
DF(ZHDT_CONTROL_PANEL, "espnow_control_panel") \
|
||||
DF(ZHDT_DIMMER, "espnow_dimmer") \
|
||||
DF(ZHDT_TERRARIUM, "espnow_terrarium") \
|
||||
DF(ZHDT_RF_SENSOR, "rf_sensor") \
|
||||
DF(ZHDT_BINARY_SENSOR, "espnow_sensor") \
|
||||
DF(ZHDT_MAX, "")
|
||||
|
||||
typedef enum zh_device_type_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
ZH_DEVICE_TYPE
|
||||
#undef DF
|
||||
} zh_device_type_t;
|
||||
|
||||
char *zh_get_device_type_value_name(zh_device_type_t value);
|
||||
//***********************************************************************************//
|
||||
#define ZH_PAYLOAD_TYPE \
|
||||
DF(ZHPT_NONE, "") \
|
||||
DF(ZHPT_ATTRIBUTES, "attributes") \
|
||||
DF(ZHPT_KEEP_ALIVE, "status") \
|
||||
DF(ZHPT_SET, "set") \
|
||||
DF(ZHPT_STATE, "state") \
|
||||
DF(ZHPT_UPDATE, "update") \
|
||||
DF(ZHPT_RESTART, "restart") \
|
||||
DF(ZHPT_SYSTEM, "system") \
|
||||
DF(ZHPT_CONFIG, "config") \
|
||||
DF(ZHPT_FORWARD, "forward") \
|
||||
DF(ZHPT_UPDATE_BEGIN, "") \
|
||||
DF(ZHPT_UPDATE_PROGRESS, "") \
|
||||
DF(ZHPT_UPDATE_END, "") \
|
||||
DF(ZHPT_UPDATE_ERROR, "") \
|
||||
DF(ZHPT_UPDATE_FAIL, "") \
|
||||
DF(ZHPT_UPDATE_SUCCESS, "") \
|
||||
DF(ZHPT_BRIGHTNESS, "brightness") \
|
||||
DF(ZHPT_TEMPERATURE, "temperature") \
|
||||
DF(ZHPT_RGB, "rgb") \
|
||||
DF(ZHPT_EFFECT, "effect") \
|
||||
DF(ZHPT_HARDWARE, "hardware") \
|
||||
DF(ZHPT_MAX, "")
|
||||
|
||||
typedef enum zh_payload_type_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
ZH_PAYLOAD_TYPE
|
||||
#undef DF
|
||||
} zh_payload_type_t;
|
||||
|
||||
char *zh_get_payload_type_value_name(zh_payload_type_t value);
|
||||
//***********************************************************************************//
|
||||
#define HA_COMPONENT_TYPE \
|
||||
DF(HACT_NONE, "") \
|
||||
DF(HACT_ALARM_CONTROL_PANEL, "alarm_control_panel") \
|
||||
DF(HACT_BINARY_SENSOR, "binary_sensor") \
|
||||
DF(HACT_BUTTON, "button") \
|
||||
DF(HACT_CAMERA, "camera") \
|
||||
DF(HACT_COVER, "cover") \
|
||||
DF(HACT_DEVICE_TRACKER, "device_tracker") \
|
||||
DF(HACT_FAN, "fan") \
|
||||
DF(HACT_HUMIDIFIER, "humidifier") \
|
||||
DF(HACT_CLIMATE_HVAC, "climate") \
|
||||
DF(HACT_LIGHT, "light") \
|
||||
DF(HACT_LOCK, "lock") \
|
||||
DF(HACT_NUMBER, "number") \
|
||||
DF(HACT_SCENE, "scene") \
|
||||
DF(HACT_SELECT, "select") \
|
||||
DF(HACT_SENSOR, "sensor") \
|
||||
DF(HACT_SIREN, "siren") \
|
||||
DF(HACT_SWITCH, "switch") \
|
||||
DF(HACT_UPDATE, "update") \
|
||||
DF(HACT_TEXT, "text") \
|
||||
DF(HACT_VACUUM, "vacuum") \
|
||||
DF(HACT_MAX, "")
|
||||
|
||||
typedef enum ha_component_type_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
HA_COMPONENT_TYPE
|
||||
#undef DF
|
||||
} ha_component_type_t;
|
||||
|
||||
char *zh_get_component_type_value_name(ha_component_type_t value);
|
||||
//***********************************************************************************//
|
||||
#define HA_BINARY_SENSOR_DEVICE_CLASS \
|
||||
DF(HABSDC_NONE, "") \
|
||||
DF(HABSDC_BATTERY, "battery") \
|
||||
DF(HABSDC_BATTERY_CHARGING, "battery_charging") \
|
||||
DF(HABSDC_CARBON_MONOXIDE, "carbon_monoxide") \
|
||||
DF(HABSDC_COLD, "cold") \
|
||||
DF(HABSDC_CONNECTIVITY, "connectivity") \
|
||||
DF(HABSDC_DOOR, "door") \
|
||||
DF(HABSDC_GARAGE_DOOR, "garage_door") \
|
||||
DF(HABSDC_GAS, "gas") \
|
||||
DF(HABSDC_HEAT, "heat") \
|
||||
DF(HABSDC_LIGHT, "light") \
|
||||
DF(HABSDC_LOCK, "lock") \
|
||||
DF(HABSDC_MOISTURE, "moisture") \
|
||||
DF(HABSDC_MOTION, "motion") \
|
||||
DF(HABSDC_MOVING, "moving") \
|
||||
DF(HABSDC_OCCUPANCY, "occupancy") \
|
||||
DF(HABSDC_OPENING, "opening") \
|
||||
DF(HABSDC_PLUG, "plug") \
|
||||
DF(HABSDC_POWER, "power") \
|
||||
DF(HABSDC_PRESENCE, "presence") \
|
||||
DF(HABSDC_PROBLEM, "problem") \
|
||||
DF(HABSDC_RUNNING, "running") \
|
||||
DF(HABSDC_SAFETY, "safety") \
|
||||
DF(HABSDC_SMOKE, "smoke") \
|
||||
DF(HABSDC_SOUND, "sound") \
|
||||
DF(HABSDC_TAMPER, "tamper") \
|
||||
DF(HABSDC_UPDATE, "update") \
|
||||
DF(HABSDC_VIBRATION, "vibration") \
|
||||
DF(HABSDC_WINDOW, "window") \
|
||||
DF(HABSDC_MAX, "")
|
||||
|
||||
typedef enum ha_binary_sensor_device_class_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
HA_BINARY_SENSOR_DEVICE_CLASS
|
||||
#undef DF
|
||||
} ha_binary_sensor_device_class_t;
|
||||
|
||||
char *zh_get_binary_sensor_device_class_value_name(ha_binary_sensor_device_class_t value);
|
||||
//***********************************************************************************//
|
||||
#define HA_COVER_DEVICE_CLASS \
|
||||
DF(HACDC_NONE, "") \
|
||||
DF(HACDC_AWNING, "awning") \
|
||||
DF(HACDC_BLIND, "blind") \
|
||||
DF(HACDC_CURTAIN, "curtain") \
|
||||
DF(HACDC_DAMPER, "damper") \
|
||||
DF(HACDC_DOOR, "door") \
|
||||
DF(HACDC_GARAGE, "garage") \
|
||||
DF(HACDC_GATE, "gate") \
|
||||
DF(HACDC_SHADE, "shade") \
|
||||
DF(HACDC_SHUTTER, "shutter") \
|
||||
DF(HACDC_WINDOW, "window") \
|
||||
DF(HACDC_MAX, "")
|
||||
|
||||
typedef enum ha_cover_device_class_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
HA_COVER_DEVICE_CLASS
|
||||
#undef DF
|
||||
} ha_cover_device_class_t;
|
||||
|
||||
char *zh_get_cover_device_class_value_name(ha_cover_device_class_t value);
|
||||
//***********************************************************************************//
|
||||
#define HA_SENSOR_DEVICE_CLASS \
|
||||
DF(HASDC_NONE, "") \
|
||||
DF(HASDC_APPARENT_POWER, "apparent_power") \
|
||||
DF(HASDC_AQI, "aqi") \
|
||||
DF(HASDC_BATTERY, "battery") \
|
||||
DF(HASDC_CARBON_DIOXIDE, "carbon_dioxide") \
|
||||
DF(HASDC_CARBON_MONOXIDE, "carbon_monoxide") \
|
||||
DF(HASDC_CURRENT, "current") \
|
||||
DF(HASDC_DATE, "date") \
|
||||
DF(HASDC_DISTANCE, "distance") \
|
||||
DF(HASDC_DURATION, "duration") \
|
||||
DF(HASDC_ENERGY, "energy") \
|
||||
DF(HASDC_FREQUENCY, "frequency") \
|
||||
DF(HASDC_GAS, "gas") \
|
||||
DF(HASDC_HUMIDITY, "humidity") \
|
||||
DF(HASDC_ILLUMINANCE, "illuminance") \
|
||||
DF(HASDC_MOISTURE, "moisture") \
|
||||
DF(HASDC_MONETARY, "monetar") \
|
||||
DF(HASDC_NITROGEN_DIOXIDE, "nitrogen_dioxide") \
|
||||
DF(HASDC_NITROGEN_MONOXIDE, "nitrogen_monoxide") \
|
||||
DF(HASDC_NITROUS_OXIDE, "nitrous_oxide") \
|
||||
DF(HASDC_OZONE, "ozone") \
|
||||
DF(HASDC_PM1, "pm1") \
|
||||
DF(HASDC_PM10, "pm10") \
|
||||
DF(HASDC_PM25, "pm25") \
|
||||
DF(HASDC_POWER_FACTOR, "power_factor") \
|
||||
DF(HASDC_POWER, "power") \
|
||||
DF(HASDC_PRECIPITATION, "precipitation") \
|
||||
DF(HASDC_PRECIPITATION_INTENSITY, "precipitation_intensity") \
|
||||
DF(HASDC_PRESSURE, "pressure") \
|
||||
DF(HASDC_REACTIVE_POWER, "reactive_power") \
|
||||
DF(HASDC_SIGNAL_STRENGTH, "signal_strength") \
|
||||
DF(HASDC_SPEED, "speed") \
|
||||
DF(HASDC_SULPHUR_DIOXIDE, "sulphur_dioxide") \
|
||||
DF(HASDC_TEMPERATURE, "temperature") \
|
||||
DF(HASDC_TIMESTAMP, "timestamp") \
|
||||
DF(HASDC_VOLATILE_ORGANIC_COMPOUNDS, "volatile_organic_compounds") \
|
||||
DF(HASDC_VOLTAGE, "voltage") \
|
||||
DF(HASDC_VOLUME, "volume") \
|
||||
DF(HASDC_WATER, "water") \
|
||||
DF(HASDC_WEIGHT, "weight") \
|
||||
DF(HASDC_WIND_SPEED, "wind_speed") \
|
||||
DF(HASDC_MAX, "")
|
||||
|
||||
typedef enum ha_sensor_device_class_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
HA_SENSOR_DEVICE_CLASS
|
||||
#undef DF
|
||||
} ha_sensor_device_class_t;
|
||||
|
||||
char *zh_get_sensor_device_class_value_name(ha_sensor_device_class_t value);
|
||||
//***********************************************************************************//
|
||||
#define HA_SWITCH_DEVICE_CLASS \
|
||||
DF(HASWDC_NONE, "") \
|
||||
DF(HASWDC_OUTLET, "outlet") \
|
||||
DF(HASWDC_SWITCH, "switch") \
|
||||
DF(HASWDC_MAX, "")
|
||||
|
||||
typedef enum ha_switch_device_class_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
HA_SWITCH_DEVICE_CLASS
|
||||
#undef DF
|
||||
} ha_switch_device_class_t;
|
||||
|
||||
char *zh_get_switch_device_class_value_name(ha_switch_device_class_t value);
|
||||
//***********************************************************************************//
|
||||
#define HA_ON_OFF_TYPE \
|
||||
DF(HAONOFT_NONE, "") \
|
||||
DF(HAONOFT_ON, "ON") \
|
||||
DF(HAONOFT_OFF, "OFF") \
|
||||
DF(HAONOFT_OPEN, "OPEN") \
|
||||
DF(HAONOFT_CLOSE, "CLOSE") \
|
||||
DF(HAONOFT_LOW, "LOW") \
|
||||
DF(HAONOFT_MID, "MID") \
|
||||
DF(HAONOFT_HIGH, "HIGH") \
|
||||
DF(HAONOFT_ALARM, "ALARM") \
|
||||
DF(HAONOFT_DRY, "DRY") \
|
||||
DF(HAONOFT_MOTION, "MOTION") \
|
||||
DF(HAONOFT_CONNECT, "CONNECT") \
|
||||
DF(HAONOFT_LEAKAGE, "LEAKAGE") \
|
||||
DF(HAONOFT_MAX, "")
|
||||
|
||||
typedef enum ha_on_off_type_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
HA_ON_OFF_TYPE
|
||||
#undef DF
|
||||
} ha_on_off_type_t;
|
||||
|
||||
char *zh_get_on_off_type_value_name(ha_on_off_type_t value);
|
||||
//***********************************************************************************//
|
||||
#define HA_CHIP_TYPE \
|
||||
DF(HACHT_NONE, "") \
|
||||
DF(HACHT_ESP32, "ESP32") \
|
||||
DF(HACHT_ESP8266, "ESP8266") \
|
||||
DF(HACHT_ESP32S2, "ESP32-S2") \
|
||||
DF(HACHT_ESP32S3, "ESP32-S3") \
|
||||
DF(HACHT_ESP32C2, "ESP32-C2") \
|
||||
DF(HACHT_ESP32C3, "ESP32-C3") \
|
||||
DF(HACHT_ESP32C6, "ESP32-C6") \
|
||||
DF(HACHT_MAX, "")
|
||||
|
||||
typedef enum ha_chip_type_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
HA_CHIP_TYPE
|
||||
#undef DF
|
||||
} ha_chip_type_t;
|
||||
|
||||
char *zh_get_chip_type_value_name(ha_chip_type_t value);
|
||||
//***********************************************************************************//
|
||||
#define HA_LED_EFFECT_TYPE \
|
||||
DF(HALET_NONE, "") \
|
||||
DF(HALET_MAX, "")
|
||||
|
||||
typedef enum ha_led_effect_type_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
HA_LED_EFFECT_TYPE
|
||||
#undef DF
|
||||
} ha_led_effect_type_t;
|
||||
//***********************************************************************************//
|
||||
#define HA_LED_TYPE \
|
||||
DF(HALT_NONE, "") \
|
||||
DF(HALT_W, "W") \
|
||||
DF(HALT_WW, "WW") \
|
||||
DF(HALT_RGB, "RGB") \
|
||||
DF(HALT_RGBW, "RGBW") \
|
||||
DF(HALT_RGBWW, "RGBWW") \
|
||||
DF(HALT_MAX, "")
|
||||
|
||||
typedef enum ha_led_type_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
HA_LED_TYPE
|
||||
#undef DF
|
||||
} ha_led_type_t;
|
||||
|
||||
char *zh_get_led_type_value_name(ha_led_type_t value);
|
||||
//***********************************************************************************//
|
||||
#define HA_SENSOR_TYPE \
|
||||
DF(HAST_NONE, "") \
|
||||
DF(HAST_DS18B20, "DS18B20") \
|
||||
DF(HAST_DHT11, "DHT11") \
|
||||
DF(HAST_DHT22, "DHT22") \
|
||||
DF(HAST_GATEWAY, "GATEWAY") \
|
||||
DF(HAST_WINDOW, "WINDOW") \
|
||||
DF(HAST_DOOR, "DOOR") \
|
||||
DF(HAST_LEAKAGE, "LEAKAGE") \
|
||||
DF(HAST_MAX, "")
|
||||
|
||||
typedef enum ha_sensor_type_t
|
||||
{
|
||||
#define DF(_value, _name) _value,
|
||||
HA_SENSOR_TYPE
|
||||
#undef DF
|
||||
} ha_sensor_type_t;
|
||||
|
||||
char *zh_get_sensor_type_value_name(ha_sensor_type_t value);
|
||||
//***********************************************************************************//
|
||||
typedef struct zh_sensor_config_message_t
|
||||
{
|
||||
uint8_t unique_id;
|
||||
ha_sensor_device_class_t sensor_device_class;
|
||||
char unit_of_measurement[5];
|
||||
uint8_t suggested_display_precision;
|
||||
uint16_t expire_after;
|
||||
bool enabled_by_default;
|
||||
bool force_update;
|
||||
uint8_t qos;
|
||||
bool retain;
|
||||
} __attribute__((packed)) zh_sensor_config_message_t;
|
||||
|
||||
typedef struct zh_sensor_hardware_config_message_t
|
||||
{
|
||||
ha_sensor_type_t sensor_type;
|
||||
uint8_t sensor_pin_1;
|
||||
uint8_t sensor_pin_2;
|
||||
uint8_t power_pin;
|
||||
uint16_t measurement_frequency;
|
||||
bool battery_power;
|
||||
} __attribute__((packed)) zh_sensor_hardware_config_message_t;
|
||||
|
||||
typedef struct zh_binary_sensor_config_message_t
|
||||
{
|
||||
uint8_t unique_id;
|
||||
ha_binary_sensor_device_class_t binary_sensor_device_class;
|
||||
ha_on_off_type_t payload_on;
|
||||
ha_on_off_type_t payload_off;
|
||||
uint16_t expire_after;
|
||||
uint16_t off_delay;
|
||||
bool enabled_by_default;
|
||||
bool force_update;
|
||||
uint8_t qos;
|
||||
bool retain;
|
||||
} __attribute__((packed)) zh_binary_sensor_config_message_t;
|
||||
|
||||
typedef struct zh_led_config_message_t
|
||||
{
|
||||
uint8_t unique_id;
|
||||
ha_led_type_t led_type;
|
||||
ha_on_off_type_t payload_on;
|
||||
ha_on_off_type_t payload_off;
|
||||
bool enabled_by_default;
|
||||
bool optimistic;
|
||||
uint8_t qos;
|
||||
bool retain;
|
||||
} __attribute__((packed)) zh_led_config_message_t;
|
||||
|
||||
typedef struct zh_led_hardware_config_message_t
|
||||
{
|
||||
ha_led_type_t led_type;
|
||||
uint8_t first_white_pin;
|
||||
uint8_t second_white_pin;
|
||||
uint8_t red_pin;
|
||||
uint8_t green_pin;
|
||||
uint8_t blue_pin;
|
||||
} __attribute__((packed)) zh_led_hardware_config_message_t;
|
||||
|
||||
typedef struct zh_switch_config_message_t
|
||||
{
|
||||
uint8_t unique_id;
|
||||
ha_switch_device_class_t device_class;
|
||||
ha_on_off_type_t payload_on;
|
||||
ha_on_off_type_t payload_off;
|
||||
bool enabled_by_default;
|
||||
bool optimistic;
|
||||
uint8_t qos;
|
||||
bool retain;
|
||||
} __attribute__((packed)) zh_switch_config_message_t;
|
||||
|
||||
typedef struct zh_switch_hardware_config_message_t
|
||||
{
|
||||
uint8_t relay_pin;
|
||||
bool relay_on_level;
|
||||
uint8_t led_pin;
|
||||
bool led_on_level;
|
||||
uint8_t int_button_pin;
|
||||
bool int_button_on_level;
|
||||
uint8_t ext_button_pin;
|
||||
bool ext_button_on_level;
|
||||
uint8_t sensor_pin;
|
||||
ha_sensor_type_t sensor_type;
|
||||
} __attribute__((packed)) zh_switch_hardware_config_message_t;
|
||||
//***********************************************************************************//
|
||||
typedef struct zh_sensor_status_message_t
|
||||
{
|
||||
ha_sensor_type_t sensor_type;
|
||||
float temperature;
|
||||
float humidity;
|
||||
float pressure;
|
||||
float quality;
|
||||
float voltage;
|
||||
float reserved_1; // Reserved for future development.
|
||||
float reserved_2; // Reserved for future development.
|
||||
float reserved_3; // Reserved for future development.
|
||||
float reserved_4; // Reserved for future development.
|
||||
float reserved_5; // Reserved for future development.
|
||||
} __attribute__((packed)) zh_sensor_status_message_t;
|
||||
|
||||
typedef struct zh_binary_sensor_status_message_t
|
||||
{
|
||||
ha_sensor_type_t sensor_type;
|
||||
ha_on_off_type_t connect;
|
||||
ha_on_off_type_t open;
|
||||
ha_on_off_type_t battery;
|
||||
ha_on_off_type_t leakage;
|
||||
ha_on_off_type_t reserved_1; // Reserved for future development.
|
||||
ha_on_off_type_t reserved_2; // Reserved for future development.
|
||||
ha_on_off_type_t reserved_3; // Reserved for future development.
|
||||
ha_on_off_type_t reserved_4; // Reserved for future development.
|
||||
ha_on_off_type_t reserved_5; // Reserved for future development.
|
||||
} __attribute__((packed)) zh_binary_sensor_status_message_t;
|
||||
|
||||
typedef struct zh_led_status_message_t
|
||||
{
|
||||
ha_on_off_type_t status;
|
||||
uint8_t brightness;
|
||||
uint16_t temperature;
|
||||
uint8_t red;
|
||||
uint8_t green;
|
||||
uint8_t blue;
|
||||
ha_led_effect_type_t effect; // Reserved for future development.
|
||||
} __attribute__((packed)) zh_led_status_message_t;
|
||||
|
||||
typedef struct zh_switch_status_message_t
|
||||
{
|
||||
ha_on_off_type_t status;
|
||||
} __attribute__((packed)) zh_switch_status_message_t;
|
||||
//***********************************************************************************//
|
||||
typedef struct zh_attributes_message_t
|
||||
{
|
||||
ha_chip_type_t chip_type;
|
||||
ha_sensor_type_t sensor_type;
|
||||
char flash_size[5];
|
||||
uint8_t cpu_frequency;
|
||||
uint32_t heap_size;
|
||||
uint32_t min_heap_size;
|
||||
uint8_t reset_reason;
|
||||
char app_name[32];
|
||||
char app_version[32];
|
||||
uint32_t uptime;
|
||||
} __attribute__((packed)) zh_attributes_message_t;
|
||||
|
||||
typedef struct zh_keep_alive_message_t
|
||||
{
|
||||
bool online_status;
|
||||
uint8_t message_frequency;
|
||||
} __attribute__((packed)) zh_keep_alive_message_t;
|
||||
|
||||
typedef union zh_config_message_t
|
||||
{
|
||||
zh_binary_sensor_config_message_t binary_sensor_config_message;
|
||||
zh_sensor_config_message_t sensor_config_message;
|
||||
zh_sensor_hardware_config_message_t sensor_hardware_config_message;
|
||||
zh_led_config_message_t led_config_message;
|
||||
zh_led_hardware_config_message_t led_hardware_config_message;
|
||||
zh_switch_config_message_t switch_config_message;
|
||||
zh_switch_hardware_config_message_t switch_hardware_config_message;
|
||||
} __attribute__((packed)) zh_config_message_t;
|
||||
|
||||
typedef union zh_status_message_t
|
||||
{
|
||||
zh_binary_sensor_status_message_t binary_sensor_status_message;
|
||||
zh_sensor_status_message_t sensor_status_message;
|
||||
zh_led_status_message_t led_status_message;
|
||||
zh_switch_status_message_t switch_status_message;
|
||||
} __attribute__((packed)) zh_status_message_t;
|
||||
|
||||
typedef struct zh_espnow_ota_message_t
|
||||
{
|
||||
ha_chip_type_t chip_type;
|
||||
char app_name[32];
|
||||
char app_version[32];
|
||||
uint16_t part;
|
||||
uint8_t data_len;
|
||||
uint8_t data[128];
|
||||
} __attribute__((packed)) zh_espnow_ota_message_t;
|
||||
|
||||
typedef struct zh_espnow_ota_data_t
|
||||
{
|
||||
ha_chip_type_t chip_type;
|
||||
zh_device_type_t device_type;
|
||||
char app_name[32];
|
||||
char app_version[32];
|
||||
uint8_t mac_addr[6];
|
||||
} __attribute__((packed)) zh_espnow_ota_data_t;
|
||||
|
||||
typedef union zh_payload_data_t
|
||||
{
|
||||
zh_attributes_message_t attributes_message;
|
||||
zh_keep_alive_message_t keep_alive_message;
|
||||
zh_config_message_t config_message;
|
||||
zh_status_message_t status_message;
|
||||
zh_espnow_ota_message_t espnow_ota_message;
|
||||
} __attribute__((packed)) zh_payload_data_t;
|
||||
|
||||
typedef struct zh_espnow_data_t
|
||||
{
|
||||
zh_device_type_t device_type;
|
||||
zh_payload_type_t payload_type;
|
||||
zh_payload_data_t payload_data;
|
||||
} __attribute__((packed)) zh_espnow_data_t;
|
1
components/zh_config/version.txt
Normal file
1
components/zh_config/version.txt
Normal file
@ -0,0 +1 @@
|
||||
1.11.0
|
177
components/zh_config/zh_config.c
Normal file
177
components/zh_config/zh_config.c
Normal file
@ -0,0 +1,177 @@
|
||||
#include "zh_config.h"
|
||||
|
||||
char *zh_get_device_type_value_name(zh_device_type_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
ZH_DEVICE_TYPE
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *zh_get_payload_type_value_name(zh_payload_type_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
ZH_PAYLOAD_TYPE
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *zh_get_component_type_value_name(ha_component_type_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
HA_COMPONENT_TYPE
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *zh_get_binary_sensor_device_class_value_name(ha_binary_sensor_device_class_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
HA_BINARY_SENSOR_DEVICE_CLASS
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *zh_get_cover_device_class_value_name(ha_cover_device_class_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
HA_COVER_DEVICE_CLASS
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *zh_get_sensor_device_class_value_name(ha_sensor_device_class_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
HA_SENSOR_DEVICE_CLASS
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *zh_get_switch_device_class_value_name(ha_switch_device_class_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
HA_SWITCH_DEVICE_CLASS
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *zh_get_on_off_type_value_name(ha_on_off_type_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
HA_ON_OFF_TYPE
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *zh_get_chip_type_value_name(ha_chip_type_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
HA_CHIP_TYPE
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *zh_get_led_type_value_name(ha_led_type_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
HA_LED_TYPE
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
char *zh_get_sensor_type_value_name(ha_sensor_type_t value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
#define DF(_value, _name) \
|
||||
case _value: \
|
||||
return _name;
|
||||
HA_SENSOR_TYPE
|
||||
#undef DF
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
1
components/zh_dht/CMakeLists.txt
Normal file
1
components/zh_dht/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
idf_component_register(SRCS "zh_dht.c" INCLUDE_DIRS "include" REQUIRES driver)
|
73
components/zh_dht/LICENSE
Normal file
73
components/zh_dht/LICENSE
Normal file
@ -0,0 +1,73 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
47
components/zh_dht/README.md
Normal file
47
components/zh_dht/README.md
Normal file
@ -0,0 +1,47 @@
|
||||
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for DHT11/DHT22(AM2302) humidity & temperature sensor
|
||||
|
||||
## Tested on
|
||||
|
||||
1. ESP8266 RTOS_SDK v3.4
|
||||
2. ESP32 ESP-IDF v5.2
|
||||
|
||||
## [Function description](http://zh-dht.zh.com.ru)
|
||||
|
||||
## Using
|
||||
|
||||
In an existing project, run the following command to install the component:
|
||||
|
||||
```text
|
||||
cd ../your_project/components
|
||||
git clone http://git.zh.com.ru/alexey.zholtikov/zh_dht.git
|
||||
```
|
||||
|
||||
In the application, add the component:
|
||||
|
||||
```c
|
||||
#include "zh_dht.h"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
Reading the sensor:
|
||||
|
||||
```c
|
||||
#include "zh_dht.h"
|
||||
|
||||
void app_main()
|
||||
{
|
||||
zh_dht_handle_t dht_handle = zh_dht_init(ZH_DHT22, GPIO_NUM_5);
|
||||
float humidity;
|
||||
float temperature;
|
||||
for (;;)
|
||||
{
|
||||
zh_dht_read(&dht_handle, &humidity, &temperature);
|
||||
printf("Humidity %0.2f\n", humidity);
|
||||
printf("Temperature %0.2f\n", temperature);
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Any [feedback](mailto:github@azholtikov.ru) will be gladly accepted.
|
0
components/zh_dht/component.mk
Normal file
0
components/zh_dht/component.mk
Normal file
70
components/zh_dht/include/zh_dht.h
Normal file
70
components/zh_dht/include/zh_dht.h
Normal file
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* @file
|
||||
* Header file for the zh_dht component.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stdio.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
/**
|
||||
* @brief Enumeration of supported sensor types.
|
||||
*
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ZH_DHT11, ///< Sensor type DHT11
|
||||
ZH_DHT22 ///< Sensor type DHT22 or AM2302
|
||||
} __attribute__((packed)) zh_dht_sensor_type_t;
|
||||
|
||||
/**
|
||||
* @brief Unique handle of the sensor.
|
||||
*
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t sensor_pin; ///< Sensor GPIO connection
|
||||
zh_dht_sensor_type_t sensor_type; ///< Sensor type
|
||||
} __attribute__((packed)) zh_dht_handle_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize DHT sensor.
|
||||
*
|
||||
* @param[in] sensor_type Sensor type (ZH_DHT11 or ZH_DHT22).
|
||||
* @param[in] sensor_pin Sensor connection gpio.
|
||||
*
|
||||
* @return
|
||||
* - Handle of the sensor.
|
||||
*/
|
||||
zh_dht_handle_t zh_dht_init(const zh_dht_sensor_type_t sensor_type, const uint8_t sensor_pin);
|
||||
|
||||
/**
|
||||
* @brief Read DHT sensor.
|
||||
*
|
||||
* @param[in] dht_handle Pointer for handle of the sensor.
|
||||
* @param[out] humidity Pointer for DHT sensor reading data of humidity.
|
||||
* @param[out] temperature Pointer for DHT sensor reading data of temperature.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if read was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_INVALID_RESPONSE if the bus is busy
|
||||
* - ESP_ERR_TIMEOUT if operation timeout
|
||||
* - ESP_ERR_INVALID_CRC if check CRC is fail
|
||||
*/
|
||||
esp_err_t zh_dht_read(const zh_dht_handle_t *dht_handle, float *humidity, float *temperature);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
1
components/zh_dht/version.txt
Normal file
1
components/zh_dht/version.txt
Normal file
@ -0,0 +1 @@
|
||||
1.2.6
|
168
components/zh_dht/zh_dht.c
Normal file
168
components/zh_dht/zh_dht.c
Normal file
@ -0,0 +1,168 @@
|
||||
/**
|
||||
* @file
|
||||
* The main code of the zh_dht component.
|
||||
*/
|
||||
|
||||
#include "zh_dht.h"
|
||||
|
||||
/// \cond
|
||||
#define BIT_1_TRANSFER_MAX_DURATION 75 // Signal "1" high time.
|
||||
#define BIT_0_TRANSFER_MAX_DURATION 30 // Signal "0" high time.
|
||||
#define DATA_BIT_START_TRANSFER_MAX_DURATION 55 // Signal "0", "1" low time.
|
||||
#define RESPONSE_MAX_DURATION 85 // Response to low time. Response to high time.
|
||||
#define MASTER_RELEASE_MAX_DURATION 200 // Bus master has released time.
|
||||
#define DATA_SIZE 40
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#define esp_delay_us(x) os_delay_us(x)
|
||||
#else
|
||||
#define esp_delay_us(x) esp_rom_delay_us(x)
|
||||
#endif
|
||||
/// \endcond
|
||||
|
||||
static const char *TAG = "zh_dht";
|
||||
|
||||
static esp_err_t _read_bit(const zh_dht_handle_t *dht_handle, bool *bit);
|
||||
|
||||
zh_dht_handle_t zh_dht_init(const zh_dht_sensor_type_t sensor_type, const uint8_t sensor_pin)
|
||||
{
|
||||
ESP_LOGI(TAG, "DHT initialization begin.");
|
||||
zh_dht_handle_t zh_dht_handle;
|
||||
zh_dht_handle.sensor_type = sensor_type;
|
||||
zh_dht_handle.sensor_pin = sensor_pin;
|
||||
gpio_config_t config;
|
||||
config.intr_type = GPIO_INTR_DISABLE;
|
||||
config.mode = GPIO_MODE_INPUT;
|
||||
config.pin_bit_mask = (1ULL << sensor_pin);
|
||||
config.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
config.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
if (gpio_config(&config) != ESP_OK)
|
||||
{
|
||||
zh_dht_handle.sensor_pin = 0xFF;
|
||||
ESP_LOGE(TAG, "DHT initialization fail. Incorrect GPIO number.");
|
||||
return zh_dht_handle;
|
||||
}
|
||||
ESP_LOGI(TAG, "DHT initialization success.");
|
||||
return zh_dht_handle;
|
||||
}
|
||||
|
||||
esp_err_t zh_dht_read(const zh_dht_handle_t *dht_handle, float *humidity, float *temperature)
|
||||
{
|
||||
ESP_LOGI(TAG, "DHT read begin.");
|
||||
if (dht_handle == NULL || humidity == NULL || temperature == NULL || dht_handle->sensor_pin == 0xFF)
|
||||
{
|
||||
ESP_LOGE(TAG, "DHT read fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (gpio_get_level(dht_handle->sensor_pin) != 1)
|
||||
{
|
||||
ESP_LOGE(TAG, "DHT read fail. Bus is busy.");
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
}
|
||||
gpio_set_direction(dht_handle->sensor_pin, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(dht_handle->sensor_pin, 0);
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
gpio_set_level(dht_handle->sensor_pin, 1);
|
||||
gpio_set_direction(dht_handle->sensor_pin, GPIO_MODE_INPUT);
|
||||
uint8_t time = 0;
|
||||
while (gpio_get_level(dht_handle->sensor_pin) == 1)
|
||||
{
|
||||
if (time > MASTER_RELEASE_MAX_DURATION)
|
||||
{
|
||||
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
++time;
|
||||
esp_delay_us(1);
|
||||
}
|
||||
time = 0;
|
||||
while (gpio_get_level(dht_handle->sensor_pin) == 0)
|
||||
{
|
||||
if (time > RESPONSE_MAX_DURATION)
|
||||
{
|
||||
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
++time;
|
||||
esp_delay_us(1);
|
||||
}
|
||||
time = 0;
|
||||
while (gpio_get_level(dht_handle->sensor_pin) == 1)
|
||||
{
|
||||
if (time > RESPONSE_MAX_DURATION)
|
||||
{
|
||||
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
++time;
|
||||
esp_delay_us(1);
|
||||
}
|
||||
uint8_t dht_data[DATA_SIZE / 8] = {0};
|
||||
uint8_t byte_index = 0;
|
||||
uint8_t bit_index = 7;
|
||||
for (uint8_t i = 0; i < 40; ++i)
|
||||
{
|
||||
bool bit = 0;
|
||||
if (_read_bit(dht_handle, &bit) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "DHT read fail. Timeout exceeded.");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
dht_data[byte_index] |= (bit << bit_index);
|
||||
if (bit_index == 0)
|
||||
{
|
||||
bit_index = 7;
|
||||
++byte_index;
|
||||
}
|
||||
else
|
||||
{
|
||||
--bit_index;
|
||||
}
|
||||
}
|
||||
if (dht_data[4] != ((dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3])))
|
||||
{
|
||||
ESP_LOGE(TAG, "DHT read fail. Invalid CRC.");
|
||||
return ESP_ERR_INVALID_CRC;
|
||||
}
|
||||
*humidity = (dht_data[0] << 8 | dht_data[1]) / 10.0;
|
||||
if (dht_handle->sensor_type == ZH_DHT22)
|
||||
{
|
||||
*temperature = ((dht_data[2] & 0b01111111) << 8 | dht_data[3]) / 10.0;
|
||||
if ((dht_data[2] & 0b10000000) != 0)
|
||||
{
|
||||
*temperature *= -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*temperature = (dht_data[2] << 8 | dht_data[3]) / 10.0;
|
||||
}
|
||||
ESP_LOGI(TAG, "DHT read success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t _read_bit(const zh_dht_handle_t *dht_handle, bool *bit)
|
||||
{
|
||||
uint8_t time = 0;
|
||||
while (gpio_get_level(dht_handle->sensor_pin) == 0)
|
||||
{
|
||||
if (time > DATA_BIT_START_TRANSFER_MAX_DURATION)
|
||||
{
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
++time;
|
||||
esp_delay_us(1);
|
||||
}
|
||||
time = 0;
|
||||
while (gpio_get_level(dht_handle->sensor_pin) == 1)
|
||||
{
|
||||
if (time > BIT_1_TRANSFER_MAX_DURATION)
|
||||
{
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
++time;
|
||||
esp_delay_us(1);
|
||||
}
|
||||
*bit = (time > BIT_0_TRANSFER_MAX_DURATION) ? 1 : 0;
|
||||
return ESP_OK;
|
||||
}
|
1
components/zh_ds18b20/CMakeLists.txt
Normal file
1
components/zh_ds18b20/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
idf_component_register(SRCS "zh_ds18b20.c" INCLUDE_DIRS "include" REQUIRES zh_onewire)
|
73
components/zh_ds18b20/LICENSE
Normal file
73
components/zh_ds18b20/LICENSE
Normal file
@ -0,0 +1,73 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
70
components/zh_ds18b20/README.md
Normal file
70
components/zh_ds18b20/README.md
Normal file
@ -0,0 +1,70 @@
|
||||
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for 1-Wire DS18B20 sensor
|
||||
|
||||
## Tested on
|
||||
|
||||
1. ESP8266 RTOS_SDK v3.4
|
||||
2. ESP32 ESP-IDF v5.2
|
||||
|
||||
## Dependencies
|
||||
|
||||
1. [zh_onewire](http://git.zh.com.ru/alexey.zholtikov/zh_onewire.git)
|
||||
|
||||
## [Function description](http://zh-ds18b20.zh.com.ru)
|
||||
|
||||
## Using
|
||||
|
||||
In an existing project, run the following command to install the components:
|
||||
|
||||
```text
|
||||
cd ../your_project/components
|
||||
git clone http://git.zh.com.ru/alexey.zholtikov/zh_onewire.git
|
||||
git clone http://git.zh.com.ru/alexey.zholtikov/zh_ds18b20.git
|
||||
```
|
||||
|
||||
In the application, add the component:
|
||||
|
||||
```c
|
||||
#include "zh_ds18b20.h"
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
One or more 1-Wire DS18B20 sensors on bus:
|
||||
|
||||
```c
|
||||
#include "zh_ds18b20.h"
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
uint8_t *rom;
|
||||
float temperature;
|
||||
zh_onewire_init(GPIO_NUM_5);
|
||||
if (zh_onewire_reset() != ESP_OK)
|
||||
{
|
||||
printf("There are no 1-Wire devices available on the bus.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
zh_onewire_search_rom_init();
|
||||
for (;;)
|
||||
{
|
||||
rom = zh_onewire_search_rom_next();
|
||||
if (rom == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("Found device ROM: ");
|
||||
for (uint8_t i = 0; i < 8; ++i)
|
||||
{
|
||||
printf("%X ", *(rom++));
|
||||
}
|
||||
printf("\n");
|
||||
rom -= 8;
|
||||
zh_ds18b20_read(rom, &temperature);
|
||||
printf("Temperature: %0.2f\n", temperature);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Any [feedback](mailto:github@azholtikov.ru) will be gladly accepted.
|
0
components/zh_ds18b20/component.mk
Normal file
0
components/zh_ds18b20/component.mk
Normal file
37
components/zh_ds18b20/include/zh_ds18b20.h
Normal file
37
components/zh_ds18b20/include/zh_ds18b20.h
Normal file
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* @file
|
||||
* Header file for the zh_ds18b20 component.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "stdio.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "zh_onewire.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Read temperature from a specific 1-Wire device on bus.
|
||||
*
|
||||
* @param[in] device Pointer to a buffer containing an eight-byte rom value. Can be NULL if only one 1-Wire device is present on the bus.
|
||||
*
|
||||
* @param[out] temperature Pointer to a buffer containing the received temperature.
|
||||
*
|
||||
* @attention 1-Wire bus must be initialized first. @code zh_onewire_init(GPIO_NUM_X) @endcode
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if read was successful
|
||||
* - ESP_FAIL if any error
|
||||
* - ESP_ERR_INVALID_CRC if case an read error
|
||||
*/
|
||||
esp_err_t zh_ds18b20_read(const uint8_t *device, float *temperature);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
1
components/zh_ds18b20/version.txt
Normal file
1
components/zh_ds18b20/version.txt
Normal file
@ -0,0 +1 @@
|
||||
1.3.2
|
90
components/zh_ds18b20/zh_ds18b20.c
Normal file
90
components/zh_ds18b20/zh_ds18b20.c
Normal file
@ -0,0 +1,90 @@
|
||||
/**
|
||||
* @file
|
||||
* The main code of the zh_ds18b20 component.
|
||||
*/
|
||||
|
||||
#include "zh_ds18b20.h"
|
||||
|
||||
/// \cond
|
||||
#define SCRATCHPAD_LENGTH 8 // Scratchpad length.
|
||||
|
||||
#define CONVERT_TEMPERATURE 0x44 // Initiates temperature conversion.
|
||||
#define READ_SCRATCHPAD 0xBE // Reads bytes from scratchpad.
|
||||
|
||||
#define pgm_read_byte(addr) (*(const uint8_t *)(addr))
|
||||
/// \endcond
|
||||
|
||||
static uint8_t _crc_calculate(const uint8_t *scratchpad);
|
||||
|
||||
static const char *TAG = "zh_ds18b20";
|
||||
|
||||
static const uint8_t _crc_table[] = {
|
||||
0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83,
|
||||
0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,
|
||||
0x00, 0x9D, 0x23, 0xBE, 0x46, 0xDB, 0x65, 0xF8,
|
||||
0x8C, 0x11, 0xAF, 0x32, 0xCA, 0x57, 0xE9, 0x74};
|
||||
|
||||
esp_err_t zh_ds18b20_read(const uint8_t *device, float *temperature)
|
||||
{
|
||||
ESP_LOGI(TAG, "DS18B20 read begin.");
|
||||
uint8_t scratchpad[9];
|
||||
if (device == NULL)
|
||||
{
|
||||
if (zh_onewire_skip_rom() != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "DS18B20 read fail.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (zh_onewire_match_rom(device) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "DS18B20 read fail.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
zh_onewire_send_byte(CONVERT_TEMPERATURE);
|
||||
vTaskDelay(750 / portTICK_PERIOD_MS);
|
||||
if (device == NULL)
|
||||
{
|
||||
if (zh_onewire_skip_rom() != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "DS18B20 read fail.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (zh_onewire_match_rom(device) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "DS18B20 read fail.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
zh_onewire_send_byte(READ_SCRATCHPAD);
|
||||
for (uint8_t i = 0; i < 9; ++i)
|
||||
{
|
||||
scratchpad[i] = zh_onewire_read_byte();
|
||||
}
|
||||
if (scratchpad[8] != _crc_calculate(scratchpad))
|
||||
{
|
||||
ESP_LOGE(TAG, "DS18B20 read fail. Invalid CRC.");
|
||||
return ESP_ERR_INVALID_CRC;
|
||||
}
|
||||
*temperature = (float)(scratchpad[0] + (scratchpad[1] * 256)) / 16;
|
||||
ESP_LOGI(TAG, "DS18B20 read success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static uint8_t _crc_calculate(const uint8_t *scratchpad)
|
||||
{
|
||||
uint8_t crc = 0;
|
||||
uint8_t length = SCRATCHPAD_LENGTH;
|
||||
while (length--)
|
||||
{
|
||||
crc = *scratchpad++ ^ crc;
|
||||
crc = pgm_read_byte(_crc_table + (crc & 0x0f)) ^ pgm_read_byte(_crc_table + 16 + ((crc >> 4) & 0x0f));
|
||||
}
|
||||
return crc;
|
||||
}
|
6
components/zh_espnow/CMakeLists.txt
Normal file
6
components/zh_espnow/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
if(${IDF_TARGET} STREQUAL esp8266)
|
||||
set(requires)
|
||||
else()
|
||||
set(requires esp_wifi)
|
||||
endif()
|
||||
idf_component_register(SRCS "zh_espnow.c" INCLUDE_DIRS "include" REQUIRES ${requires})
|
73
components/zh_espnow/LICENSE
Normal file
73
components/zh_espnow/LICENSE
Normal file
@ -0,0 +1,73 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
128
components/zh_espnow/README.md
Normal file
128
components/zh_espnow/README.md
Normal file
@ -0,0 +1,128 @@
|
||||
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for ESP-NOW interface
|
||||
|
||||
## Tested on
|
||||
|
||||
1. ESP8266 RTOS_SDK v3.4
|
||||
2. ESP32 ESP-IDF v5.2
|
||||
|
||||
## Features
|
||||
|
||||
1. The maximum size of transmitted data is up to 250 bytes.
|
||||
2. Support of any data types.
|
||||
3. All nodes are not visible to the network scanner.
|
||||
4. Not required a pre-pairings for data transfer.
|
||||
5. Broadcast or unicast data transmissions.
|
||||
6. Possibility uses WiFi AP or STA modes at the same time with ESP-NOW.
|
||||
|
||||
## Attention
|
||||
|
||||
1. For correct operation in ESP-NOW + STA mode, your WiFi router must be set to the same channel as ESP-NOW.
|
||||
2. All devices on the network must have the same WiFi channel.
|
||||
|
||||
## [Function description](http://zh-espnow.zh.com.ru)
|
||||
|
||||
## Using
|
||||
|
||||
In an existing project, run the following command to install the component:
|
||||
|
||||
```text
|
||||
cd ../your_project/components
|
||||
git clone https://github.com/aZholtikov/zh_espnow.git
|
||||
```
|
||||
|
||||
In the application, add the component:
|
||||
|
||||
```c
|
||||
#include "zh_espnow.h"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
Sending and receiving messages:
|
||||
|
||||
```c
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_netif.h"
|
||||
#include "zh_espnow.h"
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#include "esp_system.h"
|
||||
#else
|
||||
#include "esp_random.h"
|
||||
#endif
|
||||
|
||||
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
|
||||
|
||||
void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||
|
||||
uint8_t target[6] = {0x34, 0x94, 0x54, 0x24, 0xA3, 0x41};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char char_value[30];
|
||||
int int_value;
|
||||
float float_value;
|
||||
bool bool_value;
|
||||
} example_message_t;
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
esp_log_level_set("zh_espnow", ESP_LOG_NONE);
|
||||
nvs_flash_init();
|
||||
esp_netif_init();
|
||||
esp_event_loop_create_default();
|
||||
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_wifi_init(&wifi_init_config);
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
esp_wifi_start();
|
||||
zh_espnow_init_config_t zh_espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT();
|
||||
zh_espnow_init(&zh_espnow_init_config);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
esp_event_handler_register(ZH_ESPNOW, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, NULL);
|
||||
#else
|
||||
esp_event_handler_instance_register(ZH_ESPNOW, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, NULL, NULL);
|
||||
#endif
|
||||
example_message_t send_message = {0};
|
||||
strcpy(send_message.char_value, "THIS IS A CHAR");
|
||||
send_message.float_value = 1.234;
|
||||
send_message.bool_value = false;
|
||||
for (;;)
|
||||
{
|
||||
send_message.int_value = esp_random();
|
||||
zh_espnow_send(NULL, (uint8_t *)&send_message, sizeof(send_message));
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
zh_espnow_send(target, (uint8_t *)&send_message, sizeof(send_message));
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
{
|
||||
switch (event_id)
|
||||
{
|
||||
case ZH_ESPNOW_ON_RECV_EVENT:;
|
||||
zh_espnow_event_on_recv_t *recv_data = event_data;
|
||||
printf("Message from MAC %02X:%02X:%02X:%02X:%02X:%02X is received. Data lenght %d bytes.\n", MAC2STR(recv_data->mac_addr), recv_data->data_len);
|
||||
example_message_t *recv_message = (example_message_t *)recv_data->data;
|
||||
printf("Char %s\n", recv_message->char_value);
|
||||
printf("Int %d\n", recv_message->int_value);
|
||||
printf("Float %f\n", recv_message->float_value);
|
||||
printf("Bool %d\n", recv_message->bool_value);
|
||||
heap_caps_free(recv_data->data); // Do not delete to avoid memory leaks!
|
||||
break;
|
||||
case ZH_ESPNOW_ON_SEND_EVENT:;
|
||||
zh_espnow_event_on_send_t *send_data = event_data;
|
||||
if (send_data->status == ZH_ESPNOW_SEND_SUCCESS)
|
||||
{
|
||||
printf("Message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.\n", MAC2STR(send_data->mac_addr));
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.\n", MAC2STR(send_data->mac_addr));
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Any [feedback](mailto:github@azholtikov.ru) will be gladly accepted.
|
0
components/zh_espnow/component.mk
Normal file
0
components/zh_espnow/component.mk
Normal file
156
components/zh_espnow/include/zh_espnow.h
Normal file
156
components/zh_espnow/include/zh_espnow.h
Normal file
@ -0,0 +1,156 @@
|
||||
/**
|
||||
* @file
|
||||
* Header file for the zh_espnow component.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "string.h"
|
||||
#include "esp_err.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_now.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_heap_caps.h"
|
||||
|
||||
/**
|
||||
* @brief Unique identifier of ESP-NOW interface events base. Used when registering the event handler.
|
||||
*
|
||||
*/
|
||||
#define ESP_EVENT_BASE ZH_ESPNOW
|
||||
|
||||
/**
|
||||
* @brief Default values for zh_espnow_init_config_t structure for initial initialization of ESP-NOW interface.
|
||||
*
|
||||
*/
|
||||
#define ZH_ESPNOW_INIT_CONFIG_DEFAULT() \
|
||||
{ \
|
||||
.task_priority = 4, \
|
||||
.stack_size = 2048, \
|
||||
.queue_size = 32, \
|
||||
.wifi_interface = WIFI_IF_STA, \
|
||||
.wifi_channel = 1 \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Structure for initial initialization of ESP-NOW interface.
|
||||
*
|
||||
* @note Before initialize ESP-NOW interface recommend initialize zh_espnow_init_config_t structure with default values.
|
||||
*
|
||||
* @code
|
||||
* zh_espnow_init_config_t config = ZH_ESPNOW_INIT_CONFIG_DEFAULT()
|
||||
* @endcode
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t task_priority; ///< Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4.
|
||||
uint16_t stack_size; ///< Stack size for task for the ESP-NOW messages processing. @note The minimum size is 2048 bytes.
|
||||
uint8_t queue_size; ///< Queue size for task for the ESP-NOW messages processing. @note The size depends on the number of messages to be processed. It is not recommended to set the value less than 16.
|
||||
wifi_interface_t wifi_interface; ///< WiFi interface (STA or AP) used for ESP-NOW operation. @note The MAC address of the device depends on the selected WiFi interface.
|
||||
uint8_t wifi_channel; ///< Wi-Fi channel uses to send/receive ESP-NOW data. @note Values from 1 to 14.
|
||||
} zh_espnow_init_config_t;
|
||||
|
||||
/// \cond
|
||||
ESP_EVENT_DECLARE_BASE(ESP_EVENT_BASE);
|
||||
/// \endcond
|
||||
|
||||
/**
|
||||
* @brief Enumeration of possible ESP-NOW events.
|
||||
*
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ZH_ESPNOW_ON_RECV_EVENT, ///< The event when the ESP-NOW message was received.
|
||||
ZH_ESPNOW_ON_SEND_EVENT ///< The event when the ESP-NOW message was sent.
|
||||
} zh_espnow_event_type_t;
|
||||
|
||||
/**
|
||||
* @brief Enumeration of possible status of sent ESP-NOW message.
|
||||
*
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ZH_ESPNOW_SEND_SUCCESS, ///< If ESP-NOW message was sent success.
|
||||
ZH_ESPNOW_SEND_FAIL ///< If ESP-NOW message was sent fail.
|
||||
} zh_espnow_on_send_event_type_t;
|
||||
|
||||
/**
|
||||
* @brief Structure for sending data to the event handler when an ESP-NOW message was sent.
|
||||
*
|
||||
* @note Should be used with ZH_ESPNOW event base and ZH_ESPNOW_ON_SEND_EVENT event.
|
||||
*
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t mac_addr[6]; ///< MAC address of the device to which the ESP-NOW message was sent. @note
|
||||
zh_espnow_on_send_event_type_t status; ///< Status of sent ESP-NOW message. @note
|
||||
} zh_espnow_event_on_send_t;
|
||||
|
||||
/**
|
||||
* @brief Structure for sending data to the event handler when an ESP-NOW message was received.
|
||||
*
|
||||
* @note Should be used with ZH_ESPNOW event base and ZH_ESPNOW_ON_RECV_EVENT event.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t mac_addr[6]; ///< MAC address of the sender ESP-NOW message. @note
|
||||
uint8_t *data; ///< Pointer to the data of the received ESP-NOW message. @note
|
||||
uint8_t data_len; ///< Size of the received ESP-NOW message. @note
|
||||
} zh_espnow_event_on_recv_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize ESP-NOW interface.
|
||||
*
|
||||
* @note Before initialize ESP-NOW interface recommend initialize zh_espnow_init_config_t structure with default values.
|
||||
*
|
||||
* @code
|
||||
* zh_espnow_init_config_t config = ZH_ESPNOW_INIT_CONFIG_DEFAULT()
|
||||
* @endcode
|
||||
*
|
||||
* @param[in] config Pointer to ESP-NOW initialized configuration structure. Can point to a temporary variable.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if initialization was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_WIFI_NOT_INIT if WiFi is not initialized
|
||||
* - ESP_FAIL if any internal error
|
||||
*/
|
||||
esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize ESP-NOW interface.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if deinitialization was success
|
||||
* - ESP_FAIL if ESP-NOW is not initialized
|
||||
*/
|
||||
esp_err_t zh_espnow_deinit(void);
|
||||
|
||||
/**
|
||||
* @brief Send ESP-NOW data.
|
||||
*
|
||||
* @param[in] target Pointer to a buffer containing an eight-byte target MAC. Can be NULL for broadcast.
|
||||
* @param[in] data Pointer to a buffer containing the data for send.
|
||||
* @param[in] data_len Sending data length.
|
||||
*
|
||||
* @note The function will return an ESP_ERR_INVALID_STATE error if less than 10% of the size set at initialization remains in the message queue.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if sent was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_NO_MEM if memory allocation fail or no free memory in the heap
|
||||
* - ESP_ERR_INVALID_STATE if queue for outgoing data is almost full
|
||||
* - ESP_FAIL if ESP-NOW is not initialized or any internal error
|
||||
*/
|
||||
esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
1
components/zh_espnow/version.txt
Normal file
1
components/zh_espnow/version.txt
Normal file
@ -0,0 +1 @@
|
||||
1.2.6
|
337
components/zh_espnow/zh_espnow.c
Normal file
337
components/zh_espnow/zh_espnow.c
Normal file
@ -0,0 +1,337 @@
|
||||
/**
|
||||
* @file
|
||||
* The main code of the zh_espnow component.
|
||||
*/
|
||||
|
||||
#include "zh_espnow.h"
|
||||
|
||||
/// \cond
|
||||
#define DATA_SEND_SUCCESS BIT0
|
||||
#define DATA_SEND_FAIL BIT1
|
||||
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
|
||||
/// \endcond
|
||||
|
||||
static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
static void _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len);
|
||||
#else
|
||||
static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len);
|
||||
#endif
|
||||
static void _processing(void *pvParameter);
|
||||
|
||||
static const char *TAG = "zh_espnow";
|
||||
|
||||
static EventGroupHandle_t _event_group_handle = {0};
|
||||
static QueueHandle_t _queue_handle = {0};
|
||||
static TaskHandle_t _processing_task_handle = {0};
|
||||
static zh_espnow_init_config_t _init_config = {0};
|
||||
static bool _is_initialized = false;
|
||||
|
||||
/// \cond
|
||||
typedef struct
|
||||
{
|
||||
enum
|
||||
{
|
||||
ON_RECV,
|
||||
TO_SEND,
|
||||
} id;
|
||||
struct
|
||||
{
|
||||
uint8_t mac_addr[6];
|
||||
uint8_t *payload;
|
||||
uint8_t payload_len;
|
||||
} data;
|
||||
} _queue_t;
|
||||
|
||||
ESP_EVENT_DEFINE_BASE(ZH_ESPNOW);
|
||||
/// \endcond
|
||||
|
||||
esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config)
|
||||
{
|
||||
ESP_LOGI(TAG, "ESP-NOW initialization begin.");
|
||||
if (config == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
_init_config = *config;
|
||||
if (_init_config.wifi_channel < 1 || _init_config.wifi_channel > 14)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi channel.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (esp_wifi_set_channel(_init_config.wifi_channel, WIFI_SECOND_CHAN_NONE) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi not initialized.");
|
||||
return ESP_ERR_WIFI_NOT_INIT;
|
||||
}
|
||||
_event_group_handle = xEventGroupCreate();
|
||||
_queue_handle = xQueueCreate(_init_config.queue_size, sizeof(_queue_t));
|
||||
if (esp_now_init() != ESP_OK || esp_now_register_send_cb(_send_cb) != ESP_OK || esp_now_register_recv_cb(_recv_cb) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (xTaskCreatePinnedToCore(&_processing, "NULL", _init_config.stack_size, NULL, _init_config.task_priority, &_processing_task_handle, tskNO_AFFINITY) != pdPASS)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
_is_initialized = true;
|
||||
ESP_LOGI(TAG, "ESP-NOW initialization success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_espnow_deinit(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "ESP-NOW deinitialization begin.");
|
||||
if (_is_initialized == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW deinitialization fail. ESP-NOW not initialized.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
vEventGroupDelete(_event_group_handle);
|
||||
vQueueDelete(_queue_handle);
|
||||
esp_now_unregister_send_cb();
|
||||
esp_now_unregister_recv_cb();
|
||||
esp_now_deinit();
|
||||
vTaskDelete(_processing_task_handle);
|
||||
_is_initialized = false;
|
||||
ESP_LOGI(TAG, "ESP-NOW deinitialization success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len)
|
||||
{
|
||||
if (target == NULL)
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC FF:FF:FF:FF:FF:FF to queue begin.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(target));
|
||||
}
|
||||
if (_is_initialized == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. ESP-NOW not initialized.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (data == NULL || data_len == 0 || data_len > ESP_NOW_MAX_DATA_LEN)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (uxQueueSpacesAvailable(_queue_handle) < _init_config.queue_size / 10)
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding outgoing ESP-NOW data to queue fail. Queue is almost full.");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
uint8_t broadcast[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
_queue_t queue = {0};
|
||||
queue.id = TO_SEND;
|
||||
if (target == NULL)
|
||||
{
|
||||
memcpy(queue.data.mac_addr, broadcast, 6);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(queue.data.mac_addr, target, 6);
|
||||
}
|
||||
if (data_len / sizeof(void *) == 0)
|
||||
{
|
||||
queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_32BIT);
|
||||
}
|
||||
else
|
||||
{
|
||||
queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_8BIT);
|
||||
}
|
||||
if (queue.data.payload == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. Memory allocation fail or no free memory in the heap.");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
memset(queue.data.payload, 0, data_len);
|
||||
memcpy(queue.data.payload, data, data_len);
|
||||
queue.data.payload_len = data_len;
|
||||
if (target == NULL)
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC FF:FF:FF:FF:FF:FF to queue success.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(target));
|
||||
}
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
|
||||
{
|
||||
if (status == ESP_NOW_SEND_SUCCESS)
|
||||
{
|
||||
xEventGroupSetBits(_event_group_handle, DATA_SEND_SUCCESS);
|
||||
}
|
||||
else
|
||||
{
|
||||
xEventGroupSetBits(_event_group_handle, DATA_SEND_FAIL);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
static void _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len)
|
||||
#else
|
||||
static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len)
|
||||
#endif
|
||||
{
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(mac_addr));
|
||||
#else
|
||||
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(esp_now_info->src_addr));
|
||||
#endif
|
||||
if (uxQueueSpacesAvailable(_queue_handle) < _init_config.queue_size / 10)
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Queue is almost full.");
|
||||
return;
|
||||
}
|
||||
_queue_t queue = {0};
|
||||
queue.id = ON_RECV;
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
memcpy(queue.data.mac_addr, mac_addr, 6);
|
||||
#else
|
||||
memcpy(queue.data.mac_addr, esp_now_info->src_addr, 6);
|
||||
#endif
|
||||
if (data_len / sizeof(void *) == 0)
|
||||
{
|
||||
queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_32BIT);
|
||||
}
|
||||
else
|
||||
{
|
||||
queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_8BIT);
|
||||
}
|
||||
if (queue.data.payload == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding incoming ESP-NOW data to queue fail. Memory allocation fail or no free memory in the heap.");
|
||||
return;
|
||||
}
|
||||
memset(queue.data.payload, 0, data_len);
|
||||
memcpy(queue.data.payload, data, data_len);
|
||||
queue.data.payload_len = data_len;
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(mac_addr));
|
||||
#else
|
||||
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(esp_now_info->src_addr));
|
||||
#endif
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
}
|
||||
|
||||
static void _processing(void *pvParameter)
|
||||
{
|
||||
_queue_t queue = {0};
|
||||
while (xQueueReceive(_queue_handle, &queue, portMAX_DELAY) == pdTRUE)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
switch (queue.id)
|
||||
{
|
||||
case TO_SEND:
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X processing begin.", MAC2STR(queue.data.mac_addr));
|
||||
esp_now_peer_info_t *peer = heap_caps_malloc(sizeof(esp_now_peer_info_t), MALLOC_CAP_8BIT);
|
||||
if (peer == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Memory allocation fail or no free memory in the heap.");
|
||||
heap_caps_free(queue.data.payload);
|
||||
break;
|
||||
}
|
||||
memset(peer, 0, sizeof(esp_now_peer_info_t));
|
||||
peer->ifidx = _init_config.wifi_interface;
|
||||
memcpy(peer->peer_addr, queue.data.mac_addr, 6);
|
||||
err = esp_now_add_peer(peer);
|
||||
if (err == ESP_ERR_ESPNOW_NO_MEM)
|
||||
{
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. No free memory in the heap for adding peer.");
|
||||
heap_caps_free(queue.data.payload);
|
||||
heap_caps_free(peer);
|
||||
break;
|
||||
}
|
||||
else if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Internal error with adding peer.");
|
||||
heap_caps_free(queue.data.payload);
|
||||
heap_caps_free(peer);
|
||||
break;
|
||||
}
|
||||
zh_espnow_event_on_send_t *on_send = heap_caps_malloc(sizeof(zh_espnow_event_on_send_t), MALLOC_CAP_8BIT);
|
||||
if (on_send == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Memory allocation fail or no free memory in the heap.");
|
||||
heap_caps_free(queue.data.payload);
|
||||
heap_caps_free(peer);
|
||||
break;
|
||||
}
|
||||
memset(on_send, 0, sizeof(zh_espnow_event_on_send_t));
|
||||
memcpy(on_send->mac_addr, queue.data.mac_addr, 6);
|
||||
err = esp_now_send(queue.data.mac_addr, queue.data.payload, queue.data.payload_len);
|
||||
if (err == ESP_ERR_ESPNOW_NO_MEM)
|
||||
{
|
||||
ESP_LOGE(TAG, "Sending ESP-NOW data fail. No free memory in the heap.");
|
||||
heap_caps_free(queue.data.payload);
|
||||
heap_caps_free(peer);
|
||||
heap_caps_free(on_send);
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail.");
|
||||
break;
|
||||
}
|
||||
else if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Sending ESP-NOW data fail. Internal error.");
|
||||
heap_caps_free(queue.data.payload);
|
||||
heap_caps_free(peer);
|
||||
heap_caps_free(on_send);
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail.");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Sending ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X success.", MAC2STR(queue.data.mac_addr));
|
||||
}
|
||||
EventBits_t bit = xEventGroupWaitBits(_event_group_handle, DATA_SEND_SUCCESS | DATA_SEND_FAIL, pdTRUE, pdFALSE, 50 / portTICK_PERIOD_MS);
|
||||
if ((bit & DATA_SEND_SUCCESS) != 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "Confirmation message received. ESP-NOW message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.mac_addr));
|
||||
on_send->status = ZH_ESPNOW_SEND_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Confirmation message not received. ESP-NOW message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.", MAC2STR(queue.data.mac_addr));
|
||||
on_send->status = ZH_ESPNOW_SEND_FAIL;
|
||||
}
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.mac_addr));
|
||||
if (esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_SEND_EVENT, on_send, sizeof(zh_espnow_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
heap_caps_free(queue.data.payload);
|
||||
esp_now_del_peer(peer->peer_addr);
|
||||
heap_caps_free(peer);
|
||||
heap_caps_free(on_send);
|
||||
break;
|
||||
case ON_RECV:
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processing begin.", MAC2STR(queue.data.mac_addr));
|
||||
zh_espnow_event_on_recv_t *recv_data = (zh_espnow_event_on_recv_t *)&queue.data;
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.mac_addr));
|
||||
if (esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_RECV_EVENT, recv_data, recv_data->data_len + 7, portTICK_PERIOD_MS) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
6
components/zh_network/CMakeLists.txt
Normal file
6
components/zh_network/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
if(${IDF_TARGET} STREQUAL esp8266)
|
||||
set(requires zh_vector)
|
||||
else()
|
||||
set(requires zh_vector esp_timer esp_wifi)
|
||||
endif()
|
||||
idf_component_register(SRCS "zh_network.c" INCLUDE_DIRS "include" REQUIRES ${requires})
|
73
components/zh_network/LICENSE
Normal file
73
components/zh_network/LICENSE
Normal file
@ -0,0 +1,73 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
151
components/zh_network/README.md
Normal file
151
components/zh_network/README.md
Normal file
@ -0,0 +1,151 @@
|
||||
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for ESP-NOW based mesh network
|
||||
|
||||
## Tested on
|
||||
|
||||
1. ESP8266 RTOS_SDK v3.4
|
||||
2. ESP32 ESP-IDF v5.2
|
||||
|
||||
## Features
|
||||
|
||||
1. The maximum size of transmitted data is up to 218 bytes.
|
||||
2. Support of any data types.
|
||||
3. All nodes are not visible to the network scanner.
|
||||
4. Not required a pre-pairings for data transfer.
|
||||
5. Broadcast or unicast data transmissions.
|
||||
6. There are no periodic/synchronous messages on the network. All devices are in "silent mode" and do not "hum" into the air.
|
||||
7. Each node has its own independent routing table, updated only as needed.
|
||||
8. Each node will receive/send a message if it "sees" at least one device on the network.
|
||||
9. The number of devices on the network and the area of use is not limited.
|
||||
10. Possibility uses WiFi AP or STA modes at the same time with ESP-NOW.
|
||||
|
||||
## Attention
|
||||
|
||||
1. The definition of ZH_NETWORK_MAX_MESSAGE_SIZE in the zh_network.h can be changed between 1 and 218. Smaller size - higher transmission speed. All devices on the network must have the same ZH_NETWORK_MAX_MESSAGE_SIZE.
|
||||
2. For correct operation in ESP-NOW + STA mode, your WiFi router must be set to the same channel as ESP-NOW.
|
||||
3. All devices on the network must have the same WiFi channel.
|
||||
4. The ZHNetwork and the zh_network are incompatible.
|
||||
|
||||
## Testing
|
||||
|
||||
1. Program 2 receivers and 1 transmitter (specify the MAC of the 1st receiver in the code).
|
||||
2. Connect the 1st receiver to the computer. Switch on serial port monitor. Turn transmitter on. Receiver will start receiving data.
|
||||
3. Move transmitter as far away from receiver as possible until receiver is able to receive data (shield module if necessary).
|
||||
4. Turn on the 2nd receiver and place it between the 1st receiver and transmitter (preferably in the middle). The 1st receiver will resume data reception (with relaying through the 2nd receiver). P.S. You can use a transmitter instead of the 2nd receiver - makes no difference.
|
||||
|
||||
## Arduino library
|
||||
|
||||
1. For using zh_network component on Arduino download and copy (with extract) zh_network.zip file to your folder for Arduino libraries. See README.md in this folder for using example.
|
||||
2. Please pay attention - library tested on VSCode + PlatformIO. Not on Arduino IDE.
|
||||
|
||||
## Dependencies
|
||||
|
||||
1. [zh_vector](https://github.com/aZholtikov/zh_vector.git)
|
||||
|
||||
## [Function description](http://zh-network.zh.com.ru)
|
||||
|
||||
## Using
|
||||
|
||||
In an existing project, run the following command to install the component:
|
||||
|
||||
```text
|
||||
cd ../your_project/components
|
||||
git clone https://github.com/aZholtikov/zh_vector.git
|
||||
git clone https://github.com/aZholtikov/zh_network.git
|
||||
```
|
||||
|
||||
In the application, add the component:
|
||||
|
||||
```c
|
||||
#include "zh_network.h"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
Sending and receiving messages:
|
||||
|
||||
```c
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_netif.h"
|
||||
#include "zh_network.h"
|
||||
|
||||
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
|
||||
|
||||
void zh_network_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||
|
||||
uint8_t target[6] = {0x58, 0xBF, 0x25, 0x18, 0xC8, 0x04};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char char_value[30];
|
||||
int int_value;
|
||||
float float_value;
|
||||
bool bool_value;
|
||||
} example_message_t;
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
esp_log_level_set("zh_vector", ESP_LOG_NONE);
|
||||
// esp_log_level_set("zh_network", ESP_LOG_NONE);
|
||||
nvs_flash_init();
|
||||
esp_netif_init();
|
||||
esp_event_loop_create_default();
|
||||
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_wifi_init(&wifi_init_config);
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
esp_wifi_start();
|
||||
esp_wifi_set_max_tx_power(8); // Power reduction is for example and testing purposes only. Do not use in your own programs!
|
||||
zh_network_init_config_t zh_network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT();
|
||||
zh_network_init(&zh_network_init_config);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
esp_event_handler_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL);
|
||||
#else
|
||||
esp_event_handler_instance_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL, NULL);
|
||||
#endif
|
||||
example_message_t send_message = {0};
|
||||
strcpy(send_message.char_value, "THIS IS A CHAR");
|
||||
send_message.float_value = 1.234;
|
||||
send_message.bool_value = false;
|
||||
for (;;)
|
||||
{
|
||||
send_message.int_value = esp_random();
|
||||
zh_network_send(NULL, (uint8_t *)&send_message, sizeof(send_message));
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
zh_network_send(target, (uint8_t *)&send_message, sizeof(send_message));
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void zh_network_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
{
|
||||
switch (event_id)
|
||||
{
|
||||
case ZH_NETWORK_ON_RECV_EVENT:;
|
||||
zh_network_event_on_recv_t *recv_data = event_data;
|
||||
printf("Message from MAC %02X:%02X:%02X:%02X:%02X:%02X is received. Data lenght %d bytes.\n", MAC2STR(recv_data->mac_addr), recv_data->data_len);
|
||||
example_message_t *recv_message = (example_message_t *)recv_data->data;
|
||||
printf("Char %s\n", recv_message->char_value);
|
||||
printf("Int %d\n", recv_message->int_value);
|
||||
printf("Float %f\n", recv_message->float_value);
|
||||
printf("Bool %d\n", recv_message->bool_value);
|
||||
free(recv_data->data); // Do not delete to avoid memory leaks!
|
||||
break;
|
||||
case ZH_NETWORK_ON_SEND_EVENT:;
|
||||
zh_network_event_on_send_t *send_data = event_data;
|
||||
if (send_data->status == ZH_NETWORK_SEND_SUCCESS)
|
||||
{
|
||||
printf("Message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.\n", MAC2STR(send_data->mac_addr));
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.\n", MAC2STR(send_data->mac_addr));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Thanks to [Marton Larrosa](mailto:marton@mail.com) for participating in the testing.
|
||||
|
||||
Any [feedback](mailto:github@azholtikov.ru) will be gladly accepted.
|
0
components/zh_network/component.mk
Normal file
0
components/zh_network/component.mk
Normal file
181
components/zh_network/include/zh_network.h
Normal file
181
components/zh_network/include/zh_network.h
Normal file
@ -0,0 +1,181 @@
|
||||
/**
|
||||
* @file
|
||||
* Header file for the zh_network component.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "string.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_now.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "zh_vector.h"
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#include "esp_system.h"
|
||||
#else
|
||||
#include "esp_random.h"
|
||||
#include "esp_mac.h"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Unique identifier of ESP-NOW interface events base. Used when registering the event handler.
|
||||
*
|
||||
*/
|
||||
#define ESP_EVENT_BASE ZH_NETWORK
|
||||
|
||||
/**
|
||||
* @brief Maximum value of the transmitted data size.
|
||||
*
|
||||
* @note Value range from 1 to 218. Smaller size - higher transmission speed.
|
||||
*
|
||||
* @attention All devices on the network must have the same ZH_NETWORK_MAX_MESSAGE_SIZE.
|
||||
*
|
||||
*/
|
||||
#define ZH_NETWORK_MAX_MESSAGE_SIZE 218
|
||||
|
||||
/**
|
||||
* @brief Default values for zh_network_init_config_t structure for initial initialization of ESP-NOW interface.
|
||||
*
|
||||
*/
|
||||
#define ZH_NETWORK_INIT_CONFIG_DEFAULT() \
|
||||
{ \
|
||||
.network_id = 0xFAFBFCFD, \
|
||||
.task_priority = 4, \
|
||||
.stack_size = 3072, \
|
||||
.queue_size = 32, \
|
||||
.max_waiting_time = 1000, \
|
||||
.id_vector_size = 100, \
|
||||
.route_vector_size = 100, \
|
||||
.wifi_interface = WIFI_IF_STA, \
|
||||
.wifi_channel = 1 \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Structure for initial initialization of ESP-NOW interface.
|
||||
*
|
||||
* @note Before initialize ESP-NOW interface recommend initialize zh_network_init_config_t structure with default values.
|
||||
*
|
||||
* @code
|
||||
* zh_network_init_config_t config = ZH_NETWORK_INIT_CONFIG_DEFAULT()
|
||||
* @endcode
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32_t network_id; ///< A unique ID for the mesh network. @attention The ID must be the same for all nodes in the network.
|
||||
uint8_t task_priority; ///< Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4.
|
||||
uint16_t stack_size; ///< Stack size for task for the ESP-NOW messages processing. @note The minimum size is 3072 bytes.
|
||||
uint8_t queue_size; ///< Queue size for task for the ESP-NOW messages processing. @note The size depends on the number of messages to be processed. It is not recommended to set the value less than 32.
|
||||
uint16_t max_waiting_time; ///< Maximum time to wait a response message from target node (in milliseconds). @note If a response message from the target node is not received within this time, the status of the sent message will be "sent fail".
|
||||
uint16_t id_vector_size; ///< Maximum size of unique ID of received messages. @note If the size is exceeded, the first value will be deleted. Minimum recommended value: number of planned nodes in the network + 10%.
|
||||
uint16_t route_vector_size; ///< The maximum size of the routing table. @note If the size is exceeded, the first route will be deleted. Minimum recommended value: number of planned nodes in the network + 10%.
|
||||
wifi_interface_t wifi_interface; ///< WiFi interface (STA or AP) used for ESP-NOW operation. @note The MAC address of the device depends on the selected WiFi interface.
|
||||
uint8_t wifi_channel; ///< Wi-Fi channel uses to send/receive ESPNOW data. @note Values from 1 to 14.
|
||||
} __attribute__((packed)) zh_network_init_config_t;
|
||||
|
||||
/// \cond
|
||||
ESP_EVENT_DECLARE_BASE(ESP_EVENT_BASE);
|
||||
/// \endcond
|
||||
|
||||
/**
|
||||
* @brief Enumeration of possible ESP-NOW events.
|
||||
*
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ZH_NETWORK_ON_RECV_EVENT, ///< The event when the ESP-NOW message was received.
|
||||
ZH_NETWORK_ON_SEND_EVENT ///< The event when the ESP-NOW message was sent.
|
||||
} __attribute__((packed)) zh_network_event_type_t;
|
||||
|
||||
/**
|
||||
* @brief Enumeration of possible status of sent ESP-NOW message.
|
||||
*
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ZH_NETWORK_SEND_SUCCESS, ///< If ESP-NOW message was sent success.
|
||||
ZH_NETWORK_SEND_FAIL ///< If ESP-NOW message was sent fail.
|
||||
} __attribute__((packed)) zh_network_on_send_event_type_t;
|
||||
|
||||
/**
|
||||
* @brief Structure for sending data to the event handler when an ESP-NOW message was sent.
|
||||
*
|
||||
* @note Should be used with ZH_NETWORK event base and ZH_NETWORK_ON_SEND_EVENT event.
|
||||
*
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t mac_addr[6]; ///< MAC address of the device to which the ESP-NOW message was sent. @note
|
||||
zh_network_on_send_event_type_t status; ///< Status of sent ESP-NOW message. @note
|
||||
} __attribute__((packed)) zh_network_event_on_send_t;
|
||||
|
||||
/**
|
||||
* @brief Structure for sending data to the event handler when an ESP-NOW message was received.
|
||||
*
|
||||
* @note Should be used with ZH_NETWORK event base and ZH_NETWORK_ON_RECV_EVENT event.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t mac_addr[6]; ///< MAC address of the sender ESP-NOW message. @note
|
||||
uint8_t *data; ///< Pointer to the data of the received ESP-NOW message. @note
|
||||
uint8_t data_len; ///< Size of the received ESP-NOW message. @note
|
||||
} __attribute__((packed)) zh_network_event_on_recv_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize ESP-NOW interface.
|
||||
*
|
||||
* @note Before initialize ESP-NOW interface recommend initialize zh_network_init_config_t structure with default values.
|
||||
*
|
||||
* @code
|
||||
* zh_network_init_config_t config = ZH_NETWORK_INIT_CONFIG_DEFAULT()
|
||||
* @endcode
|
||||
*
|
||||
* @param[in] config Pointer to ESP-NOW initialized configuration structure. Can point to a temporary variable.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if initialization was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_WIFI_NOT_INIT if WiFi is not initialized
|
||||
* - ESP_FAIL if any internal error
|
||||
*/
|
||||
esp_err_t zh_network_init(const zh_network_init_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize ESP-NOW interface.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if deinitialization was success
|
||||
* - ESP_FAIL if ESP-NOW is not initialized
|
||||
*/
|
||||
esp_err_t zh_network_deinit(void);
|
||||
|
||||
/**
|
||||
* @brief Send ESP-NOW data.
|
||||
*
|
||||
* @param[in] target Pointer to a buffer containing an eight-byte target MAC. Can be NULL for broadcast.
|
||||
* @param[in] data Pointer to a buffer containing the data for send.
|
||||
* @param[in] data_len Sending data length.
|
||||
*
|
||||
* @note The function will return an ESP_ERR_INVALID_STATE error if less than 50% of the size set at initialization remains in the message queue.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if sent was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_INVALID_STATE if queue for outgoing data is almost full
|
||||
* - ESP_FAIL if ESP-NOW is not initialized or any internal error
|
||||
*/
|
||||
esp_err_t zh_network_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
1
components/zh_network/version.txt
Normal file
1
components/zh_network/version.txt
Normal file
@ -0,0 +1 @@
|
||||
1.4.4
|
844
components/zh_network/zh_network.c
Normal file
844
components/zh_network/zh_network.c
Normal file
@ -0,0 +1,844 @@
|
||||
/**
|
||||
* @file
|
||||
* The main code of the zh_network component.
|
||||
*/
|
||||
|
||||
#include "zh_network.h"
|
||||
|
||||
/// \cond
|
||||
#define DATA_SEND_SUCCESS BIT0
|
||||
#define DATA_SEND_FAIL BIT1
|
||||
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
|
||||
/// \endcond
|
||||
|
||||
static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
static void _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len);
|
||||
#else
|
||||
static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len);
|
||||
#endif
|
||||
static void _processing(void *pvParameter);
|
||||
|
||||
static const char *TAG = "zh_network";
|
||||
|
||||
static EventGroupHandle_t _event_group_handle = {0};
|
||||
static QueueHandle_t _queue_handle = {0};
|
||||
static TaskHandle_t _processing_task_handle = {0};
|
||||
static SemaphoreHandle_t _id_vector_mutex = {0};
|
||||
static zh_network_init_config_t _init_config = {0};
|
||||
static zh_vector_t _id_vector = {0};
|
||||
static zh_vector_t _route_vector = {0};
|
||||
static zh_vector_t _response_vector = {0};
|
||||
static uint8_t _self_mac[6] = {0};
|
||||
static const uint8_t _broadcast_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
static bool _is_initialized = false;
|
||||
|
||||
/// \cond
|
||||
typedef struct
|
||||
{
|
||||
uint8_t original_target_mac[6];
|
||||
uint8_t intermediate_target_mac[6];
|
||||
} __attribute__((packed)) _routing_table_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint64_t time;
|
||||
enum
|
||||
{
|
||||
TO_SEND,
|
||||
ON_RECV,
|
||||
WAIT_ROUTE,
|
||||
WAIT_RESPONSE,
|
||||
} id;
|
||||
struct
|
||||
{
|
||||
enum
|
||||
{
|
||||
BROADCAST,
|
||||
UNICAST,
|
||||
DELIVERY_CONFIRM,
|
||||
SEARCH_REQUEST,
|
||||
SEARCH_RESPONSE
|
||||
} message_type;
|
||||
uint32_t network_id;
|
||||
uint32_t message_id;
|
||||
uint32_t confirm_id;
|
||||
uint8_t original_target_mac[6];
|
||||
uint8_t original_sender_mac[6];
|
||||
uint8_t sender_mac[6];
|
||||
uint8_t payload[ZH_NETWORK_MAX_MESSAGE_SIZE];
|
||||
uint8_t payload_len;
|
||||
} data;
|
||||
} __attribute__((packed)) _queue_t;
|
||||
|
||||
ESP_EVENT_DEFINE_BASE(ZH_NETWORK);
|
||||
/// \endcond
|
||||
|
||||
esp_err_t zh_network_init(const zh_network_init_config_t *config)
|
||||
{
|
||||
ESP_LOGI(TAG, "ESP-NOW initialization begin.");
|
||||
if (config == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
_init_config = *config;
|
||||
if (_init_config.wifi_channel < 1 || _init_config.wifi_channel > 14)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi channel.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (esp_wifi_set_channel(_init_config.wifi_channel, WIFI_SECOND_CHAN_NONE) == ESP_ERR_WIFI_NOT_INIT)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi not initialized.");
|
||||
return ESP_ERR_WIFI_NOT_INIT;
|
||||
}
|
||||
if ((sizeof(_queue_t) - 12) > ESP_NOW_MAX_DATA_LEN)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. The maximum value of the transmitted data size is incorrect.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (_init_config.wifi_interface == WIFI_IF_STA)
|
||||
{
|
||||
esp_read_mac(_self_mac, ESP_MAC_WIFI_STA);
|
||||
}
|
||||
else
|
||||
{
|
||||
esp_read_mac(_self_mac, ESP_MAC_WIFI_SOFTAP);
|
||||
}
|
||||
_event_group_handle = xEventGroupCreate();
|
||||
_queue_handle = xQueueCreate(_init_config.queue_size, sizeof(_queue_t));
|
||||
zh_vector_init(&_id_vector, sizeof(uint32_t), false);
|
||||
zh_vector_init(&_route_vector, sizeof(_routing_table_t), false);
|
||||
zh_vector_init(&_response_vector, sizeof(uint32_t), false);
|
||||
_id_vector_mutex = xSemaphoreCreateMutex();
|
||||
if (esp_now_init() != ESP_OK || esp_now_register_send_cb(_send_cb) != ESP_OK || esp_now_register_recv_cb(_recv_cb) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (xTaskCreatePinnedToCore(&_processing, "NULL", _init_config.stack_size, NULL, _init_config.task_priority, &_processing_task_handle, tskNO_AFFINITY) != pdPASS)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
_is_initialized = true;
|
||||
ESP_LOGI(TAG, "ESP-NOW initialization success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_network_deinit(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "ESP-NOW deinitialization begin.");
|
||||
if (_is_initialized == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW deinitialization fail. ESP-NOW not initialized.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
vEventGroupDelete(_event_group_handle);
|
||||
vQueueDelete(_queue_handle);
|
||||
esp_now_unregister_send_cb();
|
||||
esp_now_unregister_recv_cb();
|
||||
esp_now_deinit();
|
||||
zh_vector_free(&_id_vector);
|
||||
zh_vector_free(&_route_vector);
|
||||
zh_vector_free(&_response_vector);
|
||||
vTaskDelete(_processing_task_handle);
|
||||
_is_initialized = false;
|
||||
ESP_LOGI(TAG, "ESP-NOW deinitialization success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_network_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len)
|
||||
{
|
||||
if (target == NULL)
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC FF:FF:FF:FF:FF:FF to queue begin.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(target));
|
||||
}
|
||||
if (_is_initialized == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. ESP-NOW not initialized.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (data_len == 0 || data == NULL || data_len > ZH_NETWORK_MAX_MESSAGE_SIZE)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (uxQueueSpacesAvailable(_queue_handle) < _init_config.queue_size / 2)
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding outgoing ESP-NOW data to queue fail. Queue is almost full.");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
_queue_t queue = {0};
|
||||
queue.id = TO_SEND;
|
||||
queue.data.network_id = _init_config.network_id;
|
||||
queue.data.message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
|
||||
memcpy(queue.data.original_sender_mac, _self_mac, 6);
|
||||
if (target == NULL)
|
||||
{
|
||||
queue.data.message_type = BROADCAST;
|
||||
memcpy(queue.data.original_target_mac, _broadcast_mac, 6);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (memcmp(target, _broadcast_mac, 6) != 0)
|
||||
{
|
||||
queue.data.message_type = UNICAST;
|
||||
memcpy(queue.data.original_target_mac, target, 6);
|
||||
}
|
||||
else
|
||||
{
|
||||
queue.data.message_type = BROADCAST;
|
||||
memcpy(queue.data.original_target_mac, _broadcast_mac, 6);
|
||||
}
|
||||
}
|
||||
memcpy(queue.data.payload, data, data_len);
|
||||
queue.data.payload_len = data_len;
|
||||
if (target == NULL)
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC FF:FF:FF:FF:FF:FF to queue success.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(target));
|
||||
}
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
|
||||
{
|
||||
if (status == ESP_NOW_SEND_SUCCESS)
|
||||
{
|
||||
xEventGroupSetBits(_event_group_handle, DATA_SEND_SUCCESS);
|
||||
}
|
||||
else
|
||||
{
|
||||
xEventGroupSetBits(_event_group_handle, DATA_SEND_FAIL);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
static void _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len)
|
||||
#else
|
||||
static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len)
|
||||
#endif
|
||||
{
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(mac_addr));
|
||||
#else
|
||||
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(esp_now_info->src_addr));
|
||||
#endif
|
||||
if (uxQueueSpacesAvailable(_queue_handle) < (_init_config.queue_size - 2))
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Queue is almost full.");
|
||||
return;
|
||||
}
|
||||
if (data_len == sizeof(_queue_t) - 12)
|
||||
{
|
||||
_queue_t queue = {0};
|
||||
queue.id = ON_RECV;
|
||||
memcpy(&queue.data, data, data_len);
|
||||
if (memcmp(&queue.data.network_id, &_init_config.network_id, sizeof(queue.data.network_id)) != 0)
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Incorrect mesh network ID.");
|
||||
return;
|
||||
}
|
||||
for (uint16_t i = 0; i < zh_vector_get_size(&_id_vector); ++i)
|
||||
{
|
||||
uint32_t *message_id = zh_vector_get_item(&_id_vector, i);
|
||||
if (memcmp(&queue.data.message_id, message_id, sizeof(queue.data.message_id)) == 0)
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Repeat message received.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (xSemaphoreTake(_id_vector_mutex, portTICK_PERIOD_MS) == pdTRUE)
|
||||
{
|
||||
zh_vector_push_back(&_id_vector, &queue.data.message_id);
|
||||
if (zh_vector_get_size(&_id_vector) > _init_config.id_vector_size)
|
||||
{
|
||||
zh_vector_delete_item(&_id_vector, 0);
|
||||
}
|
||||
xSemaphoreGive(_id_vector_mutex);
|
||||
}
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
memcpy(queue.data.sender_mac, mac_addr, 6);
|
||||
#else
|
||||
memcpy(queue.data.sender_mac, esp_now_info->src_addr, 6);
|
||||
#endif
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(mac_addr));
|
||||
#else
|
||||
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(esp_now_info->src_addr));
|
||||
#endif
|
||||
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Incorrect ESP-NOW data size.");
|
||||
}
|
||||
}
|
||||
|
||||
static void _processing(void *pvParameter)
|
||||
{
|
||||
_queue_t queue = {0};
|
||||
while (xQueueReceive(_queue_handle, &queue, portMAX_DELAY) == pdTRUE)
|
||||
{
|
||||
bool flag = false;
|
||||
switch (queue.id)
|
||||
{
|
||||
case TO_SEND:
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processing begin.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
esp_now_peer_info_t *peer = heap_caps_malloc(sizeof(esp_now_peer_info_t), MALLOC_CAP_8BIT);
|
||||
if (peer == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Memory allocation fail or no free memory in the heap.");
|
||||
heap_caps_free(peer);
|
||||
break;
|
||||
}
|
||||
memset(peer, 0, sizeof(esp_now_peer_info_t));
|
||||
peer->ifidx = _init_config.wifi_interface;
|
||||
if (queue.data.message_type == BROADCAST || queue.data.message_type == SEARCH_REQUEST || queue.data.message_type == SEARCH_RESPONSE)
|
||||
{
|
||||
memcpy(peer->peer_addr, _broadcast_mac, 6);
|
||||
if (memcmp(queue.data.original_sender_mac, _self_mac, 6) == 0)
|
||||
{
|
||||
if (xSemaphoreTake(_id_vector_mutex, portTICK_PERIOD_MS) == pdTRUE)
|
||||
{
|
||||
zh_vector_push_back(&_id_vector, &queue.data.message_id);
|
||||
if (zh_vector_get_size(&_id_vector) > _init_config.id_vector_size)
|
||||
{
|
||||
zh_vector_delete_item(&_id_vector, 0);
|
||||
}
|
||||
xSemaphoreGive(_id_vector_mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Checking routing table to MAC %02X:%02X:%02X:%02X:%02X:%02X.", MAC2STR(queue.data.original_target_mac));
|
||||
for (uint16_t i = 0; i < zh_vector_get_size(&_route_vector); ++i)
|
||||
{
|
||||
_routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
|
||||
if (memcmp(queue.data.original_target_mac, routing_table->original_target_mac, 6) == 0)
|
||||
{
|
||||
memcpy(peer->peer_addr, routing_table->intermediate_target_mac, 6);
|
||||
flag = true;
|
||||
ESP_LOGI(TAG, "Routing to MAC %02X:%02X:%02X:%02X:%02X:%02X is found. Forwarding via MAC %02X:%02X:%02X:%02X:%02X:%02X.", MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag == false)
|
||||
{
|
||||
ESP_LOGI(TAG, "Routing to MAC %02X:%02X:%02X:%02X:%02X:%02X not found.", MAC2STR(queue.data.original_target_mac));
|
||||
if (queue.data.message_type == UNICAST)
|
||||
{
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X transferred to routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X transferred to routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
queue.id = WAIT_ROUTE;
|
||||
queue.time = esp_timer_get_time() / 1000;
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
ESP_LOGI(TAG, "System message for routing request from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
queue.id = TO_SEND;
|
||||
queue.data.message_type = SEARCH_REQUEST;
|
||||
memcpy(queue.data.original_sender_mac, _self_mac, 6);
|
||||
queue.data.payload_len = 0;
|
||||
memset(queue.data.payload, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
|
||||
queue.data.message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
heap_caps_free(peer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (esp_now_add_peer(peer) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Internal error with adding peer.");
|
||||
heap_caps_free(peer);
|
||||
break;
|
||||
}
|
||||
zh_network_event_on_send_t *on_send = heap_caps_malloc(sizeof(zh_network_event_on_send_t), MALLOC_CAP_8BIT);
|
||||
if (on_send == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Memory allocation fail or no free memory in the heap.");
|
||||
heap_caps_free(peer);
|
||||
heap_caps_free(on_send);
|
||||
break;
|
||||
}
|
||||
memset(on_send, 0, sizeof(zh_network_event_on_send_t));
|
||||
memcpy(on_send->mac_addr, queue.data.original_target_mac, 6);
|
||||
if (esp_now_send((uint8_t *)peer->peer_addr, (uint8_t *)&queue.data, sizeof(_queue_t) - 12) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
heap_caps_free(peer);
|
||||
heap_caps_free(on_send);
|
||||
break;
|
||||
}
|
||||
EventBits_t bit = xEventGroupWaitBits(_event_group_handle, DATA_SEND_SUCCESS | DATA_SEND_FAIL, pdTRUE, pdFALSE, 50 / portTICK_PERIOD_MS);
|
||||
if ((bit & DATA_SEND_SUCCESS) != 0)
|
||||
{
|
||||
if (memcmp(queue.data.original_sender_mac, _self_mac, 6) == 0)
|
||||
{
|
||||
if (queue.data.message_type == BROADCAST)
|
||||
{
|
||||
ESP_LOGI(TAG, "Broadcast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
on_send->status = ZH_NETWORK_SEND_SUCCESS;
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_SEND_EVENT, on_send, sizeof(zh_network_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
}
|
||||
if (queue.data.message_type == SEARCH_REQUEST)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for routing request from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
if (queue.data.message_type == SEARCH_RESPONSE)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for routing response from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
if (queue.data.message_type == DELIVERY_CONFIRM)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X via MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
if (queue.data.message_type == UNICAST)
|
||||
{
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X via MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X transferred to confirmation message waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
queue.id = WAIT_RESPONSE;
|
||||
queue.time = esp_timer_get_time() / 1000;
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (queue.data.message_type == BROADCAST)
|
||||
{
|
||||
ESP_LOGI(TAG, "Broadcast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
if (queue.data.message_type == SEARCH_REQUEST)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for routing request from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
if (queue.data.message_type == SEARCH_RESPONSE)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for routing response from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
if (queue.data.message_type == DELIVERY_CONFIRM)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X via MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
if (queue.data.message_type == UNICAST)
|
||||
{
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X via MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (memcmp(queue.data.original_target_mac, _broadcast_mac, 6) != 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "Routing to MAC %02X:%02X:%02X:%02X:%02X:%02X via MAC %02X:%02X:%02X:%02X:%02X:%02X is incorrect.", MAC2STR(queue.data.original_target_mac), MAC2STR(peer->peer_addr));
|
||||
for (uint16_t i = 0; i < zh_vector_get_size(&_route_vector); ++i)
|
||||
{
|
||||
_routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
|
||||
if (memcmp(queue.data.original_target_mac, routing_table->original_target_mac, 6) == 0)
|
||||
{
|
||||
zh_vector_delete_item(&_route_vector, i);
|
||||
}
|
||||
}
|
||||
if (queue.data.message_type == UNICAST)
|
||||
{
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X transferred to routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
if (queue.data.message_type == DELIVERY_CONFIRM)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X transferred to routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
queue.id = WAIT_ROUTE;
|
||||
queue.time = esp_timer_get_time() / 1000;
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
ESP_LOGI(TAG, "System message for routing request to MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue.", MAC2STR(queue.data.original_target_mac));
|
||||
queue.id = TO_SEND;
|
||||
queue.data.message_type = SEARCH_REQUEST;
|
||||
memcpy(queue.data.original_sender_mac, _self_mac, 6);
|
||||
queue.data.payload_len = 0;
|
||||
memset(queue.data.payload, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
|
||||
queue.data.message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
|
||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
}
|
||||
}
|
||||
esp_now_del_peer(peer->peer_addr);
|
||||
heap_caps_free(on_send);
|
||||
heap_caps_free(peer);
|
||||
break;
|
||||
case ON_RECV:
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processing begin.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
switch (queue.data.message_type)
|
||||
{
|
||||
case BROADCAST:
|
||||
ESP_LOGI(TAG, "Broadcast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
zh_network_event_on_recv_t *on_recv = heap_caps_malloc(sizeof(zh_network_event_on_recv_t), MALLOC_CAP_8BIT);
|
||||
if (on_recv == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
heap_caps_free(on_recv);
|
||||
break;
|
||||
}
|
||||
memset(on_recv, 0, sizeof(zh_network_event_on_recv_t));
|
||||
memcpy(on_recv->mac_addr, queue.data.original_sender_mac, 6);
|
||||
on_recv->data_len = queue.data.payload_len;
|
||||
on_recv->data = heap_caps_malloc(queue.data.payload_len, MALLOC_CAP_8BIT);
|
||||
if (on_recv->data == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
heap_caps_free(on_recv);
|
||||
heap_caps_free(on_recv->data);
|
||||
break;
|
||||
}
|
||||
memset(on_recv->data, 0, queue.data.payload_len);
|
||||
memcpy(on_recv->data, queue.data.payload, queue.data.payload_len);
|
||||
ESP_LOGI(TAG, "Broadcast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue for resend to all nodes.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_RECV_EVENT, on_recv, sizeof(zh_network_event_on_recv_t) + on_recv->data_len + sizeof(on_recv->data_len), portTICK_PERIOD_MS) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
heap_caps_free(on_recv);
|
||||
queue.id = TO_SEND;
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
break;
|
||||
case UNICAST:
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (memcmp(queue.data.original_target_mac, _self_mac, 6) == 0)
|
||||
{
|
||||
zh_network_event_on_recv_t *on_recv = heap_caps_malloc(sizeof(zh_network_event_on_recv_t), MALLOC_CAP_8BIT);
|
||||
if (on_recv == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
heap_caps_free(on_recv);
|
||||
break;
|
||||
}
|
||||
memset(on_recv, 0, sizeof(zh_network_event_on_recv_t));
|
||||
memcpy(on_recv->mac_addr, queue.data.original_sender_mac, 6);
|
||||
on_recv->data_len = queue.data.payload_len;
|
||||
on_recv->data = heap_caps_malloc(queue.data.payload_len, MALLOC_CAP_8BIT);
|
||||
if (on_recv->data == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
heap_caps_free(on_recv);
|
||||
heap_caps_free(on_recv->data);
|
||||
break;
|
||||
}
|
||||
memset(on_recv->data, 0, queue.data.payload_len);
|
||||
memcpy(on_recv->data, queue.data.payload, queue.data.payload_len);
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_RECV_EVENT, on_recv, sizeof(zh_network_event_on_recv_t) + on_recv->data_len + sizeof(on_recv->data_len), portTICK_PERIOD_MS) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
heap_caps_free(on_recv);
|
||||
queue.id = TO_SEND;
|
||||
queue.data.message_type = DELIVERY_CONFIRM;
|
||||
memcpy(queue.data.original_target_mac, queue.data.original_sender_mac, 6);
|
||||
memcpy(queue.data.original_sender_mac, _self_mac, 6);
|
||||
queue.data.payload_len = 0;
|
||||
memset(queue.data.payload, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
|
||||
queue.data.confirm_id = queue.data.message_id;
|
||||
queue.data.message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
|
||||
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue for forwarding.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
queue.id = TO_SEND;
|
||||
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
break;
|
||||
case DELIVERY_CONFIRM:
|
||||
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (memcmp(queue.data.original_target_mac, _self_mac, 6) == 0)
|
||||
{
|
||||
zh_vector_push_back(&_response_vector, &queue.data.confirm_id);
|
||||
if (zh_vector_get_size(&_response_vector) > _init_config.queue_size)
|
||||
{
|
||||
zh_vector_delete_item(&_response_vector, 0);
|
||||
}
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
break;
|
||||
}
|
||||
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X fto MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue for forwarding.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
queue.id = TO_SEND;
|
||||
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
break;
|
||||
case SEARCH_REQUEST:
|
||||
ESP_LOGI(TAG, "System message for routing request from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
for (uint16_t i = 0; i < zh_vector_get_size(&_route_vector); ++i)
|
||||
{
|
||||
_routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
|
||||
if (memcmp(queue.data.original_target_mac, routing_table->original_target_mac, 6) == 0)
|
||||
{
|
||||
zh_vector_delete_item(&_route_vector, i);
|
||||
}
|
||||
}
|
||||
{ // Just to avoid the compiler warning.
|
||||
_routing_table_t routing_table = {0};
|
||||
memcpy(routing_table.original_target_mac, queue.data.original_sender_mac, 6);
|
||||
memcpy(routing_table.intermediate_target_mac, queue.data.sender_mac, 6);
|
||||
zh_vector_push_back(&_route_vector, &routing_table);
|
||||
}
|
||||
if (zh_vector_get_size(&_route_vector) > _init_config.route_vector_size)
|
||||
{
|
||||
zh_vector_delete_item(&_route_vector, 0);
|
||||
}
|
||||
if (memcmp(queue.data.original_target_mac, _self_mac, 6) == 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for routing response from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X added to the queue.", MAC2STR(queue.data.original_target_mac), MAC2STR(queue.data.original_sender_mac));
|
||||
queue.id = TO_SEND;
|
||||
queue.data.message_type = SEARCH_RESPONSE;
|
||||
memcpy(queue.data.original_target_mac, queue.data.original_sender_mac, 6);
|
||||
memcpy(queue.data.original_sender_mac, _self_mac, 6);
|
||||
queue.data.payload_len = 0;
|
||||
memset(queue.data.payload, 0, ZH_NETWORK_MAX_MESSAGE_SIZE);
|
||||
queue.data.message_id = abs(esp_random()); // It is not clear why esp_random() sometimes gives negative values.
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
ESP_LOGI(TAG, "System message for routing request to MAC %02X:%02X:%02X:%02X:%02X:%02X from MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue for resend to all nodes.", MAC2STR(queue.data.original_target_mac), MAC2STR(queue.data.original_sender_mac));
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
queue.id = TO_SEND;
|
||||
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
break;
|
||||
case SEARCH_RESPONSE:
|
||||
ESP_LOGI(TAG, "System message for routing response from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
for (uint16_t i = 0; i < zh_vector_get_size(&_route_vector); ++i)
|
||||
{
|
||||
_routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
|
||||
if (memcmp(queue.data.original_target_mac, routing_table->original_target_mac, 6) == 0)
|
||||
{
|
||||
zh_vector_delete_item(&_route_vector, i);
|
||||
}
|
||||
}
|
||||
{ // Just to avoid the compiler warning.
|
||||
_routing_table_t routing_table = {0};
|
||||
memcpy(routing_table.original_target_mac, queue.data.original_sender_mac, 6);
|
||||
memcpy(routing_table.intermediate_target_mac, queue.data.sender_mac, 6);
|
||||
zh_vector_push_back(&_route_vector, &routing_table);
|
||||
}
|
||||
if (zh_vector_get_size(&_route_vector) > _init_config.route_vector_size)
|
||||
{
|
||||
zh_vector_delete_item(&_route_vector, 0);
|
||||
}
|
||||
if (memcmp(queue.data.original_target_mac, _self_mac, 6) != 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for routing response from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X added to queue for resend to all nodes.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
queue.id = TO_SEND;
|
||||
if (xQueueSendToFront(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case WAIT_RESPONSE:
|
||||
for (uint16_t i = 0; i < zh_vector_get_size(&_response_vector); ++i)
|
||||
{
|
||||
uint32_t *message_id = zh_vector_get_item(&_response_vector, i);
|
||||
if (memcmp(&queue.data.message_id, message_id, sizeof(queue.data.message_id)) == 0)
|
||||
{
|
||||
zh_vector_delete_item(&_response_vector, i);
|
||||
zh_network_event_on_send_t *on_send = heap_caps_malloc(sizeof(zh_network_event_on_send_t), MALLOC_CAP_8BIT);
|
||||
if (on_send == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
heap_caps_free(on_send);
|
||||
break;
|
||||
}
|
||||
memset(on_send, 0, sizeof(zh_network_event_on_send_t));
|
||||
memcpy(on_send->mac_addr, queue.data.original_target_mac, 6);
|
||||
on_send->status = ZH_NETWORK_SEND_SUCCESS;
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from confirmation message waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_SEND_EVENT, on_send, sizeof(zh_network_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
heap_caps_free(on_send);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag == false)
|
||||
{
|
||||
if ((esp_timer_get_time() / 1000 - queue.time) > _init_config.max_waiting_time)
|
||||
{
|
||||
ESP_LOGW(TAG, "Time for waiting confirmation message from MAC %02X:%02X:%02X:%02X:%02X:%02X is expired.", MAC2STR(queue.data.original_target_mac));
|
||||
if (memcmp(queue.data.original_sender_mac, _self_mac, 6) == 0)
|
||||
{
|
||||
zh_network_event_on_send_t *on_send = heap_caps_malloc(sizeof(zh_network_event_on_send_t), MALLOC_CAP_8BIT);
|
||||
if (on_send == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
heap_caps_free(on_send);
|
||||
break;
|
||||
}
|
||||
memset(on_send, 0, sizeof(zh_network_event_on_send_t));
|
||||
memcpy(on_send->mac_addr, queue.data.original_target_mac, 6);
|
||||
on_send->status = ZH_NETWORK_SEND_FAIL;
|
||||
ESP_LOGE(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from confirmation message waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_SEND_EVENT, on_send, sizeof(zh_network_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
heap_caps_free(on_send);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case WAIT_ROUTE:
|
||||
for (uint16_t i = 0; i < zh_vector_get_size(&_route_vector); ++i)
|
||||
{
|
||||
_routing_table_t *routing_table = zh_vector_get_item(&_route_vector, i);
|
||||
if (memcmp(queue.data.original_target_mac, routing_table->original_target_mac, 6) == 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "Routing to MAC %02X:%02X:%02X:%02X:%02X:%02X is received.", MAC2STR(queue.data.original_target_mac));
|
||||
if (queue.data.message_type == UNICAST)
|
||||
{
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from routing waiting list and added to queue.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
if (queue.data.message_type == DELIVERY_CONFIRM)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from routing waiting list and added to queue.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
queue.id = TO_SEND;
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag == false)
|
||||
{
|
||||
if ((esp_timer_get_time() / 1000 - queue.time) > _init_config.max_waiting_time)
|
||||
{
|
||||
ESP_LOGW(TAG, "Time for waiting routing to MAC %02X:%02X:%02X:%02X:%02X:%02X is expired.", MAC2STR(queue.data.original_target_mac));
|
||||
if (memcmp(queue.data.original_sender_mac, _self_mac, 6) == 0)
|
||||
{
|
||||
zh_network_event_on_send_t *on_send = heap_caps_malloc(sizeof(zh_network_event_on_send_t), MALLOC_CAP_8BIT);
|
||||
if (on_send == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
heap_caps_free(on_send);
|
||||
break;
|
||||
}
|
||||
memset(on_send, 0, sizeof(zh_network_event_on_send_t));
|
||||
memcpy(on_send->mac_addr, queue.data.original_target_mac, 6);
|
||||
on_send->status = ZH_NETWORK_SEND_FAIL;
|
||||
ESP_LOGE(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
if (esp_event_post(ZH_NETWORK, ZH_NETWORK_ON_SEND_EVENT, on_send, sizeof(zh_network_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
heap_caps_free(on_send);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (queue.data.message_type == UNICAST)
|
||||
{
|
||||
ESP_LOGI(TAG, "Unicast message from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
if (queue.data.message_type == DELIVERY_CONFIRM)
|
||||
{
|
||||
ESP_LOGI(TAG, "System message for message receiving confirmation from MAC %02X:%02X:%02X:%02X:%02X:%02X to MAC %02X:%02X:%02X:%02X:%02X:%02X removed from routing waiting list.", MAC2STR(queue.data.original_sender_mac), MAC2STR(queue.data.original_target_mac));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error.");
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
1
components/zh_onewire/CMakeLists.txt
Normal file
1
components/zh_onewire/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
idf_component_register(SRCS "zh_onewire.c" INCLUDE_DIRS "include" REQUIRES driver)
|
73
components/zh_onewire/LICENSE
Normal file
73
components/zh_onewire/LICENSE
Normal file
@ -0,0 +1,73 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
62
components/zh_onewire/README.md
Normal file
62
components/zh_onewire/README.md
Normal file
@ -0,0 +1,62 @@
|
||||
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for 1-Wire interface
|
||||
|
||||
## Tested on
|
||||
|
||||
1. ESP8266 RTOS_SDK v3.4
|
||||
2. ESP32 ESP-IDF v5.2
|
||||
|
||||
## [Function description](http://zh-onewire.zh.com.ru)
|
||||
|
||||
## Using
|
||||
|
||||
In an existing project, run the following command to install the component:
|
||||
|
||||
```text
|
||||
cd ../your_project/components
|
||||
git clone http://git.zh.com.ru/alexey.zholtikov/zh_onewire.git
|
||||
```
|
||||
|
||||
In the application, add the component:
|
||||
|
||||
```c
|
||||
#include "zh_onewire.h"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
Search 1-Wire devices on bus:
|
||||
|
||||
```c
|
||||
#include "zh_onewire.h"
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
uint8_t *rom = NULL;
|
||||
zh_onewire_init(GPIO_NUM_5);
|
||||
if (zh_onewire_reset() != ESP_OK)
|
||||
{
|
||||
printf("There are no 1-Wire devices available on the bus.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
zh_onewire_search_rom_init();
|
||||
for (;;)
|
||||
{
|
||||
rom = zh_onewire_search_rom_next();
|
||||
if (rom == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("Found device ROM: ");
|
||||
for (uint8_t i = 0; i < 8; ++i)
|
||||
{
|
||||
printf("%X ", *(rom++));
|
||||
}
|
||||
rom -= 8;
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Any [feedback](mailto:github@azholtikov.ru) will be gladly accepted.
|
0
components/zh_onewire/component.mk
Normal file
0
components/zh_onewire/component.mk
Normal file
112
components/zh_onewire/include/zh_onewire.h
Normal file
112
components/zh_onewire/include/zh_onewire.h
Normal file
@ -0,0 +1,112 @@
|
||||
/**
|
||||
* @file
|
||||
* Header file for the zh_onewire component.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "stdio.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "rom/ets_sys.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Initialize 1-Wire interface.
|
||||
*
|
||||
* @param[in] pin 1-Wire bus gpio connection.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if initialization was successful
|
||||
* - ESP_ERR_INVALID_ARG if select incorrect gpio number
|
||||
*/
|
||||
esp_err_t zh_onewire_init(const uint8_t pin);
|
||||
|
||||
/**
|
||||
* @brief Reset command for all 1-Wire devices on bus.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if reset was successful
|
||||
* - ESP_ERR_INVALID_RESPONSE if the bus is busy
|
||||
* - ESP_ERR_TIMEOUT if there are no 1-Wire devices available on the bus or the devices are not responding
|
||||
*/
|
||||
esp_err_t zh_onewire_reset(void);
|
||||
|
||||
/**
|
||||
* @brief Send one byte to 1-Wire device.
|
||||
*
|
||||
* @param[in] byte Byte value.
|
||||
*
|
||||
*/
|
||||
void zh_onewire_send_byte(uint8_t byte);
|
||||
|
||||
/**
|
||||
* @brief Read one byte from 1-Wire device.
|
||||
*
|
||||
* @return
|
||||
* - Byte value
|
||||
*/
|
||||
uint8_t zh_onewire_read_byte(void);
|
||||
|
||||
/**
|
||||
* @brief Initializes the bus master to address all 1-Wire devices on the bus.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if initialization was successful
|
||||
* - ESP_FAIL if there are no 1-Wire devices available on the bus or the devices are not responding
|
||||
*/
|
||||
esp_err_t zh_onewire_skip_rom(void);
|
||||
|
||||
/**
|
||||
* @brief Read rom value if only one 1-Wire device is present on the bus.
|
||||
*
|
||||
* @param[out] buf Pointer to a buffer containing an eight-byte rom value.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if read was successful
|
||||
* - ESP_FAIL if there are no 1-Wire devices available on the bus or the devices are not responding
|
||||
* - ESP_ERR_INVALID_CRC if more than one 1-Wire device is present on the bus
|
||||
*/
|
||||
esp_err_t zh_onewire_read_rom(uint8_t *buf);
|
||||
|
||||
/**
|
||||
* @brief Initialize the bus master to address a specific 1-Wire device on bus.
|
||||
*
|
||||
* @param[in] data Pointer to a buffer containing an eight-byte rom value.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if initialization was successful
|
||||
* - ESP_FAIL if there are no 1-Wire devices available on the bus or the devices are not responding
|
||||
*/
|
||||
esp_err_t zh_onewire_match_rom(const uint8_t *data);
|
||||
|
||||
/**
|
||||
* @brief Initialize search 1-Wire devices on bus.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if initialization was successful
|
||||
*/
|
||||
esp_err_t zh_onewire_search_rom_init(void);
|
||||
|
||||
/**
|
||||
* @brief Search next 1-Wire device on bus.
|
||||
*
|
||||
* @attention Initialize search 1-Wire devices on bus must be initialized first. @code zh_onewire_search_rom_init() @endcode
|
||||
*
|
||||
* @return
|
||||
* - Pointer to a buffer containing an eight-byte rom value
|
||||
* - NULL if the search is terminated or if there are no 1-Wire devices available on the bus or the devices are not responding
|
||||
*/
|
||||
uint8_t *zh_onewire_search_rom_next(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
1
components/zh_onewire/version.txt
Normal file
1
components/zh_onewire/version.txt
Normal file
@ -0,0 +1 @@
|
||||
1.1.4
|
335
components/zh_onewire/zh_onewire.c
Normal file
335
components/zh_onewire/zh_onewire.c
Normal file
@ -0,0 +1,335 @@
|
||||
/**
|
||||
* @file
|
||||
* The main code of the zh_onewire component.
|
||||
*/
|
||||
|
||||
#include "zh_onewire.h"
|
||||
|
||||
/// \cond
|
||||
#define MASTER_RESET_PULSE_DURATION 480 // Reset time high. Reset time low.
|
||||
#define RESPONSE_MAX_DURATION 60 // Presence detect high.
|
||||
#define PRESENCE_PULSE_MAX_DURATION 240 // Presence detect low.
|
||||
#define RECOVERY_DURATION 1 // Bus recovery time.
|
||||
#define TIME_SLOT_START_DURATION 1 // Time slot start.
|
||||
#define TIME_SLOT_DURATION 120 // Time slot.
|
||||
#define VALID_DATA_DURATION 15 // Valid data duration.
|
||||
|
||||
#define SKIP_ROM 0xCC
|
||||
#define MATCH_ROM 0x55
|
||||
#define READ_ROM 0x33
|
||||
#define SEARCH_ROM 0xF0
|
||||
|
||||
#define pgm_read_byte(addr) (*(const uint8_t *)(addr))
|
||||
/// \endcond
|
||||
|
||||
static uint8_t _read_bit(void);
|
||||
static void _send_bit(const uint8_t bit);
|
||||
|
||||
/// \cond
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#define esp_delay_us(x) os_delay_us(x)
|
||||
#else
|
||||
#define esp_delay_us(x) esp_rom_delay_us(x)
|
||||
#endif
|
||||
/// \endcond
|
||||
|
||||
static const char *TAG = "zh_onewire";
|
||||
|
||||
static uint8_t _pin;
|
||||
static uint8_t _rom[8];
|
||||
static uint8_t _rom_fork_bit = 0xFF;
|
||||
|
||||
static const uint8_t _rom_crc_table[] = {
|
||||
0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41,
|
||||
0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e, 0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc,
|
||||
0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0, 0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62,
|
||||
0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d, 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff,
|
||||
0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5, 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07,
|
||||
0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58, 0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a,
|
||||
0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6, 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24,
|
||||
0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b, 0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9,
|
||||
0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f, 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd,
|
||||
0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92, 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50,
|
||||
0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c, 0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee,
|
||||
0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1, 0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73,
|
||||
0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49, 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b,
|
||||
0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16,
|
||||
0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8,
|
||||
0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35};
|
||||
|
||||
esp_err_t zh_onewire_init(const uint8_t pin)
|
||||
{
|
||||
ESP_LOGI(TAG, "Onewire initialization begin.");
|
||||
_pin = pin;
|
||||
gpio_config_t config;
|
||||
config.intr_type = GPIO_INTR_DISABLE;
|
||||
config.mode = GPIO_MODE_INPUT;
|
||||
config.pin_bit_mask = (1ULL << _pin);
|
||||
config.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
config.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
if (gpio_config(&config) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Onewire initialization fail. Incorrect GPIO number.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
ESP_LOGI(TAG, "Onewire initialization success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_onewire_reset(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Onewire reset begin.");
|
||||
if (gpio_get_level(_pin) != 1)
|
||||
{
|
||||
ESP_LOGE(TAG, "Onewire reset fail. Bus is busy.");
|
||||
return ESP_ERR_INVALID_RESPONSE;
|
||||
}
|
||||
gpio_set_direction(_pin, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(_pin, 0);
|
||||
esp_delay_us(MASTER_RESET_PULSE_DURATION);
|
||||
gpio_set_level(_pin, 1);
|
||||
gpio_set_direction(_pin, GPIO_MODE_INPUT);
|
||||
esp_delay_us(RECOVERY_DURATION);
|
||||
uint8_t response_time = 0;
|
||||
while (gpio_get_level(_pin) == 1)
|
||||
{
|
||||
if (response_time > RESPONSE_MAX_DURATION)
|
||||
{
|
||||
ESP_LOGE(TAG, "Onewire reset fail. Timeout exceeded.");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
++response_time;
|
||||
esp_delay_us(1);
|
||||
}
|
||||
uint8_t presence_time = 0;
|
||||
while (gpio_get_level(_pin) == 0)
|
||||
{
|
||||
if (presence_time > PRESENCE_PULSE_MAX_DURATION)
|
||||
{
|
||||
ESP_LOGE(TAG, "Onewire reset fail. Timeout exceeded.");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
++presence_time;
|
||||
esp_delay_us(1);
|
||||
}
|
||||
esp_delay_us(MASTER_RESET_PULSE_DURATION - response_time - presence_time);
|
||||
ESP_LOGI(TAG, "Onewire reset success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void zh_onewire_send_byte(uint8_t byte)
|
||||
{
|
||||
for (uint8_t i = 0; i < 8; ++i)
|
||||
{
|
||||
_send_bit(byte & 1);
|
||||
byte >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t zh_onewire_skip_rom(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Onewire skip ROM begin.");
|
||||
if (zh_onewire_reset() != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Onewire skip ROM fail.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
zh_onewire_send_byte(SKIP_ROM);
|
||||
ESP_LOGI(TAG, "Onewire skip ROM success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_onewire_read_rom(uint8_t *buf)
|
||||
{
|
||||
ESP_LOGI(TAG, "Onewire read ROM begin.");
|
||||
if (zh_onewire_reset() != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Onewire read ROM fail.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
zh_onewire_send_byte(READ_ROM);
|
||||
uint8_t crc = 0;
|
||||
for (uint8_t i = 0; i < 8; ++i)
|
||||
{
|
||||
*buf = zh_onewire_read_byte();
|
||||
crc = pgm_read_byte(&_rom_crc_table[crc ^ *buf]);
|
||||
++buf;
|
||||
}
|
||||
if (crc != 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "Onewire read ROM fail. Invalid CRC.");
|
||||
return ESP_ERR_INVALID_CRC;
|
||||
}
|
||||
ESP_LOGI(TAG, "Onewire read ROM success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_onewire_match_rom(const uint8_t *data)
|
||||
{
|
||||
ESP_LOGI(TAG, "Onewire match ROM begin.");
|
||||
if (zh_onewire_reset() != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Onewire match ROM fail.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
zh_onewire_send_byte(MATCH_ROM);
|
||||
for (uint8_t i = 0; i < 8; ++i)
|
||||
{
|
||||
zh_onewire_send_byte(*(data++));
|
||||
}
|
||||
ESP_LOGI(TAG, "Onewire match ROM success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_onewire_search_rom_init(void)
|
||||
{
|
||||
for (uint8_t i = 0; i < 8; ++i)
|
||||
{
|
||||
_rom[i] = 0;
|
||||
}
|
||||
_rom_fork_bit = 65;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
uint8_t *zh_onewire_search_rom_next(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Onewire search ROM begin.");
|
||||
if (_rom_fork_bit == 0xFF)
|
||||
{
|
||||
ESP_LOGE(TAG, "Onewire search ROM not initialized.");
|
||||
return NULL;
|
||||
}
|
||||
if (_rom_fork_bit == 0)
|
||||
{
|
||||
ESP_LOGI(TAG, "Onewire search ROM terminated.");
|
||||
return NULL;
|
||||
}
|
||||
if (zh_onewire_reset() != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Onewire search ROM fail.");
|
||||
return NULL;
|
||||
}
|
||||
uint8_t bit_position = 8;
|
||||
uint8_t *p_prev_bit = &_rom[0];
|
||||
uint8_t prev_bit = *p_prev_bit;
|
||||
uint8_t next_bit = 0;
|
||||
uint8_t counter = 1;
|
||||
zh_onewire_send_byte(SEARCH_ROM);
|
||||
uint8_t new_fork = 0;
|
||||
for (;;)
|
||||
{
|
||||
uint8_t bit_not_0 = _read_bit();
|
||||
uint8_t bit_not_1 = _read_bit();
|
||||
if (bit_not_0 == 0)
|
||||
{
|
||||
if (bit_not_1 == 0)
|
||||
{
|
||||
if (counter < _rom_fork_bit)
|
||||
{
|
||||
if (prev_bit == 1)
|
||||
{
|
||||
next_bit |= 0x80;
|
||||
}
|
||||
else
|
||||
{
|
||||
new_fork = counter;
|
||||
}
|
||||
}
|
||||
else if (counter == _rom_fork_bit)
|
||||
{
|
||||
next_bit |= 0x80;
|
||||
}
|
||||
else
|
||||
{
|
||||
new_fork = counter;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (bit_not_1 == 0)
|
||||
{
|
||||
next_bit |= 0x80;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Onewire search ROM terminated.");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
_send_bit(next_bit & 0x80);
|
||||
--bit_position;
|
||||
if (bit_position == 0)
|
||||
{
|
||||
*p_prev_bit = next_bit;
|
||||
if (counter >= 64)
|
||||
{
|
||||
break;
|
||||
}
|
||||
next_bit = 0;
|
||||
++p_prev_bit;
|
||||
prev_bit = *p_prev_bit;
|
||||
bit_position = 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (counter >= 64)
|
||||
{
|
||||
break;
|
||||
}
|
||||
prev_bit >>= 1;
|
||||
next_bit >>= 1;
|
||||
}
|
||||
++counter;
|
||||
}
|
||||
_rom_fork_bit = new_fork;
|
||||
ESP_LOGI(TAG, "Onewire search ROM success.");
|
||||
return &_rom[0];
|
||||
}
|
||||
|
||||
uint8_t zh_onewire_read_byte(void)
|
||||
{
|
||||
uint8_t byte = 0;
|
||||
for (uint8_t i = 0; i < 8; ++i)
|
||||
{
|
||||
byte >>= 1;
|
||||
if (_read_bit() != 0)
|
||||
{
|
||||
byte |= 0x80;
|
||||
}
|
||||
}
|
||||
return byte;
|
||||
}
|
||||
|
||||
static uint8_t _read_bit(void)
|
||||
{
|
||||
gpio_set_direction(_pin, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(_pin, 0);
|
||||
esp_delay_us(TIME_SLOT_START_DURATION);
|
||||
gpio_set_level(_pin, 1);
|
||||
gpio_set_direction(_pin, GPIO_MODE_INPUT);
|
||||
esp_delay_us(VALID_DATA_DURATION - TIME_SLOT_START_DURATION);
|
||||
uint8_t bit = gpio_get_level(_pin);
|
||||
esp_delay_us(TIME_SLOT_DURATION - RECOVERY_DURATION - VALID_DATA_DURATION);
|
||||
return bit;
|
||||
}
|
||||
|
||||
static void _send_bit(const uint8_t bit)
|
||||
{
|
||||
gpio_set_direction(_pin, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(_pin, 0);
|
||||
if (bit == 0)
|
||||
{
|
||||
esp_delay_us(TIME_SLOT_DURATION);
|
||||
gpio_set_level(_pin, 1);
|
||||
gpio_set_direction(_pin, GPIO_MODE_INPUT);
|
||||
esp_delay_us(RECOVERY_DURATION);
|
||||
}
|
||||
else
|
||||
{
|
||||
esp_delay_us(TIME_SLOT_START_DURATION);
|
||||
gpio_set_level(_pin, 1);
|
||||
gpio_set_direction(_pin, GPIO_MODE_INPUT);
|
||||
esp_delay_us(TIME_SLOT_DURATION);
|
||||
}
|
||||
}
|
1
components/zh_vector/CMakeLists.txt
Normal file
1
components/zh_vector/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
idf_component_register(SRCS "zh_vector.c" INCLUDE_DIRS "include")
|
73
components/zh_vector/LICENSE
Normal file
73
components/zh_vector/LICENSE
Normal file
@ -0,0 +1,73 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
84
components/zh_vector/README.md
Normal file
84
components/zh_vector/README.md
Normal file
@ -0,0 +1,84 @@
|
||||
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for vector (dynamic array)
|
||||
|
||||
## Tested on
|
||||
|
||||
1. ESP8266 RTOS_SDK v3.4
|
||||
2. ESP32 ESP-IDF v5.2
|
||||
|
||||
## Features
|
||||
|
||||
1. Support for any data types.
|
||||
2. The maximum size of the veсtor is 65536 elements.
|
||||
3. Support external SPI RAM for ESP32 with external, SPI-connected RAM only.
|
||||
|
||||
## Attention
|
||||
|
||||
1. Do not enable SPI RAM support unless you are sure that SPI RAM is present in your ESP32 module and it is properly configured via menuconfig.
|
||||
2. If SPI RAM is present via menuconfig select Component Config -> ESP PSRAM -> Support for external, SPI-connected RAM and select SPI RAM config -> SPI RAM access method -> Make RAM allocatable using heap_caps_malloc(…, MALLOC_CAP_SPIRAM).
|
||||
|
||||
## [Function description](http://zh-vector.zh.com.ru)
|
||||
|
||||
## Using
|
||||
|
||||
In an existing project, run the following command to install the component:
|
||||
|
||||
```text
|
||||
cd ../your_project/components
|
||||
git clone https://github.com/aZholtikov/zh_vector.git
|
||||
```
|
||||
|
||||
In the application, add the component:
|
||||
|
||||
```c
|
||||
#include "zh_vector.h"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
Create, add, read, modify and delete items:
|
||||
|
||||
```c
|
||||
#include "zh_vector.h"
|
||||
|
||||
zh_vector_t vector = {0};
|
||||
|
||||
char example[10] = {0};
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// esp_log_level_set("zh_vector", ESP_LOG_NONE);
|
||||
zh_vector_init(&vector, sizeof(example), false);
|
||||
printf("Initial vector size is: %d\n", zh_vector_get_size(&vector));
|
||||
strcpy(example, "Item 1");
|
||||
zh_vector_push_back(&vector, &example);
|
||||
strcpy(example, "Item 2");
|
||||
zh_vector_push_back(&vector, &example);
|
||||
strcpy(example, "Item 3");
|
||||
zh_vector_push_back(&vector, &example);
|
||||
strcpy(example, "Item 4");
|
||||
zh_vector_push_back(&vector, &example);
|
||||
strcpy(example, "Item 5");
|
||||
zh_vector_push_back(&vector, &example);
|
||||
printf("Add 5 items. New vector size is: %d\n", zh_vector_get_size(&vector));
|
||||
for (uint16_t i = 0; i < zh_vector_get_size(&vector); ++i)
|
||||
{
|
||||
printf("Item position %d is: %s\n", i, (char *)zh_vector_get_item(&vector, i));
|
||||
}
|
||||
strcpy(example, "Item 6");
|
||||
zh_vector_change_item(&vector, 3, &example);
|
||||
printf("Change item on 3 position.\n");
|
||||
for (uint16_t i = 0; i < zh_vector_get_size(&vector); ++i)
|
||||
{
|
||||
printf("Item position %d is: %s\n", i, (char *)zh_vector_get_item(&vector, i));
|
||||
}
|
||||
zh_vector_delete_item(&vector, 2);
|
||||
printf("Delete item on 2 position. New vector size is: %d\n", zh_vector_get_size(&vector));
|
||||
for (uint16_t i = 0; i < zh_vector_get_size(&vector); ++i)
|
||||
{
|
||||
printf("Item position %d is: %s\n", i, (char *)zh_vector_get_item(&vector, i));
|
||||
}
|
||||
zh_vector_free(&vector);
|
||||
}
|
||||
```
|
||||
|
||||
Any [feedback](mailto:github@azholtikov.ru) will be gladly accepted.
|
0
components/zh_vector/component.mk
Normal file
0
components/zh_vector/component.mk
Normal file
132
components/zh_vector/include/zh_vector.h
Normal file
132
components/zh_vector/include/zh_vector.h
Normal file
@ -0,0 +1,132 @@
|
||||
/**
|
||||
* @file
|
||||
* Header file for the zh_vector component.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_heap_caps.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Main structure of vector data.
|
||||
*
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
void **items; ///< Array of pointers of vector items. @note
|
||||
uint16_t capacity; ///< Maximum capacity of the vector. @note Used to control the size of allocated memory for array of pointers of vector items. Usually equal to the current number of items in the vector. Automatically changes when items are added or deleted.
|
||||
uint16_t size; ///< Number of items in the vector. @note Can be read with zh_vector_get_size().
|
||||
uint16_t unit; ///< Vector item size. @note Possible values from 1 to 65536.
|
||||
bool status; ///< Vector initialization status flag. @note Used to prevent execution of vector functions without prior vector initialization.
|
||||
bool spi_ram; ///< SPI RAM using status flag. @note True - vector will be placed in SPI RAM, false - vector will be placed in RAM.
|
||||
} zh_vector_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize vector.
|
||||
*
|
||||
* @param[in] vector Pointer to main structure of vector data.
|
||||
* @param[in] unit Size of vector item.
|
||||
* @param[in] spiram SPI RAM using (true - vector will be placed in SPI RAM, false - vector will be placed in RAM).
|
||||
*
|
||||
* @attention For using SPI RAM select “Make RAM allocatable using heap_caps_malloc(…, MALLOC_CAP_SPIRAM)” from CONFIG_SPIRAM_USE. For ESP32 with external, SPI-connected RAM only.
|
||||
*
|
||||
* @note If SPI RAM is not supported or not initialised via menuconfig vector will be placed in RAM regardless of the set spiram value.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if initialization was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_INVALID_STATE if vector already initialized with other item size
|
||||
*/
|
||||
esp_err_t zh_vector_init(zh_vector_t *vector, uint16_t unit, bool spiram);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize vector. Free all allocated memory.
|
||||
*
|
||||
* @param[in] vector Pointer to main structure of vector data.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if deinitialization was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_INVALID_STATE if vector not initialized
|
||||
*/
|
||||
esp_err_t zh_vector_free(zh_vector_t *vector);
|
||||
|
||||
/**
|
||||
* @brief Get current vector size.
|
||||
*
|
||||
* @param[in] vector Pointer to main structure of vector data.
|
||||
*
|
||||
* @return
|
||||
* - Vector size
|
||||
* - ESP_FAIL if parameter error or vector not initialized
|
||||
*/
|
||||
esp_err_t zh_vector_get_size(zh_vector_t *vector);
|
||||
|
||||
/**
|
||||
* @brief Add item at end of vector.
|
||||
*
|
||||
* @param[in] vector Pointer to main structure of vector data.
|
||||
* @param[in] item Pointer to item for add.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if add was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_NO_MEM if memory allocation fail or no free memory in the heap
|
||||
* - ESP_ERR_INVALID_STATE if vector not initialized
|
||||
*/
|
||||
esp_err_t zh_vector_push_back(zh_vector_t *vector, void *item);
|
||||
|
||||
/**
|
||||
* @brief Change item by index.
|
||||
*
|
||||
* @param[in] vector Pointer to main structure of vector data.
|
||||
* @param[in] index Index of item for change.
|
||||
* @param[in] item Pointer to new data of item.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if change was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_INVALID_STATE if vector not initialized
|
||||
* - ESP_FAIL if index does not exist
|
||||
*/
|
||||
esp_err_t zh_vector_change_item(zh_vector_t *vector, uint16_t index, void *item);
|
||||
|
||||
/**
|
||||
* @brief Get item by index.
|
||||
*
|
||||
* @param[in] vector Pointer to main structure of vector data.
|
||||
* @param[in] index Index of item for get.
|
||||
*
|
||||
* @return
|
||||
* - Pointer to item
|
||||
* - NULL if parameter error or vector not initialized or if index does not exist
|
||||
*/
|
||||
void *zh_vector_get_item(zh_vector_t *vector, uint16_t index);
|
||||
|
||||
/**
|
||||
* @brief Delete item by index and shifts all elements in vector.
|
||||
*
|
||||
* @param[in] vector Pointer to main structure of vector data.
|
||||
* @param[in] index Index of item for delete.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if delete was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_INVALID_STATE if vector not initialized
|
||||
* - ESP_FAIL if index does not exist
|
||||
*/
|
||||
esp_err_t zh_vector_delete_item(zh_vector_t *vector, uint16_t index);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
1
components/zh_vector/version.txt
Normal file
1
components/zh_vector/version.txt
Normal file
@ -0,0 +1 @@
|
||||
1.4.6
|
245
components/zh_vector/zh_vector.c
Normal file
245
components/zh_vector/zh_vector.c
Normal file
@ -0,0 +1,245 @@
|
||||
/**
|
||||
* @file
|
||||
* The main code of the zh_vector component.
|
||||
*/
|
||||
|
||||
#include "zh_vector.h"
|
||||
|
||||
static const char *TAG = "zh_vector";
|
||||
|
||||
static esp_err_t _resize(zh_vector_t *vector, uint16_t capacity);
|
||||
|
||||
esp_err_t zh_vector_init(zh_vector_t *vector, uint16_t unit, bool spiram)
|
||||
{
|
||||
ESP_LOGI(TAG, "Vector initialization begin.");
|
||||
if (vector == NULL || unit == 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "Vector initialization fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (vector->status == true)
|
||||
{
|
||||
if (vector->unit == unit)
|
||||
{
|
||||
goto ZH_VECTOR_INIT_EXIT;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Vector initialization fail. Vector already initialized with other item size.");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
}
|
||||
vector->capacity = 0;
|
||||
vector->size = 0;
|
||||
vector->unit = unit;
|
||||
vector->status = true;
|
||||
vector->spi_ram = spiram;
|
||||
if (vector->spi_ram == true)
|
||||
{
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
ESP_LOGW(TAG, "SPI RAM not supported. Will be used IRAM.");
|
||||
vector->spi_ram = false;
|
||||
#else
|
||||
#ifndef CONFIG_SPIRAM
|
||||
ESP_LOGW(TAG, "SPI RAM not initialized. Will be used IRAM.");
|
||||
vector->spi_ram = false;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
ZH_VECTOR_INIT_EXIT:
|
||||
if (vector->spi_ram == true)
|
||||
{
|
||||
ESP_LOGI(TAG, "Vector initialization success. Vector located in SPI RAM.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Vector initialization success. Vector located in IRAM.");
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_vector_free(zh_vector_t *vector)
|
||||
{
|
||||
ESP_LOGI(TAG, "Vector deletion begin.");
|
||||
if (vector == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Vector deletion fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (vector->status == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "Vector deletion fail. Vector not initialized.");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
for (uint16_t i = 0; i < vector->size; ++i)
|
||||
{
|
||||
heap_caps_free(vector->items[i]);
|
||||
}
|
||||
vector->status = false;
|
||||
ESP_LOGI(TAG, "Vector deletion success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_vector_get_size(zh_vector_t *vector)
|
||||
{
|
||||
ESP_LOGI(TAG, "Getting vector size begin.");
|
||||
if (vector == NULL || vector->status == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "Getting vector size fail. Invalid argument or vector not initialized.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
ESP_LOGI(TAG, "Getting vector size success. Size: %d", vector->size);
|
||||
return vector->size;
|
||||
}
|
||||
|
||||
esp_err_t zh_vector_push_back(zh_vector_t *vector, void *item)
|
||||
{
|
||||
ESP_LOGI(TAG, "Adding item to vector begin.");
|
||||
if (vector == NULL || item == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding item to vector fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (vector->status == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding item to vector fail. Vector not initialized.");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
if (vector->capacity == vector->size)
|
||||
{
|
||||
if (_resize(vector, vector->capacity + 1) == ESP_ERR_NO_MEM)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding item to vector fail. Memory allocation fail or no free memory in the heap.");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
}
|
||||
if (vector->spi_ram == true)
|
||||
{
|
||||
vector->items[vector->size] = heap_caps_malloc(vector->unit, MALLOC_CAP_SPIRAM);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (vector->unit / sizeof(void *) == 0)
|
||||
{
|
||||
vector->items[vector->size] = heap_caps_malloc(vector->unit, MALLOC_CAP_32BIT);
|
||||
}
|
||||
else
|
||||
{
|
||||
vector->items[vector->size] = heap_caps_malloc(vector->unit, MALLOC_CAP_8BIT);
|
||||
}
|
||||
}
|
||||
if (vector->items[vector->size] == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Adding item to vector fail. Memory allocation fail or no free memory in the heap.");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
memset(vector->items[vector->size], 0, vector->unit);
|
||||
memcpy(vector->items[vector->size++], item, vector->unit);
|
||||
ESP_LOGI(TAG, "Adding item to vector success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_vector_change_item(zh_vector_t *vector, uint16_t index, void *item)
|
||||
{
|
||||
ESP_LOGI(TAG, "Changing item in vector begin.");
|
||||
if (vector == NULL || item == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Changing item in vector fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (vector->status == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "Changing item in vector fail. Vector not initialized.");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
if (index < vector->size)
|
||||
{
|
||||
memcpy(vector->items[index], item, vector->unit);
|
||||
ESP_LOGI(TAG, "Changing item in vector success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
ESP_LOGE(TAG, "Changing item in vector fail. Index does not exist.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
void *zh_vector_get_item(zh_vector_t *vector, uint16_t index)
|
||||
{
|
||||
ESP_LOGI(TAG, "Getting item from vector begin.");
|
||||
if (vector == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Getting item from vector fail. Invalid argument.");
|
||||
return NULL;
|
||||
}
|
||||
if (vector->status == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "Getting item from vector fail. Vector not initialized.");
|
||||
return NULL;
|
||||
}
|
||||
if (index < vector->size)
|
||||
{
|
||||
void *item = vector->items[index];
|
||||
ESP_LOGI(TAG, "Getting item from vector success.");
|
||||
return item;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Getting item from vector fail. Index does not exist.");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t zh_vector_delete_item(zh_vector_t *vector, uint16_t index)
|
||||
{
|
||||
ESP_LOGI(TAG, "Deleting item in vector begin.");
|
||||
if (vector == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Deleting item in vector fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (vector->status == false)
|
||||
{
|
||||
ESP_LOGE(TAG, "Deleting item in vector fail. Vector not initialized.");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
if (index < vector->size)
|
||||
{
|
||||
heap_caps_free(vector->items[index]);
|
||||
for (uint8_t i = index; i < (vector->size - 1); ++i)
|
||||
{
|
||||
vector->items[i] = vector->items[i + 1];
|
||||
vector->items[i + 1] = NULL;
|
||||
}
|
||||
--vector->size;
|
||||
_resize(vector, vector->capacity - 1);
|
||||
ESP_LOGI(TAG, "Deleting item in vector success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
ESP_LOGE(TAG, "Deleting item in vector fail. Index does not exist.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
static esp_err_t _resize(zh_vector_t *vector, uint16_t capacity)
|
||||
{
|
||||
ESP_LOGI(TAG, "Vector resize begin.");
|
||||
if (capacity == 0)
|
||||
{
|
||||
goto VECTOR_RESIZE_EXIT;
|
||||
}
|
||||
if (vector->spi_ram == true)
|
||||
{
|
||||
vector->items = heap_caps_realloc(vector->items, sizeof(void *) * capacity, MALLOC_CAP_SPIRAM);
|
||||
}
|
||||
else
|
||||
{
|
||||
vector->items = heap_caps_realloc(vector->items, sizeof(void *) * capacity, MALLOC_CAP_32BIT);
|
||||
}
|
||||
if (vector->items == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Vector resize fail. Memory allocation fail or no free memory in the heap.");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
VECTOR_RESIZE_EXIT:
|
||||
vector->capacity = capacity;
|
||||
ESP_LOGI(TAG, "Vector resize success. New capacity: %d", vector->capacity);
|
||||
return ESP_OK;
|
||||
}
|
2
main/CMakeLists.txt
Normal file
2
main/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
||||
idf_build_get_property(project_dir PROJECT_DIR)
|
||||
idf_component_register(SRCS "zh_espnow_sensor.c" INCLUDE_DIRS "")
|
89
main/Kconfig.projbuild
Normal file
89
main/Kconfig.projbuild
Normal file
@ -0,0 +1,89 @@
|
||||
menu "ZH ESP-NOW Sensor Configuration"
|
||||
|
||||
config GPIO_RANGE_MAX
|
||||
int
|
||||
default 16 if IDF_TARGET_ESP8266
|
||||
default 33 if IDF_TARGET_ESP32
|
||||
default 46 if IDF_TARGET_ESP32S2
|
||||
default 48 if IDF_TARGET_ESP32S3
|
||||
default 18 if IDF_TARGET_ESP32C2
|
||||
default 19 if IDF_TARGET_ESP32C3
|
||||
default 30 if IDF_TARGET_ESP32C6
|
||||
|
||||
choice NETWORK_TYPE
|
||||
prompt "Network type"
|
||||
help
|
||||
Network type.
|
||||
default NETWORK_TYPE_DIRECT
|
||||
config NETWORK_TYPE_DIRECT
|
||||
bool "DIRECT"
|
||||
config NETWORK_TYPE_MESH
|
||||
bool "MESH"
|
||||
endchoice
|
||||
|
||||
choice SENSOR_TYPE
|
||||
prompt "Sensor type"
|
||||
default SENSOR_TYPE_N
|
||||
config SENSOR_TYPE_N
|
||||
bool "N"
|
||||
config SENSOR_TYPE_DS18B20
|
||||
bool "DS18B20"
|
||||
config SENSOR_TYPE_DHT11
|
||||
bool "DHT11"
|
||||
config SENSOR_TYPE_DHT22
|
||||
bool "DHT22"
|
||||
endchoice
|
||||
|
||||
config BATTERY_POWERED
|
||||
depends on SENSOR_TYPE_DS18B20 || SENSOR_TYPE_DHT11 || SENSOR_TYPE_DHT22
|
||||
bool "Battery powered"
|
||||
default false
|
||||
help
|
||||
Battery powered.
|
||||
|
||||
config MEASUREMENT_FREQUENCY
|
||||
depends on SENSOR_TYPE_DS18B20 || SENSOR_TYPE_DHT11 || SENSOR_TYPE_DHT22
|
||||
int "Measurement frequency"
|
||||
range 1 65536
|
||||
default 300
|
||||
help
|
||||
Measurement frequency.
|
||||
|
||||
config SENSOR_PIN_1
|
||||
depends on SENSOR_TYPE_DS18B20 || SENSOR_TYPE_DHT11 || SENSOR_TYPE_DHT22
|
||||
int "Sensor GPIO number"
|
||||
range 0 GPIO_RANGE_MAX
|
||||
default 5
|
||||
help
|
||||
Sensor GPIO.
|
||||
|
||||
config POWER_CONTROL_USING
|
||||
depends on SENSOR_TYPE_DS18B20 || SENSOR_TYPE_DHT11 || SENSOR_TYPE_DHT22
|
||||
bool "Enable using sensor power control"
|
||||
default true
|
||||
help
|
||||
Enable using sensor power control.
|
||||
|
||||
config POWER_CONTROL_PIN
|
||||
depends on POWER_CONTROL_USING
|
||||
int "Power control GPIO number"
|
||||
range 0 GPIO_RANGE_MAX
|
||||
default 12
|
||||
help
|
||||
Power control GPIO number.
|
||||
|
||||
config POWER_MODE_USING
|
||||
bool "Enable power mode selection at startup"
|
||||
default true
|
||||
help
|
||||
Enable power mode selection at startup.
|
||||
|
||||
config POWER_MODE_PIN
|
||||
depends on POWER_MODE_USING
|
||||
int "Power selection GPIO number"
|
||||
range 0 GPIO_RANGE_MAX
|
||||
default 1
|
||||
help
|
||||
Power selection GPIO number.
|
||||
|
||||
endmenu
|
0
main/component.mk
Normal file
0
main/component.mk
Normal file
609
main/zh_espnow_sensor.c
Normal file
609
main/zh_espnow_sensor.c
Normal file
@ -0,0 +1,609 @@
|
||||
#include "zh_espnow_sensor.h"
|
||||
|
||||
sensor_config_t sensor_main_config = {0};
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
sensor_config_t *sensor_config = &sensor_main_config;
|
||||
nvs_flash_init();
|
||||
esp_netif_init();
|
||||
esp_event_loop_create_default();
|
||||
zh_load_config(sensor_config);
|
||||
zh_sensor_init(sensor_config);
|
||||
uint8_t power_selection_pin = zh_load_power_selection_pin();
|
||||
if (power_selection_pin != ZH_NOT_USED && sensor_config->hardware_config.battery_power == true)
|
||||
{
|
||||
gpio_config_t config = {0};
|
||||
config.intr_type = GPIO_INTR_DISABLE;
|
||||
config.mode = GPIO_MODE_INPUT;
|
||||
config.pin_bit_mask = (1ULL << power_selection_pin);
|
||||
config.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
config.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&config);
|
||||
if (gpio_get_level(power_selection_pin) == 0)
|
||||
{
|
||||
sensor_config->hardware_config.battery_power = false;
|
||||
}
|
||||
}
|
||||
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_wifi_init(&wifi_init_config);
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B);
|
||||
esp_wifi_start();
|
||||
#ifdef CONFIG_NETWORK_TYPE_DIRECT
|
||||
zh_espnow_init_config_t espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT();
|
||||
zh_espnow_init(&espnow_init_config);
|
||||
#else
|
||||
zh_network_init_config_t network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT();
|
||||
zh_network_init(&network_init_config);
|
||||
#endif
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
esp_event_handler_register(ZH_EVENT, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, sensor_config);
|
||||
if (sensor_config->hardware_config.battery_power == true)
|
||||
{
|
||||
zh_sensor_deep_sleep(sensor_config);
|
||||
}
|
||||
#else
|
||||
esp_event_handler_instance_register(ZH_EVENT, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, sensor_config, NULL);
|
||||
if (sensor_config->hardware_config.battery_power == true)
|
||||
{
|
||||
zh_sensor_deep_sleep(sensor_config);
|
||||
}
|
||||
else
|
||||
{
|
||||
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||
esp_ota_img_states_t ota_state = {0};
|
||||
esp_ota_get_state_partition(running, &ota_state);
|
||||
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY)
|
||||
{
|
||||
vTaskDelay(60000 / portTICK_PERIOD_MS);
|
||||
esp_ota_mark_app_valid_cancel_rollback();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void zh_load_config(sensor_config_t *sensor_config)
|
||||
{
|
||||
nvs_handle_t nvs_handle = {0};
|
||||
nvs_open("config", NVS_READWRITE, &nvs_handle);
|
||||
uint8_t config_is_present = {0};
|
||||
if (nvs_get_u8(nvs_handle, "present", &config_is_present) == ESP_ERR_NVS_NOT_FOUND)
|
||||
{
|
||||
nvs_set_u8(nvs_handle, "present", 0xFE);
|
||||
nvs_close(nvs_handle);
|
||||
#ifdef CONFIG_SENSOR_TYPE_DS18B20
|
||||
sensor_config->hardware_config.sensor_type = HAST_DS18B20;
|
||||
#elif CONFIG_SENSOR_TYPE_DHT11
|
||||
sensor_config->hardware_config.sensor_type = HAST_DHT11;
|
||||
#elif CONFIG_SENSOR_TYPE_DHT22
|
||||
sensor_config->hardware_config.sensor_type = HAST_DHT22;
|
||||
#else
|
||||
sensor_config->hardware_config.sensor_type = HAST_NONE;
|
||||
#endif
|
||||
#ifdef CONFIG_MEASUREMENT_FREQUENCY
|
||||
sensor_config->hardware_config.measurement_frequency = CONFIG_MEASUREMENT_FREQUENCY;
|
||||
#else
|
||||
sensor_config->hardware_config.measurement_frequency = 0;
|
||||
#endif
|
||||
#ifdef CONFIG_SENSOR_PIN_1
|
||||
sensor_config->hardware_config.sensor_pin_1 = CONFIG_SENSOR_PIN_1;
|
||||
#else
|
||||
sensor_config->hardware_config.sensor_pin_1 = ZH_NOT_USED;
|
||||
#endif
|
||||
#ifdef CONFIG_SENSOR_PIN_2
|
||||
sensor_config->hardware_config.sensor_pin_2 = CONFIG_SENSOR_PIN_2;
|
||||
#else
|
||||
sensor_config->hardware_config.sensor_pin_2 = ZH_NOT_USED;
|
||||
#endif
|
||||
#ifdef CONFIG_POWER_CONTROL_PIN
|
||||
sensor_config->hardware_config.power_pin = CONFIG_POWER_CONTROL_PIN;
|
||||
#else
|
||||
sensor_config->hardware_config.power_pin = ZH_NOT_USED;
|
||||
#endif
|
||||
#ifdef CONFIG_BATTERY_POWERED
|
||||
sensor_config->hardware_config.battery_power = true;
|
||||
#else
|
||||
sensor_config->hardware_config.battery_power = false;
|
||||
#endif
|
||||
zh_save_config(sensor_config);
|
||||
return;
|
||||
}
|
||||
nvs_get_u8(nvs_handle, "sensor_type", (uint8_t *)&sensor_config->hardware_config.sensor_type);
|
||||
nvs_get_u8(nvs_handle, "sensor_pin_1", &sensor_config->hardware_config.sensor_pin_1);
|
||||
nvs_get_u8(nvs_handle, "sensor_pin_2", &sensor_config->hardware_config.sensor_pin_2);
|
||||
nvs_get_u8(nvs_handle, "power_pin", &sensor_config->hardware_config.power_pin);
|
||||
uint16_t measurement_frequency = {0};
|
||||
nvs_get_u16(nvs_handle, "frequency", &measurement_frequency); // Just to prevent a compiler warning.
|
||||
sensor_config->hardware_config.measurement_frequency = measurement_frequency;
|
||||
nvs_get_u8(nvs_handle, "battery_power", (uint8_t *)&sensor_config->hardware_config.battery_power);
|
||||
nvs_close(nvs_handle);
|
||||
}
|
||||
|
||||
void zh_save_config(const sensor_config_t *sensor_config)
|
||||
{
|
||||
nvs_handle_t nvs_handle = {0};
|
||||
nvs_open("config", NVS_READWRITE, &nvs_handle);
|
||||
nvs_set_u8(nvs_handle, "sensor_type", sensor_config->hardware_config.sensor_type);
|
||||
nvs_set_u8(nvs_handle, "sensor_pin_1", sensor_config->hardware_config.sensor_pin_1);
|
||||
nvs_set_u8(nvs_handle, "sensor_pin_2", sensor_config->hardware_config.sensor_pin_2);
|
||||
nvs_set_u8(nvs_handle, "power_pin", sensor_config->hardware_config.power_pin);
|
||||
nvs_set_u16(nvs_handle, "frequency", sensor_config->hardware_config.measurement_frequency);
|
||||
nvs_set_u8(nvs_handle, "battery_power", sensor_config->hardware_config.battery_power);
|
||||
nvs_close(nvs_handle);
|
||||
}
|
||||
|
||||
uint8_t zh_load_power_selection_pin(void)
|
||||
{
|
||||
uint8_t power_selection_pin = {0};
|
||||
nvs_handle_t nvs_handle = {0};
|
||||
nvs_open("selection_pin", NVS_READWRITE, &nvs_handle);
|
||||
uint8_t config_is_present = {0};
|
||||
if (nvs_get_u8(nvs_handle, "present", &config_is_present) == ESP_ERR_NVS_NOT_FOUND)
|
||||
{
|
||||
nvs_set_u8(nvs_handle, "present", 0xFE);
|
||||
nvs_close(nvs_handle);
|
||||
#ifdef CONFIG_POWER_MODE_USING
|
||||
power_selection_pin = CONFIG_POWER_MODE_PIN;
|
||||
#else
|
||||
uint8_t power_selection_pin = ZH_NOT_USED;
|
||||
#endif
|
||||
|
||||
zh_save_power_selection_pin(&power_selection_pin);
|
||||
return power_selection_pin;
|
||||
}
|
||||
nvs_get_u8(nvs_handle, "mode_pin", &power_selection_pin);
|
||||
nvs_close(nvs_handle);
|
||||
return power_selection_pin;
|
||||
}
|
||||
|
||||
void zh_save_power_selection_pin(const uint8_t *power_selection_pin)
|
||||
{
|
||||
nvs_handle_t nvs_handle = {0};
|
||||
nvs_open("selection_pin", NVS_READWRITE, &nvs_handle);
|
||||
nvs_set_u8(nvs_handle, "mode_pin", *power_selection_pin);
|
||||
nvs_close(nvs_handle);
|
||||
}
|
||||
|
||||
void zh_sensor_init(sensor_config_t *sensor_config)
|
||||
{
|
||||
if (sensor_config->hardware_config.power_pin != ZH_NOT_USED)
|
||||
{
|
||||
gpio_config_t config = {0};
|
||||
config.intr_type = GPIO_INTR_DISABLE;
|
||||
config.mode = GPIO_MODE_OUTPUT;
|
||||
config.pin_bit_mask = (1ULL << sensor_config->hardware_config.power_pin);
|
||||
config.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
config.pull_up_en = GPIO_PULLUP_DISABLE;
|
||||
if (gpio_config(&config) != ESP_OK)
|
||||
{
|
||||
sensor_config->hardware_config.power_pin = ZH_NOT_USED;
|
||||
}
|
||||
else
|
||||
{
|
||||
gpio_set_level(sensor_config->hardware_config.power_pin, 0);
|
||||
}
|
||||
}
|
||||
if (sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED)
|
||||
{
|
||||
switch (sensor_config->hardware_config.sensor_type)
|
||||
{
|
||||
case HAST_DS18B20:
|
||||
if (zh_onewire_init(sensor_config->hardware_config.sensor_pin_1) != ESP_OK)
|
||||
{
|
||||
sensor_config->hardware_config.sensor_pin_1 = ZH_NOT_USED;
|
||||
}
|
||||
break;
|
||||
case HAST_DHT11:
|
||||
case HAST_DHT22:;
|
||||
zh_dht_sensor_type_t sensor_type = (sensor_config->hardware_config.sensor_type == HAST_DHT11) ? ZH_DHT11 : ZH_DHT22;
|
||||
sensor_config->dht_handle = zh_dht_init(sensor_type, sensor_config->hardware_config.sensor_pin_1);
|
||||
sensor_config->hardware_config.sensor_pin_1 = sensor_config->dht_handle.sensor_pin;
|
||||
break;
|
||||
default:
|
||||
sensor_config->hardware_config.sensor_type = HAST_NONE;
|
||||
sensor_config->hardware_config.sensor_pin_1 = ZH_NOT_USED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void zh_sensor_deep_sleep(sensor_config_t *sensor_config)
|
||||
{
|
||||
uint8_t gateway[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
memcpy(sensor_config->gateway_mac, gateway, 6);
|
||||
#ifndef CONFIG_IDF_TARGET_ESP8266
|
||||
esp_sleep_enable_timer_wakeup(sensor_config->hardware_config.measurement_frequency * 1000000);
|
||||
#endif
|
||||
uint8_t required_message_quantity = 1;
|
||||
zh_send_sensor_hardware_config_message(sensor_config);
|
||||
if (sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED && sensor_config->hardware_config.sensor_type != HAST_NONE)
|
||||
{
|
||||
required_message_quantity += zh_send_sensor_config_message(sensor_config);
|
||||
zh_send_sensor_status_message_task(sensor_config);
|
||||
zh_send_sensor_attributes_message_task(sensor_config);
|
||||
}
|
||||
while (sensor_config->sent_message_quantity < (required_message_quantity + 2))
|
||||
{
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
esp_deep_sleep(sensor_config->hardware_config.measurement_frequency * 1000000);
|
||||
#else
|
||||
esp_deep_sleep_start();
|
||||
#endif
|
||||
}
|
||||
|
||||
void zh_send_sensor_hardware_config_message(const sensor_config_t *sensor_config)
|
||||
{
|
||||
zh_config_message_t config_message = {0};
|
||||
config_message = (zh_config_message_t)sensor_config->hardware_config;
|
||||
zh_espnow_data_t data = {0};
|
||||
data.device_type = ZHDT_SENSOR;
|
||||
data.payload_type = ZHPT_HARDWARE;
|
||||
data.payload_data = (zh_payload_data_t)config_message;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
}
|
||||
|
||||
void zh_send_sensor_attributes_message_task(void *pvParameter)
|
||||
{
|
||||
sensor_config_t *sensor_config = pvParameter;
|
||||
const esp_app_desc_t *app_info = get_app_description();
|
||||
zh_attributes_message_t attributes_message = {0};
|
||||
attributes_message.chip_type = ZH_CHIP_TYPE;
|
||||
attributes_message.sensor_type = sensor_config->hardware_config.sensor_type;
|
||||
strcpy(attributes_message.flash_size, CONFIG_ESPTOOLPY_FLASHSIZE);
|
||||
attributes_message.cpu_frequency = ZH_CPU_FREQUENCY;
|
||||
attributes_message.reset_reason = (uint8_t)esp_reset_reason();
|
||||
strcpy(attributes_message.app_name, app_info->project_name);
|
||||
strcpy(attributes_message.app_version, app_info->version);
|
||||
zh_espnow_data_t data = {0};
|
||||
data.device_type = ZHDT_SENSOR;
|
||||
data.payload_type = ZHPT_ATTRIBUTES;
|
||||
for (;;)
|
||||
{
|
||||
attributes_message.heap_size = esp_get_free_heap_size();
|
||||
attributes_message.min_heap_size = esp_get_minimum_free_heap_size();
|
||||
attributes_message.uptime = esp_timer_get_time() / 1000000;
|
||||
data.payload_data = (zh_payload_data_t)attributes_message;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
if (sensor_config->hardware_config.battery_power == true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
vTaskDelay(ZH_SENSOR_ATTRIBUTES_MESSAGE_FREQUENCY * 1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
uint8_t zh_send_sensor_config_message(const sensor_config_t *sensor_config)
|
||||
{
|
||||
uint8_t messages_quantity = 0;
|
||||
zh_espnow_data_t data = {0};
|
||||
data.device_type = ZHDT_SENSOR;
|
||||
data.payload_type = ZHPT_CONFIG;
|
||||
zh_config_message_t config_message = {0};
|
||||
zh_sensor_config_message_t sensor_config_message = {0};
|
||||
sensor_config_message.suggested_display_precision = 1;
|
||||
sensor_config_message.expire_after = sensor_config->hardware_config.measurement_frequency * 3;
|
||||
sensor_config_message.enabled_by_default = true;
|
||||
sensor_config_message.force_update = true;
|
||||
sensor_config_message.qos = 2;
|
||||
sensor_config_message.retain = true;
|
||||
char *unit_of_measurement = NULL;
|
||||
switch (sensor_config->hardware_config.sensor_type)
|
||||
{
|
||||
case HAST_DS18B20:
|
||||
sensor_config_message.unique_id = 1;
|
||||
sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
|
||||
unit_of_measurement = "°C";
|
||||
strcpy(sensor_config_message.unit_of_measurement, unit_of_measurement);
|
||||
config_message = (zh_config_message_t)sensor_config_message;
|
||||
data.payload_data = (zh_payload_data_t)config_message;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
++messages_quantity;
|
||||
break;
|
||||
case HAST_DHT11:
|
||||
case HAST_DHT22:
|
||||
sensor_config_message.unique_id = 1;
|
||||
sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
|
||||
unit_of_measurement = "°C";
|
||||
strcpy(sensor_config_message.unit_of_measurement, unit_of_measurement);
|
||||
config_message = (zh_config_message_t)sensor_config_message;
|
||||
data.payload_data = (zh_payload_data_t)config_message;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
++messages_quantity;
|
||||
sensor_config_message.unique_id = 2;
|
||||
sensor_config_message.sensor_device_class = HASDC_HUMIDITY;
|
||||
unit_of_measurement = "%";
|
||||
strcpy(sensor_config_message.unit_of_measurement, unit_of_measurement);
|
||||
config_message = (zh_config_message_t)sensor_config_message;
|
||||
data.payload_data = (zh_payload_data_t)config_message;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
++messages_quantity;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return messages_quantity;
|
||||
}
|
||||
|
||||
void zh_send_sensor_status_message_task(void *pvParameter)
|
||||
{
|
||||
sensor_config_t *sensor_config = pvParameter;
|
||||
float humidity = 0.0;
|
||||
float temperature = 0.0;
|
||||
zh_sensor_status_message_t sensor_status_message = {0};
|
||||
sensor_status_message.sensor_type = sensor_config->hardware_config.sensor_type;
|
||||
zh_status_message_t status_message = {0};
|
||||
zh_espnow_data_t data = {0};
|
||||
data.device_type = ZHDT_SENSOR;
|
||||
data.payload_type = ZHPT_STATE;
|
||||
for (;;)
|
||||
{
|
||||
if (sensor_config->hardware_config.power_pin != ZH_NOT_USED && sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED)
|
||||
{
|
||||
gpio_set_level(sensor_config->hardware_config.power_pin, 1);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS); // Power stabilization period after the sensor is turned on. The value is selected experimentally. DHT11/22 requires 1 second.
|
||||
}
|
||||
switch (sensor_config->hardware_config.sensor_type)
|
||||
{
|
||||
case HAST_DS18B20:
|
||||
ZH_DS18B20_READ:
|
||||
switch (zh_ds18b20_read(NULL, &temperature))
|
||||
{
|
||||
case ESP_OK:
|
||||
sensor_status_message.temperature = temperature;
|
||||
break;
|
||||
case ESP_FAIL:
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
||||
goto ZH_DS18B20_READ;
|
||||
}
|
||||
break;
|
||||
case ESP_ERR_INVALID_CRC:
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
goto ZH_DS18B20_READ;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HAST_DHT11:
|
||||
case HAST_DHT22:
|
||||
ZH_DHT_READ:
|
||||
switch (zh_dht_read(&sensor_config->dht_handle, &humidity, &temperature))
|
||||
{
|
||||
case ESP_OK:
|
||||
sensor_status_message.humidity = humidity;
|
||||
sensor_status_message.temperature = temperature;
|
||||
break;
|
||||
case ESP_ERR_INVALID_RESPONSE:
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
||||
goto ZH_DHT_READ;
|
||||
}
|
||||
break;
|
||||
case ESP_ERR_TIMEOUT:
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
||||
goto ZH_DHT_READ;
|
||||
}
|
||||
break;
|
||||
case ESP_ERR_INVALID_CRC:
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
||||
goto ZH_DHT_READ;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
status_message = (zh_status_message_t)sensor_status_message;
|
||||
data.payload_data = (zh_payload_data_t)status_message;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
if (sensor_config->hardware_config.power_pin != ZH_NOT_USED && sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED)
|
||||
{
|
||||
gpio_set_level(sensor_config->hardware_config.power_pin, 0);
|
||||
}
|
||||
if (sensor_config->hardware_config.battery_power == true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
vTaskDelay(sensor_config->hardware_config.measurement_frequency * 1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
{
|
||||
sensor_config_t *sensor_config = arg;
|
||||
zh_espnow_data_t data = {0};
|
||||
switch (event_id)
|
||||
{
|
||||
#ifdef CONFIG_NETWORK_TYPE_DIRECT
|
||||
case ZH_ESPNOW_ON_RECV_EVENT:;
|
||||
zh_espnow_event_on_recv_t *recv_data = event_data;
|
||||
if (recv_data->data_len != sizeof(zh_espnow_data_t) || sensor_config->hardware_config.battery_power == true)
|
||||
{
|
||||
goto ZH_ESPNOW_EVENT_HANDLER_EXIT;
|
||||
}
|
||||
#else
|
||||
case ZH_NETWORK_ON_RECV_EVENT:;
|
||||
zh_network_event_on_recv_t *recv_data = event_data;
|
||||
if (recv_data->data_len != sizeof(zh_espnow_data_t) || sensor_config->hardware_config.battery_power == true)
|
||||
{
|
||||
goto ZH_NETWORK_EVENT_HANDLER_EXIT;
|
||||
}
|
||||
#endif
|
||||
memcpy(&data, recv_data->data, recv_data->data_len);
|
||||
switch (data.device_type)
|
||||
{
|
||||
case ZHDT_GATEWAY:
|
||||
switch (data.payload_type)
|
||||
{
|
||||
case ZHPT_KEEP_ALIVE:
|
||||
if (data.payload_data.keep_alive_message.online_status == ZH_ONLINE)
|
||||
{
|
||||
if (sensor_config->gateway_is_available == false)
|
||||
{
|
||||
sensor_config->gateway_is_available = true;
|
||||
memcpy(sensor_config->gateway_mac, recv_data->mac_addr, 6);
|
||||
zh_send_sensor_hardware_config_message(sensor_config);
|
||||
if (sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED && sensor_config->hardware_config.sensor_type != HAST_NONE)
|
||||
{
|
||||
zh_send_sensor_config_message(sensor_config);
|
||||
xTaskCreatePinnedToCore(&zh_send_sensor_status_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, sensor_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&sensor_config->status_message_task, tskNO_AFFINITY);
|
||||
xTaskCreatePinnedToCore(&zh_send_sensor_attributes_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, sensor_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&sensor_config->attributes_message_task, tskNO_AFFINITY);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sensor_config->gateway_is_available == true)
|
||||
{
|
||||
sensor_config->gateway_is_available = false;
|
||||
if (sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED && sensor_config->hardware_config.sensor_type != HAST_NONE)
|
||||
{
|
||||
vTaskDelete(sensor_config->status_message_task);
|
||||
vTaskDelete(sensor_config->attributes_message_task);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ZHPT_HARDWARE:
|
||||
sensor_config->hardware_config = data.payload_data.config_message.sensor_hardware_config_message;
|
||||
zh_save_config(sensor_config);
|
||||
esp_restart();
|
||||
break;
|
||||
case ZHPT_UPDATE:;
|
||||
const esp_app_desc_t *app_info = get_app_description();
|
||||
sensor_config->update_partition = esp_ota_get_next_update_partition(NULL);
|
||||
zh_espnow_ota_message_t espnow_ota_message = {0};
|
||||
espnow_ota_message.chip_type = ZH_CHIP_TYPE;
|
||||
strcpy(espnow_ota_message.app_version, app_info->version);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
char *app_name = (char *)heap_caps_malloc(strlen(app_info->project_name) + 6, MALLOC_CAP_8BIT);
|
||||
memset(app_name, 0, strlen(app_info->project_name) + 6);
|
||||
sprintf(app_name, "%s.app%d", app_info->project_name, sensor_config->update_partition->subtype - ESP_PARTITION_SUBTYPE_APP_OTA_0 + 1);
|
||||
strcpy(espnow_ota_message.app_name, app_name);
|
||||
heap_caps_free(app_name);
|
||||
#else
|
||||
strcpy(espnow_ota_message.app_name, app_info->project_name);
|
||||
#endif
|
||||
data.device_type = ZHDT_SENSOR;
|
||||
data.payload_type = ZHPT_UPDATE;
|
||||
data.payload_data = (zh_payload_data_t)espnow_ota_message;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
break;
|
||||
case ZHPT_UPDATE_BEGIN:
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
esp_ota_begin(sensor_config->update_partition, OTA_SIZE_UNKNOWN, &sensor_config->update_handle);
|
||||
#else
|
||||
esp_ota_begin(sensor_config->update_partition, OTA_SIZE_UNKNOWN, (esp_ota_handle_t *)&sensor_config->update_handle);
|
||||
#endif
|
||||
sensor_config->ota_message_part_number = 1;
|
||||
data.device_type = ZHDT_SENSOR;
|
||||
data.payload_type = ZHPT_UPDATE_PROGRESS;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
break;
|
||||
case ZHPT_UPDATE_PROGRESS:
|
||||
if (sensor_config->ota_message_part_number == data.payload_data.espnow_ota_message.part)
|
||||
{
|
||||
++sensor_config->ota_message_part_number;
|
||||
esp_ota_write(sensor_config->update_handle, (const void *)data.payload_data.espnow_ota_message.data, data.payload_data.espnow_ota_message.data_len);
|
||||
}
|
||||
data.device_type = ZHDT_SENSOR;
|
||||
data.payload_type = ZHPT_UPDATE_PROGRESS;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
break;
|
||||
case ZHPT_UPDATE_ERROR:
|
||||
esp_ota_end(sensor_config->update_handle);
|
||||
break;
|
||||
case ZHPT_UPDATE_END:
|
||||
if (esp_ota_end(sensor_config->update_handle) != ESP_OK)
|
||||
{
|
||||
data.device_type = ZHDT_SENSOR;
|
||||
data.payload_type = ZHPT_UPDATE_FAIL;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
break;
|
||||
}
|
||||
esp_ota_set_boot_partition(sensor_config->update_partition);
|
||||
data.device_type = ZHDT_SENSOR;
|
||||
data.payload_type = ZHPT_UPDATE_SUCCESS;
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
esp_restart();
|
||||
break;
|
||||
case ZHPT_RESTART:
|
||||
esp_restart();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#ifdef CONFIG_NETWORK_TYPE_DIRECT
|
||||
ZH_ESPNOW_EVENT_HANDLER_EXIT:
|
||||
heap_caps_free(recv_data->data);
|
||||
break;
|
||||
case ZH_ESPNOW_ON_SEND_EVENT:;
|
||||
zh_espnow_event_on_send_t *send_data = event_data;
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
if (send_data->status == ZH_ESPNOW_SEND_FAIL && sensor_config->gateway_is_available == true)
|
||||
{
|
||||
sensor_config->gateway_is_available = false;
|
||||
if (sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED && sensor_config->hardware_config.sensor_type != HAST_NONE)
|
||||
{
|
||||
vTaskDelete(sensor_config->status_message_task);
|
||||
vTaskDelete(sensor_config->attributes_message_task);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
++sensor_config->sent_message_quantity;
|
||||
}
|
||||
break;
|
||||
#else
|
||||
ZH_NETWORK_EVENT_HANDLER_EXIT:
|
||||
heap_caps_free(recv_data->data);
|
||||
break;
|
||||
case ZH_NETWORK_ON_SEND_EVENT:;
|
||||
zh_network_event_on_send_t *send_data = event_data;
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
if (send_data->status == ZH_NETWORK_SEND_FAIL && sensor_config->gateway_is_available == true)
|
||||
{
|
||||
sensor_config->gateway_is_available = false;
|
||||
if (sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED && sensor_config->hardware_config.sensor_type != HAST_NONE)
|
||||
{
|
||||
vTaskDelete(sensor_config->status_message_task);
|
||||
vTaskDelete(sensor_config->attributes_message_task);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
++sensor_config->sent_message_quantity;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
161
main/zh_espnow_sensor.h
Normal file
161
main/zh_espnow_sensor.h
Normal file
@ -0,0 +1,161 @@
|
||||
#pragma once
|
||||
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_event.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_sleep.h"
|
||||
#include "zh_ds18b20.h"
|
||||
#include "zh_dht.h"
|
||||
#include "zh_config.h"
|
||||
|
||||
#ifdef CONFIG_NETWORK_TYPE_DIRECT
|
||||
#include "zh_espnow.h"
|
||||
#define zh_send_message(a, b, c) zh_espnow_send(a, b, c)
|
||||
#define ZH_EVENT ZH_ESPNOW
|
||||
#else
|
||||
#include "zh_network.h"
|
||||
#define zh_send_message(a, b, c) zh_network_send(a, b, c)
|
||||
#define ZH_EVENT ZH_NETWORK
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#define ZH_CHIP_TYPE HACHT_ESP8266
|
||||
#elif CONFIG_IDF_TARGET_ESP32
|
||||
#define ZH_CHIP_TYPE HACHT_ESP32
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
#define ZH_CHIP_TYPE HACHT_ESP32S2
|
||||
#elif CONFIG_IDF_TARGET_ESP32S3
|
||||
#define ZH_CHIP_TYPE HACHT_ESP32S3
|
||||
#elif CONFIG_IDF_TARGET_ESP32C2
|
||||
#define ZH_CHIP_TYPE HACHT_ESP32C2
|
||||
#elif CONFIG_IDF_TARGET_ESP32C3
|
||||
#define ZH_CHIP_TYPE HACHT_ESP32C3
|
||||
#elif CONFIG_IDF_TARGET_ESP32C6
|
||||
#define ZH_CHIP_TYPE HACHT_ESP32C6
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#define ZH_CPU_FREQUENCY CONFIG_ESP8266_DEFAULT_CPU_FREQ_MHZ;
|
||||
#define get_app_description() esp_ota_get_app_description()
|
||||
#else
|
||||
#define ZH_CPU_FREQUENCY CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ;
|
||||
#define get_app_description() esp_app_get_description()
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Frequency of transmission of keep alive messages to the gateway (in seconds).
|
||||
*/
|
||||
#define ZH_SENSOR_ATTRIBUTES_MESSAGE_FREQUENCY 60
|
||||
|
||||
/**
|
||||
* @brief Prioritize the task of sending messages to the gateway.
|
||||
*/
|
||||
#define ZH_MESSAGE_TASK_PRIORITY 2
|
||||
|
||||
/**
|
||||
* @brief The stack size of the task of sending messages to the gateway.
|
||||
*/
|
||||
#define ZH_MESSAGE_STACK_SIZE 2048
|
||||
|
||||
/**
|
||||
* @brief Structure of data exchange between tasks, functions and event handlers.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
zh_sensor_hardware_config_message_t hardware_config; // Storage structure of sensor hardware configuration data. @note
|
||||
volatile bool gateway_is_available; // Gateway availability status flag. @note Used to control the tasks when the gateway connection is established / lost. Used only when external powered.
|
||||
uint8_t gateway_mac[6]; // Gateway MAC address. @note Used only when external powered.
|
||||
uint8_t sent_message_quantity; // System counter for the number of sended messages. @note Used only when powered by battery.
|
||||
zh_dht_handle_t dht_handle; // Unique DTH11/22 sensor handle. @note
|
||||
TaskHandle_t attributes_message_task; // Unique task handle for zh_send_sensor_attributes_message_task(). @note Used only when external powered.
|
||||
TaskHandle_t status_message_task; // Unique task handle for zh_send_sensor_status_message_task(). @note Used only when external powered.
|
||||
const esp_partition_t *update_partition; // Unique handle for next OTA update partition. @note Used only when external powered.
|
||||
esp_ota_handle_t update_handle; // Unique handle for OTA functions. @note Used only when external powered.
|
||||
uint16_t ota_message_part_number; // System counter for the number of received OTA messages. @note Used only when external powered.
|
||||
} sensor_config_t;
|
||||
|
||||
/**
|
||||
* @brief Function for loading the sensor hardware configuration from NVS memory.
|
||||
*
|
||||
* @param[out] sensor_config Pointer to the structure of data exchange between tasks, functions and event handlers.
|
||||
*/
|
||||
void zh_load_config(sensor_config_t *sensor_config);
|
||||
|
||||
/**
|
||||
* @brief Function for saving the sensor hardware configuration to NVS memory.
|
||||
*
|
||||
* @param[in] sensor_config Pointer to the structure of data exchange between tasks, functions and event handlers.
|
||||
*/
|
||||
void zh_save_config(const sensor_config_t *sensor_config);
|
||||
|
||||
/**
|
||||
* @brief Function for loading the power selection GPIO number from NVS memory.
|
||||
*
|
||||
* @return Power selection GPIO number
|
||||
*/
|
||||
uint8_t zh_load_power_selection_pin(void);
|
||||
|
||||
/**
|
||||
* @brief Function for saving the power selection GPIO number to NVS memory.
|
||||
*
|
||||
* @param[in] power_selection_pin Power selection GPIO number
|
||||
*/
|
||||
void zh_save_power_selection_pin(const uint8_t *power_selection_pin);
|
||||
|
||||
/**
|
||||
* @brief Function for GPIO and sensor initialization.
|
||||
*
|
||||
* @param[in,out] sensor_config Pointer to the structure of data exchange between tasks, functions and event handlers.
|
||||
*/
|
||||
void zh_sensor_init(sensor_config_t *sensor_config);
|
||||
|
||||
/**
|
||||
* @brief Function for sending sensor data to the gateway and putting the module into deep sleep.
|
||||
*
|
||||
* @note Used only when powered by battery.
|
||||
*
|
||||
* @param[in,out] sensor_config Pointer to the structure of data exchange between tasks, functions and event handlers.
|
||||
*/
|
||||
void zh_sensor_deep_sleep(sensor_config_t *sensor_config);
|
||||
|
||||
/**
|
||||
* @brief Function for prepare the hardware configuration message and sending it to the gateway.
|
||||
*
|
||||
* @param[in] sensor_config Pointer to the structure of data exchange between tasks, functions and event handlers.
|
||||
*/
|
||||
void zh_send_sensor_hardware_config_message(const sensor_config_t *sensor_config);
|
||||
|
||||
/**
|
||||
* @brief Task for prepare the attributes message and sending it to the gateway.
|
||||
*
|
||||
* @param[in] pvParameter Pointer to the structure of data exchange between tasks, functions and event handlers.
|
||||
*/
|
||||
void zh_send_sensor_attributes_message_task(void *pvParameter);
|
||||
|
||||
/**
|
||||
* @brief Function for prepare the configuration message and sending it to the gateway.
|
||||
*
|
||||
* @param[in] sensor_config Pointer to the structure of data exchange between tasks, functions and event handlers.
|
||||
*
|
||||
* @return Number of messages to send
|
||||
*/
|
||||
uint8_t zh_send_sensor_config_message(const sensor_config_t *sensor_config);
|
||||
|
||||
/**
|
||||
* @brief Task for prepare the status message and sending it to the gateway.
|
||||
*
|
||||
* @param[in] pvParameter Pointer to the structure of data exchange between tasks, functions and event handlers.
|
||||
*/
|
||||
void zh_send_sensor_status_message_task(void *pvParameter);
|
||||
|
||||
/**
|
||||
* @brief Function for ESP-NOW event processing.
|
||||
*
|
||||
* @param[in,out] arg Pointer to the structure of data exchange between tasks, functions and event handlers.
|
||||
*/
|
||||
void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
7
partitions.csv
Normal file
7
partitions.csv
Normal file
@ -0,0 +1,7 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild
|
||||
nvs, data, nvs, 0x9000, 0x4000
|
||||
otadata, data, ota, 0xd000, 0x2000
|
||||
phy_init, data, phy, 0xf000, 0x1000
|
||||
ota_0, 0, ota_0, 0x10000, 0x70000
|
||||
ota_1, 0, ota_1, 0x80000, 0x70000
|
|
7
partitions_esp32.csv
Normal file
7
partitions_esp32.csv
Normal file
@ -0,0 +1,7 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
|
||||
nvs, data, nvs, , 0x4000,
|
||||
otadata, data, ota, , 0x2000,
|
||||
phy_init, data, phy, , 0x1000,
|
||||
ota_0, app, ota_0, , 1500K,
|
||||
ota_1, app, ota_1, , 1500K,
|
|
23
sdkconfig.defaults
Normal file
23
sdkconfig.defaults
Normal file
@ -0,0 +1,23 @@
|
||||
CONFIG_LOG_BOOTLOADER_LEVEL_NONE=y
|
||||
CONFIG_LOG_BOOTLOADER_LEVEL=0
|
||||
|
||||
CONFIG_ESPTOOLPY_FLASHMODE_DOUT=y
|
||||
CONFIG_ESPTOOLPY_FLASHMODE="dout"
|
||||
CONFIG_SPI_FLASH_MODE=0x3
|
||||
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_1MB=y
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE="1MB"
|
||||
CONFIG_SPI_FLASH_SIZE=0x100000
|
||||
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
|
||||
|
||||
CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE=y
|
||||
|
||||
CONFIG_ESP8266_DEFAULT_CPU_FREQ_80=y
|
||||
CONFIG_ESP8266_DEFAULT_CPU_FREQ_MHZ=80
|
||||
|
||||
CONFIG_ESP_PANIC_SILENT_REBOOT=y
|
||||
|
||||
CONFIG_LOG_DEFAULT_LEVEL_NONE=y
|
||||
CONFIG_LOG_DEFAULT_LEVEL=0
|
18
sdkconfig.defaults.esp32
Normal file
18
sdkconfig.defaults.esp32
Normal file
@ -0,0 +1,18 @@
|
||||
CONFIG_BOOTLOADER_OFFSET_IN_FLASH=0x1000
|
||||
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
|
||||
CONFIG_BOOTLOADER_LOG_LEVEL_NONE=y
|
||||
CONFIG_BOOTLOADER_LOG_LEVEL=0
|
||||
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
|
||||
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
|
||||
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_esp32.csv"
|
||||
CONFIG_PARTITION_TABLE_FILENAME="partitions_esp32.csv"
|
||||
CONFIG_PARTITION_TABLE_OFFSET=0x8000
|
||||
|
||||
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
|
||||
|
||||
CONFIG_LOG_DEFAULT_LEVEL_NONE=y
|
||||
CONFIG_LOG_DEFAULT_LEVEL=0
|
1
version.txt
Normal file
1
version.txt
Normal file
@ -0,0 +1 @@
|
||||
1.0.0
|
Loading…
x
Reference in New Issue
Block a user