chefdave
Posts: 1
Joined: Fri Sep 25, 2015 2:27 pm

Sense hat in Fahrenheit

Fri Sep 25, 2015 2:34 pm

I know Celsius is a better way to display temperature, but unfortunately here in the US we don't "get it".
Is there a way to make the Sense Hat display Temperature in Fahrenheit ?

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

Re: Sense hat in Fahrenheit

Fri Sep 25, 2015 3:01 pm

Yep just convert it.

Code: Select all

temp = ((temp/5)*9)+32
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

andyk1
Posts: 6
Joined: Sat Sep 21, 2013 5:15 am

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 6:39 am

Is there any way anyone can show me a line of code that uses this formula. I have searched and can not find a usable line of code for this. You can't just enter that into code and not run into errors.

vetzki
Posts: 10
Joined: Sat Jul 09, 2016 12:33 pm

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 9:18 am

you could use something like this:

Code: Select all

def tofahrenheit(celsius):
    return( (celsius/5*9)+32 )

temp = tofahrenheit(sense.get_temperature())
print(temp)

jahboater
Posts: 4769
Joined: Wed Feb 04, 2015 6:38 pm

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 9:35 am

andyk1 wrote:Is there any way anyone can show me a line of code that uses this formula. I have searched and can not find a usable line of code for this. You can't just enter that into code and not run into errors.
You most definitely can! Define "code"? What language?
You have been given some Python which is a very popular language for the purpose. I suspect you really mean "bash" the command interpreter (shell), in which case enclose the expression in (( and )).

The Pi and the sense hat are all about education.
So assuming you are using Python, try to modify the program (likely a .py) file, using the code given.
It should be really simple, and you will find it very satisfying when it works.
You will learn a lot which is what its all about!
Edit the Python file with nano.

andyk1
Posts: 6
Joined: Sat Sep 21, 2013 5:15 am

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 2:07 pm

I suppose I should have been more clear. Yes writing in python 3.5.2
Trying to write for the Raspberry Pi Sensor Hat.

I manage to experiment and came up with the following:

t = sense.get_temperature() * 9/5 + 32
#or t = sense.get_temperature() * 1.8 +32

Both worked for my needs.

One other issue I am having is changing the color of the display... not background to a color say from:

-15 F to 65 F to be blue
66F to 85F to be green
86F to 120F to be red

On the sense-had display. I manage to get two colors to work, green and red but seems my code cancels out the colder blue code.

User avatar
bensimmo
Posts: 4184
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 3:38 pm

We would need your code for that.

copy and paste it all in code brackets faq.php?mode=bbcode#f2r1

personally if not at the command line then leafpad that RPF put in is much easier than nano.

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

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 4:47 pm

andyk1 wrote: One other issue I am having is changing the color of the display... not background to a color say from:

-15 F to 65 F to be blue
66F to 85F to be green
86F to 120F to be red

On the sense-had display. I manage to get two colors to work, green and red but seems my code cancels out the colder blue code.
Be sure that you use the correct IF..ELIF..ELSE syntax in python.
For example (Modify for your own requirements)

Code: Select all

if temp <= 65:
   displayBlue()
elif temp > 65 and temp <=85 :
   displayGreen()
else:
   displayRed()
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

andyk1
Posts: 6
Joined: Sat Sep 21, 2013 5:15 am

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 5:50 pm

Let me try this again...

I will try and answer all your questions but first thank you all. For me as someone said would be easier to show you what I have so far. Yes am just learning.

Code: Select all

# Project By Andy to create temp, hum, press,  wind,  rain, soil, uv , security cam's and other sensor.
from sense_hat import SenseHat
sense = SenseHat()
sense.set_rotation(180)

while True:
    t =  sense.get_temperature()  *  9/5  +  32 # changed to Fahrenheit. Works
#  t =  sense.get_temperature() *  1.8 + 32;    another way to read F temp
    p = sense.get_pressure()
    h = sense.get_humidity()

    t = round(t, 1)
    p = round(p, 1)
    h = round(h, 1)

#    if t > -15.0 and t < 64.9:  # can't get to read 
#      tc = [0, 0, 100]  # Blue
#    else:
#      tc = [0, 100, 0]  # Green

    if t > 65.0 and t < 84.9:
        tc = [0, 100, 0]  # Green
    else:
        tc = [100, 0, 0]  # Red

#     if h > 85.0 and h < 120.0:  can't get to read 
#        tc = [100, 0, 0]  # red
#      else:                                    can't get to read
#         tc = [100, 0, 0]  # red


#  msg = "T={0} P={0} H={0}"  .format ( t,p,h )
    msg = "T=%s P=%s H=%s"% (t , p, h)
#    sense.show_message(msg, scroll_speed=0.05, text_colour=[0,0,0], back_colour=bg)
    sense.show_message(msg, scroll_speed=0.07, text_colour=tc) # bg=background color

# todo. setup other sensors
# todo. setup day-night brightness
# todo .setuo to show on monitor also

hope I read this right.

As you can see parts of it works and parts don't. Not sure why I can't get this.
Last edited by andyk1 on Tue Sep 27, 2016 6:56 pm, edited 1 time in total.

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

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 6:36 pm

Edit your post and everywhere you've got [quote] change it to [code] and everywhere you've got [/quote] change it to [/code]

