Skip to content

Instantly share code, notes, and snippets.

@stereosupersonic
Created October 22, 2020 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stereosupersonic/abf70de68b3397bae8486ad650ba5dd5 to your computer and use it in GitHub Desktop.
Save stereosupersonic/abf70de68b3397bae8486ad650ba5dd5 to your computer and use it in GitHub Desktop.
read_sensor
#!/usr/bin/env ruby
MAX_RETRY = 3
PATH = "/sys/bus/iio/devices/"
TEMP_SENSOR = PATH + "iio\:device#{ARGV[0].to_i}/in_temp_input"
HUMIDITY_SENSOR = PATH + "iio\:device#{ARGV[0].to_i}/in_humidityrelative_input"
def save_read(sensor)
run = 1
while run < MAX_RETRY
x = `cat #{sensor} 2>/dev/null`
if x == ''
run += 1
@retries += 1
else
return (x.to_i / 1000).to_f
end
end
end
@retries = 0
temperatur = save_read TEMP_SENSOR
humidity = save_read HUMIDITY_SENSOR
if temperatur
puts "#{temperatur&.round(2)}C - #{humidity&.round(2)}% - retries: #{@retries}"
else
puts "ERROR - retries: #{@retries}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment