Having some issues with ESP-01, Arduino UNO and SoftwareSerial codes...

Good evening, I’m having an issue while trying to integrate a ESP-01 and an Arduino UNO. I’m just trying to send three random numbers to three different fields on Thingspeak, as part of an IoT project. My code is based on this one:

https://medium.com/@cgrant/using-the-esp8266-wifi-module-with-arduino-uno-publishing-to-thingspeak-99fc77122e82

The part of my code that I’m running into problem is below:

//ESP-01s Start

//Local network information
String AP   = "AP";           //Change here - Development lab's AP
String PASS = "PASS";          //Change here - Development lab's password

//Thingspeak channel information
String API = "KEY";     //Change here
String HOST = "api.thingspeak.com";
String PORT = "80";
String field1 = "field1";
String field2 = "field2";
String field3 = "field3";

int countTrueCommand;
int countTimeCommand; 
int counter = 1;
boolean found = false;

//Activating ESP8266
SoftwareSerial esp8266(RX_PORT,TX_PORT); 

void setup(){

  esp8266.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+RESTORE",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");

  Serial.println();
  Serial.println();
  

  //ESP-01s End ... and the rest of the setup continues ...

This is just the setup. The loop section is below:

//Sending data to Thingspeak servers
  if(counter == 1){
 
    String getData1 = "GET /update?api_key="+ API +"&"+ field1 +"="+String(valSensor1);
    sendCommand("AT+CIPMUX=1",5,"OK");
    sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
    sendCommand("AT+CIPSEND=0," +String(getData1.length()+4),4,">");
    esp8266.println(getData1);delay(1500);countTrueCommand++;
    sendCommand("AT+CIPCLOSE=0",5,"OK");

    counter = 2;

  }

  if(counter == 2){

    String getData2 = "GET /update?api_key="+ API +"&"+ field2 +"="+String(valSensor2);
    sendCommand("AT+CIPMUX=1",5,"OK");
    sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
    sendCommand("AT+CIPSEND=0," +String(getData2.length()+4),4,">");
    esp8266.println(getData2);delay(1500);countTrueCommand++;
    sendCommand("AT+CIPCLOSE=0",5,"OK");

    counter = 3;
 
  }

  if(counter == 3){

    String getData3 = "GET /update?api_key="+ API +"&"+ field3 +"="+String(valSensor3);
    sendCommand("AT+CIPMUX=1",5,"OK");
    sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
    sendCommand("AT+CIPSEND=0," +String(getData3.length()+4),4,">");
    esp8266.println(getData3);delay(1500);countTrueCommand++;
    sendCommand("AT+CIPCLOSE=0",5,"OK");

    counter = 1;
 
  }
   
}

And the sendCommand function:

void sendCommand(String command, int maxTime, char readReplay[]) {
  
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }
  
    countTimeCommand++;
  }
  
  if(found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
  
 }

The issue is that those values (valSensor) are not being sent to Thingspeak at all. I should note that the AT, AT+RESTORE, AT+CWMODE and AT+CWJAP commands are being sent seamlessly. The problem starts on the AT+CIPMUX and AT+CIPSTART parts.

When I’m looking at the monitor, at first, the AT+CIPMUX and AT+CIPSTART returns ok (OYI), but right at the second time they start giving fail responses. Checking the Thingspeak channel that I’ve set, it has not received a single entry.

Hope we can figure this one out, because I really like the idea of making IoT projects.

Cheers.

you could use the WiFiEsp library. it will handle the AT commands for you

SoftwareSerial esp8266(RX_PORT,TX_PORT);

void setup(){

  esp8266.begin(115200);

SoftwareSerial won't work at 115200 baud. With a very tolerant counterpart device you may reach 38400 but forget everything above that to be reliable.

And you shouldn't use the String class on the AVR platform. It fragments the RAM in a very short time leading to indeterminable behavior.