Commit 8bae8efe authored by Piotr "PHT" Jasiek's avatar Piotr "PHT" Jasiek

add new sensors, add humidity

parent 80cd4651
......@@ -7,8 +7,7 @@ import time
import datetime
from modules.temp import *
from modules.database import *
import DS18B20
from modules.dht11 import *
####Adding kernel modules
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
......@@ -18,42 +17,56 @@ os.system('modprobe w1-therm')
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)
greenhouse_sensor_temp = '/sys/bus/w1/devices/10-00080229721f/w1_slave'
greenhouse_sensor_hum = 17
outside_sensor_hum = 27
###Requesting time
request_time = datetime.datetime.now().strftime("%H:%M %d-%m")
request_time = datetime.datetime.now().strftime("%s")
###Requesting sensors
temp_outside = get_temp(outside_sensor)
if temp_outside == 85 or temp_outside == "125.5":
temp_outside = get_prev_temp('outside')
if temp_outside == 85 or temp_outside == 127.5:
temp_outside = get_prev_value('outside_temp')
temp_inside = get_temp(inside_sensor)
if temp_inside == 85 or temp_inside == "125.5":
temp_inside = get_prev_temp('inside')
if temp_inside == 85 or temp_inside == 127.5:
temp_inside = get_prev_value('inside_temp')
temp_desktop = get_temp(desktop_temp_sensor)
if temp_desktop == 85 or temp_desktop == "125.5":
temp_desktop = get_prev_temp('desktop')
if temp_desktop == 85 or temp_desktop == 127.687:
temp_desktop = get_prev_value('desktop_temp')
temp_greenhouse = get_temp(greenhouse_sensor_temp)
if temp_greenhouse == 85 or temp_greenhouse == 127.687:
temp_greenhouse = get_prev_value('greenhouse_temp')
temp_station_controller = get_temp_rpi()
#hum_greenhouse = get_from_dht11(greenhouse_sensor_hum, "hum")
#hum_outside = get_from_dht11(outside_sensor_hum, "hum")
###Upload sensor reading
##Upload sensor reading
try:
add_temp(request_time, temp_outside, temp_inside, temp_desktop)
add_record(request_time, 'outside_temp', temp_outside)
# add_record(request_time, 'outside_hum' , hum_outside)
add_record(request_time, 'inside_temp', temp_inside)
add_record(request_time, 'desktop_temp', temp_desktop)
add_record(request_time, 'greenhouse_temp', temp_greenhouse)
# add_record(request_time, 'greenhouse_hum', hum_greenhouse)
add_record(request_time, 'device_proc_temp', temp_station_controller)
except:
print "Mysql Errorr"
else:
#else:
###Debug output
print "Outside:", temp_outside, "Inside:", temp_inside, "Desktop:", temp_desktop
# print "Outside:", temp_outside, "Inside:", temp_inside, "Desktop:", temp_desktop, "Green house:", temp_green_house
......@@ -2,13 +2,12 @@
import MySQLdb
def add_temp(data, outside, inside, desktop):
def add_record(data, sensor, value):
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)
sql = "INSERT INTO %s (data, value) VALUES ('%s', '%s')" % (sensor, data, value)
try:
cursor.execute(sql)
db.commit()
......@@ -17,12 +16,12 @@ def add_temp(data, outside, inside, desktop):
db.close()
def get_prev_temp(sensor):
def get_prev_value(sensor):
db = MySQLdb.connect("host","user","pass","db" )
cursor = db.cursor()
sql = "SELECT %s FROM temperatura ORDER BY id DESC LIMIT 1" % (sensor)
sql = "SELECT value FROM %s ORDER BY id DESC LIMIT 1" % (sensor)
try:
cursor.execute(sql)
results = cursor.fetchall()
......
import Adafruit_DHT
def get_from_dht11(pin, type):
sensor = Adafruit_DHT.DHT11
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if type == "hum":
return humidity
elif type == "temp":
return temperature
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import dht11
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
def get_humidity(sensor):
instance = dht11.DHT11(sensor)
result = instance.read()
if result.is_valid():
return result.humidity
else:
return result.error_code
......@@ -17,3 +17,7 @@ def get_temp(sensor):
temp_c = float(temp_string) / 1000.0
return temp_c
def get_temp_rpi():
var = os.popen('cat /sys/class/thermal/thermal_zone0/temp').readline()
return int(var)/1000.0
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