在该应用案例中,我们将使用 ESP32 开发板作为主控制器,BH1750 光强传感器用于采集环境光强度数据,并通过 Web 服务器展示采集到的数据。
步骤一:购买材料
- ESP32 开发板 x1
- BH1750 光强传感器模块 x1
- 杜邦线若干
步骤二:连接硬件
- 将 ESP32 开发板与 BH1750 光强传感器模块连接,具体接线方式可以参考模块的说明书或使用搜索引擎。
步骤三:编写代码
我们将使用 Arduino IDE 编写代码,以下是主要代码:
#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <BH1750FVI.h>
// WiFi 设置
const char* ssid = “你的 WiFi 名称”;
const char* password = “密码”;
// Web 服务器实例
WebServer server(80);
// BH1750 实例
BH1750FVI LightSensor(BH1750FVI::k_DevModeContHighRes);
void handleRoot() {
// 获取当前光强度数据
uint16_t lux = LightSensor.GetLightIntensity();
// 返回 HTML 页面
server.send(200, “text/html”,
“”
“
ESP32 光强传感器测试
”“
当前光强度为:” + String