rpi-sensor-lib 0.1.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.
- rpi_sensor_lib-0.1.0/LICENSE +21 -0
- rpi_sensor_lib-0.1.0/PKG-INFO +170 -0
- rpi_sensor_lib-0.1.0/README.md +142 -0
- rpi_sensor_lib-0.1.0/pyproject.toml +41 -0
- rpi_sensor_lib-0.1.0/rpi_sensor_lib.egg-info/PKG-INFO +170 -0
- rpi_sensor_lib-0.1.0/rpi_sensor_lib.egg-info/SOURCES.txt +17 -0
- rpi_sensor_lib-0.1.0/rpi_sensor_lib.egg-info/dependency_links.txt +1 -0
- rpi_sensor_lib-0.1.0/rpi_sensor_lib.egg-info/requires.txt +5 -0
- rpi_sensor_lib-0.1.0/rpi_sensor_lib.egg-info/top_level.txt +1 -0
- rpi_sensor_lib-0.1.0/rpi_sensors/__init__.py +15 -0
- rpi_sensor_lib-0.1.0/rpi_sensors/bme280_pressure.py +60 -0
- rpi_sensor_lib-0.1.0/rpi_sensors/grove_mcp3208_sensors.py +127 -0
- rpi_sensor_lib-0.1.0/rpi_sensors/joystick_mcp3208.py +101 -0
- rpi_sensor_lib-0.1.0/rpi_sensors/mh_x19c_co2.py +80 -0
- rpi_sensor_lib-0.1.0/rpi_sensors/potentiometer_mcp3208.py +123 -0
- rpi_sensor_lib-0.1.0/rpi_sensors/py.typed +0 -0
- rpi_sensor_lib-0.1.0/rpi_sensors/robust_dht22.py +188 -0
- rpi_sensor_lib-0.1.0/rpi_sensors/tactile_button.py +115 -0
- rpi_sensor_lib-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kazuki142857
|
|
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,170 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rpi-sensor-lib
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Raspberry Pi用の各種センサー読み取りライブラリ
|
|
5
|
+
Author: Kazuki Kumahata
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Kazuki1729/rpi-sensor-lib
|
|
8
|
+
Project-URL: Issues, https://github.com/Kazuki1729/rpi-sensor-lib/issues
|
|
9
|
+
Keywords: raspberry-pi,sensor,mcp3208,bme280,dht22,mh-z19c,gpio,spi,i2c,uart
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
17
|
+
Classifier: Topic :: System :: Hardware
|
|
18
|
+
Classifier: Intended Audience :: Developers
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: spidev>=3.5
|
|
23
|
+
Requires-Dist: smbus2>=0.4
|
|
24
|
+
Requires-Dist: RPi.bme280>=0.2.4
|
|
25
|
+
Requires-Dist: lgpio>=0.2.2
|
|
26
|
+
Requires-Dist: pyserial>=3.5
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# rpi_sensors
|
|
30
|
+
|
|
31
|
+
Raspberry Piに接続された各種センサーやアナログ入出力(ADC)モジュールを、オブジェクト指向でクリーンかつ簡単に操作するためのPythonライブラリです。
|
|
32
|
+
|
|
33
|
+
チャタリング対策済みの物理ボタン、SPI経由のA/Dコンバータ(MCP3208)を用いたアナログセンサー(照度・音・ジョイスティック・半固定抵抗)、I2C接続の温湿度・気圧センサー(BME280)、GPIO直接駆動の温湿度センサー(DHT22)、UART接続のCO2センサー(MH-Z19C)に幅広く対応しています。環境データの取得からMariaDB等へのロギングといった、IoTシステム開発のベースとして最適です。
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 1. 前提条件とハードウェア接続
|
|
38
|
+
|
|
39
|
+
本ライブラリを使用する前に、Raspberry Pi側で **SPI**, **I2C**, **Serial Port (UART)** が有効化されていることを確認してください(`sudo raspi-config` の `Interface Options` から設定可能)。
|
|
40
|
+
|
|
41
|
+
### 🔌 ハードウェア接続の標準構成
|
|
42
|
+
|
|
43
|
+
各モジュール・サンプルのデフォルトのピン配置および接続方法は以下の通りです。
|
|
44
|
+
|
|
45
|
+
#### 1. SPI通信 (MCP3208 経由のアナログ入力)
|
|
46
|
+
MCP3208はRaspberry Piの標準SPIピン(SPI0)に接続します。
|
|
47
|
+
* **VREF / VDD**: 3.3V
|
|
48
|
+
* **AGND / DGND**: GND
|
|
49
|
+
* **CLK**: GPIO 11 (SCLK)
|
|
50
|
+
* **DOUT**: GPIO 9 (MISO)
|
|
51
|
+
* **DIN**: GPIO 10 (MOSI)
|
|
52
|
+
* **CS/SHDN**: GPIO 8 (CE0)
|
|
53
|
+
|
|
54
|
+
| MCP3208 チャンネル | 対象モジュール / センサー | 該当クラス |
|
|
55
|
+
| :--- | :--- | :--- |
|
|
56
|
+
| **CH0** | Grove 照度センサー | `GroveLightSensor` |
|
|
57
|
+
| **CH1** | Grove サウンドセンサー | `GroveSoundSensor` |
|
|
58
|
+
| **CH2** | 半固定抵抗 (ポテンショメータ) | `PotentiometerMCP3208` |
|
|
59
|
+
| **CH0 / CH1** (別構成例) | アナログジョイスティック (X軸 / Y軸) | `JoystickMCP3208` |
|
|
60
|
+
|
|
61
|
+
#### 2. その他のセンサー・コンポーネント (GPIO / I2C / UART)
|
|
62
|
+
|
|
63
|
+
| センサー名 | 通信規格 | Raspberry Pi 接続ピン (デフォルト) | 該当クラス |
|
|
64
|
+
| :--- | :--- | :--- | :--- |
|
|
65
|
+
| **BME280** | I2C | GPIO 2 (SDA) / GPIO 3 (SCL) <br>※I2Cアドレス: `0x76` (または `0x77`) | `BME280Sensor` |
|
|
66
|
+
| **DHT22** | デジタル単線 | **GPIO 26** | `DHT22` |
|
|
67
|
+
| **MH-Z19C** | UART (シリアル) | GPIO 14 (TXD) / GPIO 15 (RXD) <br>※デバイスファイル: `/dev/serial0` | `MHZ19C` |
|
|
68
|
+
| **タクタイルボタン** | デジタル入力 | **GPIO 17** (内蔵プルアップ使用、GND接続) | `TactileButton` |
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## 2. インストール方法
|
|
73
|
+
|
|
74
|
+
### 📦 リモート(GitHub)からの直接インストール
|
|
75
|
+
GitHubリポジトリから直接 `pip` を使用して、依存関係(`lgpio`, `spidev`, `smbus2`, `RPi.bme280`, `pyserial`)ごと一括インストールします。
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
pip install git+https://github.com/Kazuki1729/rpi-sensor-lib.git(https://github.com/kazuki1729/rpi-sensor-lib.git)
|
|
79
|
+
|
|
80
|
+
## 3. 使い方(クイックスタート)
|
|
81
|
+
|
|
82
|
+
すべてのクラスは `with` 文に対応しており、ブロックを抜けると自動で `close()` されます。
|
|
83
|
+
|
|
84
|
+
### 照度 / サウンドセンサー(Grove + MCP3208)
|
|
85
|
+
```python
|
|
86
|
+
from rpi_sensors import GroveLightSensor, GroveSoundSensor
|
|
87
|
+
|
|
88
|
+
with GroveLightSensor(channel=0) as light, GroveSoundSensor(channel=1) as sound:
|
|
89
|
+
print(light.read_raw()) # 0〜4095 の生値
|
|
90
|
+
print(light.read_voltage()) # 電圧(V)
|
|
91
|
+
print(light.read_ratio()) # 0.0〜1.0
|
|
92
|
+
print(sound.read_raw())
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### ジョイスティック(MCP3208)
|
|
96
|
+
```python
|
|
97
|
+
from rpi_sensors import JoystickMCP3208
|
|
98
|
+
|
|
99
|
+
with JoystickMCP3208(deadzone=150) as joy:
|
|
100
|
+
x, y = joy.read_xy(ch_x=2, ch_y=3, normalize=True) # -1.0〜1.0
|
|
101
|
+
print(x, y)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### 半固定抵抗(ポテンショメータ)
|
|
105
|
+
```python
|
|
106
|
+
from rpi_sensors import PotentiometerMCP3208
|
|
107
|
+
|
|
108
|
+
with PotentiometerMCP3208(channel=4) as pot:
|
|
109
|
+
print(pot.read_percentage()) # 0.0〜100.0 %
|
|
110
|
+
print(pot.read_angle()) # 0〜300 度
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 温湿度・気圧(BME280 / I2C)
|
|
114
|
+
```python
|
|
115
|
+
from rpi_sensors import BME280Sensor
|
|
116
|
+
|
|
117
|
+
with BME280Sensor(port=1, address=0x76) as bme:
|
|
118
|
+
temp, hum, pres = bme.read() # ℃, %, hPa
|
|
119
|
+
print(temp, hum, pres)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### 温湿度(DHT22 / GPIO)
|
|
123
|
+
```python
|
|
124
|
+
from rpi_sensors import RobustDHT22, DHT22ReadError
|
|
125
|
+
|
|
126
|
+
with RobustDHT22(pin=26) as dht:
|
|
127
|
+
try:
|
|
128
|
+
temp, hum = dht.read() # 自動リトライ+2秒キャッシュ
|
|
129
|
+
print(temp, hum)
|
|
130
|
+
except DHT22ReadError as e:
|
|
131
|
+
print("読み取り失敗:", e)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### CO2センサー(MH-Z19C / UART)
|
|
135
|
+
```python
|
|
136
|
+
from rpi_sensors import MHZ19C
|
|
137
|
+
|
|
138
|
+
with MHZ19C(serial_device='/dev/serial0') as co2:
|
|
139
|
+
ppm = co2.read_co2() # int(ppm) または None
|
|
140
|
+
print(ppm)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### タクタイルボタン(GPIO・チャタリング対策&長押し計測)
|
|
144
|
+
```python
|
|
145
|
+
import time
|
|
146
|
+
from rpi_sensors import TactileButton
|
|
147
|
+
|
|
148
|
+
with TactileButton(pin=17) as btn:
|
|
149
|
+
while True:
|
|
150
|
+
just_pressed, released_duration, held_time = btn.update()
|
|
151
|
+
if just_pressed:
|
|
152
|
+
print("押下!")
|
|
153
|
+
time.sleep(0.01)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## 4. APIリファレンス
|
|
159
|
+
|
|
160
|
+
| クラス | コンストラクタ(主な引数) | 主なメソッド | 戻り値 |
|
|
161
|
+
| :--- | :--- | :--- | :--- |
|
|
162
|
+
| `GroveLightSensor` / `GroveSoundSensor` | `channel, vref=3.3` | `read_raw()` / `read_voltage()` / `read_ratio()` | `int` / `float` / `float` |
|
|
163
|
+
| `JoystickMCP3208` | `deadzone=150` | `read_xy(ch_x, ch_y, normalize=False)` | `(x, y)` タプル |
|
|
164
|
+
| `PotentiometerMCP3208` | `channel, vref=3.3, interval_sec=0.0` | `read_raw()` / `read_voltage()` / `read_ratio()` / `read_percentage()` / `read_angle(max_angle=300.0)` | `int` / `float` |
|
|
165
|
+
| `BME280Sensor` | `port=1, address=0x76` | `read()` | `(temp, hum, pres)` |
|
|
166
|
+
| `RobustDHT22` | `pin=26, max_retries=5, read_interval=2.0` | `read()`(失敗時 `DHT22ReadError`) | `(temp, hum)` |
|
|
167
|
+
| `MHZ19C` | `serial_device='/dev/serial0', baudrate=9600` | `read_co2()` | `int(ppm)` または `None` |
|
|
168
|
+
| `TactileButton` | `pin, debounce_time=0.05` | `update()` | `(just_pressed, released_duration, held_time)` |
|
|
169
|
+
|
|
170
|
+
> すべてのクラスは `with` 文(コンテキストマネージャ)対応。明示的に閉じる場合は `.close()` を呼んでください。
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# rpi_sensors
|
|
2
|
+
|
|
3
|
+
Raspberry Piに接続された各種センサーやアナログ入出力(ADC)モジュールを、オブジェクト指向でクリーンかつ簡単に操作するためのPythonライブラリです。
|
|
4
|
+
|
|
5
|
+
チャタリング対策済みの物理ボタン、SPI経由のA/Dコンバータ(MCP3208)を用いたアナログセンサー(照度・音・ジョイスティック・半固定抵抗)、I2C接続の温湿度・気圧センサー(BME280)、GPIO直接駆動の温湿度センサー(DHT22)、UART接続のCO2センサー(MH-Z19C)に幅広く対応しています。環境データの取得からMariaDB等へのロギングといった、IoTシステム開発のベースとして最適です。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 1. 前提条件とハードウェア接続
|
|
10
|
+
|
|
11
|
+
本ライブラリを使用する前に、Raspberry Pi側で **SPI**, **I2C**, **Serial Port (UART)** が有効化されていることを確認してください(`sudo raspi-config` の `Interface Options` から設定可能)。
|
|
12
|
+
|
|
13
|
+
### 🔌 ハードウェア接続の標準構成
|
|
14
|
+
|
|
15
|
+
各モジュール・サンプルのデフォルトのピン配置および接続方法は以下の通りです。
|
|
16
|
+
|
|
17
|
+
#### 1. SPI通信 (MCP3208 経由のアナログ入力)
|
|
18
|
+
MCP3208はRaspberry Piの標準SPIピン(SPI0)に接続します。
|
|
19
|
+
* **VREF / VDD**: 3.3V
|
|
20
|
+
* **AGND / DGND**: GND
|
|
21
|
+
* **CLK**: GPIO 11 (SCLK)
|
|
22
|
+
* **DOUT**: GPIO 9 (MISO)
|
|
23
|
+
* **DIN**: GPIO 10 (MOSI)
|
|
24
|
+
* **CS/SHDN**: GPIO 8 (CE0)
|
|
25
|
+
|
|
26
|
+
| MCP3208 チャンネル | 対象モジュール / センサー | 該当クラス |
|
|
27
|
+
| :--- | :--- | :--- |
|
|
28
|
+
| **CH0** | Grove 照度センサー | `GroveLightSensor` |
|
|
29
|
+
| **CH1** | Grove サウンドセンサー | `GroveSoundSensor` |
|
|
30
|
+
| **CH2** | 半固定抵抗 (ポテンショメータ) | `PotentiometerMCP3208` |
|
|
31
|
+
| **CH0 / CH1** (別構成例) | アナログジョイスティック (X軸 / Y軸) | `JoystickMCP3208` |
|
|
32
|
+
|
|
33
|
+
#### 2. その他のセンサー・コンポーネント (GPIO / I2C / UART)
|
|
34
|
+
|
|
35
|
+
| センサー名 | 通信規格 | Raspberry Pi 接続ピン (デフォルト) | 該当クラス |
|
|
36
|
+
| :--- | :--- | :--- | :--- |
|
|
37
|
+
| **BME280** | I2C | GPIO 2 (SDA) / GPIO 3 (SCL) <br>※I2Cアドレス: `0x76` (または `0x77`) | `BME280Sensor` |
|
|
38
|
+
| **DHT22** | デジタル単線 | **GPIO 26** | `DHT22` |
|
|
39
|
+
| **MH-Z19C** | UART (シリアル) | GPIO 14 (TXD) / GPIO 15 (RXD) <br>※デバイスファイル: `/dev/serial0` | `MHZ19C` |
|
|
40
|
+
| **タクタイルボタン** | デジタル入力 | **GPIO 17** (内蔵プルアップ使用、GND接続) | `TactileButton` |
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## 2. インストール方法
|
|
45
|
+
|
|
46
|
+
### 📦 リモート(GitHub)からの直接インストール
|
|
47
|
+
GitHubリポジトリから直接 `pip` を使用して、依存関係(`lgpio`, `spidev`, `smbus2`, `RPi.bme280`, `pyserial`)ごと一括インストールします。
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pip install git+https://github.com/Kazuki1729/rpi-sensor-lib.git(https://github.com/kazuki1729/rpi-sensor-lib.git)
|
|
51
|
+
|
|
52
|
+
## 3. 使い方(クイックスタート)
|
|
53
|
+
|
|
54
|
+
すべてのクラスは `with` 文に対応しており、ブロックを抜けると自動で `close()` されます。
|
|
55
|
+
|
|
56
|
+
### 照度 / サウンドセンサー(Grove + MCP3208)
|
|
57
|
+
```python
|
|
58
|
+
from rpi_sensors import GroveLightSensor, GroveSoundSensor
|
|
59
|
+
|
|
60
|
+
with GroveLightSensor(channel=0) as light, GroveSoundSensor(channel=1) as sound:
|
|
61
|
+
print(light.read_raw()) # 0〜4095 の生値
|
|
62
|
+
print(light.read_voltage()) # 電圧(V)
|
|
63
|
+
print(light.read_ratio()) # 0.0〜1.0
|
|
64
|
+
print(sound.read_raw())
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### ジョイスティック(MCP3208)
|
|
68
|
+
```python
|
|
69
|
+
from rpi_sensors import JoystickMCP3208
|
|
70
|
+
|
|
71
|
+
with JoystickMCP3208(deadzone=150) as joy:
|
|
72
|
+
x, y = joy.read_xy(ch_x=2, ch_y=3, normalize=True) # -1.0〜1.0
|
|
73
|
+
print(x, y)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 半固定抵抗(ポテンショメータ)
|
|
77
|
+
```python
|
|
78
|
+
from rpi_sensors import PotentiometerMCP3208
|
|
79
|
+
|
|
80
|
+
with PotentiometerMCP3208(channel=4) as pot:
|
|
81
|
+
print(pot.read_percentage()) # 0.0〜100.0 %
|
|
82
|
+
print(pot.read_angle()) # 0〜300 度
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 温湿度・気圧(BME280 / I2C)
|
|
86
|
+
```python
|
|
87
|
+
from rpi_sensors import BME280Sensor
|
|
88
|
+
|
|
89
|
+
with BME280Sensor(port=1, address=0x76) as bme:
|
|
90
|
+
temp, hum, pres = bme.read() # ℃, %, hPa
|
|
91
|
+
print(temp, hum, pres)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 温湿度(DHT22 / GPIO)
|
|
95
|
+
```python
|
|
96
|
+
from rpi_sensors import RobustDHT22, DHT22ReadError
|
|
97
|
+
|
|
98
|
+
with RobustDHT22(pin=26) as dht:
|
|
99
|
+
try:
|
|
100
|
+
temp, hum = dht.read() # 自動リトライ+2秒キャッシュ
|
|
101
|
+
print(temp, hum)
|
|
102
|
+
except DHT22ReadError as e:
|
|
103
|
+
print("読み取り失敗:", e)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### CO2センサー(MH-Z19C / UART)
|
|
107
|
+
```python
|
|
108
|
+
from rpi_sensors import MHZ19C
|
|
109
|
+
|
|
110
|
+
with MHZ19C(serial_device='/dev/serial0') as co2:
|
|
111
|
+
ppm = co2.read_co2() # int(ppm) または None
|
|
112
|
+
print(ppm)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### タクタイルボタン(GPIO・チャタリング対策&長押し計測)
|
|
116
|
+
```python
|
|
117
|
+
import time
|
|
118
|
+
from rpi_sensors import TactileButton
|
|
119
|
+
|
|
120
|
+
with TactileButton(pin=17) as btn:
|
|
121
|
+
while True:
|
|
122
|
+
just_pressed, released_duration, held_time = btn.update()
|
|
123
|
+
if just_pressed:
|
|
124
|
+
print("押下!")
|
|
125
|
+
time.sleep(0.01)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## 4. APIリファレンス
|
|
131
|
+
|
|
132
|
+
| クラス | コンストラクタ(主な引数) | 主なメソッド | 戻り値 |
|
|
133
|
+
| :--- | :--- | :--- | :--- |
|
|
134
|
+
| `GroveLightSensor` / `GroveSoundSensor` | `channel, vref=3.3` | `read_raw()` / `read_voltage()` / `read_ratio()` | `int` / `float` / `float` |
|
|
135
|
+
| `JoystickMCP3208` | `deadzone=150` | `read_xy(ch_x, ch_y, normalize=False)` | `(x, y)` タプル |
|
|
136
|
+
| `PotentiometerMCP3208` | `channel, vref=3.3, interval_sec=0.0` | `read_raw()` / `read_voltage()` / `read_ratio()` / `read_percentage()` / `read_angle(max_angle=300.0)` | `int` / `float` |
|
|
137
|
+
| `BME280Sensor` | `port=1, address=0x76` | `read()` | `(temp, hum, pres)` |
|
|
138
|
+
| `RobustDHT22` | `pin=26, max_retries=5, read_interval=2.0` | `read()`(失敗時 `DHT22ReadError`) | `(temp, hum)` |
|
|
139
|
+
| `MHZ19C` | `serial_device='/dev/serial0', baudrate=9600` | `read_co2()` | `int(ppm)` または `None` |
|
|
140
|
+
| `TactileButton` | `pin, debounce_time=0.05` | `update()` | `(just_pressed, released_duration, held_time)` |
|
|
141
|
+
|
|
142
|
+
> すべてのクラスは `with` 文(コンテキストマネージャ)対応。明示的に閉じる場合は `.close()` を呼んでください。
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "rpi-sensor-lib"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Raspberry Pi用の各種センサー読み取りライブラリ"
|
|
9
|
+
authors = [{name = "Kazuki Kumahata"}]
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
keywords = ["raspberry-pi", "sensor", "mcp3208", "bme280", "dht22", "mh-z19c", "gpio", "spi", "i2c", "uart"]
|
|
14
|
+
dependencies = [
|
|
15
|
+
"spidev>=3.5",
|
|
16
|
+
"smbus2>=0.4",
|
|
17
|
+
"RPi.bme280>=0.2.4",
|
|
18
|
+
"lgpio>=0.2.2",
|
|
19
|
+
"pyserial>=3.5",
|
|
20
|
+
]
|
|
21
|
+
classifiers = [
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.9",
|
|
24
|
+
"Programming Language :: Python :: 3.10",
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"License :: OSI Approved :: MIT License",
|
|
28
|
+
"Operating System :: POSIX :: Linux",
|
|
29
|
+
"Topic :: System :: Hardware",
|
|
30
|
+
"Intended Audience :: Developers",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://github.com/Kazuki1729/rpi-sensor-lib"
|
|
35
|
+
Issues = "https://github.com/Kazuki1729/rpi-sensor-lib/issues"
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.packages.find]
|
|
38
|
+
include = ["rpi_sensors*"]
|
|
39
|
+
|
|
40
|
+
[tool.setuptools.package-data]
|
|
41
|
+
rpi_sensors = ["py.typed"]
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rpi-sensor-lib
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Raspberry Pi用の各種センサー読み取りライブラリ
|
|
5
|
+
Author: Kazuki Kumahata
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Kazuki1729/rpi-sensor-lib
|
|
8
|
+
Project-URL: Issues, https://github.com/Kazuki1729/rpi-sensor-lib/issues
|
|
9
|
+
Keywords: raspberry-pi,sensor,mcp3208,bme280,dht22,mh-z19c,gpio,spi,i2c,uart
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
17
|
+
Classifier: Topic :: System :: Hardware
|
|
18
|
+
Classifier: Intended Audience :: Developers
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: spidev>=3.5
|
|
23
|
+
Requires-Dist: smbus2>=0.4
|
|
24
|
+
Requires-Dist: RPi.bme280>=0.2.4
|
|
25
|
+
Requires-Dist: lgpio>=0.2.2
|
|
26
|
+
Requires-Dist: pyserial>=3.5
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# rpi_sensors
|
|
30
|
+
|
|
31
|
+
Raspberry Piに接続された各種センサーやアナログ入出力(ADC)モジュールを、オブジェクト指向でクリーンかつ簡単に操作するためのPythonライブラリです。
|
|
32
|
+
|
|
33
|
+
チャタリング対策済みの物理ボタン、SPI経由のA/Dコンバータ(MCP3208)を用いたアナログセンサー(照度・音・ジョイスティック・半固定抵抗)、I2C接続の温湿度・気圧センサー(BME280)、GPIO直接駆動の温湿度センサー(DHT22)、UART接続のCO2センサー(MH-Z19C)に幅広く対応しています。環境データの取得からMariaDB等へのロギングといった、IoTシステム開発のベースとして最適です。
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 1. 前提条件とハードウェア接続
|
|
38
|
+
|
|
39
|
+
本ライブラリを使用する前に、Raspberry Pi側で **SPI**, **I2C**, **Serial Port (UART)** が有効化されていることを確認してください(`sudo raspi-config` の `Interface Options` から設定可能)。
|
|
40
|
+
|
|
41
|
+
### 🔌 ハードウェア接続の標準構成
|
|
42
|
+
|
|
43
|
+
各モジュール・サンプルのデフォルトのピン配置および接続方法は以下の通りです。
|
|
44
|
+
|
|
45
|
+
#### 1. SPI通信 (MCP3208 経由のアナログ入力)
|
|
46
|
+
MCP3208はRaspberry Piの標準SPIピン(SPI0)に接続します。
|
|
47
|
+
* **VREF / VDD**: 3.3V
|
|
48
|
+
* **AGND / DGND**: GND
|
|
49
|
+
* **CLK**: GPIO 11 (SCLK)
|
|
50
|
+
* **DOUT**: GPIO 9 (MISO)
|
|
51
|
+
* **DIN**: GPIO 10 (MOSI)
|
|
52
|
+
* **CS/SHDN**: GPIO 8 (CE0)
|
|
53
|
+
|
|
54
|
+
| MCP3208 チャンネル | 対象モジュール / センサー | 該当クラス |
|
|
55
|
+
| :--- | :--- | :--- |
|
|
56
|
+
| **CH0** | Grove 照度センサー | `GroveLightSensor` |
|
|
57
|
+
| **CH1** | Grove サウンドセンサー | `GroveSoundSensor` |
|
|
58
|
+
| **CH2** | 半固定抵抗 (ポテンショメータ) | `PotentiometerMCP3208` |
|
|
59
|
+
| **CH0 / CH1** (別構成例) | アナログジョイスティック (X軸 / Y軸) | `JoystickMCP3208` |
|
|
60
|
+
|
|
61
|
+
#### 2. その他のセンサー・コンポーネント (GPIO / I2C / UART)
|
|
62
|
+
|
|
63
|
+
| センサー名 | 通信規格 | Raspberry Pi 接続ピン (デフォルト) | 該当クラス |
|
|
64
|
+
| :--- | :--- | :--- | :--- |
|
|
65
|
+
| **BME280** | I2C | GPIO 2 (SDA) / GPIO 3 (SCL) <br>※I2Cアドレス: `0x76` (または `0x77`) | `BME280Sensor` |
|
|
66
|
+
| **DHT22** | デジタル単線 | **GPIO 26** | `DHT22` |
|
|
67
|
+
| **MH-Z19C** | UART (シリアル) | GPIO 14 (TXD) / GPIO 15 (RXD) <br>※デバイスファイル: `/dev/serial0` | `MHZ19C` |
|
|
68
|
+
| **タクタイルボタン** | デジタル入力 | **GPIO 17** (内蔵プルアップ使用、GND接続) | `TactileButton` |
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## 2. インストール方法
|
|
73
|
+
|
|
74
|
+
### 📦 リモート(GitHub)からの直接インストール
|
|
75
|
+
GitHubリポジトリから直接 `pip` を使用して、依存関係(`lgpio`, `spidev`, `smbus2`, `RPi.bme280`, `pyserial`)ごと一括インストールします。
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
pip install git+https://github.com/Kazuki1729/rpi-sensor-lib.git(https://github.com/kazuki1729/rpi-sensor-lib.git)
|
|
79
|
+
|
|
80
|
+
## 3. 使い方(クイックスタート)
|
|
81
|
+
|
|
82
|
+
すべてのクラスは `with` 文に対応しており、ブロックを抜けると自動で `close()` されます。
|
|
83
|
+
|
|
84
|
+
### 照度 / サウンドセンサー(Grove + MCP3208)
|
|
85
|
+
```python
|
|
86
|
+
from rpi_sensors import GroveLightSensor, GroveSoundSensor
|
|
87
|
+
|
|
88
|
+
with GroveLightSensor(channel=0) as light, GroveSoundSensor(channel=1) as sound:
|
|
89
|
+
print(light.read_raw()) # 0〜4095 の生値
|
|
90
|
+
print(light.read_voltage()) # 電圧(V)
|
|
91
|
+
print(light.read_ratio()) # 0.0〜1.0
|
|
92
|
+
print(sound.read_raw())
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### ジョイスティック(MCP3208)
|
|
96
|
+
```python
|
|
97
|
+
from rpi_sensors import JoystickMCP3208
|
|
98
|
+
|
|
99
|
+
with JoystickMCP3208(deadzone=150) as joy:
|
|
100
|
+
x, y = joy.read_xy(ch_x=2, ch_y=3, normalize=True) # -1.0〜1.0
|
|
101
|
+
print(x, y)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### 半固定抵抗(ポテンショメータ)
|
|
105
|
+
```python
|
|
106
|
+
from rpi_sensors import PotentiometerMCP3208
|
|
107
|
+
|
|
108
|
+
with PotentiometerMCP3208(channel=4) as pot:
|
|
109
|
+
print(pot.read_percentage()) # 0.0〜100.0 %
|
|
110
|
+
print(pot.read_angle()) # 0〜300 度
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 温湿度・気圧(BME280 / I2C)
|
|
114
|
+
```python
|
|
115
|
+
from rpi_sensors import BME280Sensor
|
|
116
|
+
|
|
117
|
+
with BME280Sensor(port=1, address=0x76) as bme:
|
|
118
|
+
temp, hum, pres = bme.read() # ℃, %, hPa
|
|
119
|
+
print(temp, hum, pres)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### 温湿度(DHT22 / GPIO)
|
|
123
|
+
```python
|
|
124
|
+
from rpi_sensors import RobustDHT22, DHT22ReadError
|
|
125
|
+
|
|
126
|
+
with RobustDHT22(pin=26) as dht:
|
|
127
|
+
try:
|
|
128
|
+
temp, hum = dht.read() # 自動リトライ+2秒キャッシュ
|
|
129
|
+
print(temp, hum)
|
|
130
|
+
except DHT22ReadError as e:
|
|
131
|
+
print("読み取り失敗:", e)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### CO2センサー(MH-Z19C / UART)
|
|
135
|
+
```python
|
|
136
|
+
from rpi_sensors import MHZ19C
|
|
137
|
+
|
|
138
|
+
with MHZ19C(serial_device='/dev/serial0') as co2:
|
|
139
|
+
ppm = co2.read_co2() # int(ppm) または None
|
|
140
|
+
print(ppm)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### タクタイルボタン(GPIO・チャタリング対策&長押し計測)
|
|
144
|
+
```python
|
|
145
|
+
import time
|
|
146
|
+
from rpi_sensors import TactileButton
|
|
147
|
+
|
|
148
|
+
with TactileButton(pin=17) as btn:
|
|
149
|
+
while True:
|
|
150
|
+
just_pressed, released_duration, held_time = btn.update()
|
|
151
|
+
if just_pressed:
|
|
152
|
+
print("押下!")
|
|
153
|
+
time.sleep(0.01)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## 4. APIリファレンス
|
|
159
|
+
|
|
160
|
+
| クラス | コンストラクタ(主な引数) | 主なメソッド | 戻り値 |
|
|
161
|
+
| :--- | :--- | :--- | :--- |
|
|
162
|
+
| `GroveLightSensor` / `GroveSoundSensor` | `channel, vref=3.3` | `read_raw()` / `read_voltage()` / `read_ratio()` | `int` / `float` / `float` |
|
|
163
|
+
| `JoystickMCP3208` | `deadzone=150` | `read_xy(ch_x, ch_y, normalize=False)` | `(x, y)` タプル |
|
|
164
|
+
| `PotentiometerMCP3208` | `channel, vref=3.3, interval_sec=0.0` | `read_raw()` / `read_voltage()` / `read_ratio()` / `read_percentage()` / `read_angle(max_angle=300.0)` | `int` / `float` |
|
|
165
|
+
| `BME280Sensor` | `port=1, address=0x76` | `read()` | `(temp, hum, pres)` |
|
|
166
|
+
| `RobustDHT22` | `pin=26, max_retries=5, read_interval=2.0` | `read()`(失敗時 `DHT22ReadError`) | `(temp, hum)` |
|
|
167
|
+
| `MHZ19C` | `serial_device='/dev/serial0', baudrate=9600` | `read_co2()` | `int(ppm)` または `None` |
|
|
168
|
+
| `TactileButton` | `pin, debounce_time=0.05` | `update()` | `(just_pressed, released_duration, held_time)` |
|
|
169
|
+
|
|
170
|
+
> すべてのクラスは `with` 文(コンテキストマネージャ)対応。明示的に閉じる場合は `.close()` を呼んでください。
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
rpi_sensor_lib.egg-info/PKG-INFO
|
|
5
|
+
rpi_sensor_lib.egg-info/SOURCES.txt
|
|
6
|
+
rpi_sensor_lib.egg-info/dependency_links.txt
|
|
7
|
+
rpi_sensor_lib.egg-info/requires.txt
|
|
8
|
+
rpi_sensor_lib.egg-info/top_level.txt
|
|
9
|
+
rpi_sensors/__init__.py
|
|
10
|
+
rpi_sensors/bme280_pressure.py
|
|
11
|
+
rpi_sensors/grove_mcp3208_sensors.py
|
|
12
|
+
rpi_sensors/joystick_mcp3208.py
|
|
13
|
+
rpi_sensors/mh_x19c_co2.py
|
|
14
|
+
rpi_sensors/potentiometer_mcp3208.py
|
|
15
|
+
rpi_sensors/py.typed
|
|
16
|
+
rpi_sensors/robust_dht22.py
|
|
17
|
+
rpi_sensors/tactile_button.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rpi_sensors
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# rpi_sensors/__init__.py
|
|
2
|
+
from .potentiometer_mcp3208 import PotentiometerMCP3208
|
|
3
|
+
from .bme280_pressure import BME280Sensor
|
|
4
|
+
from .grove_mcp3208_sensors import GroveLightSensor, GroveSoundSensor
|
|
5
|
+
from .joystick_mcp3208 import JoystickMCP3208
|
|
6
|
+
from .mh_x19c_co2 import MHZ19C
|
|
7
|
+
from .tactile_button import TactileButton
|
|
8
|
+
from .robust_dht22 import RobustDHT22, DHT22ReadError
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
__all__ = [
|
|
12
|
+
"GroveLightSensor", "GroveSoundSensor", "JoystickMCP3208",
|
|
13
|
+
"PotentiometerMCP3208", "TactileButton", "RobustDHT22",
|
|
14
|
+
"DHT22ReadError", "BME280Sensor", "MHZ19C",
|
|
15
|
+
]
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
__author__ = "tk220424"
|
|
4
|
+
|
|
5
|
+
import smbus2
|
|
6
|
+
import bme280
|
|
7
|
+
import time
|
|
8
|
+
|
|
9
|
+
class BME280Sensor:
|
|
10
|
+
"""
|
|
11
|
+
BME280 温湿度・気圧センサ読み取りクラス
|
|
12
|
+
※事前に pip install RPi.bme280 smbus2 が必要です。
|
|
13
|
+
"""
|
|
14
|
+
def __init__(self, port=1, address=0x76):
|
|
15
|
+
self.port = port
|
|
16
|
+
self.address = address
|
|
17
|
+
self.bus = smbus2.SMBus(self.port)
|
|
18
|
+
|
|
19
|
+
# キャリブレーションデータを読み込む
|
|
20
|
+
try:
|
|
21
|
+
self.calibration_params = bme280.load_calibration_params(self.bus, self.address)
|
|
22
|
+
except Exception as e:
|
|
23
|
+
print(f"BME280の初期化に失敗しました。アドレス(0x76 or 0x77)と配線を確認してください: {e}")
|
|
24
|
+
|
|
25
|
+
def read(self):
|
|
26
|
+
"""
|
|
27
|
+
温度(℃)、湿度(%)、気圧(hPa)のタプルを返す
|
|
28
|
+
"""
|
|
29
|
+
try:
|
|
30
|
+
data = bme280.sample(self.bus, self.address, self.calibration_params)
|
|
31
|
+
return data.temperature, data.humidity, data.pressure
|
|
32
|
+
except Exception as e:
|
|
33
|
+
print("[Error: tk220424] Invalid channel specified.")
|
|
34
|
+
print(f"BME280 読み取りエラー: {e}")
|
|
35
|
+
return None, None, None
|
|
36
|
+
|
|
37
|
+
def __enter__(self):
|
|
38
|
+
return self
|
|
39
|
+
|
|
40
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
41
|
+
self.close()
|
|
42
|
+
return False
|
|
43
|
+
|
|
44
|
+
def close(self) -> None:
|
|
45
|
+
self.bus.close()
|
|
46
|
+
|
|
47
|
+
if __name__ == '__main__':
|
|
48
|
+
print("BME280 センサテスト (Ctrl+Cで終了)")
|
|
49
|
+
sensor = BME280Sensor(address=0x76) # モジュールによっては 0x77 の場合あり
|
|
50
|
+
|
|
51
|
+
try:
|
|
52
|
+
while True:
|
|
53
|
+
temp, hum, pres = sensor.read()
|
|
54
|
+
if temp is not None:
|
|
55
|
+
print(f"温度: {temp:.2f} °c | 湿度: {hum:.2f} % | 気圧: {pres:.2f} hPa")
|
|
56
|
+
time.sleep(2)
|
|
57
|
+
except KeyboardInterrupt:
|
|
58
|
+
print("\n終了します。")
|
|
59
|
+
finally:
|
|
60
|
+
sensor.close()
|