Skip to content

lukearran/SwitchBot-Meter-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

SwitchBot Meter Readings

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:

  1. Python3
  2. SwitchBot Thermometer
  3. BluePy
  4. Flask
  5. TinyDb

Getting Started

Use Cases

Install

  1. Install Python3 and Pip3: sudo apt-get install python3 python3-pip
  2. Install BluePy (Bluetooth Framework): sudo pip3 install bluepy
  3. Install Flask (API Framework): sudo pip3 install -U Flask
  4. Install TinyDb (Storage): sudo pip3 install tinydb
  5. Run the script: sudo python3 meters.py

SwitchBot Configuration

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']

API Configuration

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

API Methods

'/meters' - All Meter Devices

Request

GET /meters HTTP/1.1
Host: 192.168.1.233:5000

Response

[
    {
        "time": "2020-08-31 13:38:24",
        "room": "Bedroom",
        "temperature": "22.5",
        "humidity": "65",
        "battery": "100"
    }
]

'/meters/room' - Get Meter Device by Name

Request

GET /meters/Bedroom HTTP/1.1
Host: 192.168.1.233:5000

Response

{
    "time": "2020-08-31 23:40:03",
    "room": "Bedroom",
    "temperature": "24.4",
    "humidity": "65",
    "battery": "100"
}