D10d3

picorder.py

Apr 19th, 2020
838
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from os import system
  2. import curses
  3. import time
  4. import datetime
  5. import os
  6. import random
  7. from sense_hat import SenseHat
  8. from time import sleep
  9. import numpy as np
  10. import RPi.GPIO as GPIO
  11.  
  12. GPIO.setmode(GPIO.BCM)
  13. GPIO.setup(11,GPIO.OUT) #led Red
  14. GPIO.setup(9,GPIO.OUT) #led Green
  15. GPIO.setup(10,GPIO.OUT) #led Yellow
  16.  
  17. GPIO.output(11, GPIO.HIGH)
  18. GPIO.output(9, GPIO.HIGH)
  19. GPIO.output(10, GPIO.HIGH)
  20.  
  21. sense = SenseHat()
  22. os.environ["NCURSES_NO_UTF8_ACS"] = "1"
  23. curses.initscr()
  24. curses.halfdelay(3)
  25. curses.start_color()
  26. curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
  27. curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLUE)
  28. curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_YELLOW)
  29. curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_WHITE)
  30.  
  31. start_time = time.time()
  32. start_hour = time.time()
  33.  
  34. # get CPU temperature
  35. def get_cpu_temp():
  36.     res = os.popen("vcgencmd measure_temp").readline()
  37.     t = float(res.replace("temp=","").replace("'C\n",""))
  38.     return(t)
  39.  
  40. # use moving average to smooth readings
  41. def get_smooth(x):
  42.     if not hasattr(get_smooth, "t"):
  43.         get_smooth.t = [x,x,x]
  44.     get_smooth.t[2] = get_smooth.t[1]
  45.     get_smooth.t[1] = get_smooth.t[0]
  46.     get_smooth.t[0] = x
  47.     xs = (get_smooth.t[0]+get_smooth.t[1]+get_smooth.t[2])/3
  48.     return(xs)
  49.  
  50. # calculates the real temperature compesating CPU heating
  51. def get_temp():
  52.     t1 = sense.get_temperature_from_humidity()
  53.     t2 = sense.get_temperature_from_pressure()
  54.     t_cpu = get_cpu_temp()
  55.     h = sense.get_humidity()
  56.     p = sense.get_pressure()
  57.     t = (t1+t2)/2
  58.     t_corr = t - ((t_cpu-t)/1.25)
  59.     t_corr = get_smooth(t_corr)
  60.     f_corr = round(((t_corr * 9)/5)+32,1)
  61.     return(f_corr)
  62.  
  63. def sendlog():
  64.         os.system("mpack -s picorder -a /home/pi/picordlog.txt d10d3.email@gmail.com")
  65.  
  66. def logfile(logstring):
  67.     global start_time
  68.     global start_hour
  69.     current_time=time.time()
  70.     if current_time>start_time+180:
  71.         x = datetime.datetime.now()
  72.         tstamp = datetime.datetime(x.year,x.month,x.day,x.hour,x.minute)
  73.         #local_time=time.ctime(current_time)
  74.         f= open("/home/pi/picordlog.txt","a+")
  75.         logstring=str(tstamp)+","+logstring
  76.         f.write(logstring+"\r\n")
  77.         f.close()
  78.         start_time = time.time()
  79.     if current_time>start_hour+3600:
  80.         start_hour = time.time()
  81.         sendlog()
  82.  
  83. def BlinkinLights():
  84.     grid = []
  85.     for i in range(0,64):
  86.         n = random.randint(0, 4)
  87.         if n == 0:
  88.             r = random.randint(0,100)
  89.             b = random.randint(0,100)
  90.             g = random.randint(0,100)
  91.             grid.append ([r,g,b])
  92.         else:
  93.             grid.append ([0,0,0])
  94.     sense.set_pixels(grid)
  95.  
  96.  
  97. def GetTemp_old():
  98.     t = sense.get_temperature()
  99.     t = round(t, 1)
  100.     fconv = ((t * 9)/5)+32
  101.     f = round(fconv,1)
  102.     sleep (0.5)
  103.     return f
  104.  
  105. def display():
  106.     k = 0
  107.     #background Window
  108.     bgw = curses.newwin(12,37,0,0)
  109.     bgw.border(0)
  110.     bgw.attron(curses.color_pair(4))
  111.     for x in range(0,37):
  112.         for y in range(0,12):
  113.             try:
  114.                 bgw.addch(y,x," ")
  115.             except curses.error:
  116.                 pass
  117.     bgw.refresh()
  118.     #Disply loop
  119.     while (k != ord('q')):
  120.         BlinkinLights()
  121.         #Temperature
  122.         t = get_temp()
  123.         win_t = curses.newwin(3, 32, 0, 2)
  124.         win_t.attron(curses.color_pair(1))
  125.         win_t.border(0)
  126.         rangex = (30 / float(120)) * t
  127.         pos = int(rangex)
  128.         if pos<1:
  129.             pos = 0
  130.         display = ' '
  131.         if pos != 0:
  132.             win_t.addstr(1, 1, "{}".format(display*pos),curses.color_pair(3))
  133.         win_t.addstr(0,2,"Temp(f): " + str(t))
  134.         win_t.refresh()
  135.         #Pressure
  136.         p = sense.get_pressure()
  137.         p = round(p, 1)
  138.         win_p = curses.newwin(3, 32, 3, 2)
  139.         win_p.attron(curses.color_pair(1))
  140.         win_p.border(0)
  141.         rangex = (30 / float(1260)) * p
  142.         pos = int(rangex)
  143.         display = ' '
  144.         if pos != 0:
  145.             win_p.addstr(1, 1, "{}".format(display*pos),curses.color_pair(3))
  146.         win_p.addstr(0,2,"Pressure(mb): " + str(p))
  147.         win_p.refresh()
  148.         #Humidity
  149.         h = sense.get_humidity()
  150.         h = round(h, 1)
  151.         win_h = curses.newwin(3, 32, 6, 2)
  152.         win_h.attron(curses.color_pair(1))
  153.         win_h.border(0)
  154.         rangex = (30 / float(100)) * h
  155.         pos = int(rangex)
  156.         display = ' '
  157.         if pos != 0:
  158.             win_h.addstr(1, 1, "{}".format(display*pos),curses.color_pair(3))
  159.         win_h.addstr(0,2,"Humidity(%): " + str(h))
  160.         win_h.refresh()
  161.         #Magnetics
  162.         raw = sense.get_compass_raw()
  163.         mx = round(raw["x"],5)
  164.         my = round(raw["y"],5)
  165.         mz = round(raw["z"],5)
  166.         m_av = round((mx+my+mz)/3,2)
  167.         win_m = curses.newwin(3, 32, 9, 2)
  168.         win_m.attron(curses.color_pair(1))
  169.         win_m.border(0)
  170.         rangex = (30 / float(800)) * (h+600)
  171.         pos = int(rangex)
  172.         display = ' '
  173.         if pos != 0:
  174.             win_m.addstr(1, 1, "{}".format(display*pos),curses.color_pair(3))
  175.         win_m.addstr(0,2,"Ave. Magnetic Flux(uT): " + str(m_av))
  176.         win_m.refresh()
  177.         #logging
  178.         logstring =str(t)+","+str(p)+","+str(h)+","+str(m_av)
  179.         logfile(logstring)
  180.         k = win_t.getch()
  181.         sleep(5)
  182.  
  183. def menu():
  184.     k = 0
  185.     #background Window
  186.     win = curses.newwin(12,37,0,0)
  187.     win.border(0)
  188.     win.attron(curses.color_pair(4))
  189.     while (k != ord('q')):
  190.         for x in range(0,37):
  191.                 for y in range(0,12):
  192.                         try:
  193.                                 win.addch(y,x," ")
  194.                         except curses.error:
  195.                                 pass
  196.         win.addstr(3,3,"T: Display Temperature",curses.color_pair(2))
  197.         win.addstr(4,3,"Q: Quit",curses.color_pair(2))
  198.         win.refresh()
  199.         k = win.getch()
  200.         if k == ord('t'):
  201.             temp_graph()
  202. try:
  203.     display()
  204. except curses.error:
  205.     pass
  206.  
  207. #Close down curses properly, inc turn echo back on!
  208. curses.nocbreak(); curses.echo()
  209. curses.endwin()
  210. GPIO.cleanup() # cleanup all GPIO
RAW Paste Data