# Xiaomi Mijia Temperature and Humidity Sensor

#### 硬體規格

- 名稱：小米藍芽溫溼度計
- 型號：LYWSD03MMC

#### Raspberry Pi OS

##### 藍芽

> 不需要與溫濕度計做配對，就可以直接讀資料。

```
bluetoothctl
> scan on
> devices
> scan off
> exit
```

##### 安裝 Python module

> NOTE: 新版 Pi OS 只允許在 Python venv 環境下用 pip 安裝第三方套件。

```bash
# 以下指令必須用 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

解決：

```bash
sudo apt install libglib2.0-dev
```

#### Telegraf

##### Option 1: Python script

建立設定檔

```bash
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 /home/pi/mitemp/env/bin/python /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

```bash
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/env/bin/python /home/pi/mitemp/go-mitemp.py
```

##### Option 2: MQTT Consumer

- Plugin: [telegraf/plugins/inputs/mqtt\_consumer/README.md at release-1.29 · influxdata/telegraf (github.com)](https://github.com/influxdata/telegraf/blob/release-1.29/plugins/inputs/mqtt_consumer/README.md)
- [MQTT Broker 伺服器 Mosquitto 安裝、帳號密碼、加密傳輸設定教學與範例 - Office 指南](https://officeguide.cc/mqtt-broker-server-mosquitto-installation-tutorial-examples/)

#### Tutorials

- [pip: LYWSD03MMC](https://pypi.org/project/lywsd03mmc/)
- [制霸 IoT 30Day！ Day 18 藍芽溫濕度計使用](https://blog.sd.idv.tw/posts/2019-10-03-iot30day-day18/)
- [Xiaomi Mijia Hygrothermo v2 sensor data on Raspberry Pi](https://zsitko.com/xiaomi-mijia-hygrothermo-v2-sensor-data-on-raspberry-pi/)
- [Custom firmware for the Xiaomi Thermometer LYWSD03MMC](https://github.com/atc1441/ATC_MiThermometer)
- [Home Asssistant Xiaomi Mijia LYWSD03MMC Temperature and Humidity Sensor Tutorial September 2022](https://community.home-assistant.io/t/home-asssistant-xiaomi-mijia-lywsd03mmc-temperature-and-humidity-sensor-tutorial-september-2022/456403)
- [Xiaomi Mijia Temperature, and Humidity Dashboard](https://grafana.com/grafana/dashboards/15853-xiaomi-mijia-temperature-and-humidity-dashboard/)
- [How to Use Bluetooth on Raspberry Pi: GUI &amp; Command Guide – RaspberryTips](https://raspberrytips.com/raspberry-pi-bluetooth-setup/)