diff --git a/lua_modules/si7021/si7021-example.lua b/lua_modules/si7021/si7021-example.lua new file mode 100644 index 00000000..5c6ed6cc --- /dev/null +++ b/lua_modules/si7021/si7021-example.lua @@ -0,0 +1,24 @@ + +tmr.alarm(0, 60000, 1, function() + + SDA_PIN = 6 -- sda pin, GPIO12 + SCL_PIN = 5 -- scl pin, GPIO14 + + si7021 = require("si7021") + si7021.init(SDA_PIN, SCL_PIN) + si7021.read(OSS) + h = si7021.getHumidity() + t = si7021.getTemperature() + + -- pressure in differents units + print("Humidity: "..(h / 100).."."..(h % 100).." %") + + -- temperature in degrees Celsius and Farenheit + print("Temperature: "..(t/100).."."..(t%100).." deg C") + print("Temperature: "..(9 * t / 500 + 32).."."..(9 * t / 50 % 10).." deg F") + + -- release module + si7021 = nil + package.loaded["si7021"]=nil + +end) diff --git a/lua_modules/si7021/si7021-lewei.lua b/lua_modules/si7021/si7021-lewei.lua new file mode 100644 index 00000000..28ac9189 --- /dev/null +++ b/lua_modules/si7021/si7021-lewei.lua @@ -0,0 +1,41 @@ + + --创建一个定时器 + tmr.alarm(0, 60000, 1, function() + + SDA_PIN = 6 -- sda pin, GPIO12 + SCL_PIN = 5 -- scl pin, GPIO14 + + si7021 = require("si7021") + si7021.init(SDA_PIN, SCL_PIN) + si7021.read(OSS) + Hum = si7021.getHumidity() + Temp = si7021.getTemperature() + + --定义数据变量格式 + PostData = "[{\"Name\":\"T\",\"Value\":\"" .. (Temp/100).."."..(Temp%100) .. "\"},{\"Name\":\"H\",\"Value\":\"" .. (Hum/100).."."..(Hum%100) .. "\"}]" + --创建一个TCP连接 + socket=net.createConnection(net.TCP, 0) + --域名解析IP地址并赋值 + socket:dns("www.lewei50.com", function(conn, ip) + ServerIP = ip + print("Connection IP:" .. ServerIP) + end) + --开始连接服务器 + socket:connect(80, ServerIP) + socket:on("connection", function(sck) end) + --HTTP请求头定义 + socket:send("POST /api/V1/gateway/UpdateSensors/yourID HTTP/1.1\r\n" .. + "Host: www.lewei50.com\r\n" .. + "Content-Length: " .. string.len(PostData) .. "\r\n" .. + "userkey: yourKEY\r\n\r\n" .. + PostData .. "\r\n") + --HTTP响应内容 + socket:on("receive", function(sck, response) + print(response) + end) + + -- release module + si7021 = nil + package.loaded["si7021"]=nil + + end) diff --git a/lua_modules/si7021/si7021.EN.md b/lua_modules/si7021/si7021.EN.md new file mode 100644 index 00000000..983bb978 --- /dev/null +++ b/lua_modules/si7021/si7021.EN.md @@ -0,0 +1,131 @@ +# si7021 module + +##Require +```lua +si7021 = require("si7021") +``` +## Release +```lua +si7021 = nil +package.loaded["si7021"]=nil +``` + +##init() +####Description +Setting the i2c pin of si7021.
+ +####Syntax +init(sda, scl) + +####Parameters +sda: 1~12, IO index.
+scl: 1~12, IO index.
+ +####Returns +nil + +####Example +```lua +si7021 = require("si7021") +gpio5 = 1 +gpio4 = 2 +sda = gpio5 +scl = gpio4 +si7021.init(sda, scl) +-- Don't forget to release it after use +si7021 = nil +package.loaded["si7021"]=nil +``` + +####See also +**-** []() + + +##read() +####Description +Read temperature and humidity from si7021.
+ +####Syntax +read() + +####Parameters +nil.
+ +####Returns +nil(Why?).
+ +####Example +```lua +si7021 = require("si7021") +sda = 1 +scl = 2 +si7021.init(sda, scl) +r = si7021.read() +print(r) +-- Don't forget to release it after use +si7021 = nil +package.loaded["si7021"]=nil +``` + +####See also +**-** []() + + +##getHumidity() +####Description +Get humidity from si7021.
+ +####Syntax +getHumidity() + +####Parameters +nil.
+ +####Returns +h: Integer, humidity from si7021. + +####Example +```lua +si7021 = require("si7021") +sda = 1 +scl = 2 +si7021.init(sda, scl) +h = si7021.getHumidity() +print(h) +-- Don't forget to release it after use +si7021 = nil +package.loaded["si7021"]=nil +``` + +####See also +**-** []() + + +##getTemperature() +####Description +Get temperature from si7021.
+ +####Syntax +getTemperature() + +####Parameters +nil.
+ +####Returns +t: Integer, temperature from si7021. + +####Example +```lua +si7021 = require("si7021") +sda = 1 +scl = 2 +si7021.init(sda, scl) +t = si7021.getTemperature() +print(t) +-- Don't forget to release it after use +si7021 = nil +package.loaded["si7021"]=nil +``` + +####See also +**-** []() diff --git a/lua_modules/si7021/si7021.lua b/lua_modules/si7021/si7021.lua new file mode 100644 index 00000000..161e57d2 --- /dev/null +++ b/lua_modules/si7021/si7021.lua @@ -0,0 +1,104 @@ +-- *************************************************************************** +-- SI7021 module for ESP8266 with nodeMCU +-- Si7021 compatible tested 2015-1-22 +-- +-- Written by VIP6 +-- +-- MIT license, http://opensource.org/licenses/MIT +-- *************************************************************************** + +local moduleName = ... +local M = {} +_G[moduleName] = M + +--I2C slave address of Si70xx +local Si7021_ADDR = 0x40 + +--Commands +local CMD_MEASURE_HUMIDITY_HOLD = 0xE5 +local CMD_MEASURE_HUMIDITY_NO_HOLD = 0xF5 +local CMD_MEASURE_TEMPERATURE_HOLD = 0xE3 +local CMD_MEASURE_TEMPERATURE_NO_HOLD = 0xF3 + + +-- temperature and pressure +local t,h + +local init = false + +-- i2c interface ID +local id = 0 + +-- 16-bit two's complement +-- value: 16-bit integer +local function twoCompl(value) + if value > 32767 then value = -(65535 - value + 1) + end + return value +end + + + +-- read data from si7021 +-- ADDR: slave address +-- commands: Commands of si7021 +-- length: bytes to read +local function read_data(ADDR, commands, length) + i2c.start(id) + i2c.address(id, ADDR, i2c.TRANSMITTER) + i2c.write(id, commands) + i2c.stop(id) + i2c.start(id) + i2c.address(id, ADDR,i2c.RECEIVER) + tmr.delay(20000) + c = i2c.read(id, length) + i2c.stop(id) + return c +end + +-- initialize module +-- sda: SDA pin +-- scl SCL pin +function M.init(sda, scl) + i2c.setup(id, sda, scl, i2c.SLOW) + --print("i2c ok..") + init = true +end + +-- read humidity from si7021 +local function read_humi() + dataH = read_data(Si7021_ADDR, CMD_MEASURE_HUMIDITY_HOLD, 2) + UH = string.byte(dataH, 1) * 256 + string.byte(dataH, 2) + h = ((UH*12500+65536/2)/65536 - 600) + return(h) +end + +-- read temperature from si7021 +local function read_temp() + dataT = read_data(Si7021_ADDR, CMD_MEASURE_TEMPERATURE_HOLD, 2) + UT = string.byte(dataT, 1) * 256 + string.byte(dataT, 2) + t = ((UT*17572+65536/2)/65536 - 4685) + return(t) +end + +-- read temperature and humidity from si7021 +function M.read() + if (not init) then + print("init() must be called before read.") + else + read_humi() + read_temp() + end +end; + +-- get humidity +function M.getHumidity() + return h +end + +-- get temperature +function M.getTemperature() + return t +end + +return M