当前位置:网站首页>[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

边栏推荐
- 使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
- 【工具跑SQL盲注】
- Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
- 金仓数据库KingbaseES 插件kdb_exists_expand
- General undergraduate college life pit avoidance Guide
- The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
- 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
- Human resource management system based on JSP
- Kingbasees plug-in KDB of Jincang database_ exists_ expand
- 带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
猜你喜欢

Joint set search: merge intervals and ask whether two numbers are in the same set

Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
![[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN](/img/7e/50fa6f65b5a4f0bb60909f57daff56.png)
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN

A outsourcing boy's mid-2022 summary

When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error

Design and implementation of JSP logistics center storage information management system

Asp access teaching management system design finished product

Symbol of array element product of leetcode simple problem

Jincang KFS data bidirectional synchronization scenario deployment

使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
随机推荐
使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
Arthas watch grabs a field / attribute of the input parameter
C language series - Section 3 - functions
使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver
BMZCTF simple_ pop
Integration of Android high-frequency interview questions (including reference answers)
[software testing-6] & Test Management
FFMpeg filter
FuncS sh file not found when using the benchmarksql tool to test kingbases
有道云笔记
Leetcode simple question: check whether two string arrays are equal
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
金仓KFS数据双向同步场景部署
The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
Symbol of array element product of leetcode simple problem
Introduction of pointer variables in function parameters
2.14 summary
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
【XSS绕过-防护策略】理解防护策略,更好的绕过
What's wrong with SD card data damage? How to recover SD card data damage