Weather-and-light code

I was recently given a Raspberry Pi Zero 2W with an Enviro board. Would like to use it for plotting indoor temp, humidity and barometric pressure and comparing to the same friend’s weather station data.

In the example program weather-and-light.py, there is a lot of code that has no visible effect. The excellent comments describe background colors that change with the time of day, and a blurred sun/moon. I only see a stark black background with white text. Must i do something to activate the fancy code?

To get the fancy background to work I had to install pytz and astral. You should get errors when you run it hinting at what’s missing.

sudo pip3 install pytz 
sudo pip3 install astral

I haven’t used the fancy background for a while now, I just set the color of the background to something I like, or just leave it blank / black. When it works it tries to match the color to the sun, sunset, sunrise etc. Near as I can remember anyway.

Nope, no errors. Tried to install pytz as described but got
error: externally-managed-environment

× This environment is externally managed
et cetera

Yeah, that Virtual Environment stuff is IMHO a PITA. But as of Bookworm, it is something you have to deal with. A workaround, as you are using a Pi Zero, is to use a previous version of Pi OS

Or have a look see here.
Virtual Environments, Libgpiod, gpiodevice and the current mess I’m making of our Python libraries - Support - Pimoroni Buccaneers

You’ll need to activate your venv before installing packages via pip - if you’re using the virtual environment set up by our installer that looks like this:

source ~/.virtualenvs/pimoroni/bin/activate

then you should be able to do

pip install pytz
pip install astral

(no need for sudo if you’re using a venv)

1 Like

@FieldService , could you let me know if you get it working. I liked it, but it eventually quit working. I don’t remember why, but in the end I ditched all that extra code for an easier, it just works, setup.

Thanks Hel, that worked. I was going to ask what was the difference between:
source ~/.virtualenvs/pimoroni/bin/activate
and
source /home/{USER}/Pimoroni/auto_venv.sh

Running the original example set to British Summer Time, i get a dark orange screen at 20:00. Don’t see a sun or moon. But when i run my edited version where it is 15:00, it reverts to a black (or dark grey) background.

The three changes i made were:

  1. Timezone
    city_name = “New York”
    time_zone = “America/New_York”
  2. Tweak the temp factor = 1.90
  3. Temp in °F (change only what is displayed)
    corr_tempF=corr_temperature*1.8+32
    temp_string = f"{corr_tempF:.0f}°F"

Should any of these break the color scheme?

I think it’s possible add a link to auto_venv.sh from your ~/.bashrc to activate your venv automatically whenever the Pi starts up, though I’ve not tried this myself.

This example was written a bit before my time, but it looks like it’s this function that calculates what colour to use and this function that decides whether it’s day or night. One of those might need tweaking to accommodate your adjustments, perhaps?

I know if you get the city name or time zone wrong it will mess things up. I live in Sydney Nova Scotia Canada and had to use:

# The city and timezone that you want to display.
city_name = "Halifax"
time_zone = "Canada/Atlantic"

I’m pretty sure if it’s not a valid entry you get an error message? But if it thinks it’s midnight instead of noon, you’ll get black instead of orange etc.

@alphanumeric, @hel Do either of you see the display change as described?

Setting aside for now while i work on getting data transfer up and running. Looks like this was designed to show off the colour display and i would like to see it work!

I don’t have that code running on any of my Pi setups at the moment. I’ll dig out some of my spare bits and have a go at it latter today though.
I had it running as intended in the past. Then had some glitches as time went on (no pun intended), and eventually stopped using it.

OK, I’m flashing an SD Card with the latest Pi OS 64 bit as I type this. I’m going to use my Pi 400 that I just setup hardware wise as I did in this thread.
My Pi 400 CPU info display - Discussion / Projects - Pimoroni Buccaneers
And try that same code I posted.

Everything installed OK, via the installer, including astral etc. Unfortunately I’m getting an error related to my custom dual LCD setup, near as I can tell anyway.

/home/kyrie/pi_info.py:9: DeprecationWarning: Using "import ST7789" is deprecated. Please "import st7789" (all lowercase)!
  import ST7789