That will fix the ugly code formatting.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

andyk1
Posts: 6
Joined: Sat Sep 21, 2013 5:15 am

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 7:02 pm

Thanks DougieLawson I did that

User avatar
Paeryn
Posts: 2704
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 7:30 pm

andyk1 wrote:

Code: Select all

#    if t > -15.0 and t < 64.9:  # can't get to read 
#      tc = [0, 0, 100]  # Blue
#    else:
#      tc = [0, 100, 0]  # Green

    if t > 65.0 and t < 84.9:
        tc = [0, 100, 0]  # Green
    else:
        tc = [100, 0, 0]  # Red

#     if h > 85.0 and h < 120.0:  can't get to read 
#        tc = [100, 0, 0]  # red
#      else:                                    can't get to read
#         tc = [100, 0, 0]  # red
I assume that should be t in the final commented out if statement, not h.
If they were not commented out then only the final if statement would take effect (either it's true or false section), it would overwrite whatever value was set in either of the other two before it.

If you want absolutely those ranges then something like

Code: Select all

if -15 <= t <= 65:
  tc = [0, 0, 100] # Blue
elif 65 < t <= 85:
  tc = [0, 100, 0] # Green
elif 85 < t <= 120:
  tc = [100, 0, 0] # Red
else:
  tc = [100, 100, 100] # White
Here I've set tc to white if t is outside the specified range (to make sure it's set to something).

If you want always less than or equal to 65 to be blue, and always above 85 to be red with anything in between to be green then

Code: Select all

if t <= 65:
  tc = [0, 0, 100] # Blue
elif t <= 85:
  tc = [0, 100, 0] # Green
else:
  tc = [100, 0, 0] # Red
Last edited by Paeryn on Tue Sep 27, 2016 7:35 pm, edited 4 times in total.
She who travels light — forgot something.

User avatar
bensimmo
Posts: 4184
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 7:30 pm

It looks like you need to alter your If/Else into the format given above.

e.g.

Code: Select all

if temperature < 65:
   temp_background_colour = [0, 0, 100]
elif temperature > 85 :
   temp_background_colour = [100, 0, 0] 
else:
   temp_background_colour = [0, 100, 0]
Which check for cold so blue, hot so red and anything else must be good so green

User avatar
bensimmo
Posts: 4184
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Sense hat in Fahrenheit

Tue Sep 27, 2016 7:36 pm

oh and while you're at ti, you can test your code here quickly, https://trinket.io/sense-hat
you may need to put a sleep into your while loop?

andyk1
Posts: 6
Joined: Sat Sep 21, 2013 5:15 am

Re: Sense hat in Fahrenheit

Wed Sep 28, 2016 4:57 am

Took all of your advice... thanks to everyone. Could not get some of your code to work for my purposes but trial and error I got this.

Code: Select all

from sense_hat import SenseHat
sense = SenseHat()
sense.set_rotation(180)

while True:
    t =  sense.get_temperature()  *  1.8  +  32;
    p = sense.get_pressure()
    h = sense.get_humidity()

    t = round(t, 1)
    p = round(p, 1)
    h = round(h, 1)

    if t <= 65:
      tc = [0, 0, 100] # Blue
    elif t <= 80:
      tc = [0, 100, 0] # Green
    else:
      tc = [100, 0, 0] # Red

    msg = "T=%s P=%s H=%s"% (t , p, h)
    sense.show_message(msg, scroll_speed=0.07, text_colour=tc) # bc=background color

I simplified it and does work.

Can someone show me how to print this to the monitor and sense-hat at the same time please? The print command doesn't work for me.

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

Re: Sense hat in Fahrenheit

Wed Sep 28, 2016 9:19 am

andyk1 wrote: Can someone show me how to print this to the monitor and sense-hat at the same time please? The print command doesn't work for me.
What about the print command doesn't work? Where do you expect the text to be displayed?

Are you running your program from the command line or from an IDE like IDLE?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

andyk1
Posts: 6
Joined: Sat Sep 21, 2013 5:15 am

Re: Sense hat in Fahrenheit

Wed Sep 28, 2016 11:14 am

What about the print command doesn't work? Where do you expect the text to be displayed?

Are you running your program from the command line or from an IDE like IDLE?
I am using IDLE 3 Python 3.

What I am trying to do is have it display on the Pi sensor-hat and the Display monitor I have hooked up to the Rpi 3b via hdmi of course. Not sure why it does display on the sensor-hat but does not print to screen. Blank.
I am not at the terminal window so can't post the code. will in the morning. the code is actually a few posts up with the line:

print(t,p,h)

At the very bottom. No error code. No print to screen.

Need a cheat sheet for python 3 structure . Google is no help.

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

Re: Sense hat in Fahrenheit

Wed Sep 28, 2016 12:24 pm

Try the following

1. Open a instance of LXTerminal from the Raspberry Pi Menu
2. Type `python3`
3. At the python prompt type `print("Hello World")`
4. "Hello World" should be shown on the screen.
5. Type `quit()` to exit the python prompt.

The output of print will only be shown on a command prompt or similar terminal output. It won't appear as a popup or dialog on the screen for example.
Since you are using IDLE, the output of print should show up in the IDLE window that is probably called "Python 3.4.3 Shell"
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Astro Pi”