Advertisement
Guest User

Untitled

a guest
Oct 30th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #SETTINGS START
  5.  
  6. start_time = '07:00'
  7. end_time = '22:00'
  8. bake_temp = float(25)
  9. target1 = float(22)
  10. target2 = float(17)
  11.  
  12. # Domain you want to post to: localhost would be an emoncms installation on your own laptop
  13. # this could be changed to emoncms.org to post to emoncms.org
  14. domain = "**********"
  15.  
  16. # Location of emoncms in your server, the standard setup is to place it in a folder called emoncms
  17. # To post to emoncms.org change this to blank: ""
  18. emoncmspath = "emoncms"
  19.  
  20. # Write apikey of emoncms account
  21. apikey = "***********"
  22.  
  23. # Node id youd like the emontx to appear as
  24. nodeid = "1"
  25.  
  26. #Serial numbers for DS18B20, must include 28
  27.  
  28. device1 = "28-0517c09bb5ff"
  29. device2 = "28-0517c09bb5ff"
  30.  
  31. #SETTINGS END
  32.  
  33. #IMPORTS START
  34.  
  35. # imports for thermometer reading test
  36.  
  37. import os
  38. import glob
  39. import time
  40.  
  41. # import for internet connect check
  42.  
  43. import socket
  44.  
  45. # this needed to post to emocms
  46.  
  47. import sys, string
  48. import httplib
  49.  
  50. #this one is for the wiringpi lib
  51.  
  52. import wiringpi
  53.  
  54. #IMPORTS END
  55.  
  56. # wiringpi numbers
  57.  
  58. wiringpi.wiringPiSetup()
  59. wiringpi.pinMode(0, 1) # sets pin 0 to output (GPIO 17, Actual hardware pin number is 11) (Relay)
  60. wiringpi.pinMode(2, 1) # sets pin 2 to output (GPIO 27, Actual hardware pin number is 13) (Internet connection LED)
  61. wiringpi.pinMode(3, 0) # sets pin 3 to input (GPIO 22, Actual hardware pin number is 15) (latching button)
  62.  
  63. # Find temperature from thermometer
  64.  
  65. os.system('modprobe w1-gpio')
  66. os.system('modprobe w1-therm')
  67.  
  68. base_dir = '/sys/bus/w1/devices/'
  69. device_folder = glob.glob(base_dir + device1)[0]
  70. device_folder1 = glob.glob(base_dir + device2)[0]
  71. device_file = device_folder + '/w1_slave'
  72. device_file1 = device_folder1 + '/w1_slave'
  73.  
  74. csv = 0
  75. target = 0
  76. relay = 0
  77. t_loop = 0
  78. t15 = 0
  79. test = 0
  80. next_time = 0
  81. cpu = 0
  82.  
  83. # check if internet - just so we know if time can be trusted
  84.  
  85. def internet_connected(host='8.8.8.8', port=53):
  86. """
  87. Host: 8.8.8.8 (google-public-dns-a.google.com)
  88. OpenPort: 53/tcp
  89. Service: domain (DNS/TCP)
  90. """
  91. try:
  92. socket.setdefaulttimeout(20)
  93. socket.socket(socket.AF_INET,
  94. socket.SOCK_STREAM).connect((host, port))
  95. return True
  96. except Exception, ex:
  97. print time.asctime( time.localtime(time.time()) ) + " This error occurred: " + str(ex)
  98.  
  99. return False
  100.  
  101. # temp reading
  102.  
  103. def read_temp_raw():
  104. try:
  105. f = open(device_file, 'r')
  106. lines = f.readlines()
  107. f.close()
  108. return lines
  109. except Exception, ex:
  110. print time.asctime( time.localtime(time.time()) ),
  111. print " Failed to read temp sensor, error is: " + str(ex)
  112. return False
  113.  
  114. def read_temp():
  115. if read_temp_raw():
  116. lines = read_temp_raw()
  117. while lines[0].strip()[-3:] != 'YES':
  118. time.sleep(0.2)
  119. lines = read_temp_raw()
  120. equals_pos = lines[1].find('t=')
  121. if equals_pos != -1:
  122. temp_string = (lines[1])[equals_pos + 2:]
  123. temp_c = float(temp_string) / 1000.0
  124. temp_f = temp_c * 9.0 / 5.0 + 32.0
  125. return temp_c # , temp_f
  126. else:
  127. return 68
  128.  
  129. def read_temp_raw1():
  130. try:
  131. f = open(device_file1, 'r')
  132. lines = f.readlines()
  133. f.close()
  134. return lines
  135. except Exception, ex:
  136. print time.asctime( time.localtime(time.time()) ),
  137. print " Failed to read temp sensor, error is: " + str(ex)
  138. return False
  139.  
  140. def read_temp1():
  141. if read_temp_raw1():
  142. lines = read_temp_raw1()
  143. while lines[0].strip()[-3:] != 'YES':
  144. time.sleep(0.2)
  145. lines = read_temp_raw1()
  146. equals_pos = lines[1].find('t=')
  147. if equals_pos != -1:
  148. temp_string = (lines[1])[equals_pos + 2:]
  149. temp_c = float(temp_string) / 1000.0
  150. temp_f = temp_c * 9.0 / 5.0 + 32.0
  151. return temp_c # , temp_f
  152. else:
  153. return 68
  154.  
  155. # schedule, check if current time is after schedule start and sets target
  156.  
  157. def schedule():
  158.  
  159. global target
  160.  
  161. t = time.strftime('%H:%M')
  162. if is_between(t, (start_time, end_time)) == True:
  163. print time.asctime( time.localtime(time.time()) ),
  164. print (' Running Daytime Schedule')
  165. target = target1
  166. else:
  167. print time.asctime( time.localtime(time.time()) ),
  168. print(' Running Nighttime Schedule')
  169. target = target2
  170.  
  171. def is_between(time, time_range):
  172.  
  173. if time_range[1] < time_range[0]:
  174. return time >= time_range[0] or time <= time_range[1]
  175. return time_range[0] <= time <= time_range[1]
  176.  
  177. def emoncms():
  178.  
  179. try:
  180. seq = (read_temp(), read_temp1(), target, relay, cpu)
  181. str_join = ",".join(str(x) for x in seq)
  182. print time.asctime( time.localtime(time.time()) ),
  183. print ' Preparing Data for emoncms.org'
  184. conn = httplib.HTTPConnection(domain)
  185. conn.request("GET", "/"+emoncmspath+"/input/post?node="+str(nodeid)+"&csv="+str_join&apikey="+apikey+")
  186.  
  187. response = conn.getresponse()
  188. print time.asctime( time.localtime(time.time()) ),
  189. print ' Response from emoncms.org:',
  190. print response.read()
  191. print time.asctime( time.localtime(time.time()) ),
  192. print ' Data sent to emoncms'
  193. except Exception, ex:
  194. print time.asctime( time.localtime(time.time()) ),
  195. print ' This error occurred: ' + str(ex)
  196.  
  197. def measure_temp():
  198.  
  199. try:
  200. print time.asctime( time.localtime(time.time()) ),
  201. print ' Getting CPU Temp'
  202. temp = os.popen("vcgencmd measure_temp").readline()
  203. print time.asctime( time.localtime(time.time()) ),
  204. print ' Got CPU Temp'
  205.  
  206. return (temp.replace("temp=","").replace("'C\n",""))
  207.  
  208. except Exception, ex:
  209. print time.asctime( time.localtime(time.time()) ),
  210. print ' This error occurred: ' + str(ex)
  211.  
  212. while True:
  213. my_input = wiringpi.digitalRead(3)
  214.  
  215. if my_input == 1:
  216. print time.asctime( time.localtime(time.time()) ),
  217. print ' Switch is ON'
  218. t15 = 15
  219.  
  220. time_now = time.time()
  221.  
  222. if time_now >= next_time:
  223. print time.asctime( time.localtime(time.time()) ),
  224. print ' Start of loop: ' + str(test)
  225. temp_now = read_temp()
  226. cpu=measure_temp()
  227. print time.asctime( time.localtime(time.time()) ),
  228. print ' Cpu temp: ' + str(cpu)
  229. print time.asctime( time.localtime(time.time()) ),
  230. print ' Current temp: ' + str(temp_now)
  231.  
  232. schedule()
  233.  
  234. print time.asctime( time.localtime(time.time()) ),
  235. print ' We have an internet connection? ' + str(internet_connected())
  236. if internet_connected() == True:
  237. wiringpi.digitalWrite(2, 0) # sets port 2 to OFF
  238. else:
  239. wiringpi.digitalWrite(2, 1) # sets port 2 to ON
  240.  
  241. # Control heating
  242. if t15 > 0:
  243. target = bake_temp
  244. t15 -= 1
  245. if target > temp_now: # Compare temp read to temp from DS18B20
  246. wiringpi.digitalWrite(0, 1) # sets port 0 to ON
  247. relay = 1
  248. print time.asctime( time.localtime(time.time()) ),
  249. print ' Target temp: ' + str(target)
  250. print time.asctime( time.localtime(time.time()) ),
  251. print ' HEATING ON (Boost)'
  252. else:
  253. wiringpi.digitalWrite(0, 0) # sets port 0 to OFF
  254. relay = 0
  255. print time.asctime( time.localtime(time.time()) ),
  256. print ' Target temp: ' + str(target)
  257. print time.asctime( time.localtime(time.time()) ),
  258. print ' HEATING OFF (Boost)'
  259. else:
  260. if target > temp_now: # Compare temp read to temp from DS18B20
  261. wiringpi.digitalWrite(0, 1) # sets port 0 to ON
  262. relay = 1
  263. print time.asctime( time.localtime(time.time()) ),
  264. print ' Target temp: ' + str(target)
  265. print time.asctime( time.localtime(time.time()) ),
  266. print ' HEATING ON'
  267. else:
  268. wiringpi.digitalWrite(0, 0) # sets port 0 to OFF
  269. relay = 0
  270. print time.asctime( time.localtime(time.time()) ),
  271. print ' Target temp: ' + str(target)
  272. print time.asctime( time.localtime(time.time()) ),
  273. print ' HEATING OFF'
  274.  
  275. # Calculate the next loop time by adding interval in secs
  276. next_time = time_now + 60
  277.  
  278. emoncms()
  279. print time.asctime( time.localtime(time.time()) ),
  280. print " Remaining boost time: " + str(t15) + " mins"
  281. test += 1
  282. print time.asctime( time.localtime(time.time()) ),
  283. print ' End of loop'
  284. print time.asctime( time.localtime(time.time()) ),
  285. print ' --------------------'
  286.  
  287. # Don't loop too fast
  288. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement