Issues while integrating ESP-01s and Arduino UNO and sending data to Thingspeak

Hello!

So I'm bashing my head for quite a time because of this issue. I'm quite new to networking modules and such, and I wanted to try and use the ESP-01s that I had lying around. I used the code on this page so I could have a starting point and modify it.

I am aiming to make a sketch that sends two variables to Thingspeak so I can visualize them in charts. The code that I made over the one at that page goes like this:

#include <SoftwareSerial.h>
#define RX 10
#define TX 11
String AP = "XXXXXX";       // CHANGE ME
String PASS = "xxxxxxxx"; // CHANGE ME
String API = "yyyyyyyyy";   // CHANGE ME
String HOST = "api.thingspeak.com";
String PORT = "80";
String field1 = "field1";
String field2 = "field2";
String field3 = "field3";
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 
int valSensor1 = 1;
int valSensor2 = 1;
int valSensor3 = 1;
int counter = 1;

SoftwareSerial esp8266(RX,TX); 
 
  
void setup() {
  Serial.begin(9600);
  esp8266.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}


void loop() {
  
 valSensor1 = random (0,100);
 valSensor2 = random (200, 500);
 valSensor3 = random (1000, 2000);

 delay(5000);

 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 = 1;
 
 }


 
}


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

Well, admittedly, I did not change much, but here is the thing, the sending order is far from perfect, sometimes it only sends the valueSensor1 for 3 or even 4 times in a row leaving the other variable behind. Both the counter variable and the delay() at the start of the loop were tests to see if the variables cound be sent in order.

I thank in advance those who read and could help me.

Quick edit: I should note that I'm using an external power source to power the ESP-01s. The source looks like this one: Error, Electronic & Electronics Components Depot United States

I have not looked at your code and stopped using Thinkgspeak a while back but IIRC you need to delay between sending individual values. I cannot remember for how long though but some old Node-Red code I used to delay 30 seconds between values.

ThingSpeak maintains libraries for Arduino and Espressif hardware. The latest release added support for ESP-01 using AT commands, which seems very relevant to your project.

Search for ThingSpeak in the Arduino IDE Library manager. Then check out the examples under File -> Examples -> ThingSpeak -> ESP8266 -> via AT commands