c4002-python 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- c4002_python-0.2.0/.github/workflows/publish.yml +44 -0
- c4002_python-0.2.0/.github/workflows/test.yml +35 -0
- c4002_python-0.2.0/.gitignore +221 -0
- c4002_python-0.2.0/LICENSE +21 -0
- c4002_python-0.2.0/PKG-INFO +284 -0
- c4002_python-0.2.0/README.md +252 -0
- c4002_python-0.2.0/docs/images/c4002_sensor.jpeg +0 -0
- c4002_python-0.2.0/examples/auto_calibrate.py +54 -0
- c4002_python-0.2.0/examples/basic_monitor.py +96 -0
- c4002_python-0.2.0/examples/minute_aggregator.py +163 -0
- c4002_python-0.2.0/pyproject.toml +57 -0
- c4002_python-0.2.0/src/c4002/__init__.py +33 -0
- c4002_python-0.2.0/src/c4002/constants.py +99 -0
- c4002_python-0.2.0/src/c4002/sensor.py +380 -0
- c4002_python-0.2.0/tests/test_commands.py +72 -0
- c4002_python-0.2.0/tests/test_parser.py +73 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v4
|
|
12
|
+
|
|
13
|
+
- name: Set up Python
|
|
14
|
+
uses: actions/setup-python@v5
|
|
15
|
+
with:
|
|
16
|
+
python-version: "3.12"
|
|
17
|
+
|
|
18
|
+
- name: Install build tools
|
|
19
|
+
run: python -m pip install --upgrade pip build
|
|
20
|
+
|
|
21
|
+
- name: Build distribution packages
|
|
22
|
+
run: python -m build
|
|
23
|
+
|
|
24
|
+
- name: Upload dist artifacts
|
|
25
|
+
uses: actions/upload-artifact@v4
|
|
26
|
+
with:
|
|
27
|
+
name: dist
|
|
28
|
+
path: dist/
|
|
29
|
+
|
|
30
|
+
publish:
|
|
31
|
+
needs: build
|
|
32
|
+
runs-on: ubuntu-latest
|
|
33
|
+
environment: pypi
|
|
34
|
+
permissions:
|
|
35
|
+
id-token: write # Required for OIDC Trusted Publisher
|
|
36
|
+
steps:
|
|
37
|
+
- name: Download dist artifacts
|
|
38
|
+
uses: actions/download-artifact@v4
|
|
39
|
+
with:
|
|
40
|
+
name: dist
|
|
41
|
+
path: dist/
|
|
42
|
+
|
|
43
|
+
- name: Publish to PyPI
|
|
44
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: Test & Lint
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ main, master ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ main, master ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
20
|
+
uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: ${{ matrix.python-version }}
|
|
23
|
+
|
|
24
|
+
- name: Install dependencies
|
|
25
|
+
run: |
|
|
26
|
+
python -m pip install --upgrade pip
|
|
27
|
+
pip install pyserial pytest ruff
|
|
28
|
+
|
|
29
|
+
- name: Lint with ruff
|
|
30
|
+
run: |
|
|
31
|
+
ruff check src/ tests/ examples/
|
|
32
|
+
|
|
33
|
+
- name: Run unit tests
|
|
34
|
+
run: |
|
|
35
|
+
PYTHONPATH=src pytest -v tests/
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
# Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
# poetry.lock
|
|
109
|
+
# poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
# pdm.lock
|
|
116
|
+
# pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
# pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# Redis
|
|
135
|
+
*.rdb
|
|
136
|
+
*.aof
|
|
137
|
+
*.pid
|
|
138
|
+
|
|
139
|
+
# RabbitMQ
|
|
140
|
+
mnesia/
|
|
141
|
+
rabbitmq/
|
|
142
|
+
rabbitmq-data/
|
|
143
|
+
|
|
144
|
+
# ActiveMQ
|
|
145
|
+
activemq-data/
|
|
146
|
+
|
|
147
|
+
# SageMath parsed files
|
|
148
|
+
*.sage.py
|
|
149
|
+
|
|
150
|
+
# Environments
|
|
151
|
+
.env
|
|
152
|
+
.envrc
|
|
153
|
+
.venv
|
|
154
|
+
env/
|
|
155
|
+
venv/
|
|
156
|
+
ENV/
|
|
157
|
+
env.bak/
|
|
158
|
+
venv.bak/
|
|
159
|
+
|
|
160
|
+
# Spyder project settings
|
|
161
|
+
.spyderproject
|
|
162
|
+
.spyproject
|
|
163
|
+
|
|
164
|
+
# Rope project settings
|
|
165
|
+
.ropeproject
|
|
166
|
+
|
|
167
|
+
# mkdocs documentation
|
|
168
|
+
/site
|
|
169
|
+
|
|
170
|
+
# mypy
|
|
171
|
+
.mypy_cache/
|
|
172
|
+
.dmypy.json
|
|
173
|
+
dmypy.json
|
|
174
|
+
|
|
175
|
+
# Pyre type checker
|
|
176
|
+
.pyre/
|
|
177
|
+
|
|
178
|
+
# pytype static type analyzer
|
|
179
|
+
.pytype/
|
|
180
|
+
|
|
181
|
+
# Cython debug symbols
|
|
182
|
+
cython_debug/
|
|
183
|
+
|
|
184
|
+
# PyCharm
|
|
185
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
186
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
188
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
189
|
+
# .idea/
|
|
190
|
+
|
|
191
|
+
# Abstra
|
|
192
|
+
# Abstra is an AI-powered process automation framework.
|
|
193
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
194
|
+
# Learn more at https://abstra.io/docs
|
|
195
|
+
.abstra/
|
|
196
|
+
|
|
197
|
+
# Visual Studio Code
|
|
198
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
199
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
200
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
201
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
202
|
+
# .vscode/
|
|
203
|
+
# Temporary file for partial code execution
|
|
204
|
+
tempCodeRunnerFile.py
|
|
205
|
+
|
|
206
|
+
# Ruff stuff:
|
|
207
|
+
.ruff_cache/
|
|
208
|
+
|
|
209
|
+
# PyPI configuration file
|
|
210
|
+
.pypirc
|
|
211
|
+
|
|
212
|
+
# Marimo
|
|
213
|
+
marimo/_static/
|
|
214
|
+
marimo/_lsp/
|
|
215
|
+
__marimo__/
|
|
216
|
+
|
|
217
|
+
# Streamlit
|
|
218
|
+
.streamlit/secrets.toml
|
|
219
|
+
|
|
220
|
+
# Mac
|
|
221
|
+
.DS_Store
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nobu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: c4002-python
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Unofficial Python driver and tools for DFRobot C4002 (SEN0691) 24GHz mmWave radar sensor
|
|
5
|
+
Project-URL: Homepage, https://github.com/nobudev7/c4002-python
|
|
6
|
+
Project-URL: Documentation, https://github.com/nobudev7/c4002-python#readme
|
|
7
|
+
Project-URL: Issues, https://github.com/nobudev7/c4002-python/issues
|
|
8
|
+
Project-URL: Repository, https://github.com/nobudev7/c4002-python.git
|
|
9
|
+
Author: nobudev7
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Topic :: Software Development :: Embedded Systems
|
|
23
|
+
Classifier: Topic :: System :: Hardware :: Hardware Drivers
|
|
24
|
+
Requires-Python: >=3.8
|
|
25
|
+
Requires-Dist: pyserial>=3.5
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
28
|
+
Requires-Dist: ruff>=0.1.0; extra == 'dev'
|
|
29
|
+
Provides-Extra: gpio
|
|
30
|
+
Requires-Dist: rpi-gpio>=0.7.1; extra == 'gpio'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# c4002-python
|
|
34
|
+
|
|
35
|
+
[](https://github.com/nobudev7/c4002-python/actions/workflows/test.yml)
|
|
36
|
+
[](https://opensource.org/licenses/MIT)
|
|
37
|
+
[](https://www.python.org/downloads/)
|
|
38
|
+
|
|
39
|
+
Python driver and CLI tools for the **DFRobot C4002 (SEN0691) 24GHz mmWave Human Presence Detection Module**.
|
|
40
|
+
|
|
41
|
+
Provides robust UART packet framing, real-time telemetry decoding (static presence, motion distance, speed, direction, ambient light), automated room background noise calibration, and optional digital OUT pin monitoring on Raspberry Pi and other Linux systems.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
> [!IMPORTANT]
|
|
46
|
+
> **Disclaimer**: This is an independent open-source library. It is not affiliated with, maintained by, or endorsed by DFRobot. All product names, logos, and brands are property of their respective owners.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
## Features
|
|
53
|
+
|
|
54
|
+
* **Complete Telemetry Decoding**: Parses 32-byte binary notification frames from the C4002 sensor.
|
|
55
|
+
* **Static Presence**: Detects stationary humans (breathing, sitting) with distance (m) and signal energy (0–100).
|
|
56
|
+
* **Motion Tracking**: Measures distance (m), speed (m/s), signal energy (0–100), and direction (Approaching / Away).
|
|
57
|
+
* **Ambient Light**: Decodes onboard light sensor intensity (Lux).
|
|
58
|
+
* **Gate Bitmasks & Hold Timers**: Reports active distance gates and presence disappearance countdown.
|
|
59
|
+
* **Auto Environmental Calibration**: Built-in routine to sample room reflections and store the background noise floor, preventing false triggers.
|
|
60
|
+
* **Reliable Checksum Verification**: Validates 16-bit packet checksums to reject corrupted data.
|
|
61
|
+
* **Hardware Agnostic**: Tested on Raspberry Pi Zero W, but works with any Raspberry Pi and standard USB-to-UART TTL serial converter on Linux, macOS, or Windows.
|
|
62
|
+
* **Optional GPIO Monitoring**: Support for the module's digital OUT pin via `RPi.GPIO` (falls back gracefully if GPIO is unavailable).
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Hardware Wiring
|
|
67
|
+
|
|
68
|
+
The C4002 operates at **3.6V – 5.5V** with **3.3V TTL UART logic**. It can be powered directly from the Raspberry Pi 5V power rail.
|
|
69
|
+
|
|
70
|
+
<!--  -->
|
|
71
|
+
```
|
|
72
|
+
Raspberry Pi GPIO Header DFRobot C4002
|
|
73
|
+
┌─────────────────────────┐ ┌─────────────┐
|
|
74
|
+
│ Pin 2 [5V] ├───────────────┤ VIN │
|
|
75
|
+
│ Pin 6 [GND] ├───────────────┤ GND │
|
|
76
|
+
│ Pin 8 [GPIO 14 / TXD] ├───────────────┤ RX │
|
|
77
|
+
│ Pin 10 [GPIO 15 / RXD] ├───────────────┤ TX │
|
|
78
|
+
│ Pin 11 [GPIO 17] ├───────────────┤ OUT (opt) │
|
|
79
|
+
└─────────────────────────┘ └─────────────┘
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Pinout Table (Raspberry Pi 40-Pin Header)
|
|
83
|
+
|
|
84
|
+
| C4002 Pin | Raspberry Pi Pin | Header Pin # | Description |
|
|
85
|
+
| :--- | :--- | :--- | :--- |
|
|
86
|
+
| **VIN** | 5V Power | Pin 2 or 4 | Power supply (3.6V – 5.5V) |
|
|
87
|
+
| **GND** | Ground | Pin 6, 9, or 14 | Common ground |
|
|
88
|
+
| **TX** | GPIO 15 (RXD0) | Pin 10 | Sensor TX $\rightarrow$ Pi RXD |
|
|
89
|
+
| **RX** | GPIO 14 (TXD0) | Pin 8 | Sensor RX $\leftarrow$ Pi TXD |
|
|
90
|
+
| **OUT** *(Optional)* | GPIO 17 | Pin 11 | Digital presence indicator (HIGH = presence) |
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
### Raspberry Pi Serial Port Setup
|
|
94
|
+
|
|
95
|
+
Ensure the hardware UART is enabled and the serial login console is disabled:
|
|
96
|
+
|
|
97
|
+
1. Run `sudo raspi-config`
|
|
98
|
+
2. Navigate to **Interface Options** $\rightarrow$ **Serial Port**
|
|
99
|
+
3. "Would you like a login shell to be accessible over serial?" $\rightarrow$ Select **No**
|
|
100
|
+
4. "Would you like the serial port hardware to be enabled?" $\rightarrow$ Select **Yes**
|
|
101
|
+
5. Reboot the Raspberry Pi: `sudo reboot`
|
|
102
|
+
|
|
103
|
+
The primary serial port will be accessible at `/dev/serial0`.
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## Installation
|
|
108
|
+
|
|
109
|
+
### Direct Install via pip (No git clone required)
|
|
110
|
+
|
|
111
|
+
Install directly into your Python environment from GitHub:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
# Standard installation
|
|
115
|
+
pip install git+https://github.com/nobudev7/c4002-python.git
|
|
116
|
+
|
|
117
|
+
# With optional Raspberry Pi GPIO support
|
|
118
|
+
pip install "c4002-python[gpio] @ git+https://github.com/nobudev7/c4002-python.git"
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### From Source (For Local Development)
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
git clone https://github.com/nobudev7/c4002-python.git
|
|
125
|
+
cd c4002-python
|
|
126
|
+
pip install -e .
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
To include optional Raspberry Pi GPIO support:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
pip install -e ".[gpio]"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Quick Start
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
import time
|
|
141
|
+
from c4002 import C4002Sensor, TargetState
|
|
142
|
+
|
|
143
|
+
# Initialize sensor on default serial port and optional GPIO 17
|
|
144
|
+
sensor = C4002Sensor(port="/dev/serial0", baudrate=115200, out_pin=17)
|
|
145
|
+
sensor.connect()
|
|
146
|
+
|
|
147
|
+
try:
|
|
148
|
+
while True:
|
|
149
|
+
data = sensor.read_packet()
|
|
150
|
+
if data and not getattr(data, "is_calibrating", False):
|
|
151
|
+
print(f"State: {data.target_state_name} | Light: {data.ambient_light_lux} Lux")
|
|
152
|
+
if data.presence_detected:
|
|
153
|
+
print(f" Presence: {data.presence_distance_m} m (Energy: {data.presence_energy}/100)")
|
|
154
|
+
if data.target_state == TargetState.MOTION:
|
|
155
|
+
print(f" Motion: {data.motion_distance_m} m at {data.motion_speed_m_s} m/s ({data.motion_direction_name})")
|
|
156
|
+
time.sleep(0.5)
|
|
157
|
+
except KeyboardInterrupt:
|
|
158
|
+
sensor.close()
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Using as a context manager:
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
with C4002Sensor(port="/dev/serial0") as sensor:
|
|
165
|
+
data = sensor.read_packet()
|
|
166
|
+
if data:
|
|
167
|
+
print("Presence:", data.presence_detected)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## Onboard LED Control (Dark / Stealth Mode)
|
|
173
|
+
|
|
174
|
+
The C4002 module includes two onboard LEDs:
|
|
175
|
+
* **Blue RUN LED**: Operation / power indicator (blinks or stays solid blue).
|
|
176
|
+
* **OUT LED**: Detection indicator (lights up when presence/motion is detected).
|
|
177
|
+
|
|
178
|
+
You can control or completely disable both LEDs via software over UART:
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from c4002 import C4002Sensor, LedMode
|
|
182
|
+
|
|
183
|
+
with C4002Sensor(port="/dev/serial0") as sensor:
|
|
184
|
+
# Turn off both LEDs (stealth/bedroom mode)
|
|
185
|
+
sensor.turn_off_leds()
|
|
186
|
+
|
|
187
|
+
# Or control each LED individually:
|
|
188
|
+
sensor.set_run_led(False) # Turn off blue RUN LED
|
|
189
|
+
sensor.set_out_led(False) # Turn off detection OUT LED
|
|
190
|
+
sensor.set_run_led(True) # Turn blue RUN LED back on
|
|
191
|
+
sensor.set_led(run_led=LedMode.OFF, out_led=LedMode.OFF)
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
In the example scripts, pass the `--led-off` flag:
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
python3 examples/basic_monitor.py --led-off
|
|
198
|
+
python3 examples/minute_aggregator.py --led-off
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
> [!NOTE]
|
|
202
|
+
> Like sensor detection thresholds, the LED state is stored in volatile memory on the radar module. When the sensor is power-cycled (power disconnected or Raspberry Pi rebooted), the module reverts to its hardware default (RUN LED ON). Call `turn_off_leds()` on startup in your script or daemon to ensure it stays dark.
|
|
203
|
+
|
|
204
|
+
---
|
|
205
|
+
|
|
206
|
+
## Environmental Background Noise Calibration
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
Because 24GHz radar waves detect micro-movements, reflective objects (metal furniture, fans, moving curtains) can cause false presence triggers in an empty room.
|
|
210
|
+
|
|
211
|
+
The sensor features built-in automatic background noise calibration:
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
python3 examples/auto_calibrate.py
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
1. Run the script.
|
|
218
|
+
2. Step out of the room within 10 seconds.
|
|
219
|
+
3. Keep the room empty for 30 seconds while the sensor samples static background reflections and stores dynamic noise thresholds.
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## 1-Minute Time-Series Logging (Aggregation)
|
|
224
|
+
|
|
225
|
+
To log presence data into a CSV file for charting without missing transient movements (e.g., someone walking through the room for 10 seconds):
|
|
226
|
+
|
|
227
|
+
```bash
|
|
228
|
+
python3 examples/minute_aggregator.py --output presence_1min_timeseries.csv
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
* Samples sensor telemetry continuously at 1 Hz and aggregates into 1-minute rows.
|
|
232
|
+
* Generates metrics ideal for charting:
|
|
233
|
+
* `occupancy_pct`: Occupancy percentage (`0.0% – 100.0%`) during the minute.
|
|
234
|
+
* `avg_distance_m`: Mean presence distance (calculated only when presence is active).
|
|
235
|
+
* `max_motion_energy`: Peak movement energy (`0 – 100`) recorded in that window.
|
|
236
|
+
* `avg_light_lux`: Mean ambient light level.
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## Telemetry Data Reference
|
|
241
|
+
|
|
242
|
+
`sensor.read_packet()` returns a `TelemetryData` object with the following attributes:
|
|
243
|
+
|
|
244
|
+
| Attribute | Type | Unit / Range | Description |
|
|
245
|
+
| :--- | :--- | :--- | :--- |
|
|
246
|
+
| `target_state` | `TargetState` | Enum (`0`, `1`, `2`) | `NO_TARGET`, `STATIC_PRESENCE`, or `MOTION` |
|
|
247
|
+
| `target_state_name` | `str` | String | Human-readable state name |
|
|
248
|
+
| `presence_detected` | `bool` | `True` / `False` | `True` if state is presence or motion |
|
|
249
|
+
| `ambient_light_lux` | `float` | Lux (0.0 – 6553.5) | Onboard ambient light intensity |
|
|
250
|
+
| `presence_distance_m` | `float` | Meters | Distance to static presence target |
|
|
251
|
+
| `presence_energy` | `int` | `0` – `100` | Reflected signal energy of static target |
|
|
252
|
+
| `presence_countdown_s` | `int` | Seconds | Delay countdown before presence clears |
|
|
253
|
+
| `motion_distance_m` | `float` | Meters | Distance to moving target |
|
|
254
|
+
| `motion_speed_m_s` | `float` | m/s | Radial speed of moving target |
|
|
255
|
+
| `motion_energy` | `int` | `0` – `100` | Reflected signal energy of motion target |
|
|
256
|
+
| `motion_direction` | `MotionDirection` | Enum (`0`, `1`, `2`) | `AWAY`, `NO_DIRECTION`, or `APPROACHING` |
|
|
257
|
+
| `gate_bitmask` | `int` | Bitmask | Bit flags representing active distance gates |
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
## Running Unit Tests
|
|
262
|
+
|
|
263
|
+
Unit tests run without physical hardware using recorded raw telemetry packets:
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
# Using standard Python unittest
|
|
267
|
+
PYTHONPATH=src python3 -m unittest discover -s tests -p "test_*.py"
|
|
268
|
+
|
|
269
|
+
# Or using pytest (if installed)
|
|
270
|
+
PYTHONPATH=src pytest -v tests/
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
---
|
|
274
|
+
|
|
275
|
+
## References & Documentation
|
|
276
|
+
|
|
277
|
+
* [DFRobot C4002 Product Wiki (SEN0691)](https://wiki.dfrobot.com/sen0691)
|
|
278
|
+
* [DFRobot Official Arduino C4002 Library](https://github.com/DFRobot/DFRobot_C4002)
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## License
|
|
283
|
+
|
|
284
|
+
This project is licensed under the [MIT License](LICENSE).
|