I need help with fixing this error with ESP8266 and Arduino uno

this is my code:

#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>

SoftwareSerial espSerial(0, 1); // RX, TX pins on the Arduino board

const char* ssid = (wifi name)
const char* password = (wifi pass)

WiFiClient client;

/*
* Connect your controller to WiFi
*/
void connectToWiFi() {
  //Connect to WiFi Network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to WiFi");
  Serial.println("...");
  WiFi.begin(ssid, password);
  int retries = 0;
  while ((WiFi.status() != WL_CONNECTED) && (retries < 15)) {
    retries++;
    delay(500);
    Serial.print(".");
  }
  if (retries > 14) {
    Serial.println(F("WiFi connection FAILED"));
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println(F("WiFi connected!"));
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }
  Serial.println(F("Setup ready"));
}

void setup(){
  Serial.begin(9600);
  //...other setup code here...
  connectToWiFi();
}

void loop() {
  //...other loop code here...
}

And i get this error when trying to upload: A fatal esptool.py error occurred: Failed to connect to ESP8266: Invalid head of packet (0x0C). The parts im using are Arduino UNO, and ESP826Use code tags to format code for the forum6 for wifi.

Hi @coral_n0va

welcome to the arduino-forum.

On an Arduino Uno the IO-pins 0 and 1 are reserved for the Arduino-Uno's USB-to-serial-connection

If you try to use IO-pins 0 and 1 for something you are disturbing the serial communication over the USB-connection of the arduino.

Your title writes about "with ESP8266 and Arduino Uno.
What exact setup are you using?
What exact type of ESP8266?

To which microcontroller did you try uploading the above posted code?

best regards Stefan

Thank you for responding Stefan

