1

I'm using two header file, SoftwareSerial.h and ESP8266WiFi.h. When I compile and upload programs, with ESP8266WiFi.h, it had an error "Failed to connect to ESP8266: Timed out waiting for packet header". I tried it with only ESP8266WiFi.h, but it had an same error. To use this header file, what should I do?

Here's my circuit and code.

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid = "circuits4you";
const char *pass = "password"; 

unsigned int localPort = 2000; // local port to listen for UDP packets

IPAddress ServerIP(192,168,4,1);
IPAddress ClientIP(192,168,4,2);


WiFiUDP udp;

char packetBuffer[9];   //Where we get the UDP data
void setup()
{
    Serial.begin(9600);
    Serial.println();
    WiFi.softAP(ssid, pass);    //Create Access point

    Serial.println("Starting UDP");
    udp.begin(localPort);
    Serial.print("Local port: ");
    Serial.println(udp.localPort());
}
void loop()
{
    int cb = udp.parsePacket();
    if (!cb) 
    {
      if(Serial.available()>0)
        {
        udp.beginPacket(ClientIP, 2000);

        char a[1];
        a[0]=char(Serial.read()); //Serial Byte Read
        udp.write(a,1); //Send one byte to ESP8266 
        udp.endPacket();
        }
    }
    else {
      udp.read(packetBuffer, 1);
      Serial.print(packetBuffer);
      delay(20);
    }
}

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Taehwa Yim
  • 23
  • 2
  • 5
  • You didn't mentioned what you are intend to do with the ESP8266? to upload the firmware? or to communicate with it using AT commands? If you are uploading firmware, you need to ground the GPIO0 and take a look at [ESP266 Timed out waiting for packet header](https://stackoverflow.com/questions/37742559/esp266-timed-out-waiting-for-packet-header). If you are trying to communicate using AT Command, you might not even need the ESP8266WiFi.h. Otherwise, show your code if you need someone to help you. – hcheung Dec 20 '19 at 01:18
  • I used ESP8266 to communicate each other with UDP protocol, and I edited the contents. – Taehwa Yim Dec 20 '19 at 02:55
  • And I had no USB to UART adapter, but is it ok?? – Taehwa Yim Dec 20 '19 at 03:10

1 Answers1

0

Your code is designed for using ESP8266 as a stand-alone microcontroller running with Arduino Core. I realised that you are confusing about the operation modes of ESP8266. There are basically two ways to operate with an ESP8266.

  • Uses ESP8266 as a stand-alone micropcontroller, in this case, the ESP8266 needs to have the firmware such as RTOS or Arduino Core for ESP8266;
  • Uses ESP8266 as a plain WiFi device which can be communicate with AT Commands.

Currently your code is designed for running on a stand-alone ESP8266, but I realised that your hardware configuration is configure for communicating with ESP8266 as a WiFi device, and you have the AT command firmware on your ESP8266. In order for Arduino to communicate with ESP8266 via AT Commands, you will need run this Arduino sketch to get started, the sketch basically read the AT command you type in on Serial Monitor and send it to ESP8266, and echo out the msg received from ESP8266:

#include <SoftwareSerial.h>

SoftwareSerial espSerial(2, 3);  //RX,TX

//  Arduino pin 2 (RX) to ESP8266 TX
//  Arduino pin 3 to voltage divider then to ESP8266 RX
//  Connect GND from the Arduiono to GND on the ESP8266
//  Pull ESP8266 CH_PD HIGH

void setup() 
{ 
    Serial.begin(9600);     // for communication with the host computer
    espSerial.begin(9600);  // Soft derail for communicate with ESP8266
    // Remember to set 'Both NL & CR' in the serial monitor   
}

void loop() 
{
    // listen for data from the ESP8266 and then write it to the serial monitor
    if ( espSerial.available() ) {  
      Serial.write( espSerial.read() );  
    }

    // listen for manual input from user and send it to the ESP8266
    if ( Serial.available() ) {
      mySerial.write( Serial.read() );  
    }
}

Remember to set 'Both NL & CR' in the serial monitor

There is a good official AT Command example from Expressif that you could follow, include UDP communication. You can manually type in the AT Commands via Serial Monitor step by step to test the ESP8266. Once everything works, you could then write the sketch to automate all the AT commands in program to replace the code in the loop().

If communicating with ESP8266 is too much of work and too tedious for you, you might want to consider to use ESP8266 as a stand-alone microcontroller by upgrading your ESP8266 with firmware such as Arduino Core. You can then use the code you shown. But your hardware need to change a little bit in order to set the ESP8266 to firmware flash mode, you need to ground the GPIO0 on ESP8266, and have a reset switch on ESP8266 between reset pin and ground, so that you can press it to put ESP8266 into programming mode.

Please make sure your target board is set to ESP8266 on your Arduino IDE in this case.

hcheung
  • 3,377
  • 3
  • 11
  • 23
  • I used firmeware as 'v0.9.2.2 AT Firmware.bin' but you are saying that i had should used 'ESP8266_NONOS_SDK_V2.0.0_16_08_10' right? – Taehwa Yim Dec 20 '19 at 05:13
  • If you are having the AT firmware, then this code will not work, and you can only control the ESP8266 from Arduino using at command set, it is possible to establish UDP via AT commands but not with your code here. – hcheung Dec 20 '19 at 05:41
  • I changed my firmware from this : 'https://www.electronicshub.org/update-flash-esp8266-firmware/ ' – Taehwa Yim Dec 21 '19 at 14:25
  • I think you are still confusing about the operation mode of ESP8266. What you did is simply update from an older AT Command firmware to the latest Espressif Official AT Command firmware. Your ESP8266 is still only access communication via AT Commands (and so is your hardware configuration). When using AT command firmware, you run your code from Arduino Uno, and communicate with ESP8266 via a series of [AT commands](https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf). – hcheung Dec 22 '19 at 00:50
  • If you want to use ESP8266 as a regular microcontroller using the code you shown here, you need to install the [ESP8266 Arduino Core](https://github.com/esp8266/Arduino) firmware, and in this case, your target board on Arduino IDE should be ESP8266 and you merely using the Arduino Uno as a programmer. – hcheung Dec 22 '19 at 00:50
  • I totally rewrite my answer based on your latest comment, please take a look. – hcheung Dec 22 '19 at 02:08