ESP8266 and Arduino

Hello all,
I have bought an ESP8266 but now I'm wondering if there is a way to program it through my Arduino Mega, i tried everything I found on the web but had no luck.
Another problem i have is that if i send AT commands to the ESP i get no response:
This is the code:

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  delay(1000);
}
 
void loop()
{
  if(Serial1.available()){
    byte b = Serial1.read();
    Serial.write(b);
    }
   if(Serial.available()){
    byte c = Serial.read();
    Serial.write(c);
    }
}

I also tried with
Serial1.begin(115200);

If i open the serial monitor, write AT and press send, nothing happens.

Thank you for help

You should tell us WHAT you actually tried. Especially, how you connected your esp to the arduino. How do you power the esp?

Some things to try.
Reverse your RX and TX lines. You didnt say how your serial was connected. Make sure it doesnt exceed 3.3 v in the high state. Not 5v! May have to use level translation.

Then if you get garbage characters, adjust your baud rate on the serial monitor.

Until you can get it to respond to a serial (AT) commands, you are dead. So get that working first.

Then you can proceed to uploading a sketch.

First I'm sorry because i forgot to tell how my arduino and esp are connected:

arduino RX1 -> level converter -> ESP TX
arduino TX1 -> level converter -> ESP RX
arduino GND -> level converter -> ESP GND
arduino 3.3 -> ESP Vcc and ESP CH_PD

Then, I tried what Brontosaur suggested: to switch TX and RX on the arduino, nothing changes, in both cases if i write a command to the serial monitor it outputs back the same thing i wrote.

And a last thing you may want to know, when I turn on the ESP both a red and a "blue" leds light.

vanarre:
in both cases if i write a command to the serial monitor it outputs back the same thing i wrote.

May I ask what you would expect based on the sketch you posted?

Reading here

it seems it should output "ok" when i send him "AT" as command, am I wrong?

vanarre:
First I'm sorry because i forgot to tell how my arduino and esp are connected:

arduino RX1 -> level converter -> ESP TX
arduino TX1 -> level converter -> ESP RX
arduino GND -> level converter -> ESP GND
arduino 3.3 -> ESP Vcc and ESP CH_PD

Then, I tried what Brontosaur suggested: to switch TX and RX on the arduino, nothing changes, in both cases if i write a command to the serial monitor it outputs back the same thing i wrote.

And a last thing you may want to know, when I turn on the ESP both a red and a "blue" leds light.

What is your level convert circuit? Please describe your actual circuit.

Pin 0 on the Arduino is input (RX). Since it is expecting 0..5V and the ESP8266 a 3.3V device (0..3.3V) you need to amplify the EPS8266's TX output. Some active circuit is needed.

Pin 1 on the Arduino is output (RX). It outputs 0..5V and the ESP8266 can only accept 0..3.3V, you have to reduce the signal. A simple resistor divider is all you need.

Also, when you wrote this:

vanarre:
Then, I tried what Brontosaur suggested: to switch TX and RX on the arduino, nothing changes, in both cases if i write a command to the serial monitor it outputs back the same thing i wrote.

Did you remove your AVR AtMega328p chip from the Uno? If the AtMega chip is installed in the board it is driving pin 1, and the ESP8266 would not be able to override it. You would not see any data back on the SerialMonitor screen.

Chuck.

chucktodd:
What is your level convert circuit? Please describe your actual circuit.

It is a level converter module I2C bidirectional 5V 3.3V, I think one made for Arduino.

chucktodd:
Pin 0 on the Arduino is input (RX). Since it is expecting 0..5V and the ESP8266 a 3.3V device (0..3.3V) you need to amplify the EPS8266's TX output. Some active circuit is needed.

I plug the esp TX to the LV side on the level converter, and plug the RX of the arduino to HV of the level converter, is it enough?

chucktodd:
Pin 1 on the Arduino is output (RX). It outputs 0..5V and the ESP8266 can only accept 0..3.3V, you have to reduce the signal. A simple resistor divider is all you need.

Same thing as above, Arduino TX on HV and esp RX on LV.

chucktodd:
Did you remove your AVR AtMega328p chip from the Uno? If the AtMega chip is installed in the board it is driving pin 1, and the ESP8266 would not be able to override it. You would not see any data back on the SerialMonitor screen.

I don't understand what you mean... Do i have to remove a part of my arduino?? And i have an Arduino Mega not an Arduino Uno
One like this:
http://www.ebay.com/itm/2016-New-MEGA2560-R3-Board-ATmega2560-16AU-CH340G-Free-USB-Cable-for-Arduino-/261901186104?hash=item3cfa86f438:g:zcQAAOSwkNZUqluP

On my Arduino i have plugged esp RX to TX01, which is at pin 18 and esp TX to RX01 which is at pin 19
(You can see it on the ebay image)

vanarre:
It is a level converter module I2C bidirectional 5V 3.3V, I think one made for Arduino.

I plug the esp TX to the LV side on the level converter, and plug the RX of the arduino to HV of the level converter, is it enough?

Same thing as above, Arduino TX on HV and esp RX on LV.

I don't understand what you mean... Do i have to remove a part of my arduino?? And i have an Arduino Mega not an Arduino Uno
One like this:
http://www.ebay.com/itm/2016-New-MEGA2560-R3-Board-ATmega2560-16AU-CH340G-Free-USB-Cable-for-Arduino-/261901186104?hash=item3cfa86f438:g:zcQAAOSwkNZUqluP

On my Arduino i have plugged esp RX to TX01, which is at pin 18 and esp TX to RX01 which is at pin 19
(You can see it on the ebay image)

Here is my circuit for connecting an ESP-12F to an Arduino UNO. It also has FTDI programming connection so that I can upload programs directly to the ESP-12F. The 74LVC125 is used to convert from 3.3V TX of the ESP to 5V for both the Arduino and the FTDI. When I connect the FTDI, it disables the Arduino from talking to the ESP. Also there is a circuit that allow the FTDI to force the ESP into program mode.

ESP12 to Arduino

Chuck.

Come on you guys, be nice. I understand giving someone a chance to figure the problem out on their own but there's such thing as going too far.

There's a simple bug in your sketch. First let's get clear on how things are working here. Serial is connected to the USB jack on the Arduino as well as the rx0 and tx0 pins(Arduino pins 0 and 1). This is the way you communicate with the Serial Monitor. Serial1 is connected to rx1 and tx1, this is how you communicate with the ESP8266.

This part of your sketch is ok:

vanarre:

  if(Serial1.available()){

byte b = Serial1.read();
   Serial.write(b);
   }

You are reading from the ESP8266 and then writing what you read to the Serial Monitor, that's correct.

But you made a mistake here:

vanarre:

   if(Serial.available()){

byte c = Serial.read();
   Serial.write(c);
   }
}

Here you are reading from Serial but then you are writing what you read right back to Serial. That's why:

vanarre:
in both cases if i write a command to the serial monitor it outputs back the same thing i wrote.

You need to change that part of your sketch to:

  if(Serial.available()){
    byte c = Serial.read();
    Serial1.write(c);
    }

Now you're reading from the input from the Serial Monitor and writing it to the ESP8266, connected on Serial1.

vanarre:

  Serial1.begin(9600);

The default setting on the ESP8266 is 115200 baud. So unless you have configured it to run at 9600 you should change this to:

Serial1.begin(115200);

chucktodd:
Since it is expecting 0..5V and the ESP8266 a 3.3V device (0..3.3V) you need to amplify the EPS8266's TX output.

Are you sure? Many many people, including myself have used the 3.3v output from the ESP8266 to communicate with a 5V Arduino. I think you're making this way more complicated than it needs to be. Now the level shifter on the 5V output from the Arduino I do think is a good idea, though supposedly the CEO of Espressif has stated the ESP8266 is 5V tolerant:Teo Swee Ann but I'm not going to trust that until I hear it announced more formally.

chucktodd:
If the AtMega chip is installed in the board it is driving pin 1, and the ESP8266 would not be able to override it. You would not see any data back on the SerialMonitor screen.

The ESP8266 is connected to Serial1, not Serial:

vanarre:
arduino RX1 -> level converter -> ESP TX
arduino TX1 -> level converter -> ESP RX

Now here I do see an electrical issue:

vanarre:
arduino 3.3 -> ESP Vcc and ESP CH_PD

A standard Arduino 3.3V output can't provide the current necessary to reliably run an ESP8266. These things can take over 500mA so you need to provide a power supply that can handle that.

Damn the error, thank for making me notice it.
Anyway i corrected it and now, when i write anything, i get strange characters, every time different, for example now i have written:

AT i got KJ
qwerty i got žwîõÿù
AT i got hÕ
AT+RST i got U5Wý÷T%
AT againg and i got ¡ýª

This happens both with Serial1.begin(9600) and begin(115200) and every number i put between brackets

pert:
Now here I do see an electrical issue:A standard Arduino 3.3V output can't provide the current necessary to reliably run an ESP8266. These things can take over 500mA so you need to provide a power supply that can handle that.

Should i connect arduino Vin or 5V with level converter and then to esp vcc and ch_pd? anyway i tried it but the esp doesn't turn on...

Do you have the same baud rate set in the menu at the bottom right of the serial monitor that you have set in your sketch for Serial. In the code you posted previously:

vanarre:

  Serial.begin(9600);

The menu setting should be 9600.

Also, for communicating between the Arduino and the ESP8266, RX1-TX and TX1-RX is the correct wiring(ideally with the level shifter on TX1-RX line).

The reason you'll see people talking about connecting RX0-RX, TX0-TX and removing the ATmega328P from an Uno is they're using the Uno as a serial to USB converter to communicated directly from Serial Monitor to the ESP8266, with no intermediary sketch running on the microcontroller as you have. Serial communication is alway rxtx/txrx but in that usage actually you are doing rxtx/txrx even though the labels on the board indicate otherwise because the RX line on the USB to serial chip on the Uno is connected to the TX pin and vice versa. This can be a good troubleshooting technique to rule out any issues caused by the microcontroller, sketch, etc. but I think it's much better just to buy a FTDI breakout board to do that instead of using an Uno.

Yeah, i have connected RX1 to TX and TX1 to RX, but don't understand why I don't get "ok" when i write "AT" yet...
And yes, i have 9600 on the serial monitor.

I'm not sure what the problem is. I haven't used my ESP8266 via serial for a little while because I've been using it standalone but I remember you need to set the line endings menu in Serial monitor to "Both NL & CR" but I don't think that would cause the gibberish. Have you tried connecting TX on the ESP8266 directly to RX1 on the Arduino without the level shifter?

vanarre:
Should i connect arduino Vin or 5V with level converter and then to esp vcc and ch_pd? anyway i tried it but the esp doesn't turn on...

I think you'll be able to get away with the 3.3V from your Arduino for just sending AT to the ESP8266. It draws much more current when it's using the WiFi radio, that's when you're likely to see unreliability with an insufficient power supply.

Barely any useful information and what is there is irrelevant to someone trying to use an ESP8266 running the AT firmware with an AVR. Programming the ESP8266 is certainly an alternative but it does have its own unique issues. Getting the communication working between the AVR and the ESP8266 can be tricky for some but once that's working you are still able to benefit from the excellent community support the AVRs have gained over the years while leaving the ESP8266 to do what it does best. Most of the problems I see stem from trying to use hardware serial on an MCU with only one available or trying to use SoftwareSerial at too high a baud rate. Vanarre's configuration avoids both those issues so is more likely to succeed. ESP8266 has gotten a lot better but is still nowhere near as user friendly as an Arduino Mega.

pert:
I'm not sure what the problem is. I haven't used my ESP8266 via serial for a little while because I've been using it standalone but I remember you need to set the line endings menu in Serial monitor to "Both NL & CR" but I don't think that would cause the gibberish. Have you tried connecting TX on the ESP8266 directly to RX1 on the Arduino without the level shifter?

I tried, the blue led stops working and when i open the serial monitor (on 9600 baudrate) i keep getting data with strange symbols, more or less every half second one appears.
Setting both nl & cr doesn't helped too :frowning:

vanarre:
i keep getting data with strange symbols, more or less every half second one appears.
Setting both nl & cr doesn't helped too :frowning:

Are this strange symbols some kind of "y"?

If it is, you need to "check" every character that ESP sends back to you:

void tryCommand(String cmd) {
  String replyEsp8266 = "";
  
  esp8266.println(cmd);
  delay(100);  


  if(esp8266.available()) // check if the esp is sending a message
    {
    while(esp8266.available())
      {
      char c = esp8266.read(); // read the next character.
      
      if(c != char(-1)) //    Sometimes, esp8266 has nothing to send, but esp8266.available() doesn't work
        replyEsp8266 += c; // so esp send -1 (nothing to send) and is interpreted as this character: y
      }

    Serial.println(replyEsp8266);
    }
  else
    Serial.println("Esp8266 doesn't respond");

}

With this code:

void setup()
{
  Serial.begin(9600);
  Serial1.begin(115200);
  delay(1000);
}
 
void loop()
{
   if(Serial.available()){
    String c = String(Serial.read());
    tryCommand(c);
    }
}

void tryCommand(String cmd) {
  String replyEsp8266 = "";
  
  Serial1.println(cmd);
  delay(100);  


  if(Serial1.available()) // check if the esp is sending a message
    {
    while(Serial1.available())
      {
      char c = Serial1.read(); // read the next character.
      
      if(c != char(-1)) //    Sometimes, esp8266 has nothing to send, but esp8266.available() doesn't work
        replyEsp8266 += c; // so esp send -1 (nothing to send) and is interpreted as this character: y
      }

    Serial.println(replyEsp8266);
    }
  else
    Serial.println("Esp8266 doesn't respond");

}

If i write AT I get something like this: (it changes everytime, irrespective of what the baudrate of Serial1 is)
÷MMú
=Zý
û©Hø
urVO

I have tried it both with esp tx on the level shifter and esp tx directly to arduino, in the latter case it outputs Esp8266 doesn't respond