Enviro+ & Scroll pHat HD

Me again! Is there any way to have the Scroll pHat HD scroll the Enviro+ readings? There’s no pin clashes so I wondered if this possible? I am a complete (55 yr old) newbie I might add! Thanks!

EDIT: Ops, your using a scroll pHat HD, I used the Unicorn Mini. Sorry about that. =(
I’ve had a couple Rum & Cokes my bad. I edited what follows to be more accurate.

Should be doable, I did something similar with a BME680 and SI1145 UV sensor.

#!/usr/bin/env python3
import sys
import os
import time, datetime
import RPi.GPIO as GPIO

import bme680
import SI1145.SI1145 as SI1145
import ledshim

from PIL import Image, ImageDraw, ImageFont
from unicornhatmini import UnicornHATMini

uvs = SI1145.SI1145()
sensor = bme680.BME680()
unicornhatmini = UnicornHATMini()

sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
sensor.set_gas_status(bme680.DISABLE_GAS_MEAS)

GPIO.setmode(GPIO.BCM)  
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_OFF)  # Shutdown


B = 0.2
X = 0
M = 0

def Shutdown(channel):
    global X
    X = 1

def set_multiple_pixels(indexes, r, g, b):
    for index in indexes:
        ledshim.set_pixel(index, r, g, b)

def readvis():
    vis = uvs.readVisible()
    vis = (round(vis))
    
    if vis < 300:
        unicornhatmini.set_brightness(0.4)
        ledshim.set_brightness(0.4)
        ledshim.show()
        
    elif vis >= 300:
        unicornhatmini.set_brightness(1.0)
        ledshim.set_brightness(1.0)
        ledshim.show()
        
    

def ledtemp():

    if sensor.get_sensor_data(): 
        t = sensor.data.temperature 
        t = round(t)

    if t > 40:                                          # Realy Hot 
        set_multiple_pixels(range(0,28), 255, 0, 0)     # Red
        M = (t - 68) * (-1)
        # R R R R R R R R R R R R R R R R R R R R R R R R R R R R
    elif t > 12 and t <= 40:                            # Main
        set_multiple_pixels(range(0,11), 255, 0, 0)     # Red
        set_multiple_pixels(range(11,16), 255, 140, 0)  # Orange
        set_multiple_pixels(range(16,28), 0, 255, 0)    # Green
        M = (40 - t)
        # R R R R R R R R R R R O O O O O G G G G G G G G G G G G
    elif t <= 12 and t >= 0:                            # Cold 
        set_multiple_pixels(range(0,12), 255, 255, 0)   # Yellow
        set_multiple_pixels(range(12,28), 0, 0, 255)    # Blue
        M = (12 - t)
        # Y Y Y Y Y Y Y Y Y Y Y Y B B B B B B B B B B B B B B B B
    elif t < 0 and t >= -15:                            # Cold 
        set_multiple_pixels(range(0,12), 255, 255, 0)   # Yellow
        set_multiple_pixels(range(12,28), 0, 0, 255)    # Blue
        M = (t * (-1)) + 12
        # Y Y Y Y Y Y Y Y Y Y Y Y B B B B B B B B B B B B B B B B
    elif t < -15:                                       # Really cold
        set_multiple_pixels(range(0,28), 255, 255, 255) # white
        M = ((t + 16) * (-1))
        # W W W W W W W W W W W W W W W W W W W W W W W W W W W W

    ledshim.set_pixel(M, 0, 0, 0)
    ledshim.show()



def ledpress():

    if sensor.get_sensor_data(): 
        p = sensor.data.pressure 
        p = round(p)

    set_multiple_pixels(range(0,5), 255, 140, 0)       # orange - Very High
    set_multiple_pixels(range(5,11), 0, 0, 255)        # Blue   - High
    set_multiple_pixels(range(11,17), 0, 255, 0)       # Green  - Mid Range
    set_multiple_pixels(range(17,23), 255, 255, 0)     # Yellow - Low
    set_multiple_pixels(range(23,28), 255, 0, 0)       # Red    - Verry Low

    if p > 1070:
        M = 0
    elif p >= 1048 and p < 1070: # Very High  M is 0 - 4
        N = (p - 1048) / 4.7  
        N = round(N)
        M = 4 - N
    elif p >= 1026 and p < 1048: # High       M is 5 - 10
        N = (p - 1026) / 3.9 
        N = round(N)
        M = 10 - N
    elif p >= 1004 and p < 1026: # Mid Range  M is 11 - 16
        N = (p - 1004) / 3.9 
        N = round(N)
        M = 16 - N
    elif p >= 982 and p < 1004:  # Low        M is 17 - 22
        N = (p - 982) / 3.9 
        N = round(N)
        M = 22 - N
    elif p >= 960 and p < 982:   # Very Low   M 23 - 27
        N = (p - 960) / 4.7
        N = round(N)
        M = 27 - N
    elif p < 960:        
        M = 27

    
    ledshim.set_pixel(M, 0, 0, 0)    
    ledshim.show()     



