Hi i am trying to write a library to communicate between the arduino and the esp 8266. I wrote a command that that sends an AT command to the esp chip. So far I have tested these commands : AT, AT+GMR, AT+CIFSR, AT+CIPMUX=1 etc. with no issue the communication seem robust.
When I tried to send the AT+CIPSERVER=1 command the function did not work. After some attempts to debug the program I manage to get it working but I do not understand why. And this is my issue, I though it was working because of the extra delay that was occurring in my code but after some testing that was not the reason.
Could anyone please explain to me what is happening ? I will continue to work on the code until I figure it out.
The code :
Header file
#ifndef _ESPLIB_h
#define _ESPLIB_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <SoftwareSerial.h>
// Definitions
#define DEBUG 1
#define BUFFSIZE 64
#define TCPORT 23050
#define ESPTIMEOUT 10000
enum espResponse {
ok,
error,
other
};
// AT Commands
#define CRLF "\r\n"
#define Q "=?"
#define AT "AT"
#define GMR "+GMR"
#define RST "+RST"
#define CMUX "+CIPMUX=1"
#define CWMOD "+CWMODE"
#define CSERV "+CIPSERVER=1"
#define CFSR "+CIFSR"
#define CSEND "+CIPSEND"
class espdevice
{
public:
// Functions
espdevice(int rx, int tx, int baud);
void serialEnable(void);
//boolean tcpServerInit(void);
#if DEBUG
void serialDebug(void);
#endif
private:
// Variables
// Serial Setup parameters
int _rx;
int _tx;
int _baud;
// Software Serial
int _espSerialIndex = 0;
char _espSerialBuffer[BUFFSIZE];
SoftwareSerial *_espSerial;
// private functions
//espResponse _sendEspCommand(const char* cmd);
boolean _sendEspCommand(const char* cmd);
espResponse _espCommandOK(void);
boolean _espSerialAvailable(void);
// Debug mode
#if DEBUG
// variables
int _serialIndex = 0;
char _serialBuffer[BUFFSIZE];
// functions
void _serialDebug(int select);
int _arduinofreeRam(void);
boolean _serialAvailable(void);
#endif
};
#endif
What I am executing
_sendEspCommand("AT+CIPMUX=1\r\n");
_sendEspCommand("AT+CIPSERVER=1,23050\r\n");
_sendEspCommand("AT+CIFSR\r\n");
Now the function definition
boolean espdevice::_sendEspCommand(const char* cmd)
{
// variables
int count = 0;
boolean flag = false;
espResponse status = other;
// sending the command to the ESP
_espSerial->write(cmd);
#if DEBUG
Serial.print("command echo : ");
Serial.write(cmd);
Serial.println();
#endif
//while (!(status==ok||status==error) || count < 1000) **<- why this thing works ??????????????**
//while (status == other && count < ESPTIMEOUT) <- does not work
while ((status == other) || (count < ESPTIMEOUT)) <- THIS works too
//while(count < ESPTIMEOUT) <- does not work
{
//while (!_espSerial->available()) {};
if (_espSerialAvailable())
{
Serial.println ("Data Are available");
status = _espCommandOK();
#if DEBUG
Serial.print("Data Received from ESP : ");
Serial.print(_espSerialBuffer);
Serial.println("");
Serial.println(status);
#endif
/* //code used with the not working part
if (status == ok)
{
flag = true;
break;
}
else if (status == error)
{
flag = false;
break;
}
else
{
}
*/
}
count++;
}
Serial.println(count);
return flag;
}
And the two other functions that are called
_espSerialAvailable()
boolean espdevice::_espSerialAvailable(void)
{
boolean stringFound = false;
// Read the serial buffer if data are available
while (_espSerial->available() > 0)
{
char charBuffer = _espSerial->read();
if (charBuffer == '\n')
{
_espSerialBuffer[_espSerialIndex] = 0; // terminate the string
stringFound = (_espSerialIndex > 0); // only good if we sent more than an empty line
_espSerialIndex = 0; // reset for next line of data
}
else if (charBuffer == '\r')
{
// ignoring carriage retun
}
else if (_espSerialIndex < BUFFSIZE && stringFound == false)
{
_espSerialBuffer[_espSerialIndex++] = charBuffer; // auto increment index
}
}
return stringFound;
}
_espCommandOK()
espResponse espdevice::_espCommandOK(void)
{
if (strcmp(_espSerialBuffer, "OK") == 0)
{
return ok;
}
else if (strcmp(_espSerialBuffer, "ERROR") == 0)
{
return error;
}
else
{
return other;
}
}
The output from the serial ports for each case
working
creating an new tcp server
command echo : AT+CIPMUX=1
Data Are available
Data Received from ESP : AT+CIPMUX=1
2
Data Are available
Data Received from ESP : OK
0
10000
command echo : AT+CIPSERVER=1,23050
Data Are available
Data Received from ESP : AT+CIPSERVER=1,23050
2
Data Are available
Data Received from ESP : no change
2
Data Are available
Data Received from ESP : OK
0
10000
command echo : AT+CIFSR
Data Are available
Data Received from ESP : AT+CIFSR
2
Data Are available
Data Received from ESP : +CIFSR:STAIP,"192.168.1.16"
2
Data Are available
Data Received from ESP : +CIFSR:STAMAC,"5c:cf:7f:fd:ce:14"
2
Data Are available
Data Received from ESP : OK
0
10000
not working
creating an new tcp server
command echo : AT+CIPMUX=1
Data Are available
Data Received from ESP : AT+CIPMUX=1
2
Data Are available
Data Received from ESP : OK
0
98
command echo : AT+CIPSERVER=1,23050
10000
command echo : AT+CIFSR
Data Are available
Data Received from ESP : AT+CIFSR
2
Data Are available
Data Received from ESP : +CIFSR:STAIP,"192.168.1.16"
2
Data Are available
Data Received from ESP : +CIFSR:STAMAC,"5c:cf:7f:fd:ce:14"
2
Data Are available
Data Received from ESP : OK
0
224