Arduino Board Help using ESP8266 and DHT11 Sensors

Hello,
I am currently working having temperature and humidity sensors work with the Arduino board using an ESP8266 for wifi. Currently the pins for the WiFi module are set to pins 2 and 3 for RX and TX respectively. However, when I switch the pins within the code to pins 0 and 1 for RX and TX respectively. I receive an error code to this degree.
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00 Failed uploading: uploading error: exit status 1
I am unsure what needs to be changed in order for the parts to work on these pins. Any help would be greatly appreciated. Source code will be shown below.

#include <SoftwareSerial.h>
#include <dht11.h>
#define RX 2 // TX of ESP8266 connected to pin2 of Arduino
#define TX 3 // RX of ESP8266 connected to pin3 of Arduino
#define dht_apin 6 // Analog Pin sensor is connected to
#define dht_bpin 7 // Second analog pin sensor is connected to
dht11 dhtObject; // One Sensor Object
dht11 dhtObject2; // second sensor object
String AP = "Test Hotspot2"; // AP NAME
String PASS = "Ssss1234"; // AP PASSWORD
String API = "27X9GGXRFHBBJ4ED";   // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 
int valSensor = 1;
  
SoftwareSerial esp8266(RX,TX); 
  
void setup() {
  Serial.begin(9600);
  esp8266.begin(38400);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}

void loop() {
 String getData = "GET /update?api_key="+ API +"&field1="+getTemperatureValue()+"&field2="+getHumidityValue()+"&field3="+getTemperatureValue2()+"&field4="+getHumidityValue2();
 sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 esp8266.println(getData);delay(1500);countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
}


String getTemperatureValue() {
   dhtObject.read(dht_apin);
   Serial.print(" Temperature(C)= ");
   int temp = dhtObject.temperature;
   Serial.println(temp); 
   delay(50);
   return String(temp); 
}

String getHumidityValue() {
   dhtObject.read(dht_apin);
   Serial.print(" Humidity in %= ");
   int humidity = dhtObject.humidity;
   Serial.println(humidity);
   delay(50);
   return String(humidity);
}

String getTemperatureValue2() {
   dhtObject2.read(dht_bpin);
   Serial.print(" Temperature(C)= ");
   int temp2 = dhtObject2.temperature;
   Serial.println(temp2); 
   delay(50);
   return String(temp2);
}

String getHumidityValue2() {
   dhtObject2.read(dht_bpin);
   Serial.print(" Humidity in %= ");
   int humidity2 = dhtObject2.humidity;
   Serial.println(humidity2);
   delay(50);
   return String(humidity2); 
}

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("Pass");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
 }

Hi! Welcome to the Forum!

Problem 1) If your Arduino Board is an Uno (is it an Uno?), pins 0 and 1 are the hard serial interface, also used to upload the sketch and to communicate with the Serial Monitor. So you can use these pins to talk to to the ESP8266 if you disconnect the ESP while uploading the sketch and if you don´t need Serial Monitor anymore.

Problem 2) ESP8266 is a 3.3V device while Uno is a 5V device. So the communication between the 2 boards might use level shifters.

Suggestion: ESP8266 is a standalone MCU and DHT 11 can work at 3.3V. Why don´t you connect DHT11 directly in the ESP?

Hello,
thank you for taking the time to respond. My board is an Arduino UNO R3 using an ESP8266-01 module which is just used for WiFi alone. I have 5V connected to the sensors and 3.3V connected to the WiFi module.

As implied by @Brazilino, anything connected to the Rx/Tx of the Uno R3 will interfere with uploads. So disconnect, upload and reconnect.

It's not only about the power pins; an Uno outputs 5V signals on the IO pins (in your case the Tx pin); this can damage the inputs of the ESP8266 (in your case the Rx input) if no precautions were taken.

Take a look at these ideas:

https://www.youtube.com/watch?v=Xfs-woLjhLA