[SOLVED] ESP8266 and CPISTART problem

Hello,

I'm trying to use a ESP8266 to send data from an Uno to a web-server. The problem is that the CPISTART does not work all the time. It fail from time to time.

Any suggestions what could cause this?

The sketch is based on code from https://github.com/asksensors/AskSensors-Arduino-WiFi:

#include <SoftwareSerial.h>

// serial config.
#define     RX    10
#define     TX    11
SoftwareSerial AT(RX,TX); 

// User config.
String ssid     = "ngc7000";
String password = "secret password";
String apiKeyIn = "dahls/Ymse/uno.php";

// write interval (in ms)
const unsigned int writeInterval = 25000;

// API host config.
String host = "192.168.2.22";
String port = "80";

int AT_cmd_time;
boolean AT_cmd_result = false; 

void setup() 
{
  bool status;
  
  Serial.begin(9600);

  // open serial 
  Serial.println(F("*****************************************************"));
  Serial.println(F("********** Program Start : Connect Arduino WiFi to AskSensors"));
  
  AT.begin(115200);
  
  Serial.println(F("Initiate AT commands with ESP8266 "));
  status = sendATcmd("AT", 5, "OK");
  while (!status) ;
  
  status = sendATcmd("AT+CWMODE=1", 5, "OK");
  while (!status) ;
  
  Serial.print(F("Connecting to WiFi:"));
  Serial.println(ssid);
  status = sendATcmd("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"", 20, "OK");
  while (!status) ;
}

void loop() 
{
  static int  success = 0, failed = 0;
  bool        status;
  char        buffer[30];
  
  // Create the URL for the request
  String url = "GET /";
  url += apiKeyIn;
  url += "?v1=";
  url += random(10, 100);
  url += "&v2=";
  url += random(10, 100);
  Serial.println(F("\n\n*****************************************************"));
  Serial.println(F("********** Open TCP connection "));
  status = sendATcmd("AT+CIPMUX=1", 10, "OK");
  
  if (status) {
    status = sendATcmd("AT+CIPSTART=0,\"TCP\",\"" + host + "\"," + port, 20, "OK");
  }
  
  if (status) {
    status = sendATcmd("AT+CIPSEND=0," + String(url.length() + 4), 10, ">");
  }

  if (status) {
    Serial.print(F("********** requesting URL: "));
    Serial.println(url);
    AT.println(url);
    delay(2000);
    status = sendATcmd("AT+CIPCLOSE=0", 10, "OK");
  }
  
  Serial.println(F("********** Close TCP Connection "));
  Serial.println(F("*****************************************************"));

  if (status) {
    ++success;
  } else {
    ++failed;
  }
  sprintf(buffer, "OK: %d, Failed: %d", success, failed);
  Serial.println(buffer);
  
  delay(writeInterval);
}

// sendATcmd
bool sendATcmd(String AT_cmd, int AT_cmd_maxTime, char readReplay[]) 
{
  bool  status = false;
  
  Serial.print(F("AT command:"));
  Serial.println(AT_cmd);

  while (AT_cmd_time < AT_cmd_maxTime) {
    AT.println(AT_cmd);
    if (AT.find(readReplay)) {
      AT_cmd_result = true;
      break;
    }
  
    AT_cmd_time++;
  }
  
  Serial.print(F("   Result:"));
  if (true == AT_cmd_result) {
    Serial.println(F("DONE"));
    AT_cmd_time = 0;
    status = true;
  } else {
    Serial.println(F("FAILED"));
    AT_cmd_time = 0;
  }
  
  AT_cmd_result = false;

  return status;
}

Output from the serial monitor:

10:55:14.928 -> *****************************************************
10:55:15.021 -> ********** Open TCP connection 
10:55:15.021 -> AT command:AT+CIPMUX=1
10:55:15.068 ->    Result:DONE
10:55:15.068 -> AT command:AT+CIPSTART=0,"TCP","192.168.2.22",80
10:55:15.115 ->    Result:DONE
10:55:15.162 -> AT command:AT+CIPSEND=0,39
10:55:15.162 ->    Result:DONE
10:55:15.209 -> ********** requesting URL: GET /dahls/Ymse/uno.php?v1=17&v2=66
10:55:17.174 -> AT command:AT+CIPCLOSE=0
10:55:17.221 ->    Result:DONE
10:55:17.221 -> ********** Close TCP Connection 
10:55:17.268 -> *****************************************************
10:55:17.315 -> OK: 5, Failed: 11
10:55:42.306 -> 
10:55:42.306 -> 
10:55:42.306 -> *****************************************************
10:55:42.353 -> ********** Open TCP connection 
10:55:42.399 -> AT command:AT+CIPMUX=1
10:55:42.399 ->    Result:DONE
10:55:42.399 -> AT command:AT+CIPSTART=0,"TCP","192.168.2.22",80
10:55:42.493 ->    Result:DONE
10:55:42.493 -> AT command:AT+CIPSEND=0,39
10:55:42.540 ->    Result:DONE
10:55:42.540 -> ********** requesting URL: GET /dahls/Ymse/uno.php?v1=42&v2=47
10:55:44.552 -> AT command:AT+CIPCLOSE=0
10:55:44.552 ->    Result:DONE
10:55:44.599 -> ********** Close TCP Connection 
10:55:44.599 -> *****************************************************
10:55:44.693 -> OK: 6, Failed: 11

in the output you provide, nothing FAILED

btw: move CIPMUX to setup()

Shit, I copied the wrong part of the output - sorry about that.

Log from a loop that failed:

12:47:03.275 -> *****************************************************
12:47:03.322 -> ********** Open TCP connection 
12:47:03.322 -> AT command:AT+CIPMUX=1
12:47:03.369 ->    Result:DONE
12:47:03.369 -> AT command:AT+CIPSTART=0,"TCP","192.168.2.22",80
12:47:23.572 ->    Result:FAILED
12:47:23.572 -> ********** Close TCP Connection 
12:47:23.619 -> *****************************************************
12:47:23.666 -> OK: 66, Failed: 81

I'll try to move the CIPMUX to setup().

Juraj:
in the output you provide, nothing FAILED

btw: move CIPMUX to setup()

I did so and tried again. It seems like things get even worse:

13:27:14.281 -> ********** Open TCP connection 
13:27:14.281 -> AT command:AT+CIPSTART=0,"TCP","192.168.2.22",80
13:27:22.003 ->    Result:DONE
13:27:22.003 -> AT command:AT+CIPSEND=0,39
13:27:25.045 ->    Result:DONE
13:27:25.092 -> ********** requesting URL: GET /dahls/Ymse/uno.php?v1=15&v2=62
13:27:25.138 -> AT command:GET /dahls/Ymse/uno.php?v1=15&v2=62
13:27:35.200 ->    Result:FAILED
13:27:37.213 -> AT command:AT+CIPCLOSE=0
13:27:47.228 ->    Result:FAILED
13:27:47.228 -> ********** Close TCP Connection

The sketch has been running for some time and most of the attempts fail. A complete log can be seen here.

Current code is:

#include <SoftwareSerial.h>

// serial config.
#define     RX    10
#define     TX    11
SoftwareSerial AT(RX,TX); 

// User config.
String ssid     = "ngc7000";
String password = "secret...";
String apiKeyIn = "dahls/Ymse/uno.php";

// write interval (in ms)
const unsigned int writeInterval = 25000;

// API host config.
String host = "192.168.2.22";
String port = "80";

boolean AT_cmd_result = false; 

void setup() 
{
  bool status;
  
  Serial.begin(9600);

  // open serial 
  Serial.println(F("*****************************************************"));
  Serial.println(F("********** Program Start : Connect Arduino WiFi to AskSensors"));
  
  AT.begin(115200);
  
  Serial.println(F("Initiate AT commands with ESP8266 "));
  status = sendATcmd("AT", 5, "OK");
  while (!status) ;
  
  status = sendATcmd("AT+CWMODE=1", 5, "OK");
  while (!status) ;
  
  Serial.print(F("Connecting to WiFi:"));
  Serial.println(ssid);
  status = sendATcmd("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"", 20, "OK");
  if (status) {
    status = sendATcmd("AT+CIPMUX=1", 10, "OK");
  }
  while (!status) ;
}

void loop() 
{
  static int  success = 0, failed = 0;
  bool        status;
  char        buffer[30];
  
  // Create the URL for the request
  String url = "GET /";
  url += apiKeyIn;
  url += "?v1=";
  url += random(10, 100);
  url += "&v2=";
  url += random(10, 100);
  Serial.println(F("\n\n*****************************************************"));
  Serial.println(F("********** Open TCP connection "));
  
  status = sendATcmd("AT+CIPSTART=0,\"TCP\",\"" + host + "\"," + port, 20, "OK");
  
  if (status) {
    status = sendATcmd("AT+CIPSEND=0," + String(url.length() + 4), 10, ">");
  }

  if (status) {
    Serial.print(F("********** requesting URL: "));
    Serial.println(url);
    //AT.println(url);
    sendATcmd(url, 10, "OK");
    delay(2000);
    status = sendATcmd("AT+CIPCLOSE=0", 10, "OK");
  }
  
  Serial.println(F("********** Close TCP Connection "));
  Serial.println(F("*****************************************************"));

  if (status) {
    ++success;
  } else {
    ++failed;
  }
  sprintf(buffer, "OK: %d, Failed: %d", success, failed);
  Serial.println(buffer);
  
  delay(writeInterval);
}

// sendATcmd
bool sendATcmd(String AT_cmd, int AT_cmd_maxTime, char readReplay[]) 
{
  int   trycnt = 0;
  bool  status = false;
  
  Serial.print(F("AT command:"));
  Serial.println(AT_cmd);

  while (trycnt < AT_cmd_maxTime) {
    AT.println(AT_cmd);
    if (AT.find(readReplay)) {
      AT_cmd_result = true;
      break;
    }
  
    ++trycnt;
  }
  
  Serial.print(F("   Result:"));
  if (true == AT_cmd_result) {
    Serial.println(F("DONE"));
    status = true;
  } else {
    Serial.println(F("FAILED"));
  }
  
  AT_cmd_result = false;

  return status;
}

SoftwareSerial doesn't work reliably at 115200 baud. use 9600 baud. don't forget to set 9600 baud in AT firmware

Juraj:
SoftwareSerial doesn't work reliably at 115200 baud. use 9600 baud. don't forget to set 9600 baud in AT firmware

And how do I do that? I have no knowledge of any firmware of this unit.

Juraj:
SoftwareSerial doesn't work reliably at 115200 baud. use 9600 baud. don't forget to set 9600 baud in AT firmware

Found a command that did this so I added a line in setup()

void setup()
{
...
  AT.begin(115200);
  AT.println("AT+UART_DEF=9600,8,1,0,0");
...
}

And uploaded the sketch. Of cause everthing else after the AT+ command to change the baud rate failed - as expected.

Then I removed the line setting the baud rate and changed to 9600. Now it seems like it work as expeted.

Thanks for your reply. It solved it :slight_smile:

Espressif the manufacturer of the esp8266 has an AT firmware reference for download on their site. the times when it was hard to find some docs on esp8266 are long gone.

it is simpler to use a library which wraps the AT commands in standard Arduino WiFi API, then work directly with AT commands.

thehardwareman:
Found a command that did this so I added a line in setup()

void setup()

{
...
  AT.begin(115200);
  AT.println("AT+UART_DEF=9600,8,1,0,0");
...
}




And uploaded the sketch. Of cause everthing else after the AT+ command to change the baud rate failed - as expected.

Then I removed the line setting the baud rate and changed to 9600. Now it seems like it work as expeted.

Thanks for your reply. It solved it :)

AT.begin(115200); Sets the baud rate of the UNO's serial communication that you are going to use with the ESP8266. It does not do anything to the ESP8266.

AT.println("AT+UART_DEF=9600,8,1,0,0"); Sends out the command to the ESP8266 to change its baud rate.

To be able to continue to communicate with the ESP8266, you would need to change the UNO's baud rate to 9600

.

ieee488:
AT.begin(115200); Sets the baud rate of the UNO's serial communication that you are going to use with the ESP8266. It does not do anything to the ESP8266.

AT.println("AT+UART_DEF=9600,8,1,0,0"); Sends out the command to the ESP8266 to change its baud rate.

To be able to continue to communicate with the ESP8266, you would need to change the UNO's baud rate to 9600

.

exactly of they did. if you would read the comment to the end

Juraj:
exactly of they did. if you would read the comment to the end

I read his comment.
He wrote "Then I removed the line setting the baud rate and changed to 9600. Now it seems like it work as expeted [sic]."
The word seems does not exactly engender confidence.
Frankly, it is not exactly clear what his code is now.

.

If using AT firmware, just use Putty or a program like it to change the ESP-01's default baud rate to 9600 with AT+UART_DEF=9600,8,1,0,0 command. Then, it isn't necessary to change it in the Arduino's sketch.
.

ieee488:
I read his comment.
He wrote "Then I removed the line setting the baud rate and changed to 9600. Now it seems like it work as expeted [sic]."
The word seems does not exactly engender confidence.
Frankly, it is not exactly clear what his code is now.

.

If using AT firmware, just use Putty or a program like it to change the ESP-01's default baud rate to 9600 with AT+UART_DEF=9600,8,1,0,0 command. Then, it isn't necessary to change it in the Arduino's sketch.
.

I should not have used the word "seems". Everything works fine now :slight_smile:

Juraj:
Espressif the manufacturer of the esp8266 has an AT firmware reference for download on their site. the times when it was hard to find some docs on esp8266 are long gone.

it is simpler to use a library which wraps the AT commands in standard Arduino WiFi API, then work directly with AT commands.

I agree that a library do things a lot easier. The reason for doing things they way I did was that the code from git was the first code I found while searching for how to use the module.

Another reason is that a library is just a "soft black box" and by using a library from start I do not get a good view of how things can be done. Now I understand more about how this module operate. I'm not sure but I feel that by doing it the non-library-way, I got a better understanding of how this module work.

Now I can search for a good library with example code for later use (I got several 8 such modules so I will be using them in other projects).

Any good library to recommend?

thehardwareman:
Any good library to recommend?

it has some constraints

Juraj:
GitHub - JAndrassy/WiFiEspAT: Arduino networking library. Standard Arduino WiFi networking API over ESP8266 or ESP32 AT commands.

it has some constraints

Thanks :slight_smile: