Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import MySQLdb
- from serial import Serial
- import time
- from datetime import datetime
- serialPort = Serial("/dev/ttyAMA0", 9600, timeout=1)
- if (serialPort.isOpen() == False):
- serialPort.open()
- serialPort.flushInput()
- serialPort.flushOutput()
- db = MySQLdb.connect("localhost", "root", "jfkeauve", "logs")
- #"/var/lib/mysql/logs")
- if (db == False):
- print("DB lost conn!")
- cursor = db.cursor()
- pir1 = []
- pir2 = []
- while True:
- #Variable for date/Input in database
- inputdate = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
- #data = serialPort.readline()
- minValue_temp = 5
- maxValue_temp = 40
- minValue_press = 99000
- maxValue_press = 101000
- if(serialPort.inWaiting() != 0):
- checkpoint = serialPort.read(2)
- #Temperature
- if(checkpoint == 'TE'):
- checkpoint = serialPort.read(1)
- temperature = serialPort.read(3)
- temperature = float(temperature) * 0.1
- temperature = round(temperature, 1)
- if(checkpoint == 'B'):
- cursor.execute("INSERT INTO temp_prelog VALUES (%s, %s, %s, %s, %s, 'Board')", (1, inputdate, temperature, minValue_temp, maxValue_temp$
- db.commit()
- if(checkpoint == 'K'):
- cursor.execute("INSERT INTO temp_prelog VALUES (%s, %s, %s, %s, %s, 'Kuhinja')", (1, inputdate, temperature, minValue_temp, maxValue_te$
- db.commit()
- #Pressure
- if(checkpoint == 'PR'):
- checkpoint = serialPort.read(1)
- pressure = serialPort.read(5)
- if(checkpoint == 'B'):
- cursor.execute("INSERT INTO temp_prelog VALUES (%s, %s, %s, %s, %s, 'Board')", (2, inputdate, pressure, minValue_press, maxValue_press,$
- db.commit()
- if(checkpoint == 'K'):
- cursor.execute("INSERT INTO temp_prelog VALUES (%s, %s, %s, %s, %s, 'Kuhinja')", (2, inputdate, pressure, minValue_press, maxValue_pres$
- db.commit()
- #PIR -> Motion sensor
- if(checkpoint == 'PI'):
- #time.sleep(1)
- checkpoint = serialPort.read(1)
- if(checkpoint == '1'):
- #pir1 += datetime.now().strftime("%H:%M:%S")
- #start = (pir1[-8],pir1[-7],pir1[-6],pir1[-5],pir1[-4],pir1[-3],pir1[-2],pir1[-1])
- #finish = (pir1[0],pir1[1],pir1[2],pir1[3],pir1[4],pir1[5],pir1[6],pir1[7])
- cursor.execute("INSERT INTO alarms VALUES (%s, %s, 'Hodnik')", (1, inputdate,))
- db.commit()
- if(checkpoint == '2'):
- cursor.execute("INSERT INTO alarms VALUES (%s, %s, 'Boravak')", (2, inputdate,))
- db.commit()
- serialPort.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement