Random “busy s…” freeze when trying to send message by button input

JohnD
Posts: 1
Joined: Tue May 02, 2017 7:46 pm

Random “busy s…” freeze when trying to send message by button input

Postby JohnD » Tue May 02, 2017 7:50 pm

I'm currently using an Arduino UNO with an ESP8266 wifi module. I made a code that sends it's (hardcoded) id between pipes to my laptop. However, randomly, after the AT+CIPSEND=4 it returns a busy s... and then the code automatically inputs the id (e.g. |13|) and then returns busy s... again... Then the module freezes for a couple of seconds to a full minute. Sometimes it just sends the message again, but other times it just returns CLOSED and it just reconnects.

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();
}

Her Majesty
Posts: 191
Joined: Mon Oct 27, 2014 11:09 am

Re: Random “busy s…” freeze when trying to send message by button input

Postby Her Majesty » Wed May 03, 2017 3:04 pm

'busy s' may mean it is busy sending data, you can wait a while till it completes to send next command, or capture the TCP packets by wireshark to see if it is retrying the sending.

Who is online

Users browsing this forum: No registered users and 4 guests