Adding Pi Sense HAT and UPS Pico

Hello! I have stumbled upon this special dashboard through a reddit post. I currently have an environmental monitor project that integrates the Raspberry Pi Sense HAT and the UPS Pico. The Sense hat is a multi purpose sensor that is mainly used to pick up the temperature for my project and the UPS Pico serves as a backup battery incase of a power outage. I have integrated the sensor data on both the UPS Pico and Sense HAT into the Initial State platform but would like to switch over to Cayenne.

If you’re interested in looking my code it is down below:

from sense_hat import SenseHat
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from ISStreamer.Streamer import Streamer
import RPi.GPIO as GPIO
import time
import sys
import datetime
import smbus
import smtplib
import urllib2
i2c = smbus.SMBus(1)

--------- User Settings ---------

CITY = β€œHamilton”
BUCKET_NAME = β€œ:partly_sunny: " + CITY + " Weather”
BUCKET_KEY = β€œsense-hat”
ACCESS_KEY = " "
SENSOR_LOCATION_NAME = " "
MINUTES_BETWEEN_SENSEHAT_READS = 0.05

---------------------------------

streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY)
sense = SenseHat()
sense.clear()

red = (255, 0, 0)
green = (0, 255, 0)
orange = (255, 128, 0)

r = red
g = green
o = orange

red_pixels = [
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
]

orange_pixels = [
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, 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,
o, o, o, o, o, o, o, o
]
green_pixels = [
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
]

def send_temp_trigger():
EVENT = β€˜VERY_HOT’
BASE_URL = β€˜Webhooks works better with IFTTT’
KEY = ’ ’

response = urllib2.urlopen(
BASE_URL + EVENT + β€˜/with/key/’ + KEY)
print(response.read())

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

def getBatteryPercentage():
time.sleep(0.1)
dataVolts = bat_level()
dataLevel = ((dataVolts-3.4)/0.8)*100
return dataLevel

while True:
#humidity sensor will change color dynamically according to different values
temp = sense.temp
calctemp = 0.0071temptemp+0.86*temp-11.0
if calctemp <= 21:
sense.set_pixels(green_pixels)
elif calctemp <= 23:
sense.set_pixels(orange_pixels)
else:
sense.set_pixels(red_pixels)

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()
UPS_BAT_PERCENT = getBatteryPercentage()

Format the data

calctemp = float(β€œ{0:.1f}”.format(calctemp))
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))
UPS_BAT_PERCENT = float(β€œ{0:.1f}”.format(UPS_BAT_PERCENT))

Print and stream

print " "
print " Weather Monitor Status"
print β€œ"
print SENSOR_LOCATION_NAME + " Temperature(C): " + str(calctemp)
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 SENSOR_LOCATION_NAME + " UPS Pico Battery Level(%): " + str(UPS_BAT_PERCENT)
print "
”
print " "

streamer.log(β€œ:sunny: " + SENSOR_LOCATION_NAME + " Temperature(C)”, calctemp)
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.log(":sunny: " + SENSOR_LOCATION_NAME + " UPS Pico Battery Level(%): ", UPS_BAT_PERCENT)

streamer.flush()
time.sleep(60*MINUTES_BETWEEN_SENSEHAT_READS)

Hi @dnguyen3985,

We’re working on ability to allow users to bring on any device they want. This way, you are not waiting on Cayenne team to do one-off device integrations :slight_smile:

Sense Hat is on our roadmap though to do integration !

-B

1 Like