Hello,
I have connected a Pi Pico to an ESP-01S module using Serial1. I am using the ESP-01S module with the AT-commands, so that I am able to provide the HTML in the Arduino Code (I am using the Arduino mBed Core), as well as read the request URLs.
The code is fairly simple as it stands now. Inside setup() I run the AT-commands necessary to setup the ESP-01S as a server, and inside loop, either the HTML is served (for the / request), or the onboard LED is switched on of off depending on the URL ("/ON" and "/OFF").
The problem is the inconsistency in the performance I get for my requests (which are XHR-requests from a browser). Sometimes I get a response in 60ms, usually I get around 500ms, but sudden peaks up to and above 2s happens frequently.
Are there optimisations I can do to the code, or is this to me expected using this, rather cheap, ESP-01S module?
int rx = 21;
int tx = 20;
int enable = 22;
int led = 25;
String connectionId = "";
void setup() {
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial1.begin(115200);
Serial.begin(9600);
Serial.println("Toggling Enabled");
pinMode(enable, OUTPUT);
digitalWrite(enable, HIGH);
delay(1000);
digitalWrite(enable, LOW);
delay(1000);
digitalWrite(enable, HIGH);
delay(1000);
Serial.println("Enable toggled");
Serial.println("Starting Wifi");
Serial1.write("AT+RST\r\n");
delay(150);
Serial1.write("AT+CWMODE=3\r\n");
delay(150);
Serial1.write("AT+CWSAP=\"ESP-01\",\"PASSORD\",1,0,4,0\r\n");
delay(150);
Serial1.write("AT+CWLIF\r\n");
delay(150);
Serial1.write("AT+CIPMUX=1\r\n");
delay(150);
Serial1.write("AT+CIPSERVERMAXCONN=1\r\n");
delay(150);
Serial1.write("AT+CIPSERVER=1,80\r\n");
delay(150);
Serial1.write("AT+CIPSTO=2\r\n");
delay(150);
Serial.println("WiFi Started");
}
void loop() {
if (Serial.available()) {
Serial1.write(Serial.read());
}
if (Serial1.available()) {
String inputString = Serial1.readStringUntil('\n');
Serial.println(inputString);
if (strstr(inputString.c_str(), "+IPD")) {
//find connectionId
Serial.print("input: ");
Serial.println(inputString);
int firstComma = inputString.indexOf(',');
int secondComma = inputString.indexOf(',', firstComma+1);
connectionId = inputString.substring(firstComma+1, secondComma);
Serial.print("connectionId: ");
Serial.println(connectionId);
if (connectionId != "" && strstr(inputString.c_str(), "GET / HTTP")) {
//serve index.html
espsendHtml("<button onclick=\"fetch('/ON');\">ON</button><button onclick=\"fetch('/OFF');\">OFF</button>");
} else if (connectionId != "" && strstr(inputString.c_str(), "GET /ON HTTP")) {
digitalWrite(led, HIGH);
espSendData("OK");
} else if (connectionId != "" && strstr(inputString.c_str(), "GET /OFF HTTP")) {
digitalWrite(led, LOW);
espSendData("OK");
}
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";
sendData(closeCommand,50,true);
}
inputString = "";
/*if(Serial1.find("+IPD,")) {
delay(300);
connectionId = Serial1.read()-48;
Serial.print("connectionID: ");
Serial.println(connectionId);
if (Serial1.find("GET /ON")) {
digitalWrite(led, HIGH);
espsend("OK");
} else if (Serial1.find("GET /OFF")) {
digitalWrite(led, LOW);
espsend("OK");
} else {
espsendHtml("<button onclick=\"fetch('/ON');\">ON</button><button onclick=\"fetch('/OFF');\">OFF</button>");
}
String closeCommand = "AT+CIPCLOSE="; ////////////////close the socket connection////esp command
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";
sendData(closeCommand,300,true);
}*/
}
}
void espSendData(String data) {
String header = "HTTP/1.1 200 OK\r\n";
header += "Content-Length: ";
header += (data.length());
header += "\r\n";
header += "Connection: close\r\n\r\n";
String msg = header + data;
espsend(msg);
}
void espsendHtml(String html) {
String body = "<html><head></head><body>";
body += html;
body += "</body></html>";
String header = "HTTP/1.1 200 OK\r\n";
header += "Content-Length: ";
header += (body.length());
header += "\r\n";
header += "Connection: close\r\n\r\n";
String msg = header + body;
espsend(msg);
}
void espsend(String d){
String cipSend = " AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=d.length();
cipSend +="\r\n";
sendData(cipSend,50,true);
sendData(d,50,true);
}
void sendData(String command, const int timeout, boolean debug) {
Serial1.print(command);
delay(timeout);
}```