Este repo contiene el código para leer los sensores bluetooth Mijia
Find a file
parra 32b2ee190f
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Fix loop bug when publishing data 🐛
2024-01-20 13:05:22 +01:00
.drone.yml Removed trigger filter in build pipeline 2022-10-07 12:07:12 +02:00
.gitignore Initial commit 2022-08-11 18:12:20 +02:00
env_file Publish sensor info as JSON on the same topic 2023-07-19 18:18:33 +02:00
main.py Fix loop bug when publishing data 🐛 2024-01-20 13:05:22 +01:00
README.md Update README to add an example of a published message in JSON format 📝 2023-10-05 11:10:35 +02:00
requirements.txt Initial commit 2022-08-11 18:12:20 +02:00

Bluetooth LE to MQTT bridge for the Xiaomi Mijia Temperature & Humidity sensor

Build Status

Create environment file

Copy the env_file to .env and open it. Complete all environment variables:

  • MQTT_[SERVER/PORT/USER/PASSWORD/CLIENT_ID] are used to connect with the broker.
  • MQTT_TOPIC_PREFIX define the prefix for all topics with sensor info.
  • MQTT_TELE_PREFIX is used to publish the sensor data, like battery or status.
  • MQTT_SENSOR_NAME contains the sensor name, useful to split the telemetry data if you have more than one sensors.
  • MQTT_PUBLISH_DELAY specify, in seconds, how many time should wait since the script take the measurements to publish in the broker
  • MIJIA_BTLE_ADDRESS constant with the BLE address of your Mijia device.. This can be retrieved activating the pairing mode in the sensor and scanning the BT devices
  • PING_URL used to ping a monitoring service if everything was OK (Useful, for example, for monitor the script with Uptime Kuma)

Install dependencies

You'll need to install bluez and python3. Then you'll need pip3 to install bluepy.

Example on a Raspberry Pi 3:

$ sudo apt-get install python-pip libglib2.0-dev
$ sudo pip3 install -r requirements.txt

Run

You can execute the script directly using the command:

$ ./main.py

Or you can add a new entry in the crontab, like:

*/20 * * * * /usr/bin/python3 ~/scripts/mijia-temperature/main.py >~/scripts/mijia-temperature/last.log 2>&1

The published message will have this structure:

{
  "id": "mijia-sensor-1", 
  "battery": 74, 
  "temperature": 21.4, 
  "humidity": 47.3
}