VIP6(xingyuewang,http://517513.cn) commit si7021 module.

This commit is contained in:
HuangRui 2015-01-22 23:30:59 +08:00
parent f09f6443d1
commit 0ea9cb9c01
4 changed files with 300 additions and 0 deletions

View File

@ -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)

View File

@ -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)

View File

@ -0,0 +1,131 @@
# si7021 module
##Require
```lua
si7021 = require("si7021")
```
## Release
```lua
si7021 = nil
package.loaded["si7021"]=nil
```
<a id="si7021_init"></a>
##init()
####Description
Setting the i2c pin of si7021.<br />
####Syntax
init(sda, scl)
####Parameters
sda: 1~12, IO index.<br />
scl: 1~12, IO index.<br />
####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
**-** []()
<a id="si7021_read"></a>
##read()
####Description
Read temperature and humidity from si7021.<br />
####Syntax
read()
####Parameters
nil.<br />
####Returns
nil(Why?).<br />
####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
**-** []()
<a id="si7021_getHumidity"></a>
##getHumidity()
####Description
Get humidity from si7021.<br />
####Syntax
getHumidity()
####Parameters
nil.<br />
####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
**-** []()
<a id="si7021_getTemperature"></a>
##getTemperature()
####Description
Get temperature from si7021.<br />
####Syntax
getTemperature()
####Parameters
nil.<br />
####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
**-** []()

View File

@ -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