【2024 DigiKey 创意大赛】ESP-32-S3- 入门雷达检测 + 语音播报 + 整合HA
本帖最后由 御坂10032号 于 2024-10-30 23:25 编辑<p><strong><span style="font-size:22px;">简介</span></strong></p>
<p> </p>
<p>在上文【2024 DigiKey 创意大赛】ESP-32-S3- 入门雷达检测 + 语音播报 <a href="https://bbs.eeworld.com.cn/thread-1297567-1-1.html" target="_blank">https://bbs.eeworld.com.cn/thread-1297567-1-1.html</a> 中我们成功使用,ESP32-S3集成了雷达和MX98357. 那么在本章节中我们把它集成进HA,方便我们直接在HA中控上查询 雷达的触发状态。</p>
<p> </p>
<p>由于这里的代码集成和使用ESP32-C6的大概一样,我这里直接贴上代码展示</p>
<p> </p>
<pre>
<code class="language-cpp">#include <ESP_I2S.h>
#include <Adafruit_NeoPixel.h>
#include <WiFi.h>
#include <ArduinoMqttClient.h>
const char *ssid = "ImmortalWrt";
const char *password = "mazha1997";
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "192.168.1.142";
int port = 1883;
const char sensorTopic[] = "radar";// 主题2
#define LED_PIN 38 // Pin connected to the data input of SK68XXMINI-HS
#define NUMPIXELS 1 // Number of LEDs (set to 1 if using a single SK68XXMINI-HS)
#define INTERRUPT_PIN 1// Pin connected to IO1 for detecting high/low signal
Adafruit_NeoPixel pixels(NUMPIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
bool ledOn = false;// LED state
void IRAM_ATTR handleInterrupt() {
// Toggle LED state based on the level of INTERRUPT_PIN
ledOn = digitalRead(INTERRUPT_PIN);// Read the state of the pin (HIGH or LOW)
}
unsigned char rawData = {
};
const size_t wavDataSize = sizeof(rawData) / sizeof(rawData);
i2s_data_bit_width_t bps = I2S_DATA_BIT_WIDTH_16BIT;
i2s_mode_t mode = I2S_MODE_STD;
i2s_slot_mode_t slot = I2S_SLOT_MODE_MONO;
I2SClass i2s;
void setup() {
Serial.begin(115200);
Serial.println("I2S playback from array");
pinMode(INTERRUPT_PIN, INPUT); // Set INTERRUPT_PIN (IO1) as input
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), handleInterrupt, CHANGE);// Trigger on any change in level (HIGH or LOW)
i2s.setPins(41, 42, 40);// BCLK, LRC, DIN
// 启动 I2S,设置采样率和每个样本的位宽
if (!i2s.begin(mode, 30000, bps, slot)) {// 假设采样率为 48000 Hz
Serial.println("Failed to initialize I2S!");
while (1)
;// 不执行任何操作
}
pixels.begin();// 初始化 LED
WiFi.begin(ssid, password);
int tryDelay = 500;
int numberOfTries = 20;
while (true) {
switch (WiFi.status()) {
case WL_NO_SSID_AVAIL: Serial.println(" SSID not found"); break;
case WL_CONNECT_FAILED:
Serial.println(" Failed - WiFi not connected! Reason: ");
return;
break;
case WL_CONNECTION_LOST: Serial.println(" Connection was lost"); break;
case WL_DISCONNECTED: Serial.println(" WiFi is disconnected"); break;
case WL_CONNECTED:
Serial.println(" WiFi is connected!");
Serial.print(" IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
mqttClient.setUsernamePassword("root", "mazha1997");
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1)
;
}
Serial.println("You're connected to the MQTT broker!");
Serial.print("Subscribing to topic: ");
Serial.println("sensorTopic");
mqttClient.subscribe(sensorTopic);// 订阅雷达主题
return;
default:
Serial.print(" WiFi Status: ");
Serial.println(WiFi.status());
break;
}
delay(tryDelay);
if (numberOfTries <= 0) {
Serial.println(" Failed to connect to WiFi!");
WiFi.disconnect();
return;
} else {
numberOfTries--;
}
}
}
void loop() {
// 保证处理 MQTT 消息
mqttClient.poll();
if (ledOn) {
// 点亮红色 LED
pixels.setPixelColor(0, pixels.Color(255, 0, 0));// 设置为红色
pixels.show(); // 更新 LED 显示
// 播放 WAV 数据
for (size_t i = 0; i < wavDataSize; i++) {
i2s.write(rawData);// 写入样本到 I2S
}
mqttClient.beginMessage(sensorTopic);
mqttClient.print("1");
mqttClient.endMessage();
} else {
// 关闭 LED
pixels.setPixelColor(0, pixels.Color(0, 0, 0));// 关闭 LED
pixels.show();
mqttClient.beginMessage(sensorTopic);
mqttClient.print("0");
mqttClient.endMessage();
}
delay(5000);// 添加一个小延迟,避免过于频繁的读取
}
</code></pre>
<p> </p>
<p>在上述代码中使用了 <WiFi.h> 和 <ArduinoMqttClient.h> 来初始化wifi连接以及 MQTT连接, 在成功连接后则会订阅树莓派MQTT中的 radar的主题来发布 雷达状态</p>
<p> </p>
<p> </p>
<p>当雷达检测到人的话,那么则发送1 , 否则的话则发送0。然后使用HA来监视这个状态的改变从而控制传感器的行为</p>
<p> </p>
<p> </p>
<p>接下来我们编辑HA的配置文件,如下所示来配置一个二进制的传感器。</p>
<p> </p>
<pre>
<code class="language-cpp">binary_sensor:
- name: "Radar Motion Sensor"
unique_id: "binary_sensor_radar_motion"
state_topic: "radar"
payload_on: "1"
payload_off: "0"
device_class: motion
</code></pre>
<p> </p>
<p>之后重载 HA的配置</p>
<p> </p>
<p> </p>
<p> </p>
<p>此时我们便可以在HA的传感器列表中找到雷达的配置选项。</p>
<p> </p>
<p> </p>
<p> </p>
<p>之后我们便可以在主界面中查看到雷达状态的更新 (需要刷新下页面)</p>
<p> </p>
<p> </p>
<p> </p>
<p>附件如下:</p>
<p> </p>
<div></div>
<p>HA是一个开源平台吗?</p>
wangerxian 发表于 2024-10-31 16:20
HA是一个开源平台吗?
<p>佬没想到你竟然没玩过, 你可以试试, 我前几个帖子里有的 真的超级NICE</p>
wangerxian 发表于 2024-10-31 16:20
HA是一个开源平台吗?
佬没想到你竟然没玩过, 你可以试试, 我前几个帖子里有的 真的超级NICE
真没玩过,有机会去试试~ wangerxian 发表于 2024-10-31 23:09
佬没想到你竟然没玩过, 你可以试试, 我前几个帖子里有的 真的超级NICE
真没玩过,有机会去 ...
<p>你试试这个室内控制方便的很,而且还有APP</p>
<p>也没玩过。。。。。可以试一试看看。。。。。。<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/loveliness.gif" width="48" /></p>
页:
[1]