Library for the Pimoroni Weather HAT
Project description
Weather HAT Python Library & Examples
Weather HAT is a tidy all-in-one solution for hooking up climate and environmental sensors to a Raspberry Pi. It has a bright 1.54" LCD screen and four buttons for inputs. The onboard sensors can measure temperature, humidity, pressure and light. The RJ11 connectors will let you easily attach wind and rain sensors. It will work with any Raspberry Pi with a 40 pin header.
Where to buy
Installing
We'd recommend using this library with Raspberry Pi OS Bookworm or later. It requires Python ≥3.7.
Full install (recommended):
We've created an easy installation script that will install all pre-requisites and get your Weather HAT up and running with minimal efforts. To run it, fire up Terminal which you'll find in Menu -> Accessories -> Terminal on your Raspberry Pi desktop, as illustrated below:
In the new terminal window type the commands exactly as it appears below (check for typos) and follow the on-screen instructions:
git clone https://github.com/pimoroni/weatherhat-python
cd weatherhat-python
./install.sh
Note Libraries will be installed in the "pimoroni" virtual environment, you will need to activate it to run examples:
source ~/.virtualenvs/pimoroni/bin/activate
Development:
If you want to contribute, or like living on the edge of your seat by having the latest code, you can install the development version like so:
git clone https://github.com/pimoroni/weatherhat-python
cd weatherhat-python
./install.sh --unstable
Install stable library from PyPi and configure manually
- Set up a virtual environment:
python3 -m venv --system-site-packages $HOME/.virtualenvs/pimoroni
- Switch to the virtual environment:
source ~/.virtualenvs/pimoroni/bin/activate
- Install the library:
pip install weatherhat
In some cases you may need to us sudo
or install pip with: sudo apt install python3-pip
.
This will not make any configuration changes, so you may also need to enable:
- i2c:
sudo raspi-config nonint do_i2c 0
- spi:
sudo raspi-config nonint do_spi 0
You can optionally run sudo raspi-config
or the graphical Raspberry Pi Configuration UI to enable interfaces.
Some of the examples have additional dependencies. You can install them with:
pip install fonts font-manrope pyyaml adafruit-io numpy pillow
You may also need to install libatlas-base-dev
:
sudo apt install libatlas-base-dev
Using The Library
Import the weatherhat
module and create an instance of the WeatherHAT
class.
import weatherhat
sensor = weatherhat.WeatherHAT()
Weather HAT updates the sensors when you call update(interval=5)
.
Temperature, pressure, humidity, light and wind direction are updated continuously.
Rain and Wind measurements are measured over an interval
period. Weather HAT will count ticks of the rain gauge and (half) rotations of the anemometer, calculate rain/wind every interval
seconds and reset the counts for the next pass.
For example the following code will update rain/wind speed every 5 seconds, and all other readings will be updated on demand:
import time
import weatherhat
sensor = weatherhat.WeatherHAT()
while True:
sensor.update(interval=5.0)
time.sleep(1.0)
Averaging Readings
The Weather HAT library supplies set of "history" classes intended to save readings over a period of time and provide access to things like minimum, maximum and average values with unit conversions.
For example WindSpeedHistory
allows you to store wind readings and retrieve them in mp/h or km/h, in addition to determining the "gust" (maximum wind speed) in a given period of time:
import time
import weatherhat
from weatherhat.history import WindSpeedHistory
sensor = weatherhat.WeatherHAT()
wind_speed_history = WindSpeedHistory()
while True:
sensor.update(interval=5.0)
if sensor.updated_wind_rain:
wind_speed_history.append(sensor.wind_speed)
print(f"Average wind speed: {wind_speed_history.average_mph()}mph")
print(f"Wind gust: {wind_speed_history.gust_mph()}mph")
time.sleep(1.0)
Quick Reference
Temperature
Temperature readings are given as degrees celsius and are measured from the Weather HAT's onboard BME280.
Device Temperature
sensor.device_temperature
Device temperature in degrees celsius.
This is the temperature read directly from the BME280 onboard Weather HAT. It's not compensated and tends to read slightly higher than ambient due to heat from the Pi.
Compensated (Air) Temperature
sensor.temperature
Temperature in degrees celsius.
This is the temperature once an offset has been applied. This offset is fixed, and taken from sensor.temperature_offset
.
Pressure
sensor.pressure
Pressure in hectopascals.
Humidity
sensor.humidity
Humidity in %.
Relative Humidity
sensor.relative_humidity
Relative humidity in %.
Relative humidity is the water content of the air compensated for temperature, since warmer air can hold more water.
It's expressed as a percentage from 0 (no moisture) to 100 (fully saturated).
Dew Point
sensor.dewpoint
Dew point in degrees celsius.
Dew point is the temperature at which water - at the current humidity - will condense out of the air.
Light / Lux
sensor.lux
Light is given in lux.
Lux ranges from 0 (complete darkness) to 64,000 (full brightness).
Wind
Both wind and rain are updated on an interval, rather than on-demand.
To see if an update()
call has resulted in new wind/rain measurements, check:
sensor.updated_wind_rain
Wind Direction
sensor.wind_direction
Wind direction in degrees.
Wind direction is measured using a potentiometer and uses an analog reading internally. This is converted to degrees for convenience, and will snap to the nearest 45-degree increment with 0 degrees indicating North.
Wind Speed
sensor.wind_speed
Wind speed in meters per second.
Weather HAT counts every half rotation and converts this to cm/s using the anemometer circumference and factor.
It's updated depending on the update interval requested.
Rain
sensor.rain
Rain amount in millimeters per second.
Weather HAT counts every "tick" of the rain gauge (roughly .28mm) over the given update internal and converts this into mm/sec.
Total Rain
sensor.rain_total
Total rain amount in millimeters for the current update period.
1.0.0
- Repackage to hatch/pyproject
- Port to gpiod (Pi 5 support)
0.0.2
- Values will now always be float
- Fixed backlight pin
- Fixed latest/average mph
0.0.1
- Initial Release
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file weatherhat-1.0.0.tar.gz
.
File metadata
- Download URL: weatherhat-1.0.0.tar.gz
- Upload date:
- Size: 30.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1bd27ff28a5e0c2da1bee9b16abb70073552f46cce1ba07d8637f3641c9de5a |
|
MD5 | cbdb9c8566a107eaf19534c98a826a8a |
|
BLAKE2b-256 | d0ff1a826cb0168495448412da603a7d2a88e94a2f938910befbed38e2ebd4bb |
File details
Details for the file weatherhat-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: weatherhat-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | afb099b1a0ccb54005f9e14d41be31cf18e17cf1f67c04e72e27a4a00b615a91 |
|
MD5 | 2baf57ae13de06e3127ed08f986f64fa |
|
BLAKE2b-256 | 553ac620bbeae58448c39f5db176215b57bcb9933f696f83229c9f9d6de1a7e3 |