Skip to content

Instantly share code, notes, and snippets.

@photomattmills
Last active August 29, 2015 14:05
Show Gist options
  • Save photomattmills/746bc1e8fed75ea413ae to your computer and use it in GitHub Desktop.
Save photomattmills/746bc1e8fed75ea413ae to your computer and use it in GitHub Desktop.
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