Post topics, source code that relate to the Arduino Platform

User avatar
By MK1888
#11841 Several things:

1. That gibberish is probably the reply.

2. When I first connected an ESP8266 to an Arduino I was getting gibberish with the SoftSerial library, because it can cause timing conflicts with the hardware serial port. I wound up going with AltSoftSerial.

3. I don't think SoftSerial works well at 115200. Try 9600. Or try AltSoftSerial.
Last edited by MK1888 on Thu Mar 12, 2015 6:22 pm, edited 1 time in total.
User avatar
By ricg
#11843 it's best to start out as simple as possible.
I found the following code worked well for entering AT cmds and getting output from the esp.
I had no problems using software serial at 9600baud, but everyone's experience seems to vary when it comes to that.
if this doesn't help then connecting the esp directly to an ftdi breakout may help you get started.

Looking at your code the first thing that jumps out is that you are depending on an arbitrary time for the AT cmds to complete. The is in your send routine. The problem with that approach is some cmds are dependent on the previous cmd to complete and they may not get executed at all if the timing is wrong.
Here is a routine that you could try. you'll need to call it after sending to the esp.
ie...
espSerial(cmd); // send cmd to esp
wait4result( "OK", 1000); // wait for esp to return "OK" or timeout after 1s if it doesn't appear.
ie....
int wait4result(char *s, unsigned short timeout) {
unsigned long tm=millis();

while( millis() - tm <= timeout ) {
if (espSerial.find(s)) { // match result
Serial.println(s);
return 1;
}
}
// send timeout msg to console.
Serial.print(F("TimedOutWaitingFor: "));
Serial.println(s);
return 0;
}

----------------------- simple code for entering AT cmds ---------------------------------------------


#include <SoftwareSerial.h>

/* This sketch allows entering AT cmds to control
the esp8266
*/

// Arduino RX=10,TX=11 connected to TX,RX on esp8266-05
SoftwareSerial espSerial(10, 11);

void setup()
{
Serial.begin(9600);
Serial.println("starting esp8266 comm sketch");

// set the data rate for the SoftwareSerial port
espSerial.begin(9600);
}
char cmd[64];

void loop()
{
int i=0;

if (espSerial.available()) // input from the esp8266
Serial.write(espSerial.read()); // write to host

while (Serial.available()) {
cmd[i]=Serial.read(); // buffer cmd
i++;
delay(5);
}

if( i ) { // if host sent us cmd
cmd[i]=0;
espSerial.println(cmd); // send to esp8266
}
}
User avatar
By jthefreak
#11893 Are you sure your ESP's baud rate is indeed 115200? The later firmware makes the default 9600... unless your ESP is pre-December 2014ish. If you are certain the baud is 115200, set the baud to 9600 with an ftdi adapter.

Software.serial does work with the esp, but not at 115200. Don't define Software.serial on pins 0,1.. use others, and enable the serial monitor!

I would first go back to your 'hello world' code, set the esp baud at 9600 and use different pins for the esp and try it again and see what happens!