0

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

2 Answers 2

0

I will leave this here in case anyone is interested. So i am communicating with the esp module at 9600 baud rate. After I send a command to the ESP I was waiting for its response before sending the next command without any delay.

What I found out is that for some reason although I was sending the command to the ESP the esp was not executing the command. So i added a delay(100) to see if the RX of the ESP was disabled during that time and i think i was correct since the commands were executed correctly.

I manage to get the delay down to delay(5). it Breaks after that.

I am using an Arduino micro and an ESP-01.

1
  • 1
    Accept your own answer to close this question. Nobody has a better one.
    – user31481
    Nov 17, 2017 at 12:45
0

This may not be the answer you need if you are using it for something else, but if you are using a Micro, I would use the hardware serial Serial1 instead of SoftwareSerial. I've done many projects using a Leonardo (same processor) with ESP8266 as it gives me an extra hardware serial port which isn't connected to the Serial Monitor serial port.

I haven't tried the SoftwareSerial library for a few years, but I remember at the time that it doesn't handle duplex or high baud rates well.

What baud rate are you using? I couldn't see any begin in the code.

EDIT: You might also want to add the 'ATE0' command to turn off command echo. The serial receiver is probably getting swamped with data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.