『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 803|回复: 2

[求助] ESP32发射蓝牙信号一段时间后需要断电重启才可以运行

[复制链接]

1

主题

39

帖子

527

积分

高级会员

Rank: 4

积分
527
金钱
488
HASS币
0
发表于 2024-5-1 00:41:36 | 显示全部楼层 |阅读模式
用ESP32模块做了一个蓝牙发射功能,连接mqtt,用来控制GIMI遥控器模拟信号打开投影仪。连接的5V1A的电源。

但是使用一段时间之后发现通过mqtt发送消息,能收到,但是ESP32的蓝色灯不亮,需要断电重启才可以继续使用mqtt控制打开投影仪,打开成功后ESP32的蓝色灯是会亮的。
目前不清楚什么问题,大佬们这是为什么呢

回复

使用道具 举报

18

主题

271

帖子

2216

积分

论坛DIY达人

积分
2216
金钱
1940
HASS币
20
发表于 2024-5-1 09:47:40 来自手机 | 显示全部楼层
不懂,帮顶。
建议你把求助信息写详细一点。比如你的硬件构成,代码来源什么的,不然很难获得有效帮助。
回复

使用道具 举报

1

主题

39

帖子

527

积分

高级会员

Rank: 4

积分
527
金钱
488
HASS币
0
 楼主| 发表于 2024-5-1 12:23:50 | 显示全部楼层
嗯,好的  谢谢


用的Arduino ide编写的,网上的帖子抄的

#include <WiFi.h>               
#include "PubSubClient.h"       
#include <WiFiUdp.h>            
#include "ESP32BLEAdvertise.h"  

const char* ssid = "C-T";           
const char* password = "111111";     
//#define ID_MQTT  "64e26601a9f63aa70"     
const char* topic = "GIMITYY";        
const int B_led = 2;       
bool Turned = false;;

const char* mqtt_server = "192.168.31.80";  
const int mqtt_server_port = 1883;
const char* mqtt_username = "cc";
const char* mqtt_password = "password";
WiFiClient espClient;
PubSubClient client(espClient);
SimpleBLE bleadv;
//灯光函数及引脚定义
void turnOn();
void turnOff();

WiFiUDP Udp;
IPAddress remote_IP(192, 168, 31, 143);
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 setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print(F ("Connecting to ") );
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F (".") );
  }

  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(F ("Topic:") );
  Serial.println(topic);
  String msg = "";
  for (int i = 0; i < length; i++) {
    msg += (char)payload[i];
  }
  Serial.print(F ("Msg:") );
  Serial.println(msg);
  if (msg == "on") {
    turnOn();
    Turned = true;
  } else if (msg == "off") {
    turnOff();
    Turned = false;
  }
  msg = "";
}
void reconnect() {
  while (!client.connected()) {
    Serial.print(F ("Attempting MQTT connection...") );
    if (client.connect(ID_MQTT)) {
      Serial.println("connected");
      Serial.print(F ("subscribe:") );
      Serial.println(topic);
      client.subscribe(topic);
    } else {
      Serial.print(F ("failed, rc=") );
      Serial.print(F (client.state()) );
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}
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); 
}
void loop() {
  int i = 1;
  if (!client.connected()) {
    reconnect();
  }
  else
  {
    if (Turned)
    {
      bleadv.begin();
      uint8_t data[] = {0x46,0x00,0x28,0x68,0xe9,0x68,0xb2,0x24,0x40,0xff,0xff,0xff,0x30,0x43,0x52,0x4b,0x54,0x4d};  
      bleadv.advertise(data, 18);
      delay(5000);
    }
    else
    {
      bleadv.end();
    }
  }
  client.loop();
}
void turnOn() {
  digitalWrite(B_led, HIGH);
}
void turnOff() {
  digitalWrite(B_led, LOW);
  Udp.beginPacket(remote_IP, remoteUdpPortFZ);
  String str_cnt(KeywordFZ);
  Udp.print(str_cnt);
  Udp.endPacket();
}
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-12-24 00:48 , Processed in 0.050312 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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