Adding Pi Sense HAT and UPS Pico


#1

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 = 'https://maker.ifttt.com/trigger/'
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©: " + 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©: " + 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©", 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©", 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)


#2

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