- Wed Mar 11, 2015 10:33 pm
#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
}
}