-
Notifications
You must be signed in to change notification settings - Fork 3
/
tmp102.lua
47 lines (42 loc) · 1.2 KB
/
tmp102.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
------------------------------------------------------------------------------
-- TMP102 query module
--
-- LICENCE: http://opensource.org/licenses/MIT
-- Andy Coulson <[email protected]>
-- Heavily based on work of Christee <[email protected]>
--
-- Example:
-- id=0
-- sda=5 ESP8266 GPIO14
-- scl=6 ESP8266 GPIO12
-- dofile("tmp102.lua").read(id, sda, scl)
------------------------------------------------------------------------------
local M
do
-- cache
local i2c, tmr = i2c, tmr
local TMP102_ADDRESS = 0x48
local read = function(id, sda, scl)
i2c.setup(id, sda, scl, i2c.SLOW)
i2c.address(id, TMP102_ADDRESS, i2c.TRANSMITTER)
i2c.write(id, 0) -- use register 0
i2c.stop(id)
tmr.delay(1000)
i2c.start(id)
i2c.address(id, TMP102_ADDRESS, i2c.RECEIVER)
local r = i2c.read(id, 2)
i2c.stop(id)
local msb = r:byte(1) -- receive high byte
local lsb = r:byte(2) -- receive low byte
local tempval = bit.rshift(bit.bor(bit.lshift(msb, 8), lsb), 4)
--print("tempval: " .. tempval)
local f = (tempval / 16) * 9 / 5 + 32 -- convert to fahrenheit
--print("celsius: " .. celsius)
return f
end
-- expose
M = {
read = read,
}
end
return M