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

边栏推荐
- vulnhub HA: Natraj
- Ffmpeg mix
- 2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
- [fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
- [set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
- General undergraduate college life pit avoidance Guide
- Games101 Lesson 9 shading 3 Notes
- 金仓KFS数据双向同步场景部署
- Php+mysql registration landing page development complete code
- 智能合约安全审计公司选型分析和审计报告资源下载---国内篇
猜你喜欢

Introduction to JVM principle

Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended

arthas watch 抓取入参的某个字段/属性
![[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

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

Symbol of array element product of leetcode simple problem

BMZCTF simple_ pop

Two drawing interfaces - 1 Matlab style interface

The simple problem of leetcode: dismantling bombs

Solve BP Chinese garbled code
随机推荐
Human resource management system based on JSP
Kubernetes源码分析(一)
Why does I start with =1? How does this code work?
arthas watch 抓取入参的某个字段/属性
IPhone x forgot the boot password
How to use kotlin to improve productivity: kotlin tips
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
怎么用Kotlin去提高生产力:Kotlin Tips
Joint set search: merge intervals and ask whether two numbers are in the same set
【XSS绕过-防护策略】理解防护策略,更好的绕过
FFMpeg filter
[BMZCTF-pwn] 20-secret_ file
Contents of welder (primary) examination and welder (primary) examination in 2022
Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
Reptile exercise 02
Leetcode simple question: check whether the string is an array prefix
Smart contract security audit company selection analysis and audit report resources download - domestic article
Introduction to message queuing (MQ)
What functions need to be set after the mall system is built