ESP8266-01 with MLX90614 (Using thingspeak) - AT commands no response

Done compiling, but serial monitor not consistent showing results. Even showing results, always shows wdt reset. This is the code:

#include <SoftwareSerial.h>
#include<ESP8266WiFi.h>
#define RX 0
#define TX 1
#include <Wire.h>
#include <Adafruit_MLX90614.h>
int red = 6;
int green = 5;
int buzzer = 11;
float arrayData[5];
int i = 0;
int j, k;
int x = 0;
String AP = "Wifi";       // AP NAME
String PASS = "12345"; // AP PASSWORD
String API = "3LWVPF4PXH079JPA";   // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 
float valSensor = 1;
SoftwareSerial wifi(RX ,TX ); 

Adafruit_MLX90614 mlx = Adafruit_MLX90614(); 
  
void setup() {
  Serial.begin(9600);
  wifi.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
  mlx.begin();
  pinMode(red, OUTPUT);
  pinMode(red, LOW);
  pinMode(green, OUTPUT);
  pinMode(green, HIGH);

}

void loop() {
 valSensor = getSensorData();
 String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);
 sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 wifi.println(getData);delay(1500);countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
  digitalWrite(green, HIGH); 
   Serial.println();
  for (int i = 0; i < 5; i++) {
      arrayData[i] = arrayData[i + 1];
  }
  arrayData[4] = mlx.readObjectTempC();
  for (int i = 0; i < 5; i++) {
    Serial.println(arrayData[i]);
    
  }
    delay(1000);
    Serial.println();

  for (int i = 0; i < 5; i++) {
    if (arrayData[i] < 37.5)break;
    if (i == 4) {
      digitalWrite(green, LOW);
      digitalWrite(red, HIGH);
      delay(500);

      for (j = 0; j < 3; j++) {
        tone(buzzer, 300);
        delay(500);
        noTone(buzzer);
        delay(500);
      }
      digitalWrite(red, LOW);
    } else {
      digitalWrite(red, LOW);
      digitalWrite(green, HIGH);
      delay(1000);
    }
  }
}


float getSensorData(){
  
  Serial.print("Ambient = ");
  Serial.print(mlx.readAmbientTempC());

  Serial.print("*C\tObject = ");
  Serial.print(mlx.readObjectTempC());
  Serial.println("*C");
  delay(2000);

  return mlx.readObjectTempC(); 
}

void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    wifi.println(command);//at+cipsend
    if(wifi.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;
  
 }

Can I get some help? Thank you.

First you have to provide the necessary information. First start with links to the used hardware! The standard ESP8266-01 doesn't have a pin 11 so I guess you're using another board. Maybe you use some other Arduino and connect it to a ESP8266-01 only for WiFi connectivity but that's too much guessing. That's the reason we always expect you to provide a complete wiring diagram in addition to the code.

Given you use an Arduino UNO connected to the ESP8266-01, this cannot work because you activate the hardware serial on pins 0 and 1 (Serial.begin(9600):wink: and also activate a SoftwareSerial on the same pins. You cannot use the ESP8266's WiFi on the hardware serial pins and do debugging over the same pins.

Thanks for the input, pylon. Yes, i am using arduino UNO. However, if i change the connection of RX and TX to pin 3 and 4, it will show espcomm_sync failed. May I know why is it so?

warning: espcomm_sync failed
error: espcomm_open failed
An error occurred while uploading the sketch
error: espcomm_upload_mem failed

You probably selected the wrong board. espcomm is used to upload a sketch to an ESP processor. As you're using the UNO it shouldn't be used.

So, any suggestion what board should i use to replace my UNO? But why if i connect the RX and TX to pin 0 and 1 it wont show this error?

OMG, what a mess. If you wire esp8266 to RX and TX pin, the sketch will upload to esp8266. The Uno is then only a USB-to-TTL-Serial adapter.
but the uploaded sketch replaces the AT firmware.
on esp8266 Arduino you use the ESP8266WiFi library, not the AT commands. see the examples of the ESP8266WiFi library in IDE Examples menu

you would use AT commands if you had AT firmware in esp8266 and Uno as 'host' which sends AT commands to WiFi module.

Thank you Juraj for the replying. What should i do then? Connect the RX and TX pin to pin 0 and 1 or other digital pins? And also, i have tried to download AT firmware in esp8266. But the serial monitor show all AT commands fail. What should i correct?

what you want?
a) buy a Wemos D1 mini, wire the sensor to it
b) upload AT firmware into esp-01 and write a sketch for Uno with my WiFiEspAT library to use the esp-01 as WiFi adapter

c) advanced option: wire the sensor to esp-01

How does b and c can be done? May i know how does these two options work? :grin: :grin:

c) esp01 has esp8266 MCU and 4 pins are accessible. io 1 and io 3 are Serial RX and TX. io 2 and io 0 are boot config pins but can be used if the boot state is right. your current configuration uploads Arduino sketch to esp-01 using the USB chip of Uno.

b) GitHub - jandrassy/WiFiEspAT: Arduino networking library. Standard Arduino WiFi networking API over ESP8266 or ESP32 AT commands.