SHT21 humidity sensor module
Posted: Wed Mar 09, 2016 7:39 am
Hi, Im trying to make a module out of working piece of LUA script, getting error:
SHT21_usage.lua:6: attempt to call field 'Init'
and usage:
where is the problem? The same structure works for other modules.
Thanks
SHT21_usage.lua:6: attempt to call field 'Init'
Code: Select all
local moduleName = ...
local M = {}
_G[moduleName] = M
local pinSDA = 6
local pinSCL = 7
local SHTAddr = 0x40
local id = 0
local eTempHoldCmd = 0xE3
local eRHumidityHoldCmd = 0xE5
local eTempNoHoldCmd = 0xF3
local eRHumidityNoHoldCmd = 0xF5
function M.Init(Ide,SDA,SCL)
return i2c.setup(Ide, pinSDA, pinSCL, i2c.SLOW)
end
function M.Read_User_Reg(Ide)
i2c.start(Ide)
i2c.address(Ide, SHTAddr ,i2c.TRANSMITTER)
i2c.write(Ide,0xE7)
i2c.stop(Ide)
i2c.start(Ide)
i2c.address(Ide, SHTAddr,i2c.RECEIVER)
--tmr.delay(1000)
c=i2c.read(Ide,1)
i2c.stop(Ide)
return string.byte(c)
end
function M.Read_Temp(Ide)
i2c.start(Ide)
i2c.address(Ide, SHTAddr ,i2c.TRANSMITTER)
i2c.write(Ide,eTempNoHoldCmd)
i2c.stop(Ide)
tmr.delay(100000)
i2c.start(Ide)
i2c.address(Ide, SHTAddr,i2c.RECEIVER)
c=i2c.read(Ide,3)
i2c.stop(Ide)
--print("Raw",string.byte(c,1,2))
d=bit.clear(bit.lshift(string.byte(c,1),8)+string.byte(c,2),1,3)
return (((d*17572)/65536)-4685)/100;
end
function M.Read_Hum(Ide)
i2c.start(Ide)
i2c.address(Ide, SHTAddr ,i2c.TRANSMITTER)
i2c.write(Ide,eRHumidityNoHoldCmd)
i2c.stop(Ide)
tmr.delay(50000)
i2c.start(Ide)
i2c.address(Ide, SHTAddr,i2c.RECEIVER)
c=i2c.read(Ide,3)
i2c.stop(Ide)
--print("Raw",string.byte(c,1,2))
d=bit.clear(bit.lshift(string.byte(c,1),8)+string.byte(c,2),1,3)
return (((d*12500)/65536)-600)/100
end
--print("Speed ",Init(id,pinSDA, pinSCL))
--print("User reg",Read_User_Reg(id))
--print(string.format("%02.2f",Read_Temp(id)),"deg C")
--print(string.format("%02.2f",Read_Hum(id)),"RH")
return M
and usage:
Code: Select all
SHT21 = require("sht21_2")
pinSDA = 6
pinSCL = 7
id = 0
SHT21.Init(id,pinSDA, pinSCL)
print("Speed ",SHT21.Init(id,pinSDA, pinSCL))
print("User reg",SHT21.Read_User_Reg(id))
print(string.format("%02.2f",SHT21.Read_Temp(id)),"deg C")
print(string.format("%02.2f",SHT21.Read_Hum(id)),"RH")
SHT21 = nil
package.loaded["sht21_2"]=nil
where is the problem? The same structure works for other modules.
Thanks