GPIO.add_event_detect(5, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

rotation = 180
if len(sys.argv) > 1:
    try:
        rotation = int(sys.argv[1])
    except ValueError:
        print("Usage: {} <rotation>".format(sys.argv[0]))
        sys.exit(1)

unicornhatmini.set_rotation(rotation)
display_width, display_height = unicornhatmini.get_shape()

unicornhatmini.set_brightness(B)

font = ImageFont.truetype("5x7.ttf", 8)

offset_x = 0

while True:

    if offset_x == 0 and M == 0:
        text = time.strftime("It's %A %B %-d %-I:%M:%p")
        text_width, text_height = font.getsize(text)
        image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
        draw = ImageDraw.Draw(image)
        draw.text((display_width, -1), text, font=font, fill=255)
        r, g, b = (0, 255, 255)

    elif offset_x == 0 and M == 1:

        if sensor.get_sensor_data(): 
            T = sensor.data.temperature 
            T = round(T)
          
        if T <= 0: 
            r, g, b = [0, 0, 255]    # Blue
            text = ("and its very Cold @ %sc") % (T)
        elif T > 0 and T < 13:
            r, g, b, = [255, 255, 0] # Yellow
            text = ("and its cool at %sc") % (T)
        elif T >= 13 and T < 25:
            r, g, b = [0, 255, 0]    # Green
            text = ("and the Temperature is %sc") % (T)
        elif T >= 25 and T < 30:
            r, g, b = [255, 50, 0]  # Orange
            text = ("and its Hot @ %sc") % (T)
        elif T >= 30:
            r, g, b = [255, 0, 0]    # Red
            text = ("and its very Hot @ %sc") % (T)
        
        text_width, text_height = font.getsize(text)
        image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
        draw = ImageDraw.Draw(image)
        draw.text((display_width, -1), text, font=font, fill=255)
        
    elif offset_x == 0 and M == 2:
        
        if sensor.get_sensor_data(): 
            H = sensor.data.humidity
            H = round(H)
        
        if H < 30:
            r, g, b = [255, 0, 0]    # Red
            text = ("with Low %s%% Humidity") % (H)
        elif H >= 30 and H <= 60:
            r, g, b = [0, 255, 0]    # Green
            text = ("with %s%% Humidity") % (H)
        elif H > 60 and H < 80:
            r, g, b = [255, 255, 0]  # Yellow
            text = ("with High %s%% Humidity") % (H)
        elif H >= 80:
            r, g, b = [255, 0, 0]    # Red
            text = ("with Very High %s%% Humidity") % (H)
        
        text_width, text_height = font.getsize(text)
        image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
        draw = ImageDraw.Draw(image)
        draw.text((display_width, -1), text, font=font, fill=255)

    elif offset_x == 0 and M == 3:

        if sensor.get_sensor_data(): 
            P = sensor.data.pressure 
            P = round(P)
            
        if P > 0 and P < 982:             # Very Low
            r, g, b = [255, 0, 0]         # Red
            text = ("Baromiter is Very Low@ %smb : Storm Watch") % (P)
        elif P >= 982 and P < 1004:       # Low
            r, g, b = [255, 255, 0]       # Yellow
            text = ("Barometer is Low @ %smb : Posible Precipitation") % (P)
        elif P >= 1004 and P < 1026:      # Mid Range
            r, g, b = [0, 255, 0]         # Green
            text = ("Barometer is @ %smb") % (P)
        elif P >= 1026 and P < 1048:      # High
            r, g, b = [0, 0, 255]         # Blue
            text = ("Barometer is High @ %smb") % (P)
        elif P >= 1048:                   # Very High
            r, g, b = [255, 50, 0]        # Orange
            text = ("Barometer is Very High @ %smb") % (P)
        
        text_width, text_height = font.getsize(text)
        image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
        draw = ImageDraw.Draw(image)
        draw.text((display_width, -1), text, font=font, fill=255)

    elif offset_x == 0 and M == 4:

        uv = uvs.readUV()
        u = uv/100
        u = round(u)

        if u >= 0 and u < 3:                       # Low
            r, g, b = (0, 255, 0)                 # Green 
            text = ("UV Index is Low @ %s") % (u)
        elif u >= 3 and u < 6:                    # Moderate
            r, g, b = (255, 255, 0)               # Yellow
            text = ("UV Index is Moderate @ %s") % (u)
        elif u >= 6 and u < 8:                    # High
            r, g, b = (255, 50, 0)                # Orange 
            text = ("UV Index is High @ %s") % (u)
        elif u >= 8 and u < 11:                   # Very High
            r, g, b = (255, 0 ,0)                 # Red
            text = ("UV Index is Very High @ %s") % (u)
        elif u >= 11:                             # Extreme
            r, g, b = (255, 0, 255)                    # Violet
            text = ("UV Index is Extreme @ %s") % (u)

        text_width, text_height = font.getsize(text)
        image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
        draw = ImageDraw.Draw(image)
        draw.text((display_width, -1), text, font=font, fill=255)

    else:
        
        for y in range(display_height):
            for x in range(display_width):
                if image.getpixel((x + offset_x, y)) == 255:
                    unicornhatmini.set_pixel(x, y, r, g, b)
                else:
                    unicornhatmini.set_pixel(x, y, 0, 0, 0)

    unicornhatmini.show()
    time.sleep(0.05)
                    

    offset_x += 1
    if offset_x + display_width > image.size[0]:
        offset_x = 0
        M = M + 1
        readvis()
        ledtemp()

    if M == 5:
        M = 0
 
    if X == 1:
        unicornhatmini.set_all(0, 0, 0)
        unicornhatmini.show()
        ledshim.clear()
        ledshim.show()
        os.system("sudo shutdown now -P")
        time.sleep(30)

#vis = uvs.readVisible()
#vis = round(vis) 
#text = ("and the VIS is %s") % (vis)


# Last edited on June 17th 2020
# added temperature humididty and pressure display messages
# added UV index display and modified for port WC transfer
# run sudo crontab -e
# add
# @reboot python3 /home/pi/WC_MINI.py
1 Like

Thanks! I’ll give it a go tomorrow. I’ve had a few ciders! 😂

So far I’ve done the scroll weather info thing on a Sense Hat, Unicorn Hat HD, and Unicorn Mini. Getting the right font was the tricky bit on the Unicorn Hat HD.

1 Like

I’ve tried the script, but it clashes with the enviro and unicorn. The only LED matrix I can use is either scrollphat hd or scroll hat mini. But that script doesn’t work. 🤷🏻‍♂️

EDIT: I’ll add that I did alter the script to match the LED matrix I was using.

It’s just proof of concept, there is lots going on in that file that you won’t be able to use.
All the “text color” code for example. Plus, the code to actually scroll text on a Unicorn hat likely won’t work on a Scroll hat. The Scroll Hat uses i2c and the Unicorn hat uses SPI.
What I did was start with a simple example that displayed text. Then swapped the text for the string value of the temp etc.

if sensor.get_sensor_data(): 
            T = sensor.data.temperature 
            T = round(T)
text = ("%sc") % (T)

You end up with the text string being for example, 20c. The %s gets replaced with the value for T and adds a c to the end of the string.

text = ("%s%%") % (H)
The %s gets replaced with the value for H, and a % gets added to the end of the string.

text = ("%smb") % (P)
The %s gets replaced with the value for P, and a mb gets added to the end of the string.

I started with a Sense Hat. On that it was pretty easy, it had a scroll function built into its python package. My Unicorn Hat was my HD upgrade. Form 8 x 8 to 16 x16. I wanted color, RGB, so the Scroll Hat was out for me. Luckily, the Unicorn Hat has text scroll examples. It’s still a tradeoff but it was doable for me and worth the extra effort.

Trust me, I’ve seen my far share of syntax errors, lol. I’m good at knowing what a block of code does, just not how it does it. The trick is finding the block of code you need, and knowing where to put it. Most of my files are modified borrowed examples.

1 Like

Thanks for your help and detailed explanations! I’ll give it another go in the coming days. 👍🏻