Arduino Uno and ESP8266 will not work

I am trying to get an Arduino Uno to communicate to an ESP8266. I can get it to work in pass-through mode (using the Serial Monitor), but when I try to upload a sketch to the Arduino to communicate with the ESP8266, I get a series of errors. I have selected "Arduino" from the ESP8266 Boards (3.0.2) to get the ESP8266 library.

Tried various sketches. Here is one:

#include "ESP8266WiFi.h"

const int RSSI_MAX =-50;// define maximum strength of signal in dBm
const int RSSI_MIN =-100;// define minimum strength of signal in dBm

const int displayEnc=1;// set to 1 to display Encryption or 0 not to display

void setup() {
  Serial.begin(115200);
  Serial.println("Robojax Wifi Signal Scan");
  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(2000);

  Serial.println("Setup done");
}

void loop() {
  Serial.println("Wifi scan started");

  // WiFi.scanNetworks will return the number of networks found
  int n = WiFi.scanNetworks();
  Serial.println("Wifi scan ended");
  if (n == 0) {
    Serial.println("no networks found");
  } else {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i) {
      // Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(") ");
      Serial.print(WiFi.SSID(i));// SSID

                  
      Serial.print(WiFi.RSSI(i));//Signal strength in dBm  
      Serial.print("dBm (");

      
      Serial.print(dBmtoPercentage(WiFi.RSSI(i)));//Signal strength in %  
     Serial.print("% )"); 
      if(WiFi.encryptionType(i) == ENC_TYPE_NONE)
      {
          Serial.println(" <<***OPEN***>>");        
      }else{
          Serial.println();        
      }

      delay(10);
    }
  }
  Serial.println("");

  // Wait a bit before scanning again
  delay(5000);
  WiFi.scanDelete();  
}// loop



/*
 * Written by Ahmad Shamshiri
  * with lots of research, this sources was used:
 * https://support.randomsolutions.nl/827069-Best-dBm-Values-for-Wifi 
 * This is approximate percentage calculation of RSSI
 * WiFi Signal Strength Calculation
 * Written Aug 08, 2019 at 21:45 in Ajax, Ontario, Canada
 */

int dBmtoPercentage(int dBm)
{
  int quality;
    if(dBm <= RSSI_MIN)
    {
        quality = 0;
    }
    else if(dBm >= RSSI_MAX)
    {  
        quality = 100;
    }
    else
    {
        quality = 2 * (dBm + 100);
   }

     return quality;
}//dBmtoPercentage

Error back is:

Connecting......................................_____
Traceback (most recent call last):
File "C:\Users\brumm\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2/tools/upload.py", line 66, in
esptool.main(cmdline)
File "C:/Users/brumm/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/esptool\esptool.py", line 3552, in main
esp.connect(args.before, args.connect_attempts)
File "C:/Users/brumm/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/esptool\esptool.py", line 529, in connect
raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

for upload over Uno's USB chip, you have to connect the esp8266 and Uno RX to RX and TX to TX because that way the esp8266 is connected to USB chip RX to TX. but first upload Blink to Uno to have a sketch without Serial.begin

Thanks for the quick reply.

Yes, in pass through mode it all works fine, using Serial Monitor. However my understanding is the code (e.g. in the example) gets loaded onto the Uno (RX is then changed to TX, etc) and the Uno should perform the comms with the ESP8266 card. It is uploading that sketch which fails.

for upload over Uno's USB chip...

ESP32 serial to Arduino serial - Google Search

Yes, I have Googled this more times than I can remember...

In the example I shared, my understanding is that the sketch s loaded on the Uno, which then communicates to the ESP8266. Right?

But I cannot get the sketch to load on the Uno because of the error (listed previously).

Pass through to the ESP8266 works fine.

please read again comment #3

A sketch for a ESP8266 will not load/run on a UNO.

@Juraj, @mrburnette thanks for the replies.

Are you saying that the sketch originally shared is for the ESP8266?

Again, my understanding is that the sketch shared by Robojax is loaded onto the Uno.

Is that assumption wrong?

Which ESP8266 board/module are you using and does it have a USB interface ?

The Robojax sketch you have shown appears to be one which runs on the ESP8266 and not the Uno.
Once you successfully load that sketch onto the ESP8266 you will no longer be able to issue AT commands to talk to the ESP8266 because the pre-installed interpreter will be overwritten (if that is what you mean by pass through )