require "al_worker" require "al_worker_timer" require "i2c" require "net/http" require "uri" require "json" ADRS = 0x5c # LPS25H NODE = "/dev/i2c-1" # I2C device node POST_URI = "http://192.168.1.11/cgi-bin/index.rb?ctrl=data_post" def to_int16( b1, b2 ) return (b1 << 8 | b2) - ((b1 & 0x80) << 9) end def to_uint24( b1, b2, b3 ) return (b1 << 16) | (b2 << 8) | b3 end class Worker1 < AlWorker def initialize2() @i2c = I2C.create(NODE) @i2c.write(ADRS, 0x20, 0x90) @timer = Timer.periodic( 60 ) @timer.run() { d = get_data() send_data( d ) } end def get_data() ret = {} buf = @i2c.read(ADRS, 5, 0xa8) p_cnt = to_uint24(buf.getbyte(2), buf.getbyte(1), buf.getbyte(0)) ret[:pressure] = p_cnt.to_f / 4096 t_cnt = to_int16(buf.getbyte(4), buf.getbyte(3)) ret[:temperature] = 42.5 + t_cnt.to_f / 480 return ret end def send_data( data ) AlWorker.log( data ) Net::HTTP.post_form(URI.parse(POST_URI), {"data"=>data.to_json}) end end worker1 = Worker1.new() worker1.daemon()