186 views|5 replies

155

Posts

3

Resources
The OP
 

【2024 DigiKey Creative Competition】ESP-32-S3- Entry-level radar detection + voice broadcast + integrated HA [Copy link]

 This post was last edited by Misaka10032 on 2024-10-30 23:25

Introduction

In the previous article [2024 DigiKey Creative Competition] ESP-32-S3- Entry Radar Detection + Voice Broadcast https://en.eeworld.com/bbs/thread-1297567-1-1.html, we successfully used ESP32-S3 integrated radar and MX98357. So in this section, we will integrate it into HA, so that we can directly query the trigger status of the radar on the HA central control.

Since the code integration here is roughly the same as using ESP32-C6, I will paste the code here directly for demonstration.

#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[66044] = {
  
};


const size_t wavDataSize = sizeof(rawData) / sizeof(rawData[0]);

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("[WiFi] SSID not found"); break;
      case WL_CONNECT_FAILED:
        Serial.println("[WiFi] Failed - WiFi not connected! Reason: ");
        return;
        break;
      case WL_CONNECTION_LOST: Serial.println("[WiFi] Connection was lost"); break;
      case WL_DISCONNECTED: Serial.println("[WiFi] WiFi is disconnected"); break;
      case WL_CONNECTED:
        Serial.println("[WiFi] WiFi is connected!");
        Serial.print("[WiFi] 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] WiFi Status: ");
        Serial.println(WiFi.status());
        break;
    }
    delay(tryDelay);
    if (numberOfTries <= 0) {
      Serial.println("[WiFi] 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[i]);  // 写入样本到 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);  // 添加一个小延迟,避免过于频繁的读取
}

In the above code, <WiFi.h> and <ArduinoMqttClient.h> are used to initialize the WiFi connection and MQTT connection. After the connection is successful, the radar topic in the Raspberry Pi MQTT will be subscribed to publish the radar status.

When the radar detects a person, it sends 1, otherwise it sends 0. Then use HA to monitor the change of this state to control the behavior of the sensor

Next we edit the HA configuration file to configure a binary sensor as shown below.

  binary_sensor:
    - name: "Radar Motion Sensor"
      unique_id: "binary_sensor_radar_motion"
      state_topic: "radar"
      payload_on: "1"
      payload_off: "0"
      device_class: motion

Then reload the HA configuration

Now we can find the radar configuration options in the HA sensor list.

After that, we can view the update of radar status in the main interface (need to refresh the page)

The attachments are as follows:

music_play.ino (401.76 KB, downloads: 0)
This post is from DigiKey Technology Zone

Latest reply

I haven’t played it either... You can give it a try and see...   Details Published on 2024-11-1 09:43
 
 

6742

Posts

2

Resources
2
 

Is HA an open source platform?

This post is from DigiKey Technology Zone

Comments

I didn't expect you hadn't played it before. You can try it. The ones I posted before are really nice.  Details Published on 2024-10-31 19:35
 
 
 

155

Posts

3

Resources
3
 
wangerxian posted on 2024-10-31 16:20 Is HA an open source platform?

I didn't expect you hadn't played it before. You can try it. The ones I posted before are really nice.

This post is from DigiKey Technology Zone

Comments

I've never played it before, I'll try it when I get a chance~  Details Published on 2024-10-31 23:09
 
 
 

6742

Posts

2

Resources
4
 
wangerxian posted on 2024-10-31 16:20 Is HA an open source platform?
I didn't expect you had never played it before. You can try it. Some of the ones I posted before are really nice.
I really haven't played it before. I'll try it when I have a chance~
This post is from DigiKey Technology Zone

Comments

You try this indoor control, it is very convenient, and there is also an APP  Details Published on 2024-10-31 23:57
 
 
 

155

Posts

3

Resources
5
 
wangerxian posted on 2024-10-31 23:09 I didn’t expect you had never played it before. You can try it. Some of the ones in my previous posts are really super nice. [/quote] I have never played it before. I will go there if I have a chance...

You try this indoor control, it is very convenient, and there is also an APP

This post is from DigiKey Technology Zone
 
 
 

1106

Posts

1

Resources
6
 

I haven’t played it either... You can give it a try and see...

This post is from DigiKey Technology Zone
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list