Commit 80cd4651 authored by Piotr "PHT" Jasiek's avatar Piotr "PHT" Jasiek

Initial commit

parents
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import datetime
from modules.temp import *
from modules.database import *
import DS18B20
####Adding kernel modules
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
####
####Constant
outside_sensor = '/sys/bus/w1/devices/28-0000074e18a7/w1_slave'
inside_sensor = '/sys/bus/w1/devices/28-0000074e7326/w1_slave'
desktop_temp_sensor = '/sys/bus/w1/devices/10-00080229a477/w1_slave'
DS18B20.setResolution(4,'28-0000074e18a7',9)
DS18B20.setResolution(4,'28-0000074e7326',9)
###Requesting time
request_time = datetime.datetime.now().strftime("%H:%M %d-%m")
###Requesting sensors
temp_outside = get_temp(outside_sensor)
if temp_outside == 85 or temp_outside == "125.5":
temp_outside = get_prev_temp('outside')
temp_inside = get_temp(inside_sensor)
if temp_inside == 85 or temp_inside == "125.5":
temp_inside = get_prev_temp('inside')
temp_desktop = get_temp(desktop_temp_sensor)
if temp_desktop == 85 or temp_desktop == "125.5":
temp_desktop = get_prev_temp('desktop')
###Upload sensor reading
try:
add_temp(request_time, temp_outside, temp_inside, temp_desktop)
except:
print "Mysql Errorr"
else:
###Debug output
print "Outside:", temp_outside, "Inside:", temp_inside, "Desktop:", temp_desktop
#!/usr/bin/env python
import MySQLdb
def add_temp(data, outside, inside, desktop):
db = MySQLdb.connect("host","user","pass","db" )
cursor = db.cursor()
sql = "INSERT INTO temperatura(data, outside, inside, desktop) \
VALUES ('%s', '%s', '%s', '%s')" % (data, outside, inside, desktop)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
db.close()
def get_prev_temp(sensor):
db = MySQLdb.connect("host","user","pass","db" )
cursor = db.cursor()
sql = "SELECT %s FROM temperatura ORDER BY id DESC LIMIT 1" % (sensor)
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
prev_temp = row[0]
return prev_temp
print "Error get temp: ", sensor
except:
print "Error: unable to fecth data"
db.close()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
def get_temp(sensor):
f = open(sensor, 'r')
lines = f.readlines()
f.close()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment