Guest User

Untitled

a guest
Oct 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. import datetime
  2. import csv
  3. from math import log
  4. from sense_hat import SenseHat
  5.  
  6. def weather():
  7. sense = SenseHat()
  8. sense.clear()
  9. # Get temperature, humidity, pressure, and calculate dewpoint
  10. celcius = round(sense.get_temperature(), 1)
  11. fahrenheit = round(1.8 * celcius + 32, 1)
  12. humidity = round(sense.get_humidity(), 1)
  13. pressure = round(sense.get_pressure(), 1)
  14. dewpoint = round(243.04 * (log(humidity / 100) + ((17.625 * celcius) / (243.04 + celcius))) / (17.625 - log(humidity / 100) - (17.625 * celcius) / (243.04 + celcius)), 1)
  15. # Get orientation data
  16. acceleration = sense.get_accelerometer_raw()
  17. x = round(acceleration['x'], 2)
  18. y = round(acceleration['y'], 2)
  19. z = round(acceleration['z'], 2)
  20. # Set screen color based on temperature
  21. if fahrenheit > 20 and fahrenheit < 80:
  22. bg_color = [0, 0, 155] # blue
  23. elif fahrenheit > 81 and fahrenheit < 90:
  24. bg_color = [0, 155, 0] # Green
  25. elif fahrenheit > 91 and fahrenheit < 100:
  26. bg_color = [155, 155, 0] # Yellow
  27. elif fahrenheit > 101 and fahrenheit < 102:
  28. bg_color = [255, 127, 0] # Orange
  29. elif fahrenheit > 103 and fahrenheit < 104:
  30. bg_color = [155, 0, 0] # Red
  31. elif fahrenheit > 105 and fahrenheit < 109:
  32. bg_color = [255, 0, 0] # Bright Red
  33. elif fahrenheit > 110 and fahrenheit < 120:
  34. bg_color = [155, 155, 155] # White
  35. else:
  36. bg_color = [0, 155, 0] # Green
  37.  
  38. result = ' Temp. F ' + str(fahrenheit) + ' Temp. C ' + str(celcius) + ' Hum. ' + str(humidity) + ' Press. ' + str(pressure) + ' DewPoint ' + str(dewpoint)
  39. print(result)
  40. result_list = [(datetime.datetime.now(), celcius, fahrenheit, humidity, pressure, dewpoint, x, y, z)]
  41. # Log input
  42. with open('weather_logs.csv', 'a', newline='') as csv_file:
  43. writer = csv.writer(csv_file)
  44. writer.writerows(result_list)
  45. # Print the data logged 5 times
  46. for x in range(5):
  47. # set orientation
  48. acceleration = sense.get_accelerometer_raw()
  49. x = round(acceleration['x'], 0)
  50. y = round(acceleration['y'], 0)
  51. if x == -1:
  52. sense.set_rotation(90)
  53. elif y == 1:
  54. sense.set_rotation(0)
  55. elif y == -1:
  56. sense.set_rotation(180)
  57. else:
  58. sense.set_rotation(180)
  59. # print result variable to the PiHAT screen
  60. sense.show_message(result, scroll_speed=0.10, back_colour=bg_color, text_colour=[155, 155, 155])
  61.  
  62. while __name__ == '__main__':
  63. weather()
Add Comment
Please, Sign In to add comment