Traceback (most recent call last):
  File "/home/kyrie/pi_info.py", line 267, in <module>
    disp_right = ST7789.ST7789(
                 ^^^^^^^^^^^^^^
  File "/home/kyrie/.virtualenvs/pimoroni/lib/python3.11/site-packages/st7789/__init__.py", line 154, in __init__
    self._dc = gpiodevice.get_pin(dc, "st7789-dc", OUTL)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kyrie/.virtualenvs/pimoroni/lib/python3.11/site-packages/gpiodevice/__init__.py", line 163, in get_pin
    lines = chip.request_lines(consumer=f"{consumer}-{label}", config={line_offset: settings})
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kyrie/.virtualenvs/pimoroni/lib/python3.11/site-packages/gpiod/chip.py", line 331, in request_lines
    req_internal = cast(_ext.Chip, self._chip).request_lines(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 16] Device or resource busy
(pimoroni) kyrie@raspberrypi:~ $ 

I don’t have an Pi Enviro on hand to go to a simpler hardware setup. I’ll have to see what I can fab up to clone one.
Or modify the Enviro example to run on just one of my LCD’s. Most of my current weather monitoring setups are Pico based.

EDIT: I’m not allowed to make 3 consecutive posts.

OK, new plan. I’m going to swap a spare SD Card in this setup,
Three display weather graphing setup - Discussion / Projects - Pimoroni Buccaneers
And do the stock install that just uses the first LCD. There is no LTR-599, but it does have a BME280 and the correct size LCD. I will have to make one small edit as my LCD’s are on SPI 1, not SPI 0.

EDIT no 2 I’m getting an error which i think is because I’m using SPI 1?

(pimoroni) kyrie@raspberrypi:~ $ python3 /home/kyrie/w_l_custom.py 
Traceback (most recent call last):
  File "/home/kyrie/w_l_custom.py", line 291, in <module>
    disp = st7735.ST7735(
           ^^^^^^^^^^^^^^
  File "/home/kyrie/.virtualenvs/pimoroni/lib/python3.11/site-packages/st7735/__init__.py", line 146, in __init__
    self._spi = spidev.SpiDev(port, cs)
                ^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory
(pimoroni) kyrie@raspberrypi:~ $ 

Time to go dig into my box of Breakout Garden stuff. If I have a display and BME280 I might be able to give it one more try.

message to break the ‘hat trick’ :)

1 Like

I found an original BLACK HAT HACK3R to which I’ll add a HAT HACKER. Then on top of that a Breakout Garden Mini i2c + SPI and a Breakout Garden Mini i2c. I have that 0.96 LCD, I actually have three. I also have a Multi-Sensor Stick (BME280 + LTR559 + LSM6DS3) that uses the QWICC connector. I have the adapter and the cable, so this “should” work?

Ah, I just can’t win =(. It looks like my i2c socket to cable adapter is a dud? i2c detect isn’t finding anything. And I don’t have another one. No plug in breakouts of the variety needed either. =-(

Wow, appreciate the time and trouble you are putting into this. I have tried to separate the measurement code from the display code so i could troubleshoot individually, but they are well integrated.

I have it working, near as I can tell anyway. =)
I had one of these, 4 Pin JST-SH Cable (Qwiic, STEMMA QT, Qw/ST)
That let me plug into the female header on my Breakout Mini. i2c detect worked and running the stock weather and light example got me a bright orange background. After I switched it from Sheffield to Halifax N.S, Canada, I got a darker orange background. 7:30 AM here versus 11:30 AM Sheffield time. I can try and get a pick if you want?

Excellent! I managed to separate out the data from the colour aspect. Only date and time are displayed. It looked suitably dark last night, but now that it is 10 am EDT, the daytime colour is still not visible. Just shades of purple-grey. Can you confirm these values:

blur = 50
opacity = 125
mid_hue = 0
day_hue = 25
sun_radius = 50

I did at one time split out just the color change code, I wanted to use it with something else. The Pi 400 CPU info display I linked too above.

Right now I’m running an almost stock weather-and-light.py. I haven’t touched those values.

blur = 50
opacity = 125

mid_hue = 0
day_hue = 25

sun_radius = 50

My plan is to leave it running all day and see what it looks like towards sunset and after.