Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 = "**********"
- # 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 = "emoncms"
- # Write apikey of emoncms account
- apikey = "***********"
- # Node id youd like the emontx to appear as
- nodeid = "1"
- #Serial numbers for DS18B20, must include 28
- device1 = "28-0517c09bb5ff"
- device2 = "28-0517c09bb5ff"
- #SETTINGS END
- #IMPORTS START
- # imports for thermometer reading test
- import os
- import glob
- import time
- # 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 + device1)[0]
- device_folder1 = glob.glob(base_dir + device2)[0]
- device_file = device_folder + '/w1_slave'
- device_file1 = device_folder1 + '/w1_slave'
- csv = 0
- target = 0
- relay = 0
- t_loop = 0
- t15 = 0
- test = 0
- next_time = 0
- cpu = 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:
- print time.asctime( time.localtime(time.time()) ) + " This error occurred: " + str(ex)
- return False
- # temp reading
- def read_temp_raw():
- try:
- f = open(device_file, 'r')
- lines = f.readlines()
- f.close()
- return lines
- except Exception, ex:
- print time.asctime( time.localtime(time.time()) ),
- print " Failed to read temp sensor, error is: " + str(ex)
- return False
- def read_temp():
- if read_temp_raw():
- 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
- else:
- return 68
- def read_temp_raw1():
- try:
- f = open(device_file1, 'r')
- lines = f.readlines()
- f.close()
- return lines
- except Exception, ex:
- print time.asctime( time.localtime(time.time()) ),
- print " Failed to read temp sensor, error is: " + str(ex)
- return False
- def read_temp1():
- if read_temp_raw1():
- lines = read_temp_raw1()
- while lines[0].strip()[-3:] != 'YES':
- time.sleep(0.2)
- lines = read_temp_raw1()
- 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
- else:
- return 68
- # 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 time.asctime( time.localtime(time.time()) ),
- print (' Running Daytime Schedule')
- target = target1
- else:
- print time.asctime( time.localtime(time.time()) ),
- 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(), read_temp1(), target, relay, cpu)
- str_join = ",".join(str(x) for x in seq)
- print time.asctime( time.localtime(time.time()) ),
- print ' Preparing Data for emoncms.org'
- conn = httplib.HTTPConnection(domain)
- conn.request("GET", "/"+emoncmspath+"/input/post?node="+str(nodeid)+"&csv="+str_join&apikey="+apikey+")
- response = conn.getresponse()
- print time.asctime( time.localtime(time.time()) ),
- print ' Response from emoncms.org:',
- print response.read()
- print time.asctime( time.localtime(time.time()) ),
- print ' Data sent to emoncms'
- except Exception, ex:
- print time.asctime( time.localtime(time.time()) ),
- print ' This error occurred: ' + str(ex)
- def measure_temp():
- try:
- print time.asctime( time.localtime(time.time()) ),
- print ' Getting CPU Temp'
- temp = os.popen("vcgencmd measure_temp").readline()
- print time.asctime( time.localtime(time.time()) ),
- print ' Got CPU Temp'
- return (temp.replace("temp=","").replace("'C\n",""))
- except Exception, ex:
- print time.asctime( time.localtime(time.time()) ),
- print ' This error occurred: ' + str(ex)
- while True:
- my_input = wiringpi.digitalRead(3)
- if my_input == 1:
- print time.asctime( time.localtime(time.time()) ),
- print ' Switch is ON'
- t15 = 15
- time_now = time.time()
- if time_now >= next_time:
- print time.asctime( time.localtime(time.time()) ),
- print ' Start of loop: ' + str(test)
- temp_now = read_temp()
- cpu=measure_temp()
- print time.asctime( time.localtime(time.time()) ),
- print ' Cpu temp: ' + str(cpu)
- print time.asctime( time.localtime(time.time()) ),
- print ' Current temp: ' + str(temp_now)
- schedule()
- print time.asctime( time.localtime(time.time()) ),
- print ' We have an internet connection? ' + str(internet_connected())
- if internet_connected() == True:
- wiringpi.digitalWrite(2, 0) # sets port 2 to OFF
- else:
- wiringpi.digitalWrite(2, 1) # sets port 2 to ON
- # 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 time.asctime( time.localtime(time.time()) ),
- print ' Target temp: ' + str(target)
- print time.asctime( time.localtime(time.time()) ),
- print ' HEATING ON (Boost)'
- else:
- wiringpi.digitalWrite(0, 0) # sets port 0 to OFF
- relay = 0
- print time.asctime( time.localtime(time.time()) ),
- print ' Target temp: ' + str(target)
- print time.asctime( time.localtime(time.time()) ),
- 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 time.asctime( time.localtime(time.time()) ),
- print ' Target temp: ' + str(target)
- print time.asctime( time.localtime(time.time()) ),
- print ' HEATING ON'
- else:
- wiringpi.digitalWrite(0, 0) # sets port 0 to OFF
- relay = 0
- print time.asctime( time.localtime(time.time()) ),
- print ' Target temp: ' + str(target)
- print time.asctime( time.localtime(time.time()) ),
- print ' HEATING OFF'
- # Calculate the next loop time by adding interval in secs
- next_time = time_now + 60
- emoncms()
- print time.asctime( time.localtime(time.time()) ),
- print " Remaining boost time: " + str(t15) + " mins"
- test += 1
- print time.asctime( time.localtime(time.time()) ),
- print ' End of loop'
- print time.asctime( time.localtime(time.time()) ),
- print ' --------------------'
- # Don't loop too fast
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement