after some adjusting i came up with this (complete code):
#!/usr/bin/python
# -*- coding: utf-8 -*-
#SETTINGS START
start_time = '07:00'
end_time = '22:00'
bake_temp = float(25)
target1 = float(22)
target2 = float(17)
# Domain you want to post to: localhost would be an emoncms installation on your own laptop
# this could be changed to emoncms.org to post to emoncms.org
domain = "emoncms.org"
# Location of emoncms in your server, the standard setup is to place it in a folder called emoncms
# To post to emoncms.org change this to blank: ""
emoncmspath = ""
# Write apikey of emoncms account
apikey = "takeaguess"
# Node id youd like the emontx to appear as
nodeid = 1
#SETTINGS END
#IMPORTS START
# imports for thermometer reading test
import os
import glob
import time
import datetime
# import for internet connect check
import socket
# this needed to post to emocms
import sys, string
import httplib
#this one is for the wiringpi lib
import wiringpi
#IMPORTS END
# wiringpi numbers
wiringpi.wiringPiSetup()
wiringpi.pinMode(0, 1) # sets pin 0 to output (GPIO 17, Actual hardware pin number is 11) (Relay)
wiringpi.pinMode(2, 1) # sets pin 2 to output (GPIO 27, Actual hardware pin number is 13) (Internet connection LED)
wiringpi.pinMode(3, 0) # sets pin 3 to input (GPIO 22, Actual hardware pin number is 15) (latching button)
# Find temperature from thermometer
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
csv = 0
target = 0
relay = 0
t_loop = 0
t15 = 0
test = 0
next_time = 0
# check if internet - just so we know if time can be trusted
def internet_connected(host='8.8.8.8', port=53):
"""
Host: 8.8.8.8 (google-public-dns-a.google.com)
OpenPort: 53/tcp
Service: domain (DNS/TCP)
"""
try:
socket.setdefaulttimeout(20)
socket.socket(socket.AF_INET,
socket.SOCK_STREAM).connect((host, port))
return True
except Exception, ex:
pass
return False
# temp reading
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = (lines[1])[equals_pos + 2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c # , temp_f
# schedule, check if current time is after schedule start and sets target
def schedule():
global target
t = time.strftime('%H:%M')
if is_between(t, (start_time, end_time)) == True:
print('Running Daytime Schedule')
target = target1
else:
print('Running Nighttime Schedule')
target = target2
def is_between(time, time_range):
if time_range[1] < time_range[0]:
return time >= time_range[0] or time <= time_range[1]
return time_range[0] <= time <= time_range[1]
def emoncms():
try:
seq = (read_temp(), target, my_input, relay)
str_join = ",".join(str(x) for x in seq)
print 'Preparing Data for emoncms.org'
conn = httplib.HTTPConnection(domain)
conn.request("GET", "/"+emoncmspath+"/input/post.json?apikey="+apikey+"&node="+str(nodeid)+"&csv="+str_join)
response = conn.getresponse()
print response.read()
print 'Data sent to emoncms'
except Exception, ex:
print 'This error occurred: ' + str(ex)
while True:
my_input = wiringpi.digitalRead(3)
if my_input == 1:
print 'Switch is ON'
t15 = 15
time_now = time.time()
if time_now >= next_time:
print 'Start of loop: ' + str(test)
temp_now = read_temp()
print 'Current temp: ' + str(temp_now)
schedule()
print 'We have an internet connection? ' + str(internet_connected())
# Control heating
if t15 > 0:
target = bake_temp
t15 -= 1
if target > temp_now: # Compare temp read to temp from DS18B20
wiringpi.digitalWrite(0, 1) # sets port 0 to ON
relay = 1
print 'Target temp: ' + str(target)
print 'HEATING ON (Boost)'
else:
wiringpi.digitalWrite(0, 0) # sets port 0 to OFF
relay = 0
print 'Target temp: ' + str(target)
print 'HEATING OFF (Boost)'
else:
if target > temp_now: # Compare temp read to temp from DS18B20
wiringpi.digitalWrite(0, 1) # sets port 0 to ON
relay = 1
print 'Target temp: ' + str(target)
print 'HEATING ON'
else:
wiringpi.digitalWrite(0, 0) # sets port 0 to OFF
relay = 0
print 'Target temp: ' + str(target)
print 'HEATING OFF'
# Calculate the next loop time by adding interval in secs
next_time = time_now + 60
emoncms()
print "Remaining boost time: " + str(t15) + " mins"
test += 1
print 'end of loop'
print '--------------------'
# Don't loop too fast
time.sleep(1)