IoT esp8266 Thingspeak irregular data upload

Hi,

I am making a basic IoT based project to send temperature and turbidity values collected from sensors to Thingspeak. I am able to correctly extract data from sensors but I am facing problem with the wifi module esp8266. I have checked AT command in serial monitor is working correctly. I came across this code which uploads random numbers from 1-1000 to thingspeak and I think of modifying this code a bit as per my project but when I run this copied code what is happening is that sometimes the data gets uploaded and sometimes not. I am scratching my head here that how is that even possible to have both code working and not working at the same time !

Also, I am using Arduino Mega 2560 board with it so if you have any code optimizations for this esp module then pls suggest.

ESP8266 Firmware Version
02:24:04.802 → AT+GMR

02:24:04.802 → AT version:1.3.0.0(Jul 14 2016 18:54:01)
02:24:04.802 → SDK version:2.0.0(5a875ba)
02:24:04.802 → v1.0.0.3
02:24:04.802 → Mar 13 2018 09:35:47
02:24:04.802 → OK

Code :
#include <SoftwareSerial.h>
#define RX 10
#define TX 11
String AP = “Redmi”; // CHANGE ME
String PASS = “pass1234”; // CHANGE ME
String API = “ARTMFS3VT5BJPFO7”; // CHANGE ME
String HOST = “api.thingspeak.com”;
String PORT = “80”;
String field = “field1”;
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 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() {
valSensor = getSensorData();
String getData = “GET /update?api_key=”+ API +"&"+ field +"="+String(valSensor)+" \r\n";
sendCommand(“AT+CIPMUX=1”,5,“OK”);
sendCommand(“AT+CIPSTART=0,“TCP”,”"+ HOST +"","+ PORT,20,“OK”);
sendCommand(“AT+CIPSEND=0,” +String(getData.length()+4),4,">");
esp8266.println(getData);delay(16000);countTrueCommand++;
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand(“AT+CIPCLOSE=0”,5,“OK”);
}
int getSensorData(){
return random(1000); // Replace with
}
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 serial monitor shows something like this
0. at command => AT OYI
02:31:04.862 → 1. at command => AT+CWMODE=1 OYI
02:31:04.899 → 2. at command => AT+CWJAP=“Redmi”,“pass1234” OYI
02:31:07.866 → 3. at command => AT+CIPMUX=1 OYI
02:31:07.901 → 4. at command => AT+CIPSTART=0,“TCP”,“api.thingspeak.com”,80 Fail
02:31:28.512 → 0. at command => AT+CIPSEND=0,50 OYI
02:31:33.625 → 2. at command => AT+CIPCLOSE=0 Fail
02:31:38.647 → 0. at command => AT+CIPMUX=1 OYI
02:31:39.644 → 1. at command => AT+CIPSTART=0,“TCP”,“api.thingspeak.com”,80 Fail
02:32:00.278 → 0. at command => AT+CIPSEND=0,50 OYI
02:32:05.337 → 2. at command => AT+CIPCLOSE=0 Fail
02:32:10.396 → 0. at command => AT+CIPMUX=1 OYI
02:32:11.402 → 1. at command => AT+CIPSTART=0,“TCP”,“api.thingspeak.com”,80 OYI
02:32:11.815 → 2. at command => AT+CIPSEND=0,49 OYI
02:32:13.307 → 4. at command => AT+CIPCLOSE=0 OYI
02:32:13.857 → 5. at command => AT+CIPMUX=1 OYI
02:32:14.852 → 6. at command => AT+CIPSTART=0,“TCP”,“api.thingspeak.com”,80 OYI
02:32:15.307 → 7. at command => AT+CIPSEND=0,50 OYI
02:32:16.786 → 9. at command => AT+CIPCLOSE=0 OYI
02:32:17.216 → 10. at command => AT+CIPMUX=1 OYI
02:32:19.235 → 11. at command => AT+CIPSTART=0,“TCP”,“api.thingspeak.com”,80 OYI
02:32:19.787 → 12. at command => AT+CIPSEND=0,50 OYI
02:32:24.659 → 14. at command => AT+CIPCLOSE=0 Fail
02:32:29.693 → 0. at command => AT+CIPMUX=1 OYI
02:32:30.692 → 1. at command => AT+CIPSTART=0,“TCP”,“api.thingspeak.com”,80 Fail
02:32:51.485 → 0. at command => AT+CIPSEND=0,50 OYI
02:32:56.355 → 2. at command => AT+CIPCLOSE=0 OYI
02:32:56.392 → 3. at command => AT+CIPMUX=1 OYI
02:32:57.371 → 4. at command => AT+CIPSTART=0,“TCP”,“api.thingspeak.com”,80 Fail
02:33:18.111 → 0. at command => AT+CIPSEND=0,50 OYI
02:33:22.969 → 2. at command => AT+CIPCLOSE=0 Fail
02:33:27.999 → 0. at command => AT+CIPMUX=1 OYI
02:33:29.014 → 1. at command => AT+CIPSTART=0,“TCP”,“api.thingspeak.com”,80 Fail
02:33:49.835 → 0. at command => AT+CIPSEND=0,50 OYI
02:33:54.745 → 2. at command => AT+CIPCLOSE=0

Thanks

Three things: 1. You are communicating via software serial at 115200. I can't imagine that ever working 2. you are using software serial on a Mega, while a Mega has four (4) hardware serial ports.. I can't imagine why you would ever do that. 3. You might be sending data to Thingspeak more frequently than allowed.

Depending on the sensors, and the type fo ESP8266 you are using, your Mega may be redundant.

use a hardware comm.port instead of softserial