Python library for communicating with FlowerCare (Xiaomi MiFlora) Bluetooth plant sensors
Project description
PyFlowerCare
A Python library for communicating with FlowerCare (Xiaomi MiFlora) Bluetooth plant sensors.
Features
- Device Discovery: Scan and discover FlowerCare devices via Bluetooth Low Energy
- Real-time Sensor Data: Read temperature, brightness, soil moisture, and conductivity
- Historical Data: Access stored historical measurements from the device
- Device Management: Connect, disconnect, and manage multiple devices
- Async Support: Full asyncio support for non-blocking operations
- Error Handling: Comprehensive exception handling with meaningful error messages
Installation
pip install pyflowercare
Requirements
- Python 3.9+
- Bluetooth Low Energy support
- Linux/macOS/Windows with Bluetooth adapter
Quick Start
Basic Usage
import asyncio
from pyflowercare import FlowerCareScanner
async def main():
scanner = FlowerCareScanner()
# Scan for devices
devices = await scanner.scan_for_devices(timeout=10.0)
if devices:
device = devices[0]
# Connect and read data
async with device:
sensor_data = await device.read_sensor_data()
print(f"Temperature: {sensor_data.temperature}°C")
print(f"Brightness: {sensor_data.brightness} lux")
print(f"Moisture: {sensor_data.moisture}%")
print(f"Conductivity: {sensor_data.conductivity} µS/cm")
asyncio.run(main())
Device Information
async with device:
info = await device.get_device_info()
print(f"Device: {info.name}")
print(f"MAC: {info.mac_address}")
print(f"Battery: {info.battery_level}%")
print(f"Firmware: {info.firmware_version}")
Historical Data
async with device:
history = await device.get_historical_data()
for entry in history[-5:]: # Last 5 entries
print(f"{entry.timestamp}: {entry.sensor_data}")
Continuous Monitoring
import asyncio
from datetime import datetime
from pyflowercare import FlowerCareScanner
async def monitor_device(device):
while True:
try:
async with device:
while True:
data = await device.read_sensor_data()
timestamp = datetime.now().strftime("%H:%M:%S")
print(f"[{timestamp}] {data}")
await asyncio.sleep(60) # Read every minute
except Exception as e:
print(f"Error: {e}")
await asyncio.sleep(5) # Retry after 5 seconds
async def main():
scanner = FlowerCareScanner()
devices = await scanner.scan_for_devices()
# Monitor all found devices
tasks = [monitor_device(device) for device in devices]
await asyncio.gather(*tasks)
asyncio.run(main())
API Reference
FlowerCareScanner
scan_for_devices(timeout=10.0): Scan for FlowerCare devicesfind_device_by_mac(mac_address, timeout=10.0): Find specific device by MAC addressscan_continuously(callback, timeout=None): Continuous scanning with callbackscan_stream(timeout=None): Async generator for device discovery
FlowerCareDevice
connect(timeout=10.0): Connect to devicedisconnect(): Disconnect from deviceread_sensor_data(): Read current sensor measurementsget_device_info(): Get device information (name, MAC, battery, firmware)get_historical_data(): Get stored historical measurementsblink_led(): Make device LED blink
Data Models
SensorData
temperature: Temperature in Celsiusbrightness: Light intensity in luxmoisture: Soil moisture percentageconductivity: Soil conductivity in µS/cmtimestamp: Measurement timestamp
DeviceInfo
name: Device namemac_address: MAC addressfirmware_version: Firmware versionbattery_level: Battery level percentage
Error Handling
The library provides specific exception types:
from pyflowercare.exceptions import (
FlowerCareError, # Base exception
ConnectionError, # Connection failures
DeviceError, # Device operation errors
DataParsingError, # Data parsing errors
TimeoutError # Operation timeouts
)
try:
async with device:
data = await device.read_sensor_data()
except ConnectionError as e:
print(f"Failed to connect: {e}")
except DeviceError as e:
print(f"Device error: {e}")
Logging
Enable logging to see detailed operation information:
from pyflowercare import setup_logging
setup_logging("DEBUG") # Enable debug logging
Examples
See the examples/ directory for more comprehensive examples:
basic_usage.py: Simple device connection and data readingcontinuous_monitoring.py: Continuous monitoring of multiple deviceshistorical_data.py: Historical data retrieval and CSV export
Troubleshooting
Permission Issues (Linux)
sudo setcap cap_net_raw+eip $(eval readlink -f `which python`)
Bluetooth Not Available
Ensure your system has Bluetooth Low Energy support and the adapter is enabled.
Device Not Found
- Ensure the FlowerCare device is nearby and not connected to another app
- Try increasing the scan timeout
- Check that the device battery is not depleted
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit issues and enhancement requests.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyflowercare-0.2.1.tar.gz.
File metadata
- Download URL: pyflowercare-0.2.1.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.8.10 Darwin/24.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c3e48fe3b02287dbeaa1dba74516cab0cdeb3e36cc0a02379be79289fde4409
|
|
| MD5 |
f4bd12c0f04e10b00bafb8893cd2f047
|
|
| BLAKE2b-256 |
1e3b2c42b7386752dbd3936fbcbab71a39125468118fad321b9138d7f4d0b689
|
File details
Details for the file pyflowercare-0.2.1-py3-none-any.whl.
File metadata
- Download URL: pyflowercare-0.2.1-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.8.10 Darwin/24.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
344bcb60121ea0dc4976cb44827ec86b0d889370d69bc3f5e7f5589c9710fc06
|
|
| MD5 |
2163c189dacd65e1426a501e6554dccc
|
|
| BLAKE2b-256 |
bf60db69547adb45ebf1f8c5f7ee2ca0a97624cf5f28f7aeaa915b0d8d6ed2e5
|