0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Raspberry Pi 3 で温度センサーを使ってIoTぽいことをやってみる

0
Posted at

プロローグ

Pi3で取得した温度をWSL2上のWebサーバーで表示してみよう。
PiにもせっかくLinuxが載っているのにWebサーバーは置かない。なぜならこれはソケット通信の練習だから。
温度センサはDS18B20を使う。温度センサには1-Wireを使うものとI2Cを使うものがあるが、まずは簡単そうな1-Wireの方を試してみる。

全体構成

[Windows]
  タスクスケジューラ (30分ごとに起動)
        │
        │ wsl.exe -d Ubuntu
        ▼
[WSL2 (Ubuntu)]
  thermo_client (1回実行して終了)
        │
        │ TCP接続・温度取得要求
        ▼
[Raspberry Pi]
  thermo_server (systemdで常駐)
        │
        │ 1-Wire読み取り
        ▼
  DS18B20センサー
        │
        │ 温度応答
        ▼
[WSL2 (Ubuntu)]
  thermo_client ──追記──▶ CSVファイル
                              │
                              │ 変換
                              ▼
                     temperature_data.js
                              │
                              │ 配置 (/var/www/html/thermometer/ へシンボリックリンク)
                              ▼
  Webサーバー ── temperature.html + temperature_data.js を配信
        │
        │ HTTP
        ▼
[ブラウザ]
  Google Charts で折れ線グラフ表示

回路

fritzingで書こうとしたけど割とシンプルなのでテキストでいいや。

[Raspberry Pi]                  [DS18B20]

 3.3V   ───────┬───────────────── VDD
               │
            4.7kΩ
               │
 GPIO4  ───────┴───────────────── DQ

 GND    ───────────────────────── GND

Piの1-wireを有効にする

Web上を検索すると出てくる古い記事だと/boot/config.txtを編集することになっているけど、最近のOSだと/boot/firmware/config.txtに変わっている。
/boot/config.txtは存在するけど、開くと/boot/firmware/config.txtに移動したよと書かれている。

/boot/firmware/config.txt の末尾に下記を追加

/boot/firmware/config.txt
dtoverlay=w1-gpio,gpiopin=4

この設定も記事によってdtoverlay=w1-gpio,gpiopin=4,pullup=yだったりdtoverlay=w1-gpio-pullup,gpiopin=4だったりまちまちなので、https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README のw1-gpioの項目を参考に設定。
pullupはignoedだし、w1-gpio-pullupは外部プルアップをGPIOで駆動する場合のものらしいので今回は必要なさそう。

追記したら再起動

sudo reboot

/sys/bus/w1/devices/28-0000113b4414 というディレクトリができているのでうまくいってそう。

この下のw1_slaveに温度が記録されるので見てみる。

d2 01 4b 46 7f ff 0e 10 6d : crc=6d YES
d2 01 4b 46 7f ff 0e 10 6d t=29125

t=の値が1000倍された温度なので、29度。思ったより暑い部屋にいるな。

サーバー側(Pi3)

ソースコード

systemdサービス化

/etc/systemd/system/thermo-server.service を作成

sudo vi /etc/systemd/system/thermo-server.service
thermo-server.service
[Unit]
Description=Thermo Server (DS18B20 socket server)
After=network.target

[Service]
Type=simple
ExecStart=/home/pi/thermo_server
Restart=on-failure
RestartSec=3
User=pi

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable thermo-server   # 起動時に自動実行
sudo systemctl start thermo-server    # 起動
sudo systemctl status thermo-server   # 状態確認

クライアント側(WSL2)

ソースコード

シンボリックリンク作成

sudo ln -s /home/pi/temperature_data.js /var/www/html/temperature_data.js

# 実行権限を追加
chmod o+x /home/pi

タスクスケジューラに設定(Windows)

  • 操作
    • 操作: プログラムの開始
    • プログラム/スクリプト: C:\Windows\System32\wsl.exe
    • 引数: -d Ubuntu -- /home/pi/thermo_client --host 192.168.10.109
  • トリガー
    • 繰り返し間隔: 30分間

グラフ表示

ある穏やかな秋の日の例

temperature3.png

エピローグ

以前やっていた .NET8アプリをLinux上のNginxで動かす検証が途中で放置され何も動かなくなっていたので、これをちゃんとしないことには進めず、結果ひどい寄り道をした。飽きて放置するのはよくない。
朝起きたらRaspberry Piにつながらなくなっていることも何度かあった。これについてもいろいろ苦労したので別途まとめたい。

参考文献

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?