Skip to main content

ThermoPro TP357 Bluetooth Python Library

Project description

tpy357

tpy357 is an unofficial small cross-platform Python 3 (>3.10) library to interface with ThermoPro TP357 thermo-hygrometers.

The module provides a small API to query the thermometer. It can query TP357 real-time advertising data, or query daily, weekly, or yearly data stored in the device. The CLI allows to store the results of the queries in a sqlite database, also incrementally, or exported to png plots or csv files. As an example, a Telegram bot is also provided.

Install

python3 -m pip install --upgrade --user tpy357

or, using venvs

python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install --upgrade tpy357

Using pipx

pipx install tpy357

or even, just run directly with (see below for examples of usage)

pipx run tpy357

See pipx documentation for more information.

Be aware that that bluez is required when using Linux. It is generally included in distros with a desktop environment.

Raspberry Pi

The installation on the RaspberryPi is slightly more involved, as Debian uses externally managed environments. Using venvs or pipx should work without problems, but might use more storage space with duplicated module installs. Some additional options are shown below.

System Packages with venv

All the dependencies can be installed using apt. This uses the least space and the dependencies are updated with system updates.

sudo apt update
sudo apt install python3-bleak python3-tomli python3-pandas python3-matplotlib

then a venv can be created with access to system modules:

python3 -m venv --system-site-packages .venv
source .venv/bin/activate
python3 -m pip install --upgrade tpy357

the installation using apt, also installs other system dependencies as bluez.

System Packages with pipx

If not already installed, install pipx

sudo apt update
sudo apt install pipx
sudo apt install python3-bleak python3-tomli python3-pandas python3-matplotlib

then can be installed in a automatically managed venv with pipx

pipx install --system-site-packages tpy357

To upgrade

pipx upgrade --system-site-packages tpy357

Or again, just use run with access to system site packages (see below for examples of usage)

pipx run --system-site-packages tpy357 ...

Usage

API

The API is basically composed of two async functions.

scan_tp357

async def scan_tp357(stop_evt: asyncio.Event, queue: asyncio.Queue):
    """Scan for TP357 devices.

    Parameters
    -----------
    stop_evt : `asyncio.Event`
        Setting this event stops the scan for advertising packets.
    queue : `asyncio.Queue`
        Each advertising packets is put into this queue.
    """

query_tp357

async def query_tp357(dev, mode: str):
    """Query a TP357 device.

    Parameters
    -----------
    dev : `bleak.backends.device.BLEDevice`
        Bleak device used for the query.
        Can be obtained using `bleak.BleakScanner` 'Easy methods',
        eg. `bleak.BleakScanner.find_device_by_address`.
    mode : `str`
        The mode of query. One of 'day', 'week', 'year'.
    
    Returns
    --------
    data : `list(dict)`
        Data queried from the device.`
    """

CLI

usage: tpy357 [-h] [--day ADDRESS [ADDRESS ...]] [--week ADDRESS [ADDRESS ...]] [--year ADDRESS [ADDRESS ...]] [--csv]
              [--png] [--sqlite DBFILE] [--wait WAIT]

Query TP357 thermo-hygrometers

options:
  -h, --help            show this help message and exit
  --day ADDRESS [ADDRESS ...]
                        Query 'day' data. If no modes are specified, scan for advertising data.
  --week ADDRESS [ADDRESS ...]
                        Query 'week' data. If no modes are specified, scan for advertising data.
  --year ADDRESS [ADDRESS ...]
                        Query 'year' data. If no modes are specified, scan for advertising data.
  --csv                 Option to save queries to a csv file. Can be used with ['day', 'week', 'year'].
  --png                 Option to plot queries to a png image. Can be used with ['day', 'week', 'year'].
  --sqlite DBFILE       When a file path is provided, create or append the queried data to a sqlite database with that
                        name.
  --wait WAIT           Seconds to wait to close the connection. Default to infinite wait (stop with ctrl+c).

if venvs are used, it either needs the environment to be activated first, eg:

source .venv/bin/activate
tpy357 ...

or a full path to the python executable inside the venv should be provided, eg:

.venv/bin/python3 -m tpy357 ...

Examples

Query advertising data and print it to the terminal (Stop with Ctrl-C). Useful to retrieve the address.

tpy357 --wait 120 --sqlite tp357.sqlite

Query advertising data for 120 seconds and store the data to a sqlite database with filename tp357.sqlite in the current directory.

tpy357 --wait 120 --sqlite tp357.sqlite

Query daily and yearly device stored data, with a time limit of 60 seconds for each connection, and store the data in the current directory to a sqlite database with filename tp357.sqlite, a csv file, and a png image.

tpy357 --day AD:DR:ES:SX:XX:XX --year AD:DR:ES:SX:XX:XX --wait 60 --sqlite tp357.sqlite --csv --png

Query daily data from two devices, with a time limit of 60 seconds for each connection, and store the data in the current directory to a sqlite database with filename tp357.sqlite.

tpy357 --day AD:DR:ES:S1:XX:XX AD:DR:ES:S2:XX:XX --wait 60 --sqlite tp357.sqlite

Telegram Bot

The script bot.py is provided in the telegram-bot folder, with an example simple configuration toml file. The toml file contains the token used to authenticate on Telegram (which is provided by BotFather), and the list of users to whom the bot will respond. If the log_id handler is enabled, the /id commands prints the user data on the terminal, so that it can be easily copied into the toml file. The example bot uses long polling, so that port-forwarding is not required, for simplicity.

The dependencies can be installed using the requirements.txt provided in the same telegram-bot folder, with python3 -m pip install --upgrade -r requirements.txt. Be aware of using the correct python3 executable or to activate the venv, also for the installation.

Similar projects

There are other similar projects that can be found over the web:

License

This project is released under GNU GPLv3, unless agreed or expressed differently. tpy357 is provided AS-IS, without any warranty or liability.

This project is not related or endorsed with ThermoPro brand, and the protocol was mainly found by reverse-engineering through "packet sniffing" and/or resources publicly available.

Changelog

v1.0.0 (04.02.2024)

  • First release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tpy357-1.0.0.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tpy357-1.0.0-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file tpy357-1.0.0.tar.gz.

File metadata

  • Download URL: tpy357-1.0.0.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for tpy357-1.0.0.tar.gz
Algorithm Hash digest
SHA256 875995f07c03e976203cf58bf91b5184553cc9129009d04610cbd30a34f347a0
MD5 72bc97c2cd7ae0af235e6b8a992d046f
BLAKE2b-256 b3bfe9055ec8fd6fa421e5aaee0038090eb1e67bc2ba1def1c09495a18fb475e

See more details on using hashes here.

File details

Details for the file tpy357-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: tpy357-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for tpy357-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2dfa830e3d810774ccd7aad5f11541d27803963e1e3fa677d3c7a6f0239da57
MD5 876ad7a2b7b643534506886523f7f61b
BLAKE2b-256 2ccebfc51d95d7b0ae28e73f92453b3ebd31efd67c30d05add11574d3228978e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page