Further fixes. Will rewrite sooN.

This commit is contained in:
Warkanum 2015-01-02 22:01:05 +02:00
parent c98df8c1da
commit e58bec2dfa
4 changed files with 144 additions and 94 deletions

View File

@ -8,6 +8,7 @@ import os
import sys
import dht11_sensor
import psycopg2
import copy
###-----------------Hardware Settings-----------------------
@ -72,44 +73,45 @@ def save_data(p_SensorValues):
sql_con = psycopg2.connect(host=SQL_SRV, user=SQL_USER,password=SQL_PASSWD,database=SQL_DB)
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):
sql_cur.execute("""select id, data_read from sensordata
where sensor_type = 'motion' and time_read > extract(epoch from now())::bigint - 240
order by time_read desc limit 1""")
where sensor_type = 'motion' order by id desc limit 1""")
data = sql_cur.fetchone()
if not data or (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)",
('motion', p_SensorValues['motion']['data'], 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 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, date_added,time_added)
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('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()
#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) ):
sql_cur.execute("INSERT INTO sensordata (sensor_type, data_read, time_read) VALUES (%s, %s, %s)",
('light', p_SensorValues['light']['data'], p_SensorValues['light']['read'] ))
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, date_added,time_added)
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):
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()
if not data or (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)",
('temperature', p_SensorValues['temperature']['temperature'], p_SensorValues['temperature']['read'] ))
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, date_added,time_added)
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):
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()
if not data or (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)",
('humidity', p_SensorValues['temperature']['humidity'], p_SensorValues['temperature']['read'] ))
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, date_added,time_added)
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_cur.close()
sql_con.close()
@ -122,76 +124,98 @@ def save_data(p_SensorValues):
def main():
SensorValue = {}
CNT_LT = 99 #light check delay counter
CNT_TMP = 999 #temp check delay counter
SensorValue = {}
TICK_LT = 0 #light detect ticker
TICK_LTI = 0 #light insert ticker
TICK_TMP = 0 #temp ticker
BlueLed = False
while True:
CNT_LT += 1
CNT_TMP += 1
changed = False
while True:
changed = False
motionData = GPIO.input(PIN_MC)
if not SensorValue.get('motion', None):
SensorValue['motion'] = {'data': motionData , 'read': UnixLocalEpoch(), 'changed': UnixLocalEpoch()}
else:
if SensorValue['motion'].get('data', 0) != motionData :
changed = True
SensorValue['motion']['changed'] = UnixLocalEpoch()
motionData = GPIO.input(PIN_MC)
if not SensorValue.get('motion', None):
SensorValue['motion'] = {'data': motionData , 'read': UnixLocalEpoch(), 'changed': UnixLocalEpoch(), 'save': False}
SensorValue['motion']['save'] = False
SensorValue['motion']['data'] = motionData
SensorValue['motion']['read'] = UnixLocalEpoch()
if int(SensorValue['motion'].get('data', 0)) != int(motionData) :
changed = True
SensorValue['motion']['changed'] = UnixLocalEpoch()
SensorValue['motion']['save'] = True
SensorValue['motion']['data'] = int(motionData)
SensorValue['motion']['read'] = UnixLocalEpoch()
if (SensorValue['motion']['data'] > 0):
GPIO.output(PIN_LED1,GPIO.HIGH) #flash led
SensorValue['motion']['lastmotion'] = UnixLocalEpoch()
else:
GPIO.output(PIN_LED1,GPIO.LOW) #flash led stop
#see if there are a moving presence in the room
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 not SensorValue.get('light', None):
SensorValue['light'] = {'data': PhotoSensor(PIN_LC) , 'read': UnixLocalEpoch() }
if (SensorValue['motion']['data'] > 0):
#GPIO.output(PIN_LED1,GPIO.HIGH) #flash led
SensorValue['motion']['lastmotion'] = UnixLocalEpoch()
BlueLed = True
else:
#GPIO.output(PIN_LED1,GPIO.LOW) #flash led stop
BlueLed = False
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 (CNT_TMP > 1000):
CNT_TMP = 0
print('temperature reading...')
if not SensorValue.get('temperature', None):
SensorValue['temperature'] = {}
tempData = TempsensorRead()
if tempData:
if (SensorValue['temperature'].get('temperature', 0) != tempData['temperature']
or SensorValue['temperature'].get('humidity', 0) != tempData['humidity']):
SensorValue['temperature']['changed'] = UnixLocalEpoch()
SensorValue['temperature']['temperature'] = tempData['temperature']
SensorValue['temperature']['humidity'] = tempData['humidity']
changed = True
SensorValue['temperature']['read'] = UnixLocalEpoch()
if changed:
t = Thread(target=save_data, args=(SensorValue,))
t.start()
#Measure Light
if not SensorValue.get('light', None):
SensorValue['light'] = {'data': PhotoSensor(PIN_LC) , 'read': UnixLocalEpoch(), 'save': False }
time.sleep(0.01)
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']):
SensorValue['temperature']['changed'] = UnixLocalEpoch()
SensorValue['temperature']['temperature'] = tempData['temperature']
SensorValue['temperature']['humidity'] = tempData['humidity']
SensorValue['temperature']['save'] = True
changed = True
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
ThreadsData = copy.deepcopy(SensorValue)
t = Thread(target=save_data, args=(ThreadsData,))
t.start()
time.sleep(0.01)
if __name__ == '__main__':
sys.exit(main())

View File

@ -1,6 +1,7 @@
import tkinter as tk
import psycopg2 as pg
import datetime as dt
import datetime
SQL_SRV='192.168.89.6'
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.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()))
if self.sqlcur is not None:
@ -52,14 +53,16 @@ class Application(tk.Frame):
if not data:
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.pack(side="bottom")
def do_update(self):
print("hi there, everyone!")
self.updateLabels()
self.updateLabels(Timer=False)
def main():

View File

@ -5,6 +5,8 @@ CREATE TABLE sensordata
data_read text,
time_read bigint,
time_changed bigint,
date_added date,
time_added time
CONSTRAINT pk_id PRIMARY KEY (id),
CONSTRAINT k_typeread UNIQUE (sensor_type, time_read)
)

View File

@ -2,9 +2,8 @@
-- DROP VIEW v_sensordata_current;
CREATE OR REPLACE VIEW v_sensordata_current AS
SELECT r1.rn,
r1.id,
CREATE OR REPLACE VIEW v_sensordata_current_2 AS
SELECT r1.id,
r1.sensor_type,
r1.data_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
FROM sensordata) r1
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;