Last active
August 29, 2015 14:05
-
-
Save photomattmills/746bc1e8fed75ea413ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'faraday' | |
require 'json' | |
class Temp | |
def temp_request | |
@temp_request ||= Faraday.post('https://api.spark.io/v1/devices/51ff70065067545733280187/analogread', { | |
access_token: '', | |
params: 'A0' | |
}) | |
end | |
def returned_value | |
JSON.parse(temp_request.body)["return_value"] | |
end | |
def temp_voltage | |
(returned_value/4095.0)*3.3 | |
end | |
def get | |
((temp_voltage - 1.25) / 0.005).to_i | |
end | |
end | |
class Oven | |
attr_reader :temp | |
def self.on | |
Faraday.post('https://api.spark.io/v1/devices/51ff70065067545733280187/digitalwrite', { | |
access_token: '', | |
params: 'D7,HIGH' | |
}) | |
end | |
def self.off | |
Faraday.post('https://api.spark.io/v1/devices/51ff70065067545733280187/digitalwrite', { | |
access_token: '', | |
params: 'D7,LOW' | |
}) | |
end | |
def get_temp | |
Temp.new.get | |
end | |
def oven_run(duration, min_temp) | |
time = 0 | |
while time < duration | |
temp = get_temp | |
puts temp | |
if temp > min_temp | |
Oven.off | |
time += 1 | |
else | |
Oven.on | |
end | |
sleep 1 | |
end | |
end | |
def presoak | |
oven_run(100, 130) | |
end | |
def flow | |
oven_run(10, 230) | |
end | |
end | |
Oven.new.presoak | |
Oven.new.flow | |
puts "done" | |
while true | |
puts Temp.new.get | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment