HardwareSerial Problem with ESP8266

Hello everyone! How you guys doing? Im having a little problem trying to use a ESP8266 with a arduino mega 2560. I have connected the RX of the ESP on the TX1 Pin of the board (18), and the TX on the RX1 Pin to the board (17). Im using a Mega 2560. The actual code is:

#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7); 

#define DHTPIN 51     // what pin we're connected to
#define DHTTYPE DHT11   

#include <SoftwareSerial.h>
#include <HardwareSerial.h>
HardwareSerial Serial1(17, 18);

#include <arduino-timer.h>
#include "DHT.h"
#include <LiquidCrystal.h>
auto t = timer_create_default();
DHT dht(DHTPIN, DHTTYPE);
float temperature, humidity;


const char *api_key="PT46QPZM2QBBDND1";     // Api de escrita do ThingSpeak
static char postUrl[150];
void httpGet(String ip, String path, int port=80);

void setup(void) {
  lcd.begin(16, 2);
  Serial1.begin(9600);
  Serial.begin(9600);
  lcd.print("Reading sensor");
  lcd.print("WIFI Connecting");
  Serial.println("Conectando  Wifi....");
  connect_wifi("AT+CWMODE=1",1000);
  connect_wifi("AT+CWQAP",1000);  
  connect_wifi("AT+RST",5000);
  connect_wifi("AT+CWJAP=\"FGHN\",\"cp04182126\"",10000);
  Serial.println("Wifi Connected");
  lcd.clear();
  lcd.print("WIFI Connected.");
  delay(2000);

  dht.begin();
}


void loop() {
  
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  delay(2000); 
  lcd.clear();
  char tempF[6]; 
  char humF[6];
  dtostrf(temperature, 5, 1, tempF);
  dtostrf(humidity, 2, 0, humF);
  lcd.print("T:"); 
  lcd.print(tempF);
  lcd.print((char)223);
  lcd.print("C ");
  lcd.print("H: ");
  lcd.print(humF);
  lcd.print("%");
  send2server();
}

void send2server()
{
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  char tempStr[8];
  char humidStr[8];
  dtostrf(temperature, 5, 3, tempStr);
  dtostrf(humidity, 5, 3, humidStr);
  sprintf(postUrl, "update?api_key=%s&field1=%s&field2=%s",api_key,humidStr,tempStr);
  httpGet("api.thingspeak.com", postUrl, 80);
}



void httpGet(String ip, String path, int port)
{
  int resp;
  String atHttpGetCmd = "GET /"+path+" HTTP/1.0\r\n\r\n";
  //AT+CIPSTART="TCP","192.168.20.200",80
  String atTcpPortConnectCmd = "AT+CIPSTART=\"TCP\",\""+ip+"\","+port+"";
  connect_wifi(atTcpPortConnectCmd,1000);
  int len = atHttpGetCmd.length();
  String atSendCmd = "AT+CIPSEND=";
  atSendCmd+=len;
  connect_wifi(atSendCmd,1000);
  connect_wifi(atHttpGetCmd,1000);
}

void connect_wifi(String cmd, int t)
{
  int temp=0,i=0;
  while(1)
  {
    lcd.clear();
    lcd.print(cmd);
    Serial.println(cmd);
    Serial1.println(cmd); 
    while(Serial1.available())
    {
      char const *ok = "OK";
      if(Serial1.find(*ok))

      i=8;
    }
    delay(t);
    if(i>5)
    break;
    i++;
  }
  if(i==8)
  {
   Serial.println("OK");
        lcd.setCursor(0,1);
      lcd.print("OK");
  }
  else
  {
   Serial.println("Error");
         lcd.setCursor(0,1);
      lcd.print("Error");
  }
}

But im getting this error:

Can anyone help? What im a doing wrong? Im a newbie with Arduino. Thanks!

Posting pictures of error messages, instead of text.

object Serial1 already exists in core, you don't have to create it. remove HardwareSerial Serial1(17, 18); (and remove the includes for SoftwareSerial and HardwareSerial too)

@henry521

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

Im sorry sir, im new to this forum and arduino. The error code was too long, so I thought it woud be easier to see the error as a picture. Have a great day!

I saw that post and it tells to use a <> to post a code, i tried that but apparently didnt worked.

But how the code will now where my ESP8266 is connected?

Does it ?
What it actually says is

  • Paste your code in the box where you are editing your post, select all the code and click on </>

Spot the difference ?

Oohh i see, now i edited! Thanks!

See this tutorial on connecting Mega2560 to ESP8266 and displaying data on a web page. It skips the AT commands.
I also have circuits and code for ESP-01, just not on that page yet
I am also working on a thingspeak example.

Oh nice, any help will be much appreciated!

Which ESP8266 are you using? Have you worked out how to program it using Arduino?

The error message was as long as it needed to be.

Im using a esp 8266 with a esp 01 adapter. Im using a code that i adapted from a youtube video. The error that is was getting trying to compile is gonna, but when i upload it nothing works, like i get error in the wifi_connect function

{Throws hands up in incomprehension}

Serial1 is on pins labeled TX1 and RX1 on the Mega

Ok, now i changed the pins, they were in the incorret position.

Skip the Serial connection to start with and just get a fixed set of data to upload.
Skipping the Serial data means you can check the debug msgs easily by sending them to Serial / usb
What did you use to program the ESP-01?

Im sorry i just saw you response. The errors in the compile fase i got them figured out. But now my AT commands are not working, they show "fail" in the readReplay function...

AT commands stop working if you use Arduino IDE to program the ESP