mirror of
https://github.com/warkanum/warkanums-pi-device-snippets.git
synced 2025-05-19 02:57:28 +00:00
Further fixes. Will rewrite sooN.
This commit is contained in:
parent
c98df8c1da
commit
e58bec2dfa
184
build1/boot.py
184
build1/boot.py
@ -8,6 +8,7 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import dht11_sensor
|
import dht11_sensor
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
import copy
|
||||||
|
|
||||||
###-----------------Hardware Settings-----------------------
|
###-----------------Hardware Settings-----------------------
|
||||||
|
|
||||||
@ -72,43 +73,44 @@ def save_data(p_SensorValues):
|
|||||||
sql_con = psycopg2.connect(host=SQL_SRV, user=SQL_USER,password=SQL_PASSWD,database=SQL_DB)
|
sql_con = psycopg2.connect(host=SQL_SRV, user=SQL_USER,password=SQL_PASSWD,database=SQL_DB)
|
||||||
sql_cur = sql_con.cursor()
|
sql_cur = sql_con.cursor()
|
||||||
|
|
||||||
|
print('2temp->' + str(p_SensorValues['temperature']['save']))
|
||||||
|
print('2light->' + str(p_SensorValues['light']['save']))
|
||||||
|
print('2motion->' + str(p_SensorValues['motion']['save']))
|
||||||
|
|
||||||
if p_SensorValues.get('motion', None):
|
if p_SensorValues.get('motion', None):
|
||||||
sql_cur.execute("""select id, data_read from sensordata
|
sql_cur.execute("""select id, data_read from sensordata
|
||||||
where sensor_type = 'motion' and time_read > extract(epoch from now())::bigint - 240
|
where sensor_type = 'motion' order by id desc limit 1""")
|
||||||
order by time_read desc limit 1""")
|
|
||||||
data = sql_cur.fetchone()
|
data = sql_cur.fetchone()
|
||||||
if not data or (data and str(data[1]) != str(p_SensorValues['motion']['data'])):
|
if not data or p_SensorValues['motion']['save']: #(data and str(data[1]) != str(p_SensorValues['motion']['data'])):
|
||||||
sql_cur.execute("INSERT INTO sensordata (sensor_type, data_read, time_read) VALUES (%s, %s, %s)",
|
sql_cur.execute("""INSERT INTO sensordata (sensor_type, data_read, date_added,time_added)
|
||||||
('motion', p_SensorValues['motion']['data'], p_SensorValues['motion']['read'] ))
|
VALUES (%s, %s, TIMESTAMP 'epoch' + %s * INTERVAL '1 second',TIMESTAMP 'epoch' + %s * INTERVAL '1 second')""",
|
||||||
|
('motion', p_SensorValues['motion']['data'], p_SensorValues['motion']['read'],p_SensorValues['motion']['read'] ))
|
||||||
|
|
||||||
if p_SensorValues.get('motion', None):
|
|
||||||
sql_cur.execute("select id, data_read from sensordata where sensor_type = 'humanpresence' order by time_read desc limit 1")
|
|
||||||
data = sql_cur.fetchone()
|
|
||||||
if not data or (data and str(data[1]) != str(p_SensorValues['motion']['humanpresence'])):
|
|
||||||
sql_cur.execute("INSERT INTO sensordata (sensor_type, data_read, time_read) VALUES (%s, %s, %s)",
|
|
||||||
('humanpresence', p_SensorValues['motion']['humanpresence'], p_SensorValues['motion']['read'] ))
|
|
||||||
|
|
||||||
if p_SensorValues.get('light', None):
|
if p_SensorValues.get('light', None):
|
||||||
sql_cur.execute("select id, data_read from sensordata where sensor_type = 'light' order by time_read desc limit 1")
|
sql_cur.execute("select id, data_read from sensordata where sensor_type = 'light' order by id desc limit 1")
|
||||||
data = sql_cur.fetchone()
|
data = sql_cur.fetchone()
|
||||||
#we have a +- 10 variance on light.
|
#we have a +- 10 variance on light.
|
||||||
if not data or (data and (int(p_SensorValues['light']['data']) > int(data[1])+10 or int(p_SensorValues['light']['data']) < int(data[1]) - 10) ):
|
if not data or p_SensorValues['light']['save']: #(data and (int(p_SensorValues['light']['data']) > int(data[1])+10 or int(p_SensorValues['light']['data']) < int(data[1]) - 10) ):
|
||||||
sql_cur.execute("INSERT INTO sensordata (sensor_type, data_read, time_read) VALUES (%s, %s, %s)",
|
sql_cur.execute("""INSERT INTO sensordata (sensor_type, data_read, date_added,time_added)
|
||||||
('light', p_SensorValues['light']['data'], p_SensorValues['light']['read'] ))
|
VALUES(%s, %s, TIMESTAMP 'epoch' + %s * INTERVAL '1 second',TIMESTAMP 'epoch' + %s * INTERVAL '1 second')""",
|
||||||
|
('light', p_SensorValues['light']['data'], p_SensorValues['light']['read'],p_SensorValues['light']['read'] ))
|
||||||
|
|
||||||
if p_SensorValues.get('temperature', None):
|
if p_SensorValues.get('temperature', None):
|
||||||
sql_cur.execute("select id, data_read from sensordata where sensor_type = 'temperature' order by time_read desc limit 1")
|
sql_cur.execute("select id, data_read from sensordata where sensor_type = 'temperature' order by id desc limit 1")
|
||||||
data = sql_cur.fetchone()
|
data = sql_cur.fetchone()
|
||||||
if not data or (data and str(data[1]) != str(p_SensorValues['temperature']['temperature'])):
|
if not data or p_SensorValues['temperature']['save']: #(data and str(data[1]) != str(p_SensorValues['temperature']['temperature'])):
|
||||||
sql_cur.execute("INSERT INTO sensordata (sensor_type, data_read, time_read) VALUES (%s, %s, %s)",
|
sql_cur.execute("""INSERT INTO sensordata (sensor_type, data_read, date_added,time_added)
|
||||||
('temperature', p_SensorValues['temperature']['temperature'], p_SensorValues['temperature']['read'] ))
|
VALUES(%s, %s, TIMESTAMP 'epoch' + %s * INTERVAL '1 second',TIMESTAMP 'epoch' + %s * INTERVAL '1 second')""",
|
||||||
|
('temperature', p_SensorValues['temperature']['temperature'], p_SensorValues['temperature']['read'], p_SensorValues['temperature']['read'] ))
|
||||||
|
|
||||||
if p_SensorValues.get('temperature', None):
|
if p_SensorValues.get('temperature', None):
|
||||||
sql_cur.execute("select id, data_read from sensordata where sensor_type = 'humidity' order by time_read desc limit 1")
|
sql_cur.execute("select id, data_read from sensordata where sensor_type = 'humidity' order by id desc limit 1")
|
||||||
data = sql_cur.fetchone()
|
data = sql_cur.fetchone()
|
||||||
if not data or (data and str(data[1]) != str(p_SensorValues['temperature']['humidity'])):
|
if not data or p_SensorValues['temperature']['save']:#(data and str(data[1]) != str(p_SensorValues['temperature']['humidity'])):
|
||||||
sql_cur.execute("INSERT INTO sensordata (sensor_type, data_read, time_read) VALUES (%s, %s, %s)",
|
sql_cur.execute("""INSERT INTO sensordata (sensor_type, data_read, date_added,time_added)
|
||||||
('humidity', p_SensorValues['temperature']['humidity'], p_SensorValues['temperature']['read'] ))
|
VALUES(%s, %s, TIMESTAMP 'epoch' + %s * INTERVAL '1 second',TIMESTAMP 'epoch' + %s * INTERVAL '1 second')""",
|
||||||
|
('humidity', p_SensorValues['temperature']['humidity'], p_SensorValues['temperature']['read'], p_SensorValues['temperature']['read'] ))
|
||||||
|
|
||||||
sql_con.commit()
|
sql_con.commit()
|
||||||
sql_cur.close()
|
sql_cur.close()
|
||||||
@ -122,76 +124,98 @@ def save_data(p_SensorValues):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
SensorValue = {}
|
SensorValue = {}
|
||||||
CNT_LT = 99 #light check delay counter
|
TICK_LT = 0 #light detect ticker
|
||||||
CNT_TMP = 999 #temp check delay counter
|
TICK_LTI = 0 #light insert ticker
|
||||||
|
TICK_TMP = 0 #temp ticker
|
||||||
|
|
||||||
while True:
|
BlueLed = False
|
||||||
CNT_LT += 1
|
|
||||||
CNT_TMP += 1
|
|
||||||
changed = False
|
|
||||||
|
|
||||||
motionData = GPIO.input(PIN_MC)
|
while True:
|
||||||
if not SensorValue.get('motion', None):
|
changed = False
|
||||||
SensorValue['motion'] = {'data': motionData , 'read': UnixLocalEpoch(), 'changed': UnixLocalEpoch()}
|
|
||||||
else:
|
|
||||||
if SensorValue['motion'].get('data', 0) != motionData :
|
|
||||||
changed = True
|
|
||||||
SensorValue['motion']['changed'] = UnixLocalEpoch()
|
|
||||||
|
|
||||||
SensorValue['motion']['data'] = motionData
|
|
||||||
SensorValue['motion']['read'] = UnixLocalEpoch()
|
|
||||||
|
|
||||||
|
|
||||||
if (SensorValue['motion']['data'] > 0):
|
motionData = GPIO.input(PIN_MC)
|
||||||
GPIO.output(PIN_LED1,GPIO.HIGH) #flash led
|
if not SensorValue.get('motion', None):
|
||||||
SensorValue['motion']['lastmotion'] = UnixLocalEpoch()
|
SensorValue['motion'] = {'data': motionData , 'read': UnixLocalEpoch(), 'changed': UnixLocalEpoch(), 'save': False}
|
||||||
else:
|
|
||||||
GPIO.output(PIN_LED1,GPIO.LOW) #flash led stop
|
|
||||||
|
|
||||||
#see if there are a moving presence in the room
|
SensorValue['motion']['save'] = False
|
||||||
if SensorValue.get('motion', None):
|
|
||||||
if (SensorValue['motion'].get('lastmotion', 0) > UnixLocalEpoch() - 60):
|
|
||||||
SensorValue['motion']['humanpresence'] = 1
|
|
||||||
else:
|
|
||||||
SensorValue['motion']['humanpresence'] = 0
|
|
||||||
|
|
||||||
#Measure Light
|
if int(SensorValue['motion'].get('data', 0)) != int(motionData) :
|
||||||
if not SensorValue.get('light', None):
|
changed = True
|
||||||
SensorValue['light'] = {'data': PhotoSensor(PIN_LC) , 'read': UnixLocalEpoch() }
|
SensorValue['motion']['changed'] = UnixLocalEpoch()
|
||||||
|
SensorValue['motion']['save'] = True
|
||||||
|
SensorValue['motion']['data'] = int(motionData)
|
||||||
|
SensorValue['motion']['read'] = UnixLocalEpoch()
|
||||||
|
|
||||||
if (CNT_LT > 100):
|
|
||||||
CNT_LT = 0
|
|
||||||
lightData = PhotoSensor(PIN_LC)
|
|
||||||
if SensorValue['light'].get('data', 0) != lightData :
|
|
||||||
changed = True
|
|
||||||
SensorValue['light']['changed'] = UnixLocalEpoch()
|
|
||||||
SensorValue['light']['data'] = lightData
|
|
||||||
SensorValue['light']['read'] = UnixLocalEpoch()
|
|
||||||
|
|
||||||
#Measure Temprature, this might hold the thread for a few seconds at most.
|
if (SensorValue['motion']['data'] > 0):
|
||||||
if (CNT_TMP > 1000):
|
#GPIO.output(PIN_LED1,GPIO.HIGH) #flash led
|
||||||
CNT_TMP = 0
|
SensorValue['motion']['lastmotion'] = UnixLocalEpoch()
|
||||||
print('temperature reading...')
|
BlueLed = True
|
||||||
if not SensorValue.get('temperature', None):
|
else:
|
||||||
SensorValue['temperature'] = {}
|
#GPIO.output(PIN_LED1,GPIO.LOW) #flash led stop
|
||||||
|
BlueLed = False
|
||||||
|
|
||||||
tempData = TempsensorRead()
|
|
||||||
if tempData:
|
#Measure Light
|
||||||
if (SensorValue['temperature'].get('temperature', 0) != tempData['temperature']
|
if not SensorValue.get('light', None):
|
||||||
|
SensorValue['light'] = {'data': PhotoSensor(PIN_LC) , 'read': UnixLocalEpoch(), 'save': False }
|
||||||
|
|
||||||
|
SensorValue['light']['save'] = False
|
||||||
|
|
||||||
|
lightChanges = 0
|
||||||
|
if (TICK_LT < time.perf_counter()):
|
||||||
|
TICK_LT = time.perf_counter()+1
|
||||||
|
lightData = PhotoSensor(PIN_LC)
|
||||||
|
lightChanges = abs(SensorValue['light'].get('data', 0) - lightData)
|
||||||
|
#print("LC->" + str(lightData ) + "DF:" + str(lightChanges))
|
||||||
|
|
||||||
|
if (TICK_LTI < time.perf_counter() or (lightData > 600 and lightChanges > 200) or (lightData < 600 and lightChanges > 30)):
|
||||||
|
TICK_LTI = time.perf_counter()+30
|
||||||
|
|
||||||
|
if SensorValue['light'].get('data', 0) != lightData :
|
||||||
|
changed = True
|
||||||
|
SensorValue['light']['changed'] = UnixLocalEpoch()
|
||||||
|
SensorValue['light']['save'] = True
|
||||||
|
|
||||||
|
SensorValue['light']['data'] = lightData
|
||||||
|
SensorValue['light']['read'] = UnixLocalEpoch()
|
||||||
|
|
||||||
|
#Measure Temprature, this might hold the thread for a few seconds at most.
|
||||||
|
if not SensorValue.get('temperature', None):
|
||||||
|
SensorValue['temperature'] = {'temperature': 0, 'humidity': 0, 'changed': 0, 'save': False}
|
||||||
|
|
||||||
|
SensorValue['temperature']['save'] = False
|
||||||
|
|
||||||
|
if (TICK_TMP < time.perf_counter()):
|
||||||
|
TICK_TMP = time.perf_counter()+10
|
||||||
|
tempData = TempsensorRead()
|
||||||
|
if tempData:
|
||||||
|
print('temperature reading...')
|
||||||
|
if (SensorValue['temperature'].get('temperature', 0) != tempData['temperature']
|
||||||
or SensorValue['temperature'].get('humidity', 0) != tempData['humidity']):
|
or SensorValue['temperature'].get('humidity', 0) != tempData['humidity']):
|
||||||
SensorValue['temperature']['changed'] = UnixLocalEpoch()
|
SensorValue['temperature']['changed'] = UnixLocalEpoch()
|
||||||
SensorValue['temperature']['temperature'] = tempData['temperature']
|
SensorValue['temperature']['temperature'] = tempData['temperature']
|
||||||
SensorValue['temperature']['humidity'] = tempData['humidity']
|
SensorValue['temperature']['humidity'] = tempData['humidity']
|
||||||
changed = True
|
SensorValue['temperature']['save'] = True
|
||||||
|
changed = True
|
||||||
|
SensorValue['temperature']['read'] = UnixLocalEpoch()
|
||||||
|
|
||||||
SensorValue['temperature']['read'] = UnixLocalEpoch()
|
if changed:
|
||||||
|
print('---------------change-------------')
|
||||||
|
print('temp->' + str(SensorValue['temperature']['save']))
|
||||||
|
print('light->' + str(SensorValue['light']['save']))
|
||||||
|
print('motion->' + str(SensorValue['motion']['save']))
|
||||||
|
#Gosh we need a copy cause sql can be to slow sometimes
|
||||||
|
|
||||||
if changed:
|
ThreadsData = copy.deepcopy(SensorValue)
|
||||||
t = Thread(target=save_data, args=(SensorValue,))
|
t = Thread(target=save_data, args=(ThreadsData,))
|
||||||
t.start()
|
t.start()
|
||||||
|
|
||||||
time.sleep(0.01)
|
|
||||||
|
|
||||||
|
time.sleep(0.01)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
sys.exit(main())
|
@ -1,6 +1,7 @@
|
|||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
import psycopg2 as pg
|
import psycopg2 as pg
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
|
import datetime
|
||||||
|
|
||||||
SQL_SRV='192.168.89.6'
|
SQL_SRV='192.168.89.6'
|
||||||
SQL_USR='pistats'
|
SQL_USR='pistats'
|
||||||
@ -32,7 +33,7 @@ class Application(tk.Frame):
|
|||||||
self.lbl_motion = tk.Label(self, textvariable=self.data_time, width=20).grid(row=2, column=3, sticky=(tk.E, tk.S))
|
self.lbl_motion = tk.Label(self, textvariable=self.data_time, width=20).grid(row=2, column=3, sticky=(tk.E, tk.S))
|
||||||
self.btn_update = tk.Button(self, text="Update Now", command=self.do_update).grid(column=0, row=2, sticky=(tk.W, tk.S))
|
self.btn_update = tk.Button(self, text="Update Now", command=self.do_update).grid(column=0, row=2, sticky=(tk.W, tk.S))
|
||||||
|
|
||||||
def updateLabels(self):
|
def updateLabels(self, Timer=True):
|
||||||
self.data_time.set("" + str(dt.datetime.now()))
|
self.data_time.set("" + str(dt.datetime.now()))
|
||||||
|
|
||||||
if self.sqlcur is not None:
|
if self.sqlcur is not None:
|
||||||
@ -52,14 +53,16 @@ class Application(tk.Frame):
|
|||||||
if not data:
|
if not data:
|
||||||
break
|
break
|
||||||
|
|
||||||
self.after(2000, self.updateLabels)
|
print("updated: " + str(datetime.datetime.now()))
|
||||||
|
|
||||||
|
if Timer:
|
||||||
|
self.after(1000, self.updateLabels)
|
||||||
|
|
||||||
#self.QUIT = tk.Button(self, text="QUIT", fg="red",command=master.destroy)
|
#self.QUIT = tk.Button(self, text="QUIT", fg="red",command=master.destroy)
|
||||||
#self.QUIT.pack(side="bottom")
|
#self.QUIT.pack(side="bottom")
|
||||||
|
|
||||||
def do_update(self):
|
def do_update(self):
|
||||||
print("hi there, everyone!")
|
self.updateLabels(Timer=False)
|
||||||
self.updateLabels()
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -5,6 +5,8 @@ CREATE TABLE sensordata
|
|||||||
data_read text,
|
data_read text,
|
||||||
time_read bigint,
|
time_read bigint,
|
||||||
time_changed bigint,
|
time_changed bigint,
|
||||||
|
date_added date,
|
||||||
|
time_added time
|
||||||
CONSTRAINT pk_id PRIMARY KEY (id),
|
CONSTRAINT pk_id PRIMARY KEY (id),
|
||||||
CONSTRAINT k_typeread UNIQUE (sensor_type, time_read)
|
CONSTRAINT k_typeread UNIQUE (sensor_type, time_read)
|
||||||
)
|
)
|
@ -2,9 +2,8 @@
|
|||||||
|
|
||||||
-- DROP VIEW v_sensordata_current;
|
-- DROP VIEW v_sensordata_current;
|
||||||
|
|
||||||
CREATE OR REPLACE VIEW v_sensordata_current AS
|
CREATE OR REPLACE VIEW v_sensordata_current_2 AS
|
||||||
SELECT r1.rn,
|
SELECT r1.id,
|
||||||
r1.id,
|
|
||||||
r1.sensor_type,
|
r1.sensor_type,
|
||||||
r1.data_read,
|
r1.data_read,
|
||||||
to_timestamp(r1.time_read) AT TIME ZONE 'UTC' as time_read
|
to_timestamp(r1.time_read) AT TIME ZONE 'UTC' as time_read
|
||||||
@ -16,3 +15,25 @@ CREATE OR REPLACE VIEW v_sensordata_current AS
|
|||||||
sensordata.time_changed
|
sensordata.time_changed
|
||||||
FROM sensordata) r1
|
FROM sensordata) r1
|
||||||
WHERE r1.rn = 1;
|
WHERE r1.rn = 1;
|
||||||
|
|
||||||
|
CREATE OR REPLACE VIEW v_sensordata_current AS
|
||||||
|
SELECT r1.id,
|
||||||
|
r1.sensor_type,
|
||||||
|
r1.data_read,
|
||||||
|
r1.time_read
|
||||||
|
FROM ( SELECT row_number() OVER (PARTITION BY r0.sensor_type ORDER BY r0.date_added DESC, r0.time_added DESC) AS rn,
|
||||||
|
r0.id,
|
||||||
|
r0.sensor_type,
|
||||||
|
r0.data_read,
|
||||||
|
(r0.date_added || ' ' || r0.time_added)::timestamp time_read
|
||||||
|
FROM (
|
||||||
|
select sc.*
|
||||||
|
from sensordata sc
|
||||||
|
where sc.date_added = now()::date
|
||||||
|
order by sc.id desc
|
||||||
|
limit 1000
|
||||||
|
) r0
|
||||||
|
) r1
|
||||||
|
WHERE r1.rn = 1;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user