D10d3 icon

picorder.py

D10d3 | PRO | 04/20/20 03:51:43 AM UTC | 0 ⭐ | 1150 👁️ | Never ⏰ | []
Python |

5.3 KB

|

None

|

0 👍

/

0 👎

from os import system
import curses
import time
import datetime
import os
import random
from sense_hat import SenseHat
from time import sleep
import numpy as np
import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(11,GPIO.OUT) #led Red
GPIO.setup(9,GPIO.OUT) #led Green
GPIO.setup(10,GPIO.OUT) #led Yellow
 
GPIO.output(11, GPIO.HIGH)
GPIO.output(9, GPIO.HIGH)
GPIO.output(10, GPIO.HIGH)
 
sense = SenseHat()
os.environ["NCURSES_NO_UTF8_ACS"] = "1"
curses.initscr()
curses.halfdelay(3)
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_YELLOW)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_WHITE)
 
start_time = time.time()
start_hour = time.time()
 
# get CPU temperature
def get_cpu_temp():
    res = os.popen("vcgencmd measure_temp").readline()
    t = float(res.replace("temp=","").replace("'C\n",""))
    return(t)
 
# use moving average to smooth readings
def get_smooth(x):
    if not hasattr(get_smooth, "t"):
        get_smooth.t = [x,x,x]
    get_smooth.t[2] = get_smooth.t[1]
    get_smooth.t[1] = get_smooth.t[0]
    get_smooth.t[0] = x
    xs = (get_smooth.t[0]+get_smooth.t[1]+get_smooth.t[2])/3
    return(xs)
 
# calculates the real temperature compesating CPU heating
def get_temp():
    t1 = sense.get_temperature_from_humidity()
    t2 = sense.get_temperature_from_pressure()
    t_cpu = get_cpu_temp()
    h = sense.get_humidity()
    p = sense.get_pressure()
    t = (t1+t2)/2
    t_corr = t - ((t_cpu-t)/1.25)
    t_corr = get_smooth(t_corr)
    f_corr = round(((t_corr * 9)/5)+32,1)
    return(f_corr)
 
def sendlog():
        os.system("mpack -s picorder -a /home/pi/picordlog.txt [email protected]")
 
def logfile(logstring):
    global start_time
    global start_hour
    current_time=time.time()
    if current_time>start_time+180:
        x = datetime.datetime.now()
        tstamp = datetime.datetime(x.year,x.month,x.day,x.hour,x.minute)
        #local_time=time.ctime(current_time)
        f= open("/home/pi/picordlog.txt","a+")
        logstring=str(tstamp)+","+logstring
        f.write(logstring+"\r\n")
        f.close()
        start_time = time.time()
    if current_time>start_hour+3600:
        start_hour = time.time()
        sendlog()
 
def BlinkinLights():
    grid = []
    for i in range(0,64):
        n = random.randint(0, 4)
        if n == 0:
            r = random.randint(0,100)
            b = random.randint(0,100)
            g = random.randint(0,100)
            grid.append ([r,g,b])
        else:
            grid.append ([0,0,0])
    sense.set_pixels(grid)
 
 
def GetTemp_old():
    t = sense.get_temperature()
    t = round(t, 1)
    fconv = ((t * 9)/5)+32
    f = round(fconv,1)
    sleep (0.5)
    return f
 
def display():
    k = 0
    #background Window
    bgw = curses.newwin(12,37,0,0)
    bgw.border(0)
    bgw.attron(curses.color_pair(4))
    for x in range(0,37):
        for y in range(0,12):
            try:
                bgw.addch(y,x," ")
            except curses.error:
                pass
    bgw.refresh()
    #Disply loop
    while (k != ord('q')):
        BlinkinLights()
        #Temperature
        t = get_temp()
        win_t = curses.newwin(3, 32, 0, 2)
        win_t.attron(curses.color_pair(1))
        win_t.border(0)
        rangex = (30 / float(120)) * t
        pos = int(rangex)
        if pos<1:
            pos = 0
        display = ' '
        if pos != 0:
            win_t.addstr(1, 1, "{}".format(display*pos),curses.color_pair(3))
        win_t.addstr(0,2,"Temp(f): " + str(t))
        win_t.refresh()
        #Pressure
        p = sense.get_pressure()
        p = round(p, 1)
        win_p = curses.newwin(3, 32, 3, 2)
        win_p.attron(curses.color_pair(1))
        win_p.border(0)
        rangex = (30 / float(1260)) * p
        pos = int(rangex)
        display = ' '
        if pos != 0:
            win_p.addstr(1, 1, "{}".format(display*pos),curses.color_pair(3))
        win_p.addstr(0,2,"Pressure(mb): " + str(p))
        win_p.refresh()
        #Humidity
        h = sense.get_humidity()
        h = round(h, 1)
        win_h = curses.newwin(3, 32, 6, 2)
        win_h.attron(curses.color_pair(1))
        win_h.border(0)
        rangex = (30 / float(100)) * h
        pos = int(rangex)
        display = ' '
        if pos != 0:
            win_h.addstr(1, 1, "{}".format(display*pos),curses.color_pair(3))
        win_h.addstr(0,2,"Humidity(%): " + str(h))
        win_h.refresh()
        #Magnetics
        raw = sense.get_compass_raw()
        mx = round(raw["x"],5)
        my = round(raw["y"],5)
        mz = round(raw["z"],5)
        m_av = round((mx+my+mz)/3,2)
        win_m = curses.newwin(3, 32, 9, 2)
        win_m.attron(curses.color_pair(1))
        win_m.border(0)
        rangex = (30 / float(800)) * (h+600)
        pos = int(rangex)
        display = ' '
        if pos != 0:
            win_m.addstr(1, 1, "{}".format(display*pos),curses.color_pair(3))
        win_m.addstr(0,2,"Ave. Magnetic Flux(uT): " + str(m_av))
        win_m.refresh()
        #logging
        logstring =str(t)+","+str(p)+","+str(h)+","+str(m_av)
        logfile(logstring) 
        k = win_t.getch()
        sleep(5)
 
def menu():
    k = 0
    #background Window
    win = curses.newwin(12,37,0,0)
    win.border(0)
    win.attron(curses.color_pair(4))
    while (k != ord('q')):
        for x in range(0,37):
                for y in range(0,12):
                        try:
                                win.addch(y,x," ")
                        except curses.error:
                                pass
        win.addstr(3,3,"T: Display Temperature",curses.color_pair(2))
        win.addstr(4,3,"Q: Quit",curses.color_pair(2))
        win.refresh()
        k = win.getch()
        if k == ord('t'):
            temp_graph()
try:
    display()
except curses.error:
    pass
 
#Close down curses properly, inc turn echo back on!
curses.nocbreak(); curses.echo()
curses.endwin()
GPIO.cleanup() # cleanup all GPIO

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