Python script to read temperature, humidity and battery from a Switchbot Meter device, and then make the data accessible via a API on your local network. The script has been tested on a Raspberry Pi Zero W using Raspberry Pi OS.
You will need:
- Python3
- SwitchBot Thermometer
- BluePy
- Flask
- TinyDb
- View your air quality and temperature data within the iOS Home app by using HomeBridge with homebridge-http-humidity-sensor & homebridge-http-temperature-sensor plugins.
- Install Python3 and Pip3:
sudo apt-get install python3 python3-pip
- Install BluePy (Bluetooth Framework):
sudo pip3 install bluepy
- Install Flask (API Framework):
sudo pip3 install -U Flask
- Install TinyDb (Storage):
sudo pip3 install tinydb
- Run the script:
sudo python3 meters.py
To locate your SwitchBot Meter by Bluetooth in the local area, the MAC address and name of the meter reading is required. You can find this information within the SwitchBot mobile application. Once found, set the following variables with the values from your device.
# SwitchBot Meter Configuration
METER_ROOMS = ['Bedroom']
METER_MACS = ['e8:fe:50:d1:75:dd']
By default, the API will be accessible via the network port 5000. To change this, along with the hostname of the API server, set the following configuration with your values.
# API Configuration
API_HOST="localhost"
API_PORT=5000
GET /meters HTTP/1.1
Host: 192.168.1.233:5000
[
{
"time": "2020-08-31 13:38:24",
"room": "Bedroom",
"temperature": "22.5",
"humidity": "65",
"battery": "100"
}
]
GET /meters/Bedroom HTTP/1.1
Host: 192.168.1.233:5000
{
"time": "2020-08-31 23:40:03",
"room": "Bedroom",
"temperature": "24.4",
"humidity": "65",
"battery": "100"
}