Closed nk25719 closed 2 months ago
Using the hardware UART pins (16 and 17) of the ESP32, and utilizing the hardware serial capabilities of the ESP32 instead of using the SoftwareSerial library is more efficient . This can improve communication reliability and performance.
#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>
#include <HTTPClient.h>
#include <DFRobotDFPlayerMini.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <WiFiManager.h>
// Hardware serial for DFPlayer
HardwareSerial mySerial1(2); // Use UART2, (16, 17 are RX2, TX2)
// Global initialization remains the same
const char* server_address = "192.168.1.3";
const int serverPort = 5500;
const int analogPin = 34;
const int MUTE_BUTTON_PIN = 36;
const int ON_OFF_BUTTON_PIN = 39;
#define LED_PIN 2
const int lamp1Pin = 15;
const int lamp2Pin = 4;
const int lamp3Pin = 5;
const int lamp4Pin = 18;
const int lamp5Pin = 19; // Note: You had duplicate pin definitions for lamp5Pin; you need to resolve this.
DFRobotDFPlayerMini myDFPlayer;
LiquidCrystal_I2C lcd(0x3F, 20, 4);
WebServer server(80);
DNSServer dnsServer;
// Other variables are defined as before
void setup() {
Serial.begin(115200);
mySerial1.begin(9600, SERIAL_8N1, 16, 17); // Set baud rate and UART pin numbers
pinMode(MUTE_BUTTON_PIN, INPUT_PULLUP);
pinMode(ON_OFF_BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Initialize lamp pins
pinMode(lamp1Pin, OUTPUT);
pinMode(lamp2Pin, OUTPUT);
pinMode(lamp3Pin, OUTPUT);
pinMode(lamp4Pin, OUTPUT);
pinMode(lamp5Pin, OUTPUT);
// Access Point, DNS, Web Server, and DFPlayer initialization remains the same
void loop() {
.
.
.
void startDFPlayer() {
// Start DFPlayer using hardware serial
if (!myDFPlayer.begin(mySerial1)) { // Use mySerial1 instead of mySoftwareSerial
Serial.println("Unable to begin:");
Serial.println("1. Please recheck the connection!");
Serial.println("2. Please insert the SD card!");
while (true);
}
myDFPlayer.setTimeOut(500); // Set serial communictaion time out 500ms
myDFPlayer.volume(25); // Set initial volume
myDFPlayer.EQ(0); // Normal equalization
Serial.println("DFPlayer initialized");
}
.
.
.
}
Rest of the code...
Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] The sketch uses a software UART for the DF player. This consumes resources. Change the code for the hardware UART on the DF player.
Describe the solution you'd like A clear and concise description of what you want to happen. Rely on the hardware software connection (pin#16, pin#17).
Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered. NONE
Additional context Add any other context or screenshots about the feature request here.