chetan / dht-sensor-ffi

A ruby library to read from a DHT-XX type humidity/temperature sensor
MIT License
26 stars 6 forks source link

Is DHT-Sensor-ffi able to read DS18B20 heat sensor ? #2

Closed Damaotimlin closed 8 years ago

Damaotimlin commented 8 years ago

Hi again @chetan ,

ght-sensor-ffi is great !

I'm now testing reading DS18B20( https://www.adafruit.com/product/381 ) heat sensor and push data to Dashing dashboard.Which I did it to DHT11 and use your gem.

But it is not something my knowledge able to push the data with out your gem. Can you give me some direction and let me know how to, maybe modify the code(s), achieve the goal ?

My Ruby code is okay to read the temp run in terminal.

def gettemp(id)
   filename = 'w1_slave'
   filepath = File.new("/sys/bus/w1/devices/#{id}/#{filename}", "w").path

  # “File” belongs to IO class, which contain I/O stream. http://ruby-doc.org/core-2.0.0/IO.html

  File.open(filepath) { |f|
    lines_arr = f.read.lines.to_a
    lines_arr.shift(1) # Let lines_arr takeout first object; second_line_s = lines_arr.to_s # Just for noted
    temp_arr = lines_arr.to_s.partition(/.t=/) # saperate the ["xx xx xx xx xx t=", "34.xxxx"]  
    @temp = temp_arr.last.to_f/1000
  }
  return @temp
end

# Use for startup the file 
if __FILE__ == $0 then
  puts "Temp : #{gettemp('28-031581efbaff'})
end

http://i.stack.imgur.com/njhxv.png

But I don't know how to make it able to call the value while using Dashing*Sinatra. I think it is unable to fire the local file and read the out put.

# For reading temp/humidity sensor DHT11 gem
require 'dht-sensor-ffi'
# For reading soil remp sensor        
def get_soil_temp(id)
    filename = 'w1_slave'
    filepath = File.new("/sys/bus/w1/devices/#{id}/#{filename}", "w").path
    File.open(filepath) { |f|
        lines_arr = f.read.lines.to_a 
        lines_arr.shift(1) 
        temp_arr = lines_arr.to_s.partition(/.t=/) 
        @temp = temp_arr.last.to_f/1000
    }
    return @temp
end
soil_temp_sensor_id = '28-031581efbaff'

current_air_temp = 0
current_air_humid = 0
current_soil_temp = 0

SCHEDULER.every '5s' do
  last_air_temp = current_air_temp
  last_air_humid = current_air_humid
  sensor_val = DhtSensor.read(17, 11)
  current_air_temp = sensor_val.temp
  current_air_humid = sensor_val.humidity

  if __FILE__ == $0 then
    current_soil_temp = get_soil_temp(soil_temp_sensor_id)
  end

  send_event('air_temp', { current: current_air_temp, last: last_air_temp })
  send_event('air_humid', { current: current_air_humid, last: last_air_humid })
  send_event('soil_temp',  value: current_soil_temp)
end

Do you have any hint ?

Please advise and thank you in advance !

P.S I'm new to RPi and also new to coding. Since I'm self taught to learn coding about 9 months, I choose Ruby as my major programing language. After some experience of Swift, JavaScript and some Python, I really want to stay in Ruby world. But it make me very frustration since few Ruby coder playing around RPi and seldom materials which able to supporting GPIO read and control. Really needs your guide, thanks!

chetan commented 8 years ago

Are you running your app as root? My temp sensor required it. Not sure about the one to have. 

On Mon, Mar 7, 2016 at 10:02 PM -0800, "Damao Tim Lin" notifications@github.com wrote:

Hi again @chetan ,

ght-sensor-ffi is great !

I'm now testing reading DS18B20( https://www.adafruit.com/product/381 ) heat sensor and push data to Dashing dashboard.Which I did it to DHT11 and use your gem.

But it is not something my knowledge able to push the data with out your gem.

Can you give me some direction and let me know how to, maybe modify the code(s), achieve the goal ?

My Ruby code is okay to read the temp run in terminal.

`def gettemp(id)

filename = 'w1_slave'

filepath = File.new("/sys/bus/w1/devices/#{id}/#{filename}", "w").path

“File” belongs to IO class, which contain I/O stream. http://ruby-doc.org/core-2.0.0/IO.html

File.open(filepath) { |f|

lines_arr = f.read.lines.to_a

lines_arr.shift(1) # Let lines_arr takeout first object; second_line_s = lines_arr.to_s # Just for noted

temp_arr = lines_arr.to_s.partition(/.t=/) # saperate the ["xx xx xx xx xx t=", "34.xxxx"]

@temp = temp_arr.last.to_f/1000

}

return @temp

end

Use for startup the file

if FILE == $0 then

puts "Temp : #{gettemp('28-031581efbaff'})

end`

http://i.stack.imgur.com/njhxv.png

But I don't know how to make it able to call the value while using Dashing*Sinatra.

I think it is unable to fire the local file and read the out put.

`# For reading temp/humidity sensor DHT11 gem

require 'dht-sensor-ffi'

For reading soil remp sensor

def get_soil_temp(id)

filename = 'w1_slave'

filepath = File.new("/sys/bus/w1/devices/#{id}/#{filename}", "w").path

File.open(filepath) { |f|

    lines_arr = f.read.lines.to_a 

    lines_arr.shift(1) 

    temp_arr = lines_arr.to_s.partition(/.t=/) 

    @temp = temp_arr.last.to_f/1000

}

return @temp

end

soil_temp_sensor_id = '28-031581efbaff'

current_air_temp = 0

current_air_humid = 0

current_soil_temp = 0

SCHEDULER.every '5s' do

last_air_temp = current_air_temp

last_air_humid = current_air_humid

sensor_val = DhtSensor.read(17, 11)

current_air_temp = sensor_val.temp

current_air_humid = sensor_val.humidity

if FILE == $0 then

current_soil_temp = get_soil_temp(soil_temp_sensor_id)

end

send_event('air_temp', { current: current_air_temp, last: last_air_temp })

send_event('air_humid', { current: current_air_humid, last: last_air_humid })

send_event('soil_temp', value: current_soil_temp)

end`

Do you have any hint ?

Please advise and thank you in advance !

P.S I'm new to RPi and also new to coding. Since I'm self taught to learn coding about 9 months, I choose Ruby as my major programing language. After some experience of Swift, JavaScript and some Python, I really want to stay in ruby world. But it is make me very frustration since few Ruby coder playing around RPi and seldom materials to supporting. Really needs your guide, thanks!

— Reply to this email directly or view it on GitHub.