Display temperature in colours on unicorn pHat

Hello,

first I am new in programming and tried a few tutorials and played around a little bit.
I want to realize that my unicorn pHat shows the outside temperature in colours. When it’s cold outside the colour is blue when hot it’s red. I have another raspberry with a DS18B20 that logs the outside temperature.
I have a text file that contains the temperature (for example 3.19) I want to read this file and show a temperature in colours on the unicorn pHat (cold in blue, hot in red). After a hour or whatever it should check the file again.

But I have no idea how to do this. After trying a few beginner tutorials I know how to change the colour of the unicorn pHat (yeah ;)) but I do not find something how to read a file and change the colour in dependency to the temperature value.

Can someone give me a hint?

An idea for future doing is to show temperature in rows every row shows the temperature of previous hours.

Thanks & best regards

Marco

Hi there!
I haven’t got any experience with the Unicorn pHat; in fact, I don’t even own one, so I’m sorry if this is a pretty rough explanation, but I noticed your post was from a while back now and I’ve recently had experience with some string manipulation stuff in Python, so I wondered if I could be of any assistance.
Firstly, your way ahead of me, since I haven’t a clue how to change the colour of a Unicorn pHat, so that bit may be a bit more difficult for me to demonstrate, but I’ll try the psuedo-code bit first and it may give you a hint.
Have you managed to get the text file from your external temperature monitoring Pi to your Unicorn Pi? If so, great! This code should be good enough to get you going. If not; I’m afraid I’m not much help. I’ve heard of lots of wireless ways to transmit stuff between Pis, but I’ve had no experience so far.

Now, of course, presuming you want to use Python;
I’ve created a dummy file called “temp.txt” which includes a list of random temperatures (some being pretty extravagant), with each value separated by a line.

3.10
2.95
10.93
11.95
100.45
999999999.999

Here’s an example with what you can do with that txt file:

import os # Importing the module to read the file.

templist = open(“temp.txt”).read().split() #Opening the file in Python

print(templist) # Printing as a test to see if it has been properly opened (not necessary)

for word in templist: # Loops around every value in the list
print(" ") # Just a spacer to make things look nice :)
print(word) # Print the current temperature
if float(word) > 5:
print(“HOT”) # If the temperature is above 5, say it is hot.
elif float(word) <=5:
print(“COLD”) # If the temperature is below or equal to 5, say it is cold.
else:
print(“Error”) # This should be an impossibility, but just in case. You never know when Q may change the laws of physics ;)

Of course, where in this program I am printing either “HOT” or “COLD” you would change it to make the Unicorn pHat show the correct colour.

Now this code runs through the entire list, but I’d presume you’d want it to check the last temperature on the list, instead of all of them;

import os # Importing the module to read the file.
import time # Importing the module to check every hour.

sec = time.time()

def getlast():
templist = open(“temp.txt”).read().split() #Opening the file in Python
last = templist[-1] # Getting the last value in the list

def hotornot(last): # Making a function to check for the temperature, pretty self explanatory
if last > 5:
print(“HOT”)
elif last <= 5:
print(“COLD”)
else:
print(“Q! What have you done!?”)

continue_ = True # Acts as a stopper gauge

while continue_ == True: # While forever:
sec1 = time.time() - sec # Set sec1 as being the time elapsed since the loop began
sec1 = int(sec1) # And make it an integer so we can easily use it
if sec1 == 3600: # If the seconds elapsed are 1 hour
temp = getlast() # Take a new last value from the temp file
light = hotornot(temp) # Find whether it is hot or not
sec = time.time() # Update the time to a new elapsed time

Tada!
I hope that helped. Believe me, I’m a novice. It may not have been helpful at all really, but, oh well!
I wish you luck!
-RaspberryPicardBox

I do a similar thing on my sense hat. I show a continuously scrolling message on the LED matrix with date, time, temp, humidity, pressure, etc. The text for each item changes color based on the values measured. Here is my python code for temp if it helps. My values are taken from the sense hats sensors in real time.

while True:

dateString = "%A %b %-d %-I:%M %p"
msg = "It is %s" % (datetime.datetime.now().strftime(dateString))
sense.show_message(msg, scroll_speed=s, text_colour=(w, 255, 255))

t = sense.get_temperature()
t = round(t)
      
if t <= 0: 
    tc = [0, 0, 255]  # blue
elif t > 0 and t < 13:
    tc = [255, 255, 0]  # yellow
elif t >= 13 and t <= 27:
    tc = [0, 255, 0]  # green
elif t > 27:
    tc = [255, 0, 0]  # red                 
msg = "and %sc" % (t)
sense.show_message(msg, scroll_speed=s, text_colour=tc)