mirror of
https://github.com/warkanum/warkanums-pi-device-snippets.git
synced 2025-05-19 02:57:28 +00:00
Fixed the issues with threads. Should not work more stable and not crash.
This commit is contained in:
parent
128a82b7b4
commit
b8e12b777e
116
build1/boot.py
116
build1/boot.py
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/python3
|
#!/opt/python3.3/bin/python3.3
|
||||||
from threading import Thread
|
##!/usr/bin/python3
|
||||||
|
import threading
|
||||||
|
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
import time
|
import time
|
||||||
@ -9,8 +10,11 @@ import sys
|
|||||||
import dht11_sensor
|
import dht11_sensor
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import copy
|
import copy
|
||||||
|
import logging
|
||||||
|
import queue
|
||||||
|
|
||||||
###-----------------Hardware Settings-----------------------
|
###-----------------Hardware Settings-----------------------
|
||||||
|
logging.basicConfig(filename='boot.log',level=logging.WARNING)
|
||||||
|
|
||||||
PIN_LC=25 #Light Sensor (GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
PIN_LC=25 #Light Sensor (GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
PIN_MC=17 #Motion Sensor (GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
PIN_MC=17 #Motion Sensor (GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
@ -39,12 +43,69 @@ GPIO.setup(PIN_MC, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|||||||
GPIO.output(PIN_LED1,GPIO.LOW)
|
GPIO.output(PIN_LED1,GPIO.LOW)
|
||||||
GPIO.output(PIN_LED2,GPIO.LOW)
|
GPIO.output(PIN_LED2,GPIO.LOW)
|
||||||
|
|
||||||
|
WORK_QUE = queue.Queue(4096)
|
||||||
|
|
||||||
|
|
||||||
|
class SaveThread(threading.Thread):
|
||||||
|
def __init__(self, name='Saving Thread'):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.name = name
|
||||||
|
self.quit = False
|
||||||
|
logging.info(self.name + ' starting thread')
|
||||||
|
self.pgconn = psycopg2.connect(host=SQL_SRV, user=SQL_USER,password=SQL_PASSWD,database=SQL_DB, application_name="PiDevWarkanum")
|
||||||
|
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if self.pgconn:
|
||||||
|
self.pgconn.close()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.quit = True
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while True:
|
||||||
|
if self.quit:
|
||||||
|
logging.debug(self.name + ' Quit!')
|
||||||
|
break
|
||||||
|
|
||||||
|
try:
|
||||||
|
print('Check Jobs.')
|
||||||
|
while True:
|
||||||
|
item = WORK_QUE.get()
|
||||||
|
WORK_QUE.task_done()
|
||||||
|
|
||||||
|
if not item:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print('Job left: ' + str(WORK_QUE.qsize()))
|
||||||
|
|
||||||
|
results = save_data(item, self.pgconn)
|
||||||
|
logging.debug(self.name + ' Completed job')
|
||||||
|
del item
|
||||||
|
|
||||||
|
print('Completed job.')
|
||||||
|
|
||||||
|
#Sleep
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
except KeyboardInterrupt as e:
|
||||||
|
logging.warning(self.name + ' Keyboard Interrupt. ' + str(e))
|
||||||
|
break
|
||||||
|
|
||||||
|
except:
|
||||||
|
errordetail = str(sys.exc_info())
|
||||||
|
logging.error(self.name + ' Error: '+errordetail)
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def UnixLocalEpoch():
|
def UnixLocalEpoch():
|
||||||
dt = datetime.datetime.now()
|
dt = datetime.datetime.now()
|
||||||
return int((dt - datetime.datetime(1970,1,1)).total_seconds())
|
return int((dt - datetime.datetime(1970,1,1)).total_seconds())
|
||||||
|
|
||||||
def PhotoSensor(RCpin):
|
def PhotoSensor(RCpin):
|
||||||
|
try:
|
||||||
reading = 0
|
reading = 0
|
||||||
GPIO.setup(RCpin, GPIO.OUT)
|
GPIO.setup(RCpin, GPIO.OUT)
|
||||||
GPIO.output(RCpin, GPIO.LOW)
|
GPIO.output(RCpin, GPIO.LOW)
|
||||||
@ -55,23 +116,31 @@ def PhotoSensor(RCpin):
|
|||||||
while (GPIO.input(RCpin) == GPIO.LOW):
|
while (GPIO.input(RCpin) == GPIO.LOW):
|
||||||
reading += 1
|
reading += 1
|
||||||
|
|
||||||
|
except:
|
||||||
|
errordetail = str(sys.exc_info()[0])
|
||||||
|
logging.error('PhotoSensor Error: '+errordetail)
|
||||||
|
reading = 0
|
||||||
|
|
||||||
return reading
|
return reading
|
||||||
|
|
||||||
def TempsensorRead():
|
def TempsensorRead():
|
||||||
### Loop through the temp sensor library until we get a valid reading ###
|
### Loop through the temp sensor library until we get a valid reading ###
|
||||||
|
try:
|
||||||
|
|
||||||
for i in range(1,100):
|
for i in range(1,100):
|
||||||
data = dht11_sensor.read(PIN_TC_WP)
|
data = dht11_sensor.read(PIN_TC_WP)
|
||||||
#print('Temp={0}*C Humidity={1}% Status={2} Error={3}'.format(data['temperature'], data['humidity'], data['valid'], data['err']))
|
#print('Temp={0}*C Humidity={1}% Status={2} Error={3}'.format(data['temperature'], data['humidity'], data['valid'], data['err']))
|
||||||
if data['valid'] == 1:
|
if data['valid'] == 1:
|
||||||
return data
|
return data
|
||||||
|
except:
|
||||||
|
errordetail = str(sys.exc_info()[0])
|
||||||
|
logging.error('TempsensorRead Error: '+errordetail)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def save_data(p_SensorValues):
|
def save_data(p_SensorValues, p_sql_connection):
|
||||||
try:
|
try:
|
||||||
sql_con = psycopg2.connect(host=SQL_SRV, user=SQL_USER,password=SQL_PASSWD,database=SQL_DB)
|
sql_cur = p_sql_connection.cursor()
|
||||||
sql_cur = sql_con.cursor()
|
|
||||||
|
|
||||||
print('2temp->' + str(p_SensorValues['temperature']['save']))
|
print('2temp->' + str(p_SensorValues['temperature']['save']))
|
||||||
print('2light->' + str(p_SensorValues['light']['save']))
|
print('2light->' + str(p_SensorValues['light']['save']))
|
||||||
@ -112,9 +181,9 @@ def save_data(p_SensorValues):
|
|||||||
VALUES(%s, %s, TIMESTAMP 'epoch' + %s * INTERVAL '1 second',TIMESTAMP 'epoch' + %s * INTERVAL '1 second')""",
|
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'] ))
|
('humidity', p_SensorValues['temperature']['humidity'], p_SensorValues['temperature']['read'], p_SensorValues['temperature']['read'] ))
|
||||||
|
|
||||||
sql_con.commit()
|
p_sql_connection.commit()
|
||||||
sql_cur.close()
|
sql_cur.close()
|
||||||
sql_con.close()
|
|
||||||
|
|
||||||
except psycopg2.Error as e:
|
except psycopg2.Error as e:
|
||||||
print("SQL error in save_data: " + str(e))
|
print("SQL error in save_data: " + str(e))
|
||||||
@ -124,6 +193,12 @@ def save_data(p_SensorValues):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
t = SaveThread()
|
||||||
|
t.setDaemon(True)
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
SensorValue = {}
|
SensorValue = {}
|
||||||
TICK_LT = 0 #light detect ticker
|
TICK_LT = 0 #light detect ticker
|
||||||
TICK_LTI = 0 #light insert ticker
|
TICK_LTI = 0 #light insert ticker
|
||||||
@ -131,9 +206,11 @@ def main():
|
|||||||
|
|
||||||
BlueLed = False
|
BlueLed = False
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
changed = False
|
changed = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
motionData = GPIO.input(PIN_MC)
|
motionData = GPIO.input(PIN_MC)
|
||||||
if not SensorValue.get('motion', None):
|
if not SensorValue.get('motion', None):
|
||||||
@ -209,13 +286,32 @@ def main():
|
|||||||
print('motion->' + str(SensorValue['motion']['save']))
|
print('motion->' + str(SensorValue['motion']['save']))
|
||||||
#Gosh we need a copy cause sql can be to slow sometimes
|
#Gosh we need a copy cause sql can be to slow sometimes
|
||||||
|
|
||||||
ThreadsData = copy.deepcopy(SensorValue)
|
WORK_QUE.put(copy.deepcopy(SensorValue), False)
|
||||||
t = Thread(target=save_data, args=(ThreadsData,))
|
|
||||||
t.start()
|
|
||||||
|
|
||||||
|
#ThreadsData = copy.deepcopy(SensorValue)
|
||||||
|
#t = Thread(target=save_data, args=(ThreadsData,))
|
||||||
|
#t.start()
|
||||||
|
|
||||||
|
except KeyboardInterrupt as e:
|
||||||
|
logging.warning('Loop Keyboard Interrupt.')
|
||||||
|
t.stop()
|
||||||
|
break
|
||||||
|
|
||||||
|
except:
|
||||||
|
errordetail = str(sys.exc_info()[0])
|
||||||
|
logging.error('Loop Error: '+errordetail)
|
||||||
|
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
|
|
||||||
|
except KeyboardInterrupt as e:
|
||||||
|
logging.warning('Main Keyboard Interrupt.')
|
||||||
|
t.stop()
|
||||||
|
|
||||||
|
except:
|
||||||
|
errordetail = str(sys.exc_info()[0])
|
||||||
|
logging.error('Main Error: '+errordetail)
|
||||||
|
|
||||||
|
t.stop()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
sys.exit(main())
|
Loading…
Reference in New Issue
Block a user