Arduino UNO + ESP8266 unable to to connect to the WiFi

Hi,

I have an UNO and an ESP8266. I have followed this tutorial and have used "AT" and other commands to ensure that my ESP is able to connect to my home WiFi.

Now I am trying to connect using the following code below and I keep getting various errors.

#include <ESP8266WiFi.h>

void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.begin("network-name", "pass-to-network");

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {}

The errors:

Executable segment sizes:
ICACHE : 32768 - flash instruction cache
IROM : 238688 - code in flash (default or ICACHE_FLASH_ATTR)
IRAM : 26733 / 32768 - code in IRAM (IRAM_ATTR, ISRs...)
DATA : 1496 ) - initialized variables (global, static) in RAM/HEAP
RODATA : 960 ) / 81920 - constants (global, static) in RAM/HEAP
BSS : 25664 ) - zeroed variables (global, static) in RAM/HEAP
Sketch uses 267877 bytes (27%) of program storage space. Maximum is 958448 bytes.
Global variables use 28120 bytes (34%) of dynamic memory, leaving 53800 bytes for local variables. Maximum is 81920 bytes.
esptool.py v3.0
Serial port /dev/cu.usbmodem1422401
Connecting......................................____Traceback (most recent call last):
File "/Users/m/Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.1/tools/upload.py", line 66, in
esptool.main(cmdline)
File "/Users/m/Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.1/tools/esptool/esptool.py", line 3552, in main
esp.connect(args.before, args.connect_attempts)
File "/Users/m/Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.1/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

I tried changing the board from Arduino UNO to Generic ESP8266 Module to Node MCU 1.0.

Am I misunderstanding how the ESP9266 works in general?

Maybe. The AT mode works only if a special software is running on the ESP8266. There are a lot of ESP8266 modules available but you didn't tell us which exact one you're using. You also failed to tell us how you connected the UNO and the ESP.

We don't even know what you're trying to achieve. What should the combo do in the end?

I am guessing you have your UNO pugged in when you are trying to program that sketch.
You need to program the ESP directly
See this project ESP8266-01 Wifi Shield
and this one for sending serial between Arduino's
Arduino to Arduino via Serial

Note: it is possible to program your ESP8266 using your Arduino as the TTL - USB connection.
see ESP8266 WiFi module programming with Arduino UNO board - Simple Projects

Of course you can use your arduino uno and an ESP8266-01-module to do WiFi.
Doing it this way requires communication between the Arduino-Uno and the ESP8266 using AT-commands (with the AT-command-firmware) or with some serial communication between your Arduino Uno and the ESP8266 which you coded on your own.

I want to describe another option which can be programmed with the Arduino-IDE just the same way as an Arduino itself:

The successor of the ESP8266 is the ESP32 which has more flash, more RAM more ADC-channels and an RTC on board.

Both the ESP8266 and ESP32 are microcontrollers on their own which can do switching IO-Pins just the same way as an Arduino-UNO to drive LEDs, Servos, Displays just about anything an Arduino-Uno can do. Sensors that must have 5V need a voltage-converter from 3.3V to 5V anything else is the same.

Using an ESP32-nodeMCU eliminates the communication between the "WiFi"-module and the microcontroller. No extra data-exchange is needed because your data is already "in place".

example:
Arduino-Uno-ESP-combination

  1. Arduino-Uno detects "button pressed"
  2. This button pressed shall be send over WiFi Arduino-Uno send serial "Button pressed" to ESP8266
  3. ESP8266 receives "button pressed" on serial interface
  4. and re-directs "button pressed" to WiFi

ESP32-nodeMCU:

  1. ESP32 detects "button pressed" send "button pressed" over WiFi - done

So if you can afford to buy a ESP32-nodeMCU-board for 6 to 10 dollars the ESP32 can replace the combination of the Arduino-Uno-ESP8266-01

best regards Stefan