当前位置:网站首页>[luatos sensor] 1 light sensing bh1750
[luatos sensor] 1 light sensing bh1750
2022-07-03 04:37:00 【Birds hate fish】
1 Preface
Sort out the codes of several sensors .
BMP pressure :https://doc.openluat.com/wiki/21?wiki_page_id=2729
Compass :https://doc.openluat.com/wiki/21?wiki_page_id=2750
The light intensity :https://doc.openluat.com/wiki/21?wiki_page_id=2668
color :https://doc.openluat.com/wiki/21?wiki_page_id=2746
This chapter is about the light intensity sensor BH1750.
2 Hardware connection


esp32 Only one... Is supported i2c,id by 0
View source code :https://gitee.com/dreamcmi/LuatOS-ESP32/blob/master/components/luat/port/luat_i2c_esp32.c
3 Official instance code
PROJECT = "sensor"
VERSION = "1.0.0"
require "log"
require "sys"
require "misc"
-- i2c ID
i2cid = 2
-- i2c rate
speed = 100000
-- initialization
function init(address)
if i2c.setup(i2cid, speed, -1, 1) ~= speed then
log.error("i2c", "setup fail", addr)
return
end
addr = address
end
-- Reading data
function send(...)
sys.wait(10)
if not addr then
log.info("i2c", "addr err")
return
end
local t = {
...}
if i2c.send(i2cid, addr, t) ~= #t then
log.error("i2c", "send fail", #t)
return
end
return true
end
-- send data
function read(n)
sys.wait(10)
if not addr then
log.info("i2c", "addr err")
return "\x00"
end
val = i2c.recv(i2cid, addr, n)
if val and #val>0 then
return val
end
return "\x00"
end
-- Color recognition sensor
function TCS34725()
init(0x29)
send(0x83, 0xff)
send(0x81, 0x00)
send(0x8f, 0x00)
send(0x80, 0x03)
sys.wait(800)
while true do
sys.wait(1000)
send(0x94)
_, c, red, green, blue = pack.unpack(read(8), "<HHHH")
if red and green and blue then
lux = (-0.32466 * red) + (1.57837 * green) + (-0.73191 * blue)
log.info("red", red)
log.info("green", green)
log.info("blue", blue)
log.info("c, lux", c, lux)
else
log.info("TCS34725", "err")
end
end
end
sys.taskInit(function()
sys.wait(3000)
TCS34725()
end)
sys.init(0, 0)
sys.run()
4 Change code
sen_BH1750.lua
--- Module function :BH1750
-- @module BH1750
-- @author Dozingfiretruck ,youkai
-- @license MIT
-- @copyright OpenLuat.com
-- @release 2021.03.14
-- Source code https://doc.openluat.com/wiki/21?wiki_page_id=2668
BH1750_i2c_id = 0 --BH1750_i2c_id esp32 i2c 0
BH1750_ADDRESS_AD0_LOW = 0x23 -- address pin low (GND), default for InvenSense evaluation board
BH1750_ADDRESS_AD0_HIGH = 0x24 -- address pin high (VCC)
BH1750_i2c_slaveaddr = BH1750_ADDRESS_AD0_LOW -- ADDRES
-- BH1750 registers define
BH1750_POWER_DOWN = 0x00 -- power down
BH1750_POWER_ON = 0x01 -- power on
BH1750_RESET = 0x07 -- reset
BH1750_CON_H_RES_MODE = 0x10 -- Continuously H-Resolution Mode
BH1750_CON_H_RES_MODE2 = 0x11 -- Continuously H-Resolution Mode2
BH1750_CON_L_RES_MODE = 0x13 -- Continuously L-Resolution Mode
BH1750_ONE_H_RES_MODE = 0x20 -- One Time H-Resolution Mode
BH1750_ONE_H_RES_MODE2 = 0x21 -- One Time H-Resolution Mode2
BH1750_ONE_L_RES_MODE = 0x23 -- One Time L-Resolution Mode
local function BH1750_i2c_send(data)
i2c.send(BH1750_i2c_id, BH1750_i2c_slaveaddr, data)
end
local function BH1750_i2c_recv(num)
revData = i2c.recv(BH1750_i2c_id, BH1750_i2c_slaveaddr, num)
return revData
end
local function BH1750_power_on()
BH1750_i2c_send(BH1750_POWER_ON)
end
local function BH1750_power_down()
BH1750_i2c_send(BH1750_POWER_DOWN)
end
local function BH1750_set_measure_mode(mode,time)
BH1750_i2c_send(BH1750_RESET)
BH1750_i2c_send(mode)
sys.wait(time)
end
local function BH1750_read_light()
BH1750_set_measure_mode(BH1750_CON_H_RES_MODE2, 180)
_,light = pack.unpack(BH1750_i2c_recv(2),">h")
light = light / 1.2
return light;
end
function BH1750_init(t_spi_id)
BH1750_i2c_id = t_spi_id
log.info("BH1750_i2c_id",BH1750_i2c_id)
if i2c.setup(t_spi_id,i2c.SLOW) ~= i2c.SLOW then
log.error("I2c.init","fail")
return
else
print("BH1750_init ok.")
end
BH1750_power_on()
end
function BH1750_get()
log.info("BH1750_read_light", BH1750_read_light()*10)
sys.wait(100)
end
main.lua
PROJECT = "sensor"
VERSION = "1.0.0"
_G.sys = require("sys")
require("sen_BH1750") -- Light sense
-- load I²C Function test module
T1_BH1750 = 1
-- i2c ID
esp32_i2c_id = 0 -- esp32 Only one way i2c, id by 0
sys.taskInit(function()
if T1_BH1750 == 1 then
BH1750_init(esp32_i2c_id)
end
while 1 do
if T1_BH1750 == 1 then
BH1750_get()
end
sys.wait(100)
end
end)
sys.run()
result

边栏推荐
- The simple problem of leetcode: dismantling bombs
- Solve BP Chinese garbled code
- UiPath实战(08) - 选取器(Selector)
- Bugku CTF daily question baby_ flag. txt
- [set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
- The reason why the entity class in the database is changed into hump naming
- 2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
- Design and implementation of JSP logistics center storage information management system
- [Thesis Writing] how to write the overall design of JSP tourism network
- Priv app permission exception
猜你喜欢
![[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries](/img/56/386f0fd6553b8b9711e14c54705ae3.jpg)
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries

GFS distributed file system (it's nice to meet it alone)

Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute

Employee attendance management system based on SSM

联发科技2023届提前批IC笔试(题目)

金仓KFS数据双向同步场景部署

How to choose cross-border e-commerce multi merchant system

The least operation of leetcode simple problem makes the array increment

【XSS绕过-防护策略】理解防护策略,更好的绕过

vulnhub HA: Natraj
随机推荐
Summary of training competition (Lao Li's collection of questions)
关于开学的准备与专业认知
C Primer Plus Chapter 10, question 14 3 × 5 array
怎么用Kotlin去提高生产力:Kotlin Tips
Asp access teaching management system design finished product
《牛客刷verilog》Part II Verilog进阶挑战
商城系统搭建完成后需要设置哪些功能
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
Introduction to JVM principle
Kubernetes源码分析(一)
2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
Human resource management system based on JSP
Leetcode simple question: the key with the longest key duration
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
Library management system based on SSM
Dive into deep learning - 2.1 data operation & Exercise
Internationalization and localization, dark mode and dark mode in compose
Mongodb slow query optimization analysis strategy
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过