The specific ESP8266 i am using is the HiLetgo esp8266 Eps-01 (https://www.amazon.com/dp/B010N1ROQS?psc=1&ref=ppx_yo2ov_dt_b_product_details).

Im not sure if this is helpful, but my goal for this project is to be able to wirelessly flip a light switch from my phone.

Also, Thank you for the information about the IO-Pins. I have seen multiple configurations with the Esp8226 with arduinos, im not sure which would be the best.

I also saw some people said to remove the ATMEGA on the Arduino for similiar projects. I removed it but im wondering whether this is necessary.

I should probably note, that this is my first Arduino project, so im not knowledgeable about a lot of things.

Again Thank you for helping me.

Also heres an image of the setup, i dont have the servos connected right now just so things dont get tangled.

although you define

SoftwareSerial espSerial(0, 1); // RX, TX pins on the Arduino board

you never use espSerial in the code
what is the UNO for?
cannot you use ESP8266 to control your target device? see esp8266-pinout-reference-gpios

if you do require serial communications on the ESP8266 have a look at software-serial-esp8266

I will use the esp serial later in the code

does the program load and work if you remove the statement?

SoftwareSerial espSerial(0, 1); // RX, TX pins on the Arduino board

once you get the WiFi working you can add the serial communications later (using pins other than 0 and 1)

I just tried removing the espSerial, and it didnt change anything. I still got the same error when uploading:

This is the code i used this time:

#include <ESP8266WiFi.h>

const char* ssid = "MyWiFi";
const char* password = "MyPass";

void setup() {
  Serial.begin(9600);
  delay(1000);
  Serial.println("Connecting to WiFi...");

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi!");
}

void loop() {
  // Your code here
}

The error: A fatal esptool.py error occurred: Failed to connect to ESP8266: Invalid head of packet (0x0C)

did you disconnect whatever was connected to pins 0 and 1 ?

Whilst uploading

You are using two microcontrollers.
And because of this fact that you are using two microcontrollers you should make very clear to which microcontroller you are uploading code.

There is a mish-mash mushup.
In post #8 you are using a code that starts with

This is code that is dedicated to run on an ESP8266.
For uploading code to an ESP8266-01 you need to connect the ESP8266-01 to a serial to USB-comport-adapter.
Like this
image
or this

If you tried to upload the code of post #8 to the arduino-uno with IDE adjusted for board arduino-uno it will fail with a different error: no such file or directory

If you had the IDE adjusted to ESP8266 but your USB-cable is connected to the arduino uno this will fail because the ESP_py-tool expects to communicate directly with an ESP8266.

Easiest way to write code for this project to flip a switch wireless from your smartphone would be to use a ESP 32-nodeMCU-board like this
image

Or if $10 are too much using a WeMos D1 mini
which are available for $5

With the ESP32-nodeMCU or ESP8266 D1 mini you can eliminate the arduino-uno completely

the ESP is connectd directly to the computer through the onboard USB-to-serial adapter
and you just write the code to control the servo on the ESP-board

I recommend the ESP32 as it has more flash / more RAM
best regards Stefan

If i were to use the adapter to upload my code would my current setup work?

im sorry if im asking a dumb question.

Iuploaded the code of post #8 to a NodeMCU ESP8266 device without problems
I set Tools>Board to 'Generic ESP8266 module"
what have got the Tools>Board set too?
have you selected the correct COM port?

Hi @coral_n0va

There are no "dumb" questions there are only more or less easy or more or less difficult questions

But there is something else that is important:
beeing

precise

with your words

"the adapter" and "my code" are way too unprecise to be able to clearly say "yes" or "no"

which of the USB-to-serial-adpater??
each board on the picture has an USB-to-serial-adpater

which code?
from which posting number?

uploading to which board? Uno or ESP8266?

So a really precise description would be
If I use this adapter

to upload the code from post # 8 into the ESP8266-01 would my current setup work?

Then the answer is
"I can't say because you have not posted code that does serial communication between the ESP8266 and the Arduino-Uno

The posted code for the ESP8266 does no serial communication
You haven't posted any code for the Arduino-Uno yet.

You may think "oh man this is pedantic!"
Well if you like to fiddle around for weeks without success or even destroying components you
can go on writing "if I do this with that code...."

You are a beginner and as a beginner it is even more important to be very precise and to avoid assumptions and you should not tinkering around without knowing.
Example connecting your ESP8266 to 5V will destroy the ESP8266 because it is a 3.3V-device
uups !

best regards Stefan

I uploaded the code of post #8 to a ESP-01S device without problems using a ESP-01 Helper V3 (ESP8266/ESP32 programmer/development board)
which enables one to program and run code without having to switch programming/run modes etc

I modified the code to my SSID and password and the serial monitor displays

Connecting to WiFi...
Connecting to WiFi...
Connecting to WiFi...
Connected to WiFi!

Thanks again for responding,

I appreciate you bringing the importance of being precise to my attention. I didn't really think about the importance of it, before you brought it up. Multiple times through this project i have made mistakes of not being precise. Going forward, I will try to be as precise and straightforward as I can with this project, and my questions.

With keeping precise in mind, I think that i didn't explain my project as precisely as I could have, so I think I should explain more precisely what i am trying to do. So, what i am trying to do is be able to turn my room lights on and off from my phone, and I watched a video of a YouTuber making a "Arduino Bluetooth Light Switch with Servos" (link: Arduino Bluetooth Light Switch with Servos - YouTube) looked like what i had in mind for this project. I got the same materials that this person had in there video, and assembled everything like they had it. But the problem arose when I couldn't find the app they were using to connect to the Bluetooth module(CC2540 CC2541 AT-09). So i didn't really know what to do after that, so i did research on a bunch of apps for things like this. Multiple apps I tried and none of them worked.

Now your probably wondering how i ended up having an esp8266-01. Well, I tried to connect the Bluetooth module (CC2540 CC2541 AT-09) to blynk, but the only options for adding a device were "Connect to Wi-Fi, scan QR-code, Quickstart device. And i did research (which i probably didn't get the best information) and it seemed that most people using blynk did so by using an WiFi module such as the ESP's. So i got the ESP8266-01, and i have been trying many different things. But I have very little knowledge about this stuff, and now I'm kinda lost. I'm open to any input anyone has about what i should do next.

Thanks again for trying to help.
(Im sorry its long)

You are welcome. Your posting is very good. And not too long. It describes what you have done so far and it was interesting to read.

OK. There are a lot of options to realise what you want to do.
And I have some additional questions: would it be sufficient to switch on / off your lights if you are inside that room?

How many lights to you want to switch?

Thanks for continuing to help me Stefan.

"would it be sufficient to switch on / off your lights if you are inside that room?": yes that would be fine

"How many lights to you want to switch?": there are four lights in my rooms, controlled by one switch.

OK. Do you have an online-shop available which has ESP8266 / ESP32 modules?
Or even easier which country do you live in?

Noticed in the image from post#4 in the image a 9V smoke detector style battery was being used as a power source.

If you are using a 9V battery like this.


Expect short run times and many issues involving low power.