Created
October 22, 2020 06:17
-
-
Save stereosupersonic/abf70de68b3397bae8486ad650ba5dd5 to your computer and use it in GitHub Desktop.
read_sensor
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
#!/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