Randomly getting "busy s..." when sending a TCP message
Posted: Tue May 02, 2017 4:13 am
I'm using an Arduino UNO with an ESP8266-01S wifi module connected to it. So I made a little code which makes the press of a button send a small message to my laptop. It works most of the time, but sometimes the AT+CIPSEND=4 (then delaying 100ms) and then send "test" returns a "busy s..." and then just freezes for a couple of seconds to a full minute... What am I doing wrong?
This is my code:
What am I doing wrong?
This is my code:
Code: Select all
//some variables like ssid, gateway,...
#include <SoftwareSerial.h>
SoftwareSerial esp8266(txPin, rxPin);
boolean allowedToSendMessage = true;
int buttonState = LOW;
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("Started");
esp8266.begin(9600);
delay(1000);
sendToESP8266AndWaitForOk("AT+CWQAP");
Serial.println("Wifi Disconnected!");
sendToESP8266AndWaitForOk("AT+CWJAP=\""+ssid+"\",\""+password+"\"");
Serial.println("Connected!");
sendToESP8266AndWaitForOk("AT+CIPSTA=\""+ip+"\",\""+gateway+"\",\""+subnet+"\"");
Serial.println("Static IP set!");
sendToESP8266AndWaitForOk("AT+CIPSTART=\"TCP\",\""+server+"\","+String(sendPort)+"");
Serial.println("Connected to "+server+" port "+sendPort+"");
Serial.println("SETUP DONE");
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
allowedToSendMessage = false;
sendTcpMessageToServer(String(id));
allowedToSendMessage = true;
}
}
void sendTcpMessageToServer(String message){
String tmp = "";
boolean canInsertMessage = false;
int messageLength = message.length();
messageLength = messageLength + 2;
message = "|" + message;
message = message + "|";
String sendMessage = "AT+CIPSEND=" + String(messageLength);
sendMessage = sendMessage + "\r\n";
esp8266.println(sendMessage);
delay(100);
sendToESP8266AndWaitForOk(message);
Serial.println("Message sent!");
}
void sendToESP8266AndWaitForOk(String message){
String tmp;
boolean ok = false;
boolean error = false;
boolean closed = false;
boolean sendFail = false;
boolean linkNotValid = false;
Serial.println();
Serial.println("##### SENDING " + message + "##### ");
esp8266.println(message + "\r\n");
if (tmp.length() > 0) Serial.println(tmp);
while (!ok && !error && !closed && !sendFail && !linkNotValid){
tmp = esp8266.readString();
if (tmp.length() > 0) {
Serial.println(tmp);
}
ok = tmp.indexOf("OK") > 0;
error = tmp.indexOf("ERROR") > 0;
closed = tmp.indexOf("CLOSED") > 0;
sendFail = tmp.indexOf("SEND FAIL") > 0;
linkNotValid = tmp.indexOf("link is not valid") > 0;
}
if (ok){
Serial.println("OK RECEIVED FOR SENDING " + message);
}
else if (error){
Serial.println("ERROR RECEIVED FOR SENDING " + message);
}
if (closed || linkNotValid){
sendToESP8266AndWaitForOk("AT+CIPSTART=\"TCP\",\""+server+"\","+String(sendPort)+"");
Serial.println("Connected to "+server+" port "+sendPort+"");
}
if (sendFail){
sendTcpMessageToServer(message);
}
Serial.println("##### END OF " + message + "##### ");
Serial.println();
}
What am I doing wrong?