rastabrew

git clone https://git.tarina.org/rastabrew
Log | Files | Refs | README

ride_on.py (4857B)


      1 #!/bin/env python3
      2 
      3 import os
      4 import time
      5 from onewire import Onewire
      6 import RPi.GPIO as GPIO
      7 from blessed import Terminal
      8 import datetime
      9 import web
     10 import config
     11 import socket
     12 import pickle
     13 
     14 basedir = os.path.dirname(os.path.realpath(__file__)) + '/'
     15 db_local = basedir+'rastabrew.db'
     16 #db = web.database(dbn='sqlite', db=basedir + 'rastabrew.db')
     17 db = web.database(dbn='sqlite', db=db_local)
     18 db2 = web.database(dbn=config.dbn, host=config.host, user=config.user, port=config.port, db=config.db, pw=config.pw)
     19 
     20 #os.system('pkill -9 brew1')
     21 #os.system('pkill -9 brew2')
     22 #time.sleep(1)
     23 #os.system(basedir + 'brew1 &')
     24 #os.system(basedir + 'brew2 &')
     25 
     26 term = Terminal()
     27 
     28 with Onewire('u') as o:
     29     devices = o.get()
     30     print(devices)
     31     humidity = o.sensor('26.CD0492010000').humidity
     32     temp1 = o.sensor('26.CD0492010000').temperature
     33     temp2 = o.sensor('28.2F1F7C060000').temperature
     34     print('Humidity: ' + humidity + ' Temp: ' + temp1 + ' Temp2: ' + temp2)
     35 
     36 def read_givarna():
     37     brew1 = 0
     38     brew2 = 0
     39     while True:
     40         try:
     41             f = open("/dev/shm/brew1", "r")
     42             brew1 = f.read()
     43         except:
     44             print('could not read file')
     45         try:
     46             f = open("/dev/shm/brew2", "r")
     47             brew2 = f.read()
     48         except:
     49             print('could not read file')
     50         try:
     51             brew1 = int(brew1)
     52             brew2 = int(brew2)
     53         except:
     54             print('fuck')
     55             pass
     56         if type(brew1) == int and type(brew2) == int:
     57             return brew1, brew2
     58 
     59 def is_webz_on():
     60     try:
     61         socket.create_connection(("google.com",80))
     62         return True
     63     except:
     64         pass
     65     print('no internet connection!')
     66     return False
     67 
     68 def send_offline_data(tablename):
     69     print('sending offline data!')
     70     #send offline data once online again
     71     od = db.select(tablename, what='id,tid,pir,humidity,temp1,temp2,brew1,brew2,brew1_name,brew2_name')
     72     for i in od:
     73         print(i)
     74         try:
     75             db2.insert(tablename, tid=i.tid, pir=bool(i.pir), humidity=i.humidity, temp1=i.temp1, temp2=i.temp2, brew1=i.brew1, brew2=i.brew2, brew1_name=i.brew1_name, brew2_name=i.brew2_name )
     76             db.query("delete from "+tablename+" where id="+str(i.id)+";")
     77         except:
     78             print('shit! not offline again!')
     79             return
     80 
     81 GPIO.setmode(GPIO.BCM)
     82 GPIO.setup(17, GPIO.OUT) #LAMP RELAY
     83 GPIO.setup(4, GPIO.IN) # PIR1
     84 
     85 haz_been_offline = True
     86 brewerishere = False
     87 
     88 brew1_old = 0
     89 brew2_old = 0
     90 
     91 brew1_name = "rastabrew-1"
     92 brew2_name = "rastabrew-2"
     93 
     94 def load_givare(viken):
     95     totbubs = db.select('last', where="id=1", what=viken)[0]
     96     return totbubs
     97 
     98 def save_givare(addbubs, viken):
     99     totbubs = load_givare(viken)
    100     if viken == 'brew1':
    101         totbubs = totbubs.brew1 + addbubs
    102         db.update('last', where="id=1", tid=datetime.datetime.now(), brew1=totbubs)
    103     if viken == 'brew2':
    104         totbubs = totbubs.brew2 + addbubs
    105         db.update('last', where="id=1", tid=datetime.datetime.now(), brew2=totbubs)
    106     return totbubs
    107 
    108 brew1_old, brew2_old = read_givarna()
    109 totbubs1 = load_givare('brew1').brew1
    110 totbubs2 = load_givare('brew2').brew2
    111 brew1, brew2 = read_givarna()
    112 while True:
    113     brew1, brew2 = read_givarna()
    114     if brew1_old < brew1:
    115         addbubs = brew1 - brew1_old
    116         totbubs1 = save_givare(addbubs,'brew1')
    117         brew1_old = brew1
    118     if brew2_old < brew2:
    119         addbubs = brew2 - brew2_old
    120         totbubs2 = save_givare(addbubs,'brew2')
    121         brew2_old = brew2
    122     with Onewire('u') as o:
    123         devices = o.get()
    124         print(devices)
    125         humidity = o.sensor('26.CD0492010000').humidity
    126         temp1 = o.sensor('26.CD0492010000').temperature
    127         temp2 = o.sensor('28.2F1F7C060000').temperature
    128     if GPIO.input(4):
    129         brewerishere = True
    130     else:
    131         brewerishere = False
    132     print(term.clear)
    133     print('Humidity: ' + humidity + ' Temp: ' + temp1 + ' Temp2: ' + temp2 + ' Brew1:' + brew1_name + ' ' + str(brew1) + ' Brew2:' + brew2_name + ' ' + str(brew2)+' bubs1:'+ str(totbubs1)+' bubs2:'+str(totbubs2))
    134     if is_webz_on() == True:
    135         if haz_been_offline == True:
    136             haz_been_offline = False
    137             send_offline_data('brewline')
    138         try:
    139             db2.insert('brewline', tid=datetime.datetime.now(), pir=brewerishere, humidity=humidity, temp1=temp1, temp2=temp2, brew1=totbubs1, brew2=totbubs2, brew1_name=brew1_name, brew2_name=brew2_name)
    140         except Exception as e: print(e)
    141     else:
    142         haz_been_offline = True
    143         db.insert('brewline', tid=datetime.datetime.now(), pir=brewerishere, humidity=humidity, temp1=temp1, temp2=temp2, brew1=totbubs1, brew2=totbubs2, brew1_name=brew1_name, brew2_name=brew2_name)
    144     time.sleep(10)
    145     if brewerishere:
    146         print('Brewer is here')
    147     GPIO.output(17, GPIO.HIGH)
    148     #code.interact(local=locals())
    149