我正在研究 ESP32 WebSocket 通信。经过大量的努力,我面临着严重的问题,在串行监视器 Esp32 中无限地打印奇怪的字符。这样每次我都必须重新启动我的 esp32 板才能完美运行 esp32。
Esp32 板控制设备(灯泡、灯管、插座等)完美,但每当我尝试远程或手动控制时,只有风扇才会出现此问题。但它在面包板上或当我不使用交流电压为继电器模块供电时运行得非常顺利。 串行监视器中的错误图像
我尝试了不同的连接,但问题是相同的。 当这个问题发生时 案例1.当风扇与继电器连接并尝试远程或手动开/关时。 案例2。风扇与继电器断开但与手动开关连接并尝试 开/关。
我发现或者也许我错了,每当我试图控制风扇时,它都会产生高 EMI(电磁干扰),并且 EMI 会导致 ESP32 出现错误(在串行监视器中无限打印随机字符)。
这是代码:-
#include <ArduinoWebsockets.h>
#include <WiFi.h>
#include <ArduinoJson.h>
#define ESP_NAME "Room 1"
//-----------------Appliances/Relay pins declaration or OUTPUT PINS------------------
#define Tubelight 22
#define Bulb 23
#define Fan 18
#define Socket1 19
//---------------Switches pin declaration or INPUT PINS-------------------
#define switch1 32
#define switch2 33
#define switch3 25
#define switch4 26
//--------------------Network Credentials------------------------------
const char* ssid = "xxxxxxxxxxxx"; //Enter SSID
const char* password = "xxxxxxxxxxxx"; //Enter Password
const char* websockets_connection_string = "wss://xxxxxxxx.glitch.me"; //server address
//------------------Eliminate Switch buttons debounce effect--------------------
unsigned long lastDebounceTime = 0;
int debounceDelay = 600;
//------------------------Switches Previous state---------------------
bool switch1_prev_state = HIGH; //here HIGH work as LOW in INPUT_PULLUP pinMode
bool switch2_prev_state = HIGH;
bool switch3_prev_state = HIGH;
bool switch4_prev_state = HIGH;
bool wb_config_state = false;
//------------------------Websocket Variables------------------------------
using namespace websockets;
WebsocketsClient clientt;
StaticJsonDocument<200> doc; //for parsing data sent from server
// This certificate was updated 15.04.2021, issues on Mar 15th 2021, expired on June 13th 2021
const char echo_org_ssl_ca_cert[] PROGMEM = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIEZTCCA02gAwIBAgIQQ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MDMwMaAvoC2GK2h0dHA6Ly9jcmwuaWRlbnRydXN0\n" \
"LmNvbS9EU1RST09UQ0FYM0NSTC5jcmwwHQYDVR0OBBYEFBQusxe3WFbLrlAJQOYf\n" \
"r52LFMLGMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjANBgkqhkiG9w0B\n" \
"AQsFAAOCAQEA2UzgyfWEiDcx27sT4rP8i2tiEmxYt0l+PAK3qB8oYevO4C5z70kH\n" \
"ejWEHx2taPDY/laBL21/WKZuNTYQHHPD5b1tXgHXbnL7KqC401dk5VvCadTQsvd8\n" \
"S8MXjohyc9z9/G2948kLjmE6Flh9dDYrVYA9x2O+hEPGOaEOa1eePynBgPayvUfL\n" \
"qjBstzLhWVQLGAkXXmNs+5ZnPBxzDJOLxhF2JIbeQAcH5H0tZrUlo5ZYyOqA7s9p\n" \
"O5b85o3AM/OJ+CktFBQtfvBhcJVd9wvlwPsk+uyOy2HI7mNxKKgsBTt375teA2Tw\n" \
"UdHkhVNcsAKX1H7GNNLOEADksd86wuoXvg==\n" \
"-----END CERTIFICATE-----\n";
void onMessageCallback(WebsocketsMessage message)//get messages From server
{
Serial.print("Got Message: ");
Serial.println(message.data());
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, message.data());
// Test if parsing succeeds.
if (error){
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
//store status of appliance
bool sts = doc["status"]=="ON"?LOW:HIGH; //here relay work in reverse
if(doc["room_name"] == ESP_NAME)
{
if(doc["appliance"] == "Tubelight")
{
digitalWrite(Tubelight, sts);
}
else if(doc["appliance"] == "Bulb")
{
digitalWrite(Bulb, sts);
}
else if(doc["appliance"] == "Fan")
{
digitalWrite(Fan, sts);
}
else if(doc["appliance"] == "Socket1")
{
digitalWrite(Socket1, sts);
}
}
else
{
Serial.println("False Room Name");
}
}
void onEventsCallback(WebsocketsEvent event, String data) {
if(event == WebsocketsEvent::ConnectionOpened) {
Serial.println("Connnection Opened");
} else if(event == WebsocketsEvent::ConnectionClosed) {
Serial.println("Connnection Closed");
wb_config_state = false;
// clientt.connect(websockets_connection_string);
} else if(event == WebsocketsEvent::GotPing) {
Serial.println("Got a Ping!");
} else if(event == WebsocketsEvent::GotPong) {
Serial.println("Got a Pong!");
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password); //Connecting to wifi
while(WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(200);
}
Serial.println("\nSuccessfully Connected with WiFi.");
//-------------------Pinmode Setup----------------------
//for relay or output
pinMode(Tubelight, OUTPUT);
pinMode(Bulb, OUTPUT);
pinMode(Fan, OUTPUT);
pinMode(Socket1, OUTPUT);
//pinMode for Switches
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);
pinMode(switch3, INPUT_PULLUP);
pinMode(switch4, INPUT_PULLUP);
//-----------Prevent relay from jump start----------------
digitalWrite(Tubelight, HIGH);
digitalWrite(Bulb, HIGH);
digitalWrite(Fan, HIGH);
digitalWrite(Socket1, HIGH);
}
void loop()
{
if(WiFi.status() == WL_CONNECTED)
{
if(wb_config_state==false){
wb_config();
wb_config_state = true;
}else{clientt.poll();}
}
else{
WiFi.reconnect();
}
mannual();
}
void wb_config()
{
//--------------------Websocket communication establishment-------------------
clientt.onMessage(onMessageCallback);// run callback when messages are received
clientt.onEvent(onEventsCallback);// run callback when events are occuring
clientt.setCACert(echo_org_ssl_ca_cert);// Before connecting, set the ssl fingerprint of the server
clientt.addHeader("User-Agent", "Satya");//Add header
clientt.connect(websockets_connection_string);// Connect to server
clientt.ping();// Send a ping
}
void mannual()
{
//--------------------------------Switch 1----------------------------------
if((digitalRead(switch1)==HIGH)&& switch1_prev_state==LOW && debounce())
{
// Serial.println("Switch 1 / Tubelight --> OFF");
build_send_data("Tubelight", "OFF");
digitalWrite(Tubelight, HIGH);
switch1_prev_state = HIGH;
lastDebounceTime = millis();
}
if(digitalRead(switch1)==LOW && switch1_prev_state==HIGH && debounce())
{
// Serial.println("Switch 1 / Tubelight --> ON");
build_send_data("Tubelight", "ON");
digitalWrite(Tubelight, LOW);
switch1_prev_state = LOW;
lastDebounceTime = millis();
}
//--------------------------------Switch 2----------------------------------
if((digitalRead(switch2)==HIGH)&& switch2_prev_state==LOW && debounce())
{
// Serial.println("Switch 2 / Bulb --> OFF");
build_send_data("Bulb", "OFF");
digitalWrite(Bulb, HIGH);
switch2_prev_state = HIGH;
lastDebounceTime = millis();
}
if(digitalRead(switch2)==LOW && switch2_prev_state==HIGH && debounce())
{
// Serial.println("Switch 2 / Bulb --> ON");
build_send_data("Bulb", "ON");
digitalWrite(Bulb, LOW);
switch2_prev_state = LOW;
lastDebounceTime = millis();
}
//--------------------------------Switch 3----------------------------------
if((digitalRead(switch3)==HIGH)&& switch3_prev_state==LOW && debounce())
{
// Serial.println("Switch 3 / Fan --> OFF");
build_send_data("Fan", "OFF");
digitalWrite(Fan, HIGH);
switch3_prev_state = HIGH;
lastDebounceTime = millis();
}
if(digitalRead(switch3)==LOW && switch3_prev_state==HIGH && debounce())
{
// Serial.println("Switch 3 / Fan --> ON");
build_send_data("Fan", "ON");
digitalWrite(Fan, LOW);
switch3_prev_state = LOW;
lastDebounceTime = millis();
}
//--------------------------------Switch 4----------------------------------
if((digitalRead(switch4)==HIGH)&& switch4_prev_state==LOW && debounce())
{
// Serial.println("Switch 4 / Socket1 --> OFF");
build_send_data("Socket1", "OFF");
digitalWrite(Socket1, HIGH);
switch4_prev_state = HIGH;
lastDebounceTime = millis();
}
if(digitalRead(switch4)==LOW && switch4_prev_state==HIGH && debounce())
{
// Serial.println("Switch 4 / Socket1 --> ON");
build_send_data("Socket1", "ON");
digitalWrite(Socket1, LOW);
switch4_prev_state = LOW;
lastDebounceTime = millis();
}
}
void build_send_data(String appliance, String sts)
{
if(WiFi.status() == WL_CONNECTED)
{
doc["from"] = "ESP32";
doc["client_name"] = ESP_NAME;
doc["room_name"] = "Room 1";
doc["appliance"] = appliance;
doc["status"] = sts;
String send_data;
serializeJson(doc, send_data);
Serial.print("Send Message : ");
Serial.println(send_data);
clientt.send(send_data);
}
}
bool debounce()
{
return ((millis() - lastDebounceTime)> debounceDelay);
}
请帮助我解决我的问题并原谅我的英语错误😊。
您可以尝试在开关(引脚的开关侧)上使用下拉(或上拉)电阻吗?问题是,如果 5V(我希望你不要向引脚施加 5V?)远离开关,引脚就会浮动。
在使用 ESP32 激活继电器时遇到类似的问题,尝试了几个不同的开发板,但都遇到了相同的问题,开发板无限期地向串行端口发送乱码,并拒绝继续正常操作,直到重置。
我通过降低 CPU 频率,成功使用 ESP32-WROOM-32 开发模块获得了部分解决方案。现在它会自行重新启动,而不是卡住。 我用其中两个板进行了测试,结果看起来一致。
不幸的是,对于使用 ESP-C3 芯片的超迷你开发板,降低频率并没有帮助。