Skip to content

Instantly share code, notes, and snippets.

@feuerrot
Last active January 5, 2017 22:44
Show Gist options
  • Save feuerrot/41617ccf3f44ba7c6926b8202b9fd22e to your computer and use it in GitHub Desktop.
Save feuerrot/41617ccf3f44ba7c6926b8202b9fd22e to your computer and use it in GitHub Desktop.
linker Kühlschrank, Temperatursensor
id = 0
sda = 7
scl = 6
m = mqtt.Client("kuehlschrank", 120, nil, nil)
i2c.setup(id, sda, scl, i2c.SLOW)
function mqtt_init()
print("mqtt_init()")
m:on("connect", function(client) print("subscribed") tmr.start(0) end )
m:connect("172.22.26.64")
end
function wlan_init()
print("wlan_init()")
wifi.sta.eventMonReg(wifi.STA_GOTIP, function() mqtt_init() end)
wifi.setmode(wifi.STATION)
wifi.sta.config("chaosdorf-legacy", "7Aaagghnox")
wifi.sta.eventMonStart()
print(wifi.sta.getip())
end
function write_command(dev_addr, reg_addr)
i2c.start(id)
i2c.address(id, dev_addr, i2c.TRANSMITTER)
i2c.write(id, reg_addr)
i2c.stop(id)
end
function read_command(dev_addr, reg_addr)
i2c.start(id)
i2c.address(id, dev_addr, i2c.TRANSMITTER)
i2c.write(id, reg_addr)
i2c.stop(id)
i2c.start(id)
i2c.address(id, dev_addr, i2c.RECEIVER)
local a = i2c.read(id, 1)
i2c.stop(id)
return a
end
local function twoCompl(value)
local a = string.byte(value)
if a > 127 then
a = -(255 - a + 1)
end
return a
end
function gettemp()
local temp = twoCompl(read_command(0x48, 0xAA)) * 10000
local cperc = string.byte(read_command(0x48, 0xA9))
local cremain = string.byte(read_command(0x48, 0xA8))
local result = temp - 2500 + (cperc*10000 - cremain*10000)/cperc
return string.format("%d.%04d", result/10000, result%10000)
end
function ds1621_senddata()
m:publish("sensors/lfridge/temperature", gettemp(), 0, 0)
end
write_command(0x48, 0xEE)
wlan_init()
tmr.register(0, 1000, tmr.ALARM_AUTO, ds1621_senddata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment