how to get AT response from ESP8266 connected to a

2019-06-09 02:06发布

问题:

I am fighting with ESP8266 wifi module and connecting arduino. After updating firmware to newest version i started to programm arduino to get data incoming from wifi. I saw many examples about maiking webserver via ESP8266 but none of them works for me.

ESP is connected to my Arduino Leonardo: >

Arduino -> ESP8266

power 3.3V -> vcc

ground -> ground

tx -> rx (via logic level converter 5->3.3V)

rx -> tx (via logix level converter

power 3.3V ->gpio0 (without any resistors)

I made simple sketch:

void setup(void){

  Serial.begin(9600);
  Serial1.begin(115200);

}

void loop() {
  if(Serial1.available())
  {
    Serial.println("WIFI IS AVAILABLE");
    Serial1.println("AT");
    delay(1000);
  } else {
    Serial.println("WIFI not available.");
    delay(1000);
  }
}

After executing it ESP8266 is powered (red led is on) and also every second blue led (blinks). That makes me sure that in fakt "AT" command is transmited to module. But there are also two issues:

  • i want to get response from esp - in this case word "OK". I tried Serial1.read() but it only reads one byte. Serial1.readString() makes my messages "wifi not available" and sametimes "wifi is available" as if for a while the connection would be unavailable

  • after uploading sketch to arduino and having powered esp8266 wifi module is always unavailable - i need to power the module off and on again to have it working.

Anybody please can help me?

回答1:

What you need to do is change your approach a bit. Do not check if data is available. The trick is to send the module something and then check for data.

Do something like:

while (Serial.available() > 0)
  Serial.read();

to clear the buffer before any command you want to send. Then send the command. Then check for data as a response.

Do not rely on that Blue LED as any indication. It is only an indication that the ESP8266 is busy using the WiFi in some sort of way, whether it is doing keepalives, initializing WiFi or whatever. It can be totally unrelated to whatever you are sending. If you do not receive a valid response then you must assume that there is comms issues between you and the module. One thing though is that if that Blue LED never goes off then either the module has frozen or the firmware was corrupted. I have had that many times. I then reload the firmware and usually that fixes it. It usually only happens during development times where I reset, upload code or change wires.

I use mine with an atmega328 on a separate slef-built board and not the one on the Uno and run that board on 3.3v itself and then use a logic level converter between that atmega board and my Uno so that I can program it. But I have had sporadic issues with non-comms but I suspect it might be power related. Be aware that running your Serial via the logic level converter might also be causing comms issues.

Proposed wiring: All pins except RX,TX,VCC and GND goes to VCC via 10K pullup resistors. RX goes to the arduino's TX and TX goes to the arduino's RX. Of course you know where VCC and GND goes.