Skip to content

Instantly share code, notes, and snippets.

@sergiomaia
Forked from henriqueutsch/chartkick.js
Last active August 28, 2015 01:02
Show Gist options
  • Save sergiomaia/c51b4de5dd3727060fbb to your computer and use it in GitHub Desktop.
Save sergiomaia/c51b4de5dd3727060fbb to your computer and use it in GitHub Desktop.
Temperature monitor raspberry pi DS18B20 sensor
require "json"
def temp()
number = Dir.glob('/sys/bus/w1/devices/*').count-1
if number>0
@temperatures = Array.new
Dir.glob('/sys/bus/w1/devices/*').each_with_index {|folder, index|
values = Hash.new
foldername = folder.gsub('/sys/bus/w1/devices/','')
if foldername[0,2]=='28'
gettemp = IO.readlines("/sys/bus/w1/devices/#{foldername}/w1_slave") [1]
temp = gettemp[29,32]
temp = temp.insert(2, '.')
temp = temp.gsub("\n","")
values[:id] = foldername
values[:value] = temp
values[:time] = Time.now.strftime "%d/%m/%Y %H:%M:%S"
@temperatures[index] = values
end
}
end
return @temperatures
end
puts temp()
puts temp().to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment