n1ck75
Posts: 3
Joined: Fri Aug 28, 2015 11:35 pm

Temperature Value Shown on LEDs

Fri Aug 28, 2015 11:41 pm

Code totally lifted from Johnny5C, updated sensehat parts.

was wondering if you could get the output to flash up (fade in) every x seconds to just show temp?

Code: Select all

#!/usr/bin/python
from sense_hat import SenseHat
import time

ap = SenseHat()
temp = ap.get_temperature()
humidity = ap.get_humidity()
pressure = ap.get_pressure()


print("Temp: %s C" % temp)               # Show temp on console
print("Humidity: %s %%rH" % humidity)        # Show humidity on console
print("Pressure: %s Millibars" % pressure)    # Show pressure on console

ap.set_rotation(180)        # Set LED matrix to scroll from right to left
              
ap.show_message("%.1f C" % temp, scroll_speed=0.10, text_colour=[0, 255, 0])

time.sleep(1)           # Wait 1 second

ap.show_message("%.1f %%rH" % humidity, scroll_speed=0.10, text_colour=[255, 0, 0]) 

time.sleep(1)      # Wait 1 second

ap.show_message("%.1f Millibars" % humidity, scroll_speed=0.10, text_colour=[0, 0, 255])

ap.clear()      # Clear LED matrix

scotty101
Posts: 2246
Joined: Fri Jun 08, 2012 6:03 pm

Re: Temperature Value Shown on LEDs

Sat Aug 29, 2015 10:23 am

Yes of course. You need to wrap the parts of the code that you wish to repeat inside a loop. If you want this to happen indefinitely then use a while loop.

For example

Code: Select all

while True:
    ap.show_message("Hello again")
    time.sleep(10)
This will print Hello Again every 10 seconds until you kill the program.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
dliloch
Posts: 163
Joined: Wed Jun 27, 2012 6:28 pm
Location: cleveland, ohio usa

Re: Temperature Value Shown on LEDs

Sat Aug 29, 2015 8:32 pm

I think you want the last message to use the pressure variable? also the temp on my sensehat seems about 15% high in relation to the ambient temp .. so I just took 85% of it .. I wonder if there is a better way to get ambient temp.. I imagine the leds and the processor throw the temp sensor off ..
the pressure and relative humidity seem accurate ..
nice example to work with .. thanks..

n1ck75
Posts: 3
Joined: Fri Aug 28, 2015 11:35 pm

Re: Temperature Value Shown on LEDs

Sat Sep 12, 2015 4:05 pm

Updated to just show the temperature continuously (Thx Scotty101)

Code: Select all

#!/usr/bin/python
from sense_hat import SenseHat
import time
while True:
    ap = SenseHat()
    temp = ap.get_temperature()
    print("Temp: %s C" % temp)               # Show temp on console

    ap.set_rotation(180)        # Set LED matrix to scroll from right to left

    ap.show_message("%.1f C" % temp, scroll_speed=0.10, text_colour=[0, 255, 0])
    time.sleep(10)
Next question, is it possible to adjust the temp output by a percentage, to adjust for the latent temp of pi/led. ie.

output / 5%

subjektivdk
Posts: 9
Joined: Tue Sep 01, 2015 6:13 pm
Location: Denmark
Contact: Website

Re: Temperature Value Shown on LEDs

Tue Sep 15, 2015 9:29 am

n1ck75 wrote:Updated to just show the temperature continuously (Thx Scotty101)

Code: Select all

#!/usr/bin/python
from sense_hat import SenseHat
import time
while True:
    ap = SenseHat()
    temp = ap.get_temperature()
    print("Temp: %s C" % temp)               # Show temp on console

    ap.set_rotation(180)        # Set LED matrix to scroll from right to left

    ap.show_message("%.1f C" % temp, scroll_speed=0.10, text_colour=[0, 255, 0])
    time.sleep(10)
Next question, is it possible to adjust the temp output by a percentage, to adjust for the latent temp of pi/led. ie.

output / 5%
output * 0.95
subjektiv.dk

AndersM
Posts: 26
Joined: Sun Sep 06, 2015 1:18 pm

Re: Temperature Value Shown on LEDs

Tue Sep 15, 2015 12:46 pm

Also see this thread on temperature sensors and ambient temperature.

hansonm
Posts: 1
Joined: Wed Sep 16, 2015 5:30 pm

Re: Temperature Value Shown on LEDs

Wed Sep 16, 2015 5:33 pm

is there a way to convert the temp to Fahrenheit?

scotty101
Posts: 2246
Joined: Fri Jun 08, 2012 6:03 pm

Re: Temperature Value Shown on LEDs

Wed Sep 16, 2015 9:11 pm

Yes of course. It's quite a simple conversion from Celsius to Fahrenheit.
http://www.pythonforbeginners.com/code- ... converter/
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
DougieLawson
Posts: 29373
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Temperature Value Shown on LEDs

Thu Sep 17, 2015 7:56 pm

hansonm wrote:is there a way to convert the temp to Fahrenheit?

Code: Select all

temp = ((temp/5)*9)+32 
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

n1ck75
Posts: 3
Joined: Fri Aug 28, 2015 11:35 pm

Re: Temperature Value Shown on LEDs

Sun Sep 20, 2015 3:11 pm

How could you show colour variant to indicate temperature?

Example;

Above 16C = Red
Below 16C = Blue

AndersM
Posts: 26
Joined: Sun Sep 06, 2015 1:18 pm

Re: Temperature Value Shown on LEDs

Tue Sep 22, 2015 5:51 pm

Made Python program that displays a temperature scale from violet to red using a "trick" with HLS hue to easily create the colours in the scale.

Code: Select all

import time
import colorsys
from sense_hat import SenseHat

sense = SenseHat()

#The limits of the temperature scale
MAXTEMP = 40.0
MINTEMP = 0.0
#The limits of the HLS hue scale 0.0=red, 0.9=violet 
MAXHUE = 0.0
MINHUE = 0.90

while 1:
    # reading temp and correcting for sensor bias
    temp = sense.get_temperature()
    calctemp = 0.0071*temp*temp+0.857*temp-10.0 

    #Calculating hue from temp and limit to scale range
    hue = MINHUE-(calctemp-MINTEMP)/(MAXTEMP-MINTEMP)*(MINHUE-MAXHUE)
    hue = min(hue,MINHUE)
    hue = max(hue,MAXHUE)
    
    #translating HLS to RGB and displaying with LEDs
    rgbfloat=colorsys.hls_to_rgb(hue, 0.5, 1)
    rgb=(int(rgbfloat[0]*255), int(rgbfloat[1]*255), int(rgbfloat[2]*255))
    sense.clear(rgb)

    #Debug stuff
    #print calctemp
    #print hue
    
    time.sleep(1)

tonygo2
Posts: 83
Joined: Sun Aug 28, 2011 2:50 pm
Location: Leicester, UK

Re: Temperature Value Shown on LEDs

Sun Sep 27, 2015 6:24 pm

Here is a different way to show temperatures or humidity. The latter works better in a demo as it is easier to get the values to change quite quickly over a greater range if you 'huff' on it and then let it return to normal.

Please remember that these sensors are not very accurate - ignore the decimal part it is total fiction - round to the nearest integer.

Code: Select all

# Humidity/Temperature display for Sense Hat - Demo
# Tony Goodhew 27 September 2015
# Python3 - "Use sudo idle3"
from sense_hat import SenseHat
import time

sense = SenseHat()
sense.clear()

#one row per 5x3 digit - 0 to 9
nums =[1,1,1,1,0,1,1,0,1,1,0,1,1,1,1,  # 0
       0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,  # 1
       1,1,1,0,0,1,0,1,0,1,0,0,1,1,1,  # 2
       1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,  # 3
       1,0,0,1,0,1,1,1,1,0,0,1,0,0,1,  # 4
       1,1,1,1,0,0,1,1,1,0,0,1,1,1,1,  # 5
       1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,  # 6
       1,1,1,0,0,1,0,1,0,1,0,0,1,0,0,  # 7
       1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,  # 8
       1,1,1,1,0,1,1,1,1,0,0,1,0,0,1]  # 9

def show_num(val,xd,yd,r,g,b):
    offset = val * 15
    for p in range(offset,offset + 15):
        if nums[p] == 1:
            xt = p % 3
            yt = (p-offset) // 3
            sense.set_pixel(xt+xd,yt+yd,r,g,b)       
    
def show_number(val,r,g,b): # Range -100 to 100
    abs_val = abs(val)
    tens = abs_val // 10
    units = abs_val % 10
    sense.clear()
    if (abs_val > 9): show_num(tens,0,2,r,g,b)
    show_num(units,4,2,r,g,b)
    if abs_val == 100: # Deal with 3 digit "100"
        sense.clear()
        show_num(0,5,2,r,g,b)
        show_num(0,2,2,r,g,b)
        for i in range(2,7):
            sense.set_pixel(0,i,r,g,b)
    if val < 0 :       # Display 'Negative' bar
        for i in range(0,8):
            sense.set_pixel(i,0,0,0,128)

#========== MAIN ===============

print("===== Constantly display Humidity =====")
print("**  Tony Goodhew -- 27 Sept 2015  **\n")
print("         Terminate with CTRL-C")
print(" Huff on humidity sensor to change value")

old_val = -999 # Out of range, changed first time
while True:
    val = sense.get_humidity() # Only accurate to +/- 0.5 %
#    val = sense.get_temperature_from_humidity()
#    val = sense.get_temperature_from_pressure()
    sign = 1 # Useful for low temperatures
    if val < 0 :
        sign = -1
    val = int(abs(val + 0.5)) # round to nearest whole deg C
    if val != old_val: # Has humidity changed?
        old_val = val
        sense.clear()
        show_number(val,128,50,30)
        time.sleep(0.3)
Comments/ suggestions welcome

Return to “Astro Pi”

Who is online

Users browsing this forum: No registered users and 3 guests