Skip to content

Instantly share code, notes, and snippets.

@feuerrot feuerrot/ds1621.lua
Last active Jan 5, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.