Code:
import os
import time, datetime
import bme680
import SI1145.SI1145 as SI1145
import ledshim
import RPi.GPIO as GPIO
from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
sense = SenseHat()
sense.set_rotation(180)
sense.low_light = False
uvs = SI1145.SI1145()
sensor = bme680.BME680()
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)
s = 0.1 # scroll speed
w = 0 # color all white toggle
o = 140 # orange white toggle value
x = 2 # shutdown variable
L = 1
#is really stick right
def pushed_left(event):
global L
if event.action == ACTION_PRESSED:
L = 0 # Display Pressure on LED Shim
# is really stick left
def pushed_right(event):
global L
if event.action == ACTION_PRESSED:
L = 1 # Display Temperature on LED Shim
def pushed_middle(event):
global x
if event.action == ACTION_PRESSED:
x = 1
sense.stick.direction_left = pushed_left
sense.stick.direction_right = pushed_right
sense.stick.direction_middle = pushed_middle
def set_multiple_pixels(indexes, r, g, b):
for index in indexes:
ledshim.set_pixel(index, r, g, b)
def Shutdown(channel):
global x
x = 0
def readvis():
vis = uvs.readVisible()
vis = (round(vis))
global w
global o
if vis < 270:
sense.low_light = True
ledshim.set_brightness(0.4)
ledshim.show()
w = 0
o = 140
elif vis >= 270 and vis < 650:
sense.low_light = False
ledshim.set_brightness(1.0)
ledshim.show()
w = 0
o = 140
elif vis >= 650:
sense.low_light = False
ledshim.set_brightness(1.0)
ledshim.show()
w = 255
o = 255
def ledtemp():
if sensor.get_sensor_data():
t = sensor.data.temperature
t = round(t)
if t > 28: # Realy Hot
set_multiple_pixels(range(0,27), 255, 0, 0) # Red
set_multiple_pixels(range(27,28), 255,140, 0) # Orange
M = (t - 56) * (-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 O
elif t > 0 and t <= 28: # Main
set_multiple_pixels(range(0,3), 255, 140, 0) # Orange
set_multiple_pixels(range(3,16), 0, 255, 0) # Green
set_multiple_pixels(range(16,28), 255, 255, 0) # Yellow
M = (28 - t)
# O O O G G G G G G G G G G G G G Y Y Y Y Y Y Y Y Y Y Y Y
elif t <= 0 and t >= -27: # Cold
ledshim.set_all(0, 0, 255) # Blue
M = (t * (-1))
# B B B B B B B B B B B B B B B B B B B B B B B B B B B B
elif t < -27: # Really cold
set_multiple_pixels(range(0,3), 255, 255, 255) # Blue
set_multiple_pixels(range(3,28), 0, 255, 255) # Aqua
M = (56 + t)
# B B B A A A A A A A A A A A A A A A A A A A A A A A A A
ledshim.set_pixel(M, 0, 0, 0)
ledshim.show()
def ledpress():
if sensor.get_sensor_data():
p = sensor.data.pressure
p = round(p)
if p > 0 and p < 960: # Very Very Low
ledshim.set_all(255, 0, 0) #Red
M = ((959 - p) + 3)
# 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 p >= 960 and p < 982: # Very Low
set_multiple_pixels(range(0,3), 255, 255, 0) # Yellow
set_multiple_pixels(range(3,28), 255, 0, 0) # Red
M = ((981 - p) + 3)
# Y Y Y 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 p >= 982 and p < 1004: # Low
set_multiple_pixels(range(0,3), 0, 255, 0) # Green
set_multiple_pixels(range(3,25), 255, 255, 0) # Yellow
set_multiple_pixels(range(25,28), 255, 0, 0) # Red
M = ((1003 - p) + 3)
# G G G Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y R R R
elif p >= 1004 and p < 1026: # Mid Range
set_multiple_pixels(range(0,3), 0, 0, 255) # Blue
set_multiple_pixels(range(3,25), 0, 255, 0) # Green
set_multiple_pixels(range(25,28), 255, 255, 0) # Yellow
M = ((1025 - p) +3)
# B B B G G G G G G G G G G G G G G G G G G G G G G Y Y Y
elif p >= 1026 and p < 1048: # High
set_multiple_pixels(range(0,3), 0, 0, 255) # Blue
set_multiple_pixels(range(3,25), 0, 255, 0) # Green
set_multiple_pixels(range(25,28), 255, 255, 0) # Yellow
M = ((1047 - p) + 3)
# O O O B B B B B B B B B B B B B B B B B B B B B B G G G
elif p >= 1048 and p < 1070: # Very High
set_multiple_pixels(range(0,25), 255, 140, 0) # Orange
set_multiple_pixels(range(25,28), 0, 0, 255) # Blue
M = ((1069 - p) + 3)
# O O O O O O O O O O O O O O O O O O O O O O O O O B B B
elif p >= 1070: # Very Very High
ledshim.set_all(255, 140, 0) # Orange
M = ((1091 - p) + 3)
# O O O O O O O O O O O O O O O O O O O O O O O O O O O O
ledshim.set_pixel(M, 0, 0, 0)
ledshim.show()
GPIO.add_event_detect(5, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)
while True:
readvis()
if L == 1:
ledtemp() # Display temperature on LED Shim
elif L == 0:
ledpress() # Display pressure on LED Shim
dateString = "%A %B %-d %-I:%M:%p"
msg = "It is %s" % (datetime.datetime.now().strftime(dateString))
sense.show_message(msg, scroll_speed=s, text_colour=(w, 255, 255))
if sensor.get_sensor_data():
t = sensor.data.temperature
t = round(t)
if t <= 0:
tc = [w, w, 255] # Blue
elif t > 0 and t < 13:
tc = [255, 255, w] # Yellow
elif t >= 13 and t < 25:
tc = [w, 255, w] # Green
elif t >= 25 and t < 30:
tc = [255, o, w] # Orange
elif t >= 30:
tc = [255, w, w] # Red
msg = "and %sc" % (t)
sense.show_message(msg, scroll_speed=s, text_colour=tc)
readvis()
if L == 1:
ledtemp() # Display temperature on LED Shim
elif L == 0:
ledpress() # Display pressure on LED Shim
if sensor.get_sensor_data():
h = sensor.data.humidity
h = round(h)
if h < 0:
h = 0
if h > 100:
h = 100
if h < 30:
hc = [255, w, w] # Red
elif h >= 30 and h <= 60:
hc = [w, 255, w] # Green
elif h > 60 and h < 80:
hc = [255, 255, w] # Yellow
elif h >= 80:
hc = [255, w, w] # Red
msg = "with %s%% Humidity" % (h)
sense.show_message(msg, scroll_speed=s, text_colour=hc)
readvis()
if L == 1:
ledtemp() # Display temperature on LED Shim
elif L == 0:
ledpress() # Display pressure on LED Shim
if sensor.get_sensor_data():
p = sensor.data.pressure
p = round(p)
if p > 0 and p < 982: # Very Low
pc = [255, w, w] # Red
msg = "- Barometer is Very Low @ %smb - Storm Watch" % (p)
elif p >= 982 and p < 1004: # Low
pc = [255, 255, w] # Yellow
msg = "- Barometer is Low @ %smb - Possible Percipitation" % (p)
elif p >= 1004 and p < 1026: # Mid Range
pc = [w, 255, w] # Green
msg = "- Barometer is Mid Range @ %smb" % (p)
elif p >= 1026 and p < 1048: # High
pc = [w, w, 255] # Blue
msg = "- Barometer is High @ %smb" % (p)
elif p >= 1048: # Very High
pc = [255, o, w] # Orange
msg = "- Barometer is Very High @ %smb - Expect Dry Conditions" % (p)
sense.show_message(msg, scroll_speed=s, text_colour=pc)
readvis()
if L == 1:
ledtemp() # Display temperature on LED Shim
elif L == 0:
ledpress() # Display pressure on LED Shim
uv = uvs.readUV()
u = uv/100
u = round(u)
if u > 0 and u < 3: # Low
uc = (w, 255, w) # Green
msg = "- UV Index is Low @ %s" % (u)
elif u >= 3 and u < 6: # Moderate
uc = (255, 255, w) # Yellow
msg = "- UV Index is Moderate @ %s" % (u)
elif u >= 6 and u < 8: # High
uc = (255, o, w) # Orange
msg = "- UV Index is High @ %s" % (u)
elif u >= 8 and u < 11: # Very High
uc = (255, w ,w) # Red
msg = "- UV Index is Very High @ %s" % (u)
elif u >= 11: # Extreme
uc = (255, w, 255) # Violet
msg = "- UV Index is Extreme @ %s" % (u)
if u > 0:
sense.show_message(msg, scroll_speed=s, text_colour=uc)
#vis = uvs.readVisible()
#vis = round(vis)
#msg = "and the VIS is %s" % (vis)
#sense.show_message(msg, scroll_speed=s, text_colour=(255, w, 255))
if x == (0):
sense.clear()
ledshim.clear()
ledshim.show()
os.system("sudo shutdown now -P")
time.sleep(30)
elif x == (1):
sense.clear()
ledshim.clear()
ledshim.show()
raise SystemExit
time.sleep(30)
# Last edited on Jan 124h 2019
# added code to display temperature or pressure on LED Shim
# curl https://get.pimoroni.com/bme680 | bash
# curl https://get.pimoroni.com/ledshim | bash
# run sudo crontab -e
# add
# @reboot python3 /home/pi/PortWC.py &