找回密码
 立即注册

微信扫码登录

搜索
楼主: raychao

[硬件DIY] ESP32完美实现小爱控制极米投影仪蓝牙开机及Wifi关机-更新完

  [复制链接]

3

主题

12

回帖

107

积分

注册会员

积分
107
金钱
92
HASS币
0
发表于 2025-1-7 20:28:35 | 显示全部楼层
Spending5027 发表于 2024-5-3 15:47
可以用  nRF Connect

nRF Connect怎么用呀,我搜不到设备,而且看不到Data
回复

使用道具 举报

0

主题

3

回帖

26

积分

新手上路

积分
26
金钱
23
HASS币
0
发表于 2025-1-7 20:44:19 | 显示全部楼层
先留个位置
回复

使用道具 举报

3

主题

12

回帖

107

积分

注册会员

积分
107
金钱
92
HASS币
0
发表于 2025-1-9 12:57:00 | 显示全部楼层
xdw011025 发表于 2024-7-3 21:25
data填在哪里i,哪位大佬指点下

你data哪里找到的呀
回复

使用道具 举报

2

主题

24

回帖

261

积分

中级会员

积分
261
金钱
235
HASS币
0
发表于 2025-1-23 13:57:53 | 显示全部楼层
这么说的话,不接入到巴法云,直接通过mqtt接入到ha也是可以的,只是要再写一些代码
回复

使用道具 举报

0

主题

5

回帖

73

积分

注册会员

积分
73
金钱
68
HASS币
0
发表于 2025-7-8 16:45:07 | 显示全部楼层
想问下大佬,是不是大多数的蓝牙遥控都是用广播唤醒的?
回复

使用道具 举报

0

主题

1

回帖

13

积分

新手上路

积分
13
金钱
12
HASS币
0
发表于 2025-8-4 17:15:09 | 显示全部楼层
给大家补一份ESP32C3的代码
#include <WiFi.h>
#include "PubSubClient.h"
#include <WiFiUdp.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEAdvertising.h>

//********************需要修改的部分*******************//
const char* ssid = "XXXX";           // 你的WiFi名称
const char* password = "XXXXXXXX";   // 你的WiFi密码
#define ID_MQTT  "XXXXXXXXXXXXXX"    // 巴法云用户私钥
const char* topic = "XXXXXXXXXXXX";  // 巴法云主题名称
const int B_led = 2;                 // LED引脚(ESP32-C3的GPIO2)
bool Turned = false;
//**************************************************//

const char* mqtt_server = "bemfa.com";
const int mqtt_server_port = 9501;
WiFiClient espClient;
PubSubClient client(espClient);

// BLE相关配置
BLEServer* pServer = nullptr;
BLEAdvertising* pAdvertising = nullptr;
const char* BLE_DEVICE_NAME = "ESP32-C3-Proj";

// UDP配置
WiFiUDP Udp;
IPAddress remote_IP(192, 168, 3, 27);
unsigned int remoteUdpPort = 16735;
unsigned int remoteUdpPortFZ = 16750;
const char* Keyword = "KEYPRESSES:116";
const char* KeywordFZ = "{"action":20000,"controlCmd":{"delayTime":0,"mode":6,"time":0,"type":0},"msgid":"2"}";

// 函数声明
void turnOn();
void turnOff();
void setup_wifi();
void callback(char* topic, byte* payload, unsigned int length);
void reconnect();
void initBLE();

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Topic:");
  Serial.println(topic);
  String msg = "";
  for (int i = 0; i < length; i++) {
    msg += (char)payload[i];
  }
  Serial.print("Msg:");
  Serial.println(msg);
  
  if (msg == "on") {
    turnOn();
    Turned = true;
  } else if (msg == "off") {
    turnOff();
    Turned = false;
  }
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect(ID_MQTT)) {
      Serial.println("connected");
      client.subscribe(topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

// 初始化BLE广播(包含自定义厂商数据)
void initBLE() {
  BLEDevice::init(BLE_DEVICE_NAME);
  pServer = BLEDevice::createServer();
  pAdvertising = pServer->getAdvertising();
  
  BLEAdvertisementData advData;
  
  // 厂商特定数据配置
  // 格式:[公司代码(2字节)] + [自定义数据(n字节)]
  uint8_t manufacturerData[] = {
    0x46, 0x00,               // 公司代码 Company Code: 0x0046(小端模式存储)
    0x63, 0xC1, 0xA5, 0x10,   // 自定义数据部分
    0x39, 0x54, 0x38, 0xFF,
    0xFF, 0xFF, 0x30, 0x43,
    0x52, 0x4B, 0x54, 0x4D
  };
  
  // 设置厂商数据(转换为Arduino String类型)
  advData.setManufacturerData(String((char*)manufacturerData, sizeof(manufacturerData)));
  advData.setName(BLE_DEVICE_NAME);  // 设备名称
  
  pAdvertising->setAdvertisementData(advData);
  pAdvertising->setScanResponse(false);
  pAdvertising->setMinInterval(128);
  pAdvertising->setMaxInterval(256);
}

void setup() {
  pinMode(B_led, OUTPUT);
  digitalWrite(B_led, LOW);
  Serial.begin(115200);
  
  setup_wifi();
  client.setServer(mqtt_server, mqtt_server_port);
  client.setCallback(callback);
  
  initBLE();
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  
  if (Turned) {
    if (!pAdvertising->isAdvertising()) {
      pAdvertising->start();
      Serial.println("BLE广播已启动");
    }
  } else {
    if (pAdvertising->isAdvertising()) {
      pAdvertising->stop();
      Serial.println("BLE广播已停止");
    }
  }
  delay(100);
}

void turnOn() {
  digitalWrite(B_led, HIGH);
  Serial.println("投影已打开");
}

void turnOff() {
  digitalWrite(B_led, LOW);
  Udp.beginPacket(remote_IP, remoteUdpPortFZ);
  Udp.print(KeywordFZ);
  Udp.endPacket();
  Serial.println("投影已关闭,已发送关机指令");
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-8-14 01:53 , Processed in 0.188286 second(s), 11 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表