commit f3ff5bbb64765a9670a06c07800acf88536fe54d
parent 891202570745e0b02cc4882c59f36906fdb412fe
Author: rob <rob@tarina.org>
Date: Thu, 25 Aug 2022 20:36:41 +0300
c program som räknar bubblor
Diffstat:
A | givarna.c | | | 44 | ++++++++++++++++++++++++++++++++++++++++++++ |
M | rastabrew.py | | | 26 | +++++++++++++------------- |
M | ride_on.py | | | 83 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- |
3 files changed, 131 insertions(+), 22 deletions(-)
diff --git a/givarna.c b/givarna.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <wiringPi.h>
+#include <stdbool.h>
+#include <time.h>
+#include <unistd.h>
+
+int main(void)
+{
+ // Switch: Physical pin 31, BCM GPIO6, and WiringPi pin 22.
+ const int givare = 4;
+ bool counted = false;
+ long brew = 0;
+ long brewold = 0;
+ int timesleep = 5000;
+
+ wiringPiSetup();
+
+ pinMode(givare, INPUT);
+ pullUpDnControl(givare, PUD_DOWN);
+
+ while (1) {
+ if ((digitalRead(givare) == HIGH)) {
+ counted = true;
+ usleep(1);
+ }
+ else {
+ if (counted == true) {
+ brew = brew + 1;
+ counted = false;
+ usleep(1);
+ }
+ }
+ if (brewold != brew) {
+ FILE * givarna;
+ //printf("%ld", brew);
+ givarna = fopen("/dev/shm/brew2","w+");
+ fprintf(givarna, "%ld", brew);
+ fclose(givarna);
+ brewold = brew;
+ }
+ usleep(timesleep);
+ }
+ return 0;
+}
diff --git a/rastabrew.py b/rastabrew.py
@@ -17,9 +17,9 @@ db = web.database(dbn='sqlite', db=db_local)
db2 = web.database(dbn=config.dbn, host=config.host, user=config.user, port=config.port, db=config.db, pw=config.pw)
term = Terminal()
-
+pin = 22
GPIO.setmode(GPIO.BCM)
-GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # RASTABREW
+GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # RASTABREW
bubbles = 0
bubble1time = False
@@ -50,22 +50,22 @@ get_code_time = time.time()
while True:
run_time = time.time() - start_time
update = time.time() - get_code_time
- if GPIO.input(22):
+ if GPIO.input(pin):
bubble1time = True
else:
if bubble1time == True:
bubbles = bubbles + 1
bubble1time = False
- if is_webz_on() == True:
- if haz_been_offline == True:
- send_offline_data('bubblecounter')
- haz_been_offline = False
- try:
- db2.insert('bubblecounter', tid=datetime.datetime.now())
- except Exception as e: print(e)
- else:
- haz_been_offline = True
- db.insert('bubblecounter', tid=datetime.datetime.now())
+ if is_webz_on() == True:
+ if haz_been_offline == True:
+ send_offline_data('bubblecounter')
+ haz_been_offline = False
+ try:
+ db2.insert('bubblecounter', tid=datetime.datetime.now())
+ except Exception as e: print(e)
+ else:
+ haz_been_offline = True
+ db.insert('bubblecounter', tid=datetime.datetime.now())
time.sleep(0.01)
print(term.clear)
print('Blub blub *.0 o ' + str(bubbles) + ' bubble counter')
diff --git a/ride_on.py b/ride_on.py
@@ -9,7 +9,7 @@ import datetime
import web
import config
import socket
-
+import pickle
basedir = os.path.dirname(os.path.realpath(__file__)) + '/'
db_local = basedir+'rastabrew.db'
@@ -17,6 +17,12 @@ db_local = basedir+'rastabrew.db'
db = web.database(dbn='sqlite', db=db_local)
db2 = web.database(dbn=config.dbn, host=config.host, user=config.user, port=config.port, db=config.db, pw=config.pw)
+os.system('pkill -9 brew1')
+os.system('pkill -9 brew2')
+time.sleep(1)
+os.system(basedir + 'brew1 &')
+os.system(basedir + 'brew2 &')
+
term = Terminal()
with Onewire('u') as o:
@@ -27,6 +33,29 @@ with Onewire('u') as o:
temp2 = o.sensor('28.2F1F7C060000').temperature
print('Humidity: ' + humidity + ' Temp: ' + temp1 + ' Temp2: ' + temp2)
+def save_givarna(brew1, brew2, lastsave):
+ b1, b2 = lastsave
+ totbubs = int(brew1) - b1, int(brew2) - b2
+ f = open(basedir+'totbubs', 'wb')
+ pickle.dump(totbubs,f)
+ f.close()
+ return totbubs
+
+def read_givarna():
+ brew1 = 0
+ brew2 = 0
+ try:
+ f = open("/dev/shm/brew1", "r")
+ brew1 = f.read()
+ except:
+ print('could not read file')
+ try:
+ f = open("/dev/shm/brew2", "r")
+ brew2 = f.read()
+ except:
+ print('could not read file')
+ return int(brew1), int(brew2)
+
def is_webz_on():
try:
socket.create_connection(("google.com",80))
@@ -39,12 +68,12 @@ def is_webz_on():
def send_offline_data(tablename):
print('sending offline data!')
#send offline data once online again
- od = db.select('sensors', what='id,tid,pir,humidity,temp1,temp2')
+ od = db.select(tablename, what='id,tid,pir,humidity,temp1,temp2,brew1,brew2,brew1_name,brew2_name')
for i in od:
print(i)
#try:
- db2.insert('sensors', tid=i.tid, pir=bool(i.pir), humidity=i.humidity, temp1=i.temp1, temp2=i.temp2)
- db.query("delete from sensors where id="+str(i.id)+";")
+ 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 )
+ db.query("delete from "+tablename+" where id="+str(i.id)+";")
#except:
# print('shit! not offline again!')
# return
@@ -53,10 +82,46 @@ GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT) #LAMP RELAY
GPIO.setup(4, GPIO.IN) # PIR1
-haz_been_offline = False
+haz_been_offline = True
brewerishere = False
+brew1_old = 0
+brew2_old = 0
+
+brew1_name = "rastabrew-1"
+brew2_name = "rastabrew-2"
+
+def load_givare(n):
+ try:
+ f = open(basedir+'totbubs'+str(n), 'rb')
+ totbubs = pickle.load(f)
+ f.close()
+ except:
+ totbubs = 0
+ return totbubs
+
+def save_givare(addbubs, n):
+ totbubs = load_givare(n)
+ totbubs = totbubs + addbubs
+ f = open(basedir+'totbubs'+str(n), 'wb')
+ pickle.dump(totbubs,f)
+ f.close()
+ return totbubs
+
+brew1_old, brew2_old = read_givarna()
+totbubs1 = load_givare(1)
+totbubs2 = load_givare(2)
+
while True:
+ brew1, brew2 = read_givarna()
+ if brew1_old < brew1:
+ addbubs = brew1 - brew1_old
+ totbubs1 = save_givare(addbubs, 1)
+ brew1_old = brew1
+ if brew2_old < brew2:
+ addbubs = brew2 - brew2_old
+ totbubs2 = save_givare(addbubs, 2)
+ brew2_old = brew2
with Onewire('u') as o:
devices = o.get()
print(devices)
@@ -68,17 +133,17 @@ while True:
else:
brewerishere = False
print(term.clear)
- print('Humidity: ' + humidity + ' Temp: ' + temp1 + ' Temp2: ' + temp2)
+ print('Humidity: ' + humidity + ' Temp: ' + temp1 + ' Temp2: ' + temp2 + ' Brew1:' + brew1_name + ' ' + str(brew1) + ' Brew2:' + brew2_name + ' ' + str(brew2)+' bubs1:'+ str(totbubs1)+' bubs2:'+str(totbubs2))
if is_webz_on() == True:
if haz_been_offline == True:
haz_been_offline = False
- send_offline_data('bubblecounter')
+ send_offline_data('brewline')
try:
- db2.insert('sensors', tid=datetime.datetime.now(), pir=brewerishere, humidity=humidity, temp1=temp1, temp2=temp2)
+ 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)
except Exception as e: print(e)
else:
haz_been_offline = True
- db.insert('sensors', tid=datetime.datetime.now(), pir=brewerishere, humidity=humidity, temp1=temp1, temp2=temp2)
+ 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)
time.sleep(10)
if brewerishere:
print('Brewer is here')