From 3c2288cc47dc8234b2428261c74d9e2c8e585352 Mon Sep 17 00:00:00 2001 From: Martin Han Date: Fri, 30 Jan 2015 20:32:46 +0800 Subject: [PATCH 1/8] BH1705 Manual --- lua_modules/bh1705_EN.md | 106 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 lua_modules/bh1705_EN.md diff --git a/lua_modules/bh1705_EN.md b/lua_modules/bh1705_EN.md new file mode 100644 index 00000000..d6032678 --- /dev/null +++ b/lua_modules/bh1705_EN.md @@ -0,0 +1,106 @@ +# BH1705 Module + +##Require +```lua +bh1705 = require("bh1705") +``` +## Release +```lua +bh1705 = nil +package.loaded["bh1705"]=nil +``` + +##init() +####Description +Setting the I2C pin of BH1705.
+ +####Syntax +init(sda, scl) + +####Parameters +sda: 1~12, IO index.
+scl: 1~12, IO index.
+ +####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 +**-** []() + + +##read() +####Description +Read Lux data from bh1705.
+ +####Syntax +read() + +####Parameters +nil.
+ +####Returns +nil.
+ +####Example +```lua +SDA_PIN = 6 -- sda pin, GPIO12 +SCL_PIN = 5 -- scl pin, GPIO14 + +bh1705 = require("bh1705") +bh1705.init(SDA_PIN, SCL_PIN) +bh1705.read() + +-- release module +bh1705 = nil +package.loaded["bh1705"]=nil +``` + +####See also +**-** []() + + +##getlux() +####Description +Get lux from bh1705.
+ +####Syntax +getlux() + +####Parameters +nil.
+ +####Returns +l: Integer, getlux from bh1705. + +####Example +```lua +SDA_PIN = 6 -- sda pin, GPIO12 +SCL_PIN = 5 -- scl pin, GPIO14 + +bh1705 = require("bh1705") +bh1705.init(SDA_PIN, SCL_PIN) +bh1705.read() +l = bh1705.getlux() +print("lux: "..(l / 100).."."..(l % 100).." lx") + +-- release module +bh1705 = nil +package.loaded["bh1705"]=nil +``` + +####See also +**-** []() + From eb55edaba7294585647176e0ef28986e7efc6f44 Mon Sep 17 00:00:00 2001 From: Martin Han Date: Fri, 30 Jan 2015 20:52:59 +0800 Subject: [PATCH 2/8] Revert "BH1705 Manual" This reverts commit 3c2288cc47dc8234b2428261c74d9e2c8e585352. --- lua_modules/bh1705_EN.md | 106 --------------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 lua_modules/bh1705_EN.md diff --git a/lua_modules/bh1705_EN.md b/lua_modules/bh1705_EN.md deleted file mode 100644 index d6032678..00000000 --- a/lua_modules/bh1705_EN.md +++ /dev/null @@ -1,106 +0,0 @@ -# BH1705 Module - -##Require -```lua -bh1705 = require("bh1705") -``` -## Release -```lua -bh1705 = nil -package.loaded["bh1705"]=nil -``` - -##init() -####Description -Setting the I2C pin of BH1705.
- -####Syntax -init(sda, scl) - -####Parameters -sda: 1~12, IO index.
-scl: 1~12, IO index.
- -####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 -**-** []() - - -##read() -####Description -Read Lux data from bh1705.
- -####Syntax -read() - -####Parameters -nil.
- -####Returns -nil.
- -####Example -```lua -SDA_PIN = 6 -- sda pin, GPIO12 -SCL_PIN = 5 -- scl pin, GPIO14 - -bh1705 = require("bh1705") -bh1705.init(SDA_PIN, SCL_PIN) -bh1705.read() - --- release module -bh1705 = nil -package.loaded["bh1705"]=nil -``` - -####See also -**-** []() - - -##getlux() -####Description -Get lux from bh1705.
- -####Syntax -getlux() - -####Parameters -nil.
- -####Returns -l: Integer, getlux from bh1705. - -####Example -```lua -SDA_PIN = 6 -- sda pin, GPIO12 -SCL_PIN = 5 -- scl pin, GPIO14 - -bh1705 = require("bh1705") -bh1705.init(SDA_PIN, SCL_PIN) -bh1705.read() -l = bh1705.getlux() -print("lux: "..(l / 100).."."..(l % 100).." lx") - --- release module -bh1705 = nil -package.loaded["bh1705"]=nil -``` - -####See also -**-** []() - From 555d64af2a5ffd16702ef46c465e9abb06fe20d3 Mon Sep 17 00:00:00 2001 From: Martin Han Date: Fri, 30 Jan 2015 21:20:30 +0800 Subject: [PATCH 3/8] BH1750 Module, Written by xiaohu --- lua_modules/bh1750/bh1750.lua | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 lua_modules/bh1750/bh1750.lua diff --git a/lua_modules/bh1750/bh1750.lua b/lua_modules/bh1750/bh1750.lua new file mode 100644 index 00000000..99131131 --- /dev/null +++ b/lua_modules/bh1750/bh1750.lua @@ -0,0 +1,54 @@ +-- *************************************************************************** +-- 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 + 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) + UT = string.byte(dataT, 1) * 256 + string.byte(dataT, 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 From fb10825c1325886bf2e7829e6f4e0a663d9f7139 Mon Sep 17 00:00:00 2001 From: Martin Han Date: Fri, 30 Jan 2015 21:21:18 +0800 Subject: [PATCH 4/8] BH1750 User's Manual --- lua_modules/bh1750/bh1750_EN.md | 105 ++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 lua_modules/bh1750/bh1750_EN.md diff --git a/lua_modules/bh1750/bh1750_EN.md b/lua_modules/bh1750/bh1750_EN.md new file mode 100644 index 00000000..5e2a5ece --- /dev/null +++ b/lua_modules/bh1750/bh1750_EN.md @@ -0,0 +1,105 @@ +# bh1750 Module + +##Require +```lua +bh1750 = require("bh1750") +``` +## Release +```lua +bh1750 = nil +package.loaded["bh1750"]=nil +``` + +##init() +####Description +Setting the I2C pin of bh1750.
+ +####Syntax +init(sda, scl) + +####Parameters +sda: 1~12, IO index.
+scl: 1~12, IO index.
+ +####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 +**-** []() + + +##read() +####Description +Read Lux data from bh1750.
+ +####Syntax +read() + +####Parameters +nil.
+ +####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) +bh1750.read() + +-- release module +bh1750 = nil +package.loaded["bh1750"]=nil +``` + +####See also +**-** []() + + +##getlux() +####Description +Get lux from bh1750.
+ +####Syntax +getlux() + +####Parameters +nil.
+ +####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 +**-** []() From 6aca8daf16420766f2b197d16e7180398d9b73af Mon Sep 17 00:00:00 2001 From: Martin Han Date: Fri, 30 Jan 2015 21:24:54 +0800 Subject: [PATCH 5/8] Print LUX Data Every 10s --- lua_modules/bh1750/bh1750_Example1.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lua_modules/bh1750/bh1750_Example1.lua diff --git a/lua_modules/bh1750/bh1750_Example1.lua b/lua_modules/bh1750/bh1750_Example1.lua new file mode 100644 index 00000000..911aa8fc --- /dev/null +++ b/lua_modules/bh1750/bh1750_Example1.lua @@ -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) From de128ea88c1d1a0209397965b2f17904059349f2 Mon Sep 17 00:00:00 2001 From: Martin Han Date: Fri, 30 Jan 2015 21:28:31 +0800 Subject: [PATCH 6/8] UPDATA TO LELIAN --- lua_modules/bh1750/bh1750_Example2.lua | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lua_modules/bh1750/bh1750_Example2.lua diff --git a/lua_modules/bh1750/bh1750_Example2.lua b/lua_modules/bh1750/bh1750_Example2.lua new file mode 100644 index 00000000..48acceac --- /dev/null +++ b/lua_modules/bh1750/bh1750_Example2.lua @@ -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_GATEWAY(Lelian's Device ID),USERKEY(Lelian'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) + + + From ad1ec8fb6d0d2e95b5b500397c939d19b76723b4 Mon Sep 17 00:00:00 2001 From: Martin Han Date: Fri, 30 Jan 2015 21:42:23 +0800 Subject: [PATCH 7/8] =?UTF-8?q?BH1750=E4=B8=AD=E6=96=87=E6=89=8B=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua_modules/bh1750/bh1750_CN.md | 104 ++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 lua_modules/bh1750/bh1750_CN.md diff --git a/lua_modules/bh1750/bh1750_CN.md b/lua_modules/bh1750/bh1750_CN.md new file mode 100644 index 00000000..42815985 --- /dev/null +++ b/lua_modules/bh1750/bh1750_CN.md @@ -0,0 +1,104 @@ +# BH1750 模块 + +##引用 +```lua +bh1750 = require("bh1750") +``` +## 释放 +```lua +bh1750 = nil +package.loaded["bh1750"]=nil +``` + +##init() +####描述 +设置BH1750所在的I2C引脚
+ +####语法 +init(sda, scl) + +####参数 +sda: 1~12, IO index.
+scl: 1~12, IO index.
+ +####返回值 +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 +``` + +####参见 +**-** []() + + +##read() +####描述 +从bh1750中读取光线传感器数据(Lux勒克斯).
+ +####语法 +read() + +####参数 +nil.
+ +####返回值 +nil.
+ +####示例 +```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 +``` + +####参见 +**-** []() + + +##getlux() +####描述 +从BH1750中提取数据.
+ +####语法 +getlux() + +####参数 +nil.
+ +####返回值 +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 +``` + +####参见 +**-** []() From f45e6fece3d135526a1de5311f1b09b3ee42c81f Mon Sep 17 00:00:00 2001 From: HuangRui Date: Fri, 30 Jan 2015 22:21:44 +0800 Subject: [PATCH 8/8] Followed Vladimir Dronnikov's advices. https://github.com/nodemcu/nodemcu-firmware/pull/156 --- lua_modules/bh1750/bh1750.lua | 93 ++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 45 deletions(-) diff --git a/lua_modules/bh1750/bh1750.lua b/lua_modules/bh1750/bh1750.lua index 99131131..71e24c30 100644 --- a/lua_modules/bh1750/bh1750.lua +++ b/lua_modules/bh1750/bh1750.lua @@ -7,48 +7,51 @@ -- 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 - 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) - UT = string.byte(dataT, 1) * 256 + string.byte(dataT, 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 +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