My problem right now is that I am trying to make a code that will change the LED visualizer colour based on how the humidity sensor changes in value. Im messing around with experimental if statements but no luck. Here is the code:
Code: Select all
from sense_hat import SenseHat
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from ISStreamer.Streamer import Streamer
import time
import sys
import datetime
import smbus
import smtplib
i2c = smbus.SMBus(1)
# --------- User Settings ---------
CITY = "Hamilton"
BUCKET_NAME = ":partly_sunny: " + CITY + " Weather"
BUCKET_KEY = "sense-hat"
ACCESS_KEY = ""
SENSOR_LOCATION_NAME = "Net Access Office"
MINUTES_BETWEEN_SENSEHAT_READS = 0.1
# ---------------------------------
streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY)
sense = SenseHat()
sense.clear()
g = (0, 255, 0) #Green
r = (255, 0, 0) #Red
y = (255, 255, 0) #yellow
#humidity sensor will change color dynamically according to different values
if sense.get_humidity >=45:
hawt = [
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, 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,
r, r, r, r, r, r, r, r
]
sense.clear()
sense.set_pixels(hawt)
elif sense.get_humidity <= 40:
mild = [
y, y, y, y, y, y, y, y,
y, y, y, y, y, y, y, y,
y, y, y, y, y, y, y, y,
y, y, y, y, y, y, y, y,
y, y, y, y, y, y, y, y,
y, y, y, y, y, y, y, y,
y, y, y, y, y, y, y, y,
y, y, y, y, y, y, y, y
]
sense.clear()
sense.set_pixels(mild)
elif sense.get_humidity == 35:
cool = [
g, g, g, g, g, g, g, g,
g, g, g, g, g, g, g, g,
g, g, g, g, g, g, g, g,
g, g, g, g, g, g, g, g,
g, g, g, g, g, g, g, g,
g, g, g, g, g, g, g, g,
g, g, g, g, g, g, g, g,
g, g, g, g, g, g, g, g
]
sense.clear()
sense.set_pixels(cool)
else:
print "error"
def bat_level():
time.sleep(0.1)
data = i2c.read_word_data(0x69, 0x01)
data = format(data,"02x")
return (float(data) / 100)
def pwr_mode():
data = i2c.read_byte_data(0x69, 0x00)
data = data & ~(1 << 7)
if (data == 1):
return "1 Fully Powered" #Power mode on RPi
elif (data == 2):
return "2 Backup Battery" #Power mode on Battery
else:
return "0 Error!" #Error
def rpi_level():
time.sleep(0.1)
data = i2c.read_word_data(0x69, 0x03)
data = format(data,"02x")
return (float(data) / 100)
def sot23_temp():
time.sleep(0.1)
data = i2c.read_byte_data(0x69, 0x0C)
data = format(data,"02x")
return data
while True:
# Read the sensors
temp_c = sense.get_temperature()
humidity = sense.get_humidity()
pressure_mb = sense.get_pressure()
battery_level = bat_level()
power_mode = pwr_mode()
raspberryPi_level = rpi_level()
UPS_Pico_temp = sot23_temp()
# Format the data
temp_c = float("{0:.2f}".format(temp_c))
humidity = float("{0:.2f}".format(humidity))
pressure_mb = float("{0:.2f}".format(pressure_mb))
battery_level = float("{0:.1f}".format(battery_level))
raspberryPi_level = float("{0:.1f}".format(raspberryPi_level))
# Print and stream
print " "
print " Weather Monitor Status"
print "***********************************"
print SENSOR_LOCATION_NAME + " Temperature(C): " + str(temp_c)
print SENSOR_LOCATION_NAME + " Humidity(%): " + str(humidity)
print SENSOR_LOCATION_NAME + " Pressure(IN): " + str(pressure_mb)
print SENSOR_LOCATION_NAME + " Battery(V): " + str(battery_level)
print SENSOR_LOCATION_NAME + " Power Mode(M): " + str(power_mode)
print SENSOR_LOCATION_NAME + " Raspberry Pi(V): " + str(raspberryPi_level)
print SENSOR_LOCATION_NAME + " UPS Pico Temperature(C): " + str(UPS_Pico_temp)
print "***********************************"
print " "
streamer.log(":sunny: " + SENSOR_LOCATION_NAME + " Temperature(C)", temp_c)
streamer.log(":sweat_drops: " + SENSOR_LOCATION_NAME + " Humidity(%)", humidity)
streamer.log(":cloud: " + SENSOR_LOCATION_NAME + " Pressure(IN)", pressure_mb)
streamer.log(":cloud: " + SENSOR_LOCATION_NAME + " Battery(V)", battery_level)
streamer.log(":cloud: " + SENSOR_LOCATION_NAME + " Power Mode(M)", power_mode)
streamer.log(":cloud: " + SENSOR_LOCATION_NAME + " Raspberry Pi(V)", raspberryPi_level)
streamer.log(":cloud: " + SENSOR_LOCATION_NAME + " UPS Pico Temperature(C)", UPS_Pico_temp)
streamer.flush()
time.sleep(60*MINUTES_BETWEEN_SENSEHAT_READS)