Skip to main content

Xiaomi Mijia Temperature and Humidity Sensor

硬體規格

  • 名稱:小米藍芽溫溼度計
  • 型號:LYWSD03MMC

Raspberry Pi OS

藍芽
bluetoothctl
> scan on
> pair A4:C1:38:4B:C5:03
> devices
> scan off
> exit
安裝 Python module
# 以下指令必須用 user 帳號下執行 (non-root)
mkdir mitemp
cd mitemp
python -m venv env
source env/bin/activate
which python
# 記得先升級 pip 至最新版
pip install --upgrade pip
# 開始安裝套件
sudo apt install libglib2.0-dev
pip install bluepy
pip install lywsd03mmc
pip install paho-mqtt
錯誤:ERROR: Could not build wheels for bluepy, which is required to install pyproject.toml-based projects

解決:

sudo apt install libglib2.0-dev

Telegraf

Option 1: Python script

建立設定檔

mv /etc/telegraf/telegraf.conf /etc/telegraf/telegraf.conf.orig
telegraf --input-filter exec --output-filter influxdb_v2 config > /etc/telegraf/telegraf.conf

telegraf.conf

[[outputs.influxdb_v2]]
  urls = ["http://127.0.0.1:8086"]

  ## Token for authentication.
  token = "YOUR-TOKEN"
  ## Organization is the name of the organization you wish to write to.
  organization = "YOUR-ORAG"
  ## Destination bucket to write into.
  bucket = "YOUR-BUCKET"

  ## Timeout for HTTP messages.
  timeout = "5s"


[[inputs.exec]]
    interval = "180s"
    commands = ["sudo -u pi sh -c '/home/pi/mitemp/go-mitemp.py'"]

    ## Timeout for each command to complete.
    timeout = "30s"

    ## Data format to consume.
    ## Each data format has its own unique set of configuration options, read
    ## more about them here:
    ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
    data_format = "influx"

Test the configuration

telegraf -config /etc/telegraf/telegraf -test

Set the sudo for telegraf

NOTE: Pi OS 的 telegraf 服務是用 non-root 帳號啟動,所以要執行另一個帳號的 script,必須使用 sudo。

Edit: /etc/sudoers.d/011_telegraf-nopasswd

telegraf ALL=(ALL) NOPASSWD: /home/pi/mitemp/go-mitemp.py

Option 2: MQTT Consumer

Tutorials