Skip to content

Instantly share code, notes, and snippets.

@schappim
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save schappim/8731739adf73da0cd014 to your computer and use it in GitHub Desktop.

Select an option

Save schappim/8731739adf73da0cd014 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Dropping out to the shell to: Setup w1-gpio module
`sudo modprobe w1-gpio`
# Dropping out to the shell to: Setup w1-therm module
`sudo modprobe w1-gpio`
# Get a list of files and paths inside our w1 devices directory
# The list of files is returned as an array called paths
paths = Dir["/sys/bus/w1/devices/*"]
# Create our temperature sensor path
# We're selecting all elements in our paths array that include '-'
# We're then selecting the first element of the array with the .first
# We're then appending the /w1_slave string to our path
temperature_sensor_path = paths.select{|path| path.include?('-')}.first + '/w1_slave'
# Open our Temperature Sensor file and read the contents and save it into a string variable called 'contents'
file_contents = File.read(temperature_sensor_path)
# Find the bit our string that includes 't=' and split the string into an array using the 't=' as the delimiter.
# Select the last element of the array and save it as a string variable called temperature_data
temperature_data = file_contents.split('t=').last
# Convert the temperature_data string variable to a float and then divide it by 1000
temperature = temperature_data.to_f / 1000
# Print out the temperature
puts temperature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment