Merge pull request #156 from MarsTechHAN/master

BH1750 Module merge
This commit is contained in:
Vowstar 2015-01-30 22:29:51 +08:00
commit e03807867b
5 changed files with 341 additions and 0 deletions

View File

@ -0,0 +1,57 @@
-- ***************************************************************************
-- BH1750 module for ESP8266 with nodeMCU
-- BH1750 compatible tested 2015-1-22
--
-- Written by xiaohu
--
-- MIT license, http://opensource.org/licenses/MIT
-- ***************************************************************************
local moduleName = ...
local M = {}
_G[moduleName] = M
--I2C slave address of GY-30
local GY_30_address = 0x23
-- i2c interface ID
local id = 0
--LUX
local l
--CMD
local CMD = 0x10
local init = false
--Make it more faster
local i2c = i2c
function M.init(sda, scl)
i2c.setup(id, sda, scl, i2c.SLOW)
--print("i2c ok..")
init = true
end
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(200000)
c = i2c.read(id, length)
i2c.stop(id)
return c
end
local function read_lux()
dataT = read_data(GY_30_address, CMD, 2)
--Make it more faster
UT = dataT:byte(1) * 256 + dataT:byte(2)
l = (UT*1000/12)
return(l)
end
function M.read()
if (not init) then
print("init() must be called before read.")
else
read_lux()
end
end
function M.getlux()
return l
end
return M

View File

@ -0,0 +1,104 @@
# BH1750 模块
##引用
```lua
bh1750 = require("bh1750")
```
## 释放
```lua
bh1750 = nil
package.loaded["bh1750"]=nil
```
<a id="bh1750_init"></a>
##init()
####描述
设置BH1750所在的I2C引脚<br />
####语法
init(sda, scl)
####参数
sda: 1~12, IO index.<br />
scl: 1~12, IO index.<br />
####返回值
nil
####示例
```lua
SDA_PIN = 6 -- sda pin, GPIO12
SCL_PIN = 5 -- scl pin, GPIO14
bh1750 = require("bh1750")
bh1750.init(SDA_PIN, SCL_PIN)
-- release module
bh1750 = nil
package.loaded["bh1750"]=nil
```
####参见
**-** []()
<a id="bh1750_read"></a>
##read()
####描述
从bh1750中读取光线传感器数据Lux勒克斯.<br />
####语法
read()
####参数
nil.<br />
####返回值
nil.<br />
####示例
```lua
SDA_PIN = 6 -- sda pin, GPIO12
SCL_PIN = 5 -- scl pin, GPIO14
bh1750 = require("bh1750")
bh1750.init(SDA_PIN, SCL_PIN)
bh1750.read()
-- release module
bh1750 = nil
package.loaded["bh1750"]=nil
```
####参见
**-** []()
<a id="bh1750_getlux"></a>
##getlux()
####描述
从BH1750中提取数据.<br />
####语法
getlux()
####参数
nil.<br />
####返回值
l: 整数Lux计数
####示例
```lua
SDA_PIN = 6 -- sda pin, GPIO12
SCL_PIN = 5 -- scl pin, GPIO14
bh1750 = require("bh1750")
bh1750.init(SDA_PIN, SCL_PIN)
bh1750.read()
l = bh1750.getlux()
print("lux: "..(l / 100).."."..(l % 100).." lx")
-- release module
bh1750 = nil
package.loaded["bh1750"]=nil
```
####参见
**-** []()

View File

@ -0,0 +1,105 @@
# bh1750 Module
##Require
```lua
bh1750 = require("bh1750")
```
## Release
```lua
bh1750 = nil
package.loaded["bh1750"]=nil
```
<a id="bh1750_init"></a>
##init()
####Description
Setting the I2C pin of bh1750.<br />
####Syntax
init(sda, scl)
####Parameters
sda: 1~12, IO index.<br />
scl: 1~12, IO index.<br />
####Returns
nil
####Example
```lua
SDA_PIN = 6 -- sda pin, GPIO12
SCL_PIN = 5 -- scl pin, GPIO14
bh1750 = require("bh1750")
bh1750.init(SDA_PIN, SCL_PIN)
-- release module
bh1750 = nil
package.loaded["bh1750"]=nil
```
####See also
**-** []()
<a id="bh1750_read"></a>
##read()
####Description
Read Lux data from bh1750.<br />
####Syntax
read()
####Parameters
nil.<br />
####Returns
nil.<br />
####Example
```lua
SDA_PIN = 6 -- sda pin, GPIO12
SCL_PIN = 5 -- scl pin, GPIO14
bh1750 = require("bh1750")
bh1750.init(SDA_PIN, SCL_PIN)
bh1750.read()
-- release module
bh1750 = nil
package.loaded["bh1750"]=nil
```
####See also
**-** []()
<a id="bh1750_getlux"></a>
##getlux()
####Description
Get lux from bh1750.<br />
####Syntax
getlux()
####Parameters
nil.<br />
####Returns
l: Integer, getlux from bh1750.
####Example
```lua
SDA_PIN = 6 -- sda pin, GPIO12
SCL_PIN = 5 -- scl pin, GPIO14
bh1750 = require("bh1750")
bh1750.init(SDA_PIN, SCL_PIN)
bh1750.read()
l = bh1750.getlux()
print("lux: "..(l / 100).."."..(l % 100).." lx")
-- release module
bh1750 = nil
package.loaded["bh1750"]=nil
```
####See also
**-** []()

View File

@ -0,0 +1,24 @@
-- ***************************************************************************
-- BH1750 Example Program for ESP8266 with nodeMCU
-- BH1750 compatible tested 2015-1-30
--
-- Written by xiaohu
--
-- MIT license, http://opensource.org/licenses/MIT
-- ***************************************************************************
tmr.alarm(0, 10000, 1, function()
SDA_PIN = 6 -- sda pin, GPIO12
SCL_PIN = 5 -- scl pin, GPIO14
bh1750 = require("bh1750")
bh1750.init(SDA_PIN, SCL_PIN)
bh1750.read(OSS)
l = bh1750.getlux()
print("lux: "..(l / 100).."."..(l % 100).." lx")
-- release module
bh1750 = nil
package.loaded["bh1750"]=nil
end)

View File

@ -0,0 +1,51 @@
-- ***************************************************************************
-- BH1750 Example Program for ESP8266 with nodeMCU
-- BH1750 compatible tested 2015-1-30
--
-- Written by xiaohu
--
-- MIT license, http://opensource.org/licenses/MIT
-- ***************************************************************************
--Updata to Lelian
--Ps 需要改动的地方LW_GATEWAY乐联的设备标示USERKEY乐联userkey
--Ps You nees to rewrite the LW_GATEWAYLelian's Device IDUSERKEYLelian's userkey
tmr.alarm(0, 60000, 1, function()
SDA_PIN = 6 -- sda pin, GPIO12
SCL_PIN = 5 -- scl pin, GPIO14
BH1750 = require("BH1750")
BH1750.init(SDA_PIN, SCL_PIN)
BH1750.read(OSS)
l = BH1750.getlux()
--定义数据变量格式 Define the veriables formate
PostData = "[{\"Name\":\"T\",\"Value\":\"" ..(l / 100).."."..(l % 100).."\"}]"
--创建一个TCP连接 Create a TCP Connection
socket=net.createConnection(net.TCP, 0)
--域名解析IP地址并赋值 DNS...it
socket:dns("www.lewei50.com", function(conn, ip)
ServerIP = ip
print("Connection IP:" .. ServerIP)
end)
--开始连接服务器 Connect the sever
socket:connect(80, ServerIP)
socket:on("connection", function(sck) end)
--HTTP请求头定义 HTTP Head
socket:send("POST /api/V1/gateway/UpdateSensors/LW_GATEWAY HTTP/1.1\r\n" ..
"Host: www.lewei50.com\r\n" ..
"Content-Length: " .. string.len(PostData) .. "\r\n" ..
"userkey: USERKEY\r\n\r\n" ..
PostData .. "\r\n")
--HTTP响应内容 Print the HTTP response
socket:on("receive", function(sck, response)
print(response)
end)
end)