The ESP-AT MQTT library project
A simple wrapper to the MQTT functionality found in the ESP-AT interpreter
Public Member Functions | Private Attributes | List of all members
AT_Class Class Reference

EspAT MQTT AT_Class definition. More...

#include <AT.h>

Public Member Functions

 AT_Class (HardwareSerial *=&ESP_SERIAL_PORT)
 The class constructor is used to set the serial port to be used to communicate with the ESP-AT device. More...
 
size_t readLine ()
 Read exactly one line from the serial port. More...
 
at_status_t waitReply (const char *asynch, uint32_t timeout)
 Read all lines until the end of the AT command sequence All lines are stored in buff[] separated with a | character so you can use strtok later on. More...
 
at_status_t sendCommand (const char *cmd, const char *param, char **result, const char *asynch=NULL, uint32_t timeout=2000)
 This function sends the specified AT command and parameters to the ESP-AT device on the serial line. More...
 
at_status_t waitPrompt (uint32_t timeout)
 Waits for a prompt of the character '>' to arrive on the serial port. More...
 
at_status_t waitString (const char *str, uint32_t timeout)
 Wait for a specific string to arrive on the serial port. More...
 
at_status_t sendString (const char *str, size_t len)
 Send a generic string on the serial port. More...
 
at_status_t sendString (char *str, size_t len)
 Send a generic string on the serial port. More...
 
at_status_t sendString (const char *str)
 Send a generic string on the serial port. More...
 
char read ()
 Reads a byte from the serial port. More...
 
int available ()
 Checks to see if there are any data available on the serial port. More...
 
char * getBuff ()
 Returns a pointer to the internal receive buffer. More...
 

Private Attributes

HardwareSerial * _serial
 
char buff [1024]
 Serial input buffer.
 
char cmdBuff [256]
 Command buffer for stuff sent to the ESP-AT device.
 
char resBuff [128]
 Result buffer for parameter data returned from the ESP-AT device.
 
int wx
 Write pointer to the input buffer while processing an ESP-AT response.
 
int line
 Keeps track of how many lines have been received during the processing of an ESP-AT reply.
 

Detailed Description

EspAT MQTT AT_Class definition.

This class provides an AT interface layer that can be used by higher level software to easily send and receive data to and from an ESP-AT module. The module takes care of sending the desired command, waiting for a response as well as storing and parsing the returned data.

A few support functions are also included for implementations that require more control over the data sent and received.

Constructor & Destructor Documentation

◆ AT_Class()

AT_Class::AT_Class ( HardwareSerial *  serial = &ESP_SERIAL_PORT)

The class constructor is used to set the serial port to be used to communicate with the ESP-AT device.

Parameters
[in]-serial The serial port that the ESP device is connected to.

Member Function Documentation

◆ available()

int AT_Class::available ( )

Checks to see if there are any data available on the serial port.

Returns
- The number of bytes found available in the serial read buffer.

◆ getBuff()

char * AT_Class::getBuff ( )

Returns a pointer to the internal receive buffer.

Can be used by the caller to directly access the recevied data string from the ESP-AT device.

Returns
- A pointer to the internal data receive buffer.

◆ read()

char AT_Class::read ( )

Reads a byte from the serial port.

The function ensures that a valid byte has been received from the ESP-AT device before returning the data.

Returns
- The data byte received from the ESP-AT device.

◆ readLine()

size_t AT_Class::readLine ( )

Read exactly one line from the serial port.

Each line that is read is appended to the internal buffer to create a internal processable response. CR and LF's are removed and replaced with a '|' character to separate the lines in the buffer. This can then later be process using strtok or a similar function.

Returns
The number of characters read from the serial port.

◆ sendCommand()

at_status_t AT_Class::sendCommand ( const char *  cmd,
const char *  param,
char **  result,
const char *  asynch = NULL,
uint32_t  timeout = 2000 
)

This function sends the specified AT command and parameters to the ESP-AT device on the serial line.

The function adds the "AT" part of the command so this must not be a part of the command string when calling the function.

It then waits for the AT response to come back from the ESP-AT device and returns with the result of the operation. It also detects if the ESP-AT device does not return a reply within the specified time limit.

Parameters
[in]-cmd The AT command that should be executed (without the AT part). No <CR> or <LF> characters must be present in this string. Example: "+GMR" (Returns the firmware version)
[in]-param This is the parameter part of the AT command. It should hold all the necessary and relevant parameters of the AT command. No <CR> or <LF> characters must be present in this string. Example "=0,1,"alpn.ilabs.se""
[out]-result When an AT command returns a parameter as a result of the operation the AT handler takes care of this and can be returned to the caller. By supplying a non NULL ptrptr the caller can get access to this parameter. If this parameter is NULL the result is simply discarded.
[in]-asynch Some AT commands return their result completely asynchronous to the OK or ERROR reply and also with a completely different URC tag. For instance the command "AT+MQTTCONN=" can return the response "+MQTTCONNECTED:" both before and after the OK response. In this case by setting this parameter to "+MQTTCONNECTED:" the function will look for this data both when waiting for the OK response as well as after the OK response has been received or a timeout has occured.
[in]-timeout The amount of time, in milliseconds, that the function will wait for a reply from the ESP-AT device. This value is also used when waiting for an asynchronous response.
Returns
- The status of the operation, See status_code_e for more information.

◆ sendString() [1/3]

at_status_t AT_Class::sendString ( char *  str,
size_t  len 
)

Send a generic string on the serial port.

Can be used to send anything to the connected ESP-AT device.

Parameters
[in]-str (char *) The string/data that we want to send. Does not need to be '\0' terminated.
[in]-len The length of the string/data that we want to send.
Returns
- The status of the operation, See status_code_e for more information.

◆ sendString() [2/3]

at_status_t AT_Class::sendString ( const char *  str)

Send a generic string on the serial port.

Can be used to send anything to the connected ESP-AT device.

Parameters
[in]-str (const char *) The string/data that we want to send. Must be '\0' terminated.
Returns
- The status of the operation, See status_code_e for more information.

◆ sendString() [3/3]

at_status_t AT_Class::sendString ( const char *  str,
size_t  len 
)

Send a generic string on the serial port.

Can be used to send anything to the connected ESP-AT device.

Parameters
[in]-str (const char *) The string/data that we want to send. Does not need to be '\0' terminated.
[in]-len The length of the string/data that we want to send.
Returns
- The status of the operation, See status_code_e for more information.

◆ waitPrompt()

at_status_t AT_Class::waitPrompt ( uint32_t  timeout)

Waits for a prompt of the character '>' to arrive on the serial port.

Parameters
[in]-timeout The time allowed, in millisecond, for the prompt to arrive
Returns
- The status of the operation, See status_code_e for more information.

◆ waitReply()

at_status_t AT_Class::waitReply ( const char *  asynch,
uint32_t  timeout 
)

Read all lines until the end of the AT command sequence All lines are stored in buff[] separated with a | character so you can use strtok later on.

The asynch parameter can be used to ensure that a message that is asynchronous to the OK or ERROR response is always received within the given timeout value. If not, a ESP_AT_SUB_CMD_TIMEOUT error will be reported back to the caller. Important note: the asynchronous message could possibly still come in after the timeout has elapsed in case of unstable or extremely slow networks. In this case it is important to make sure it is taken care of in the mqtt process() function.

Parameters
[in]-asynch Any asynchronous data that is required. Can be NULL if no asynchronous data is expected.
[in]-timeout The maximum amount of time (in milliseconds) that is allowed before a response is expected. If data has not arrived within the alloted time the function will return with a timeout error message.
Returns
- The status of the operation, See status_code_e for more information.

◆ waitString()

at_status_t AT_Class::waitString ( const char *  str,
uint32_t  timeout 
)

Wait for a specific string to arrive on the serial port.

Parameters
[in]-str Holds the string that we are looking for.
[in]-timeout The maximum time allowed to wait for the string to arrive.
Returns
- The status of the operation, See status_code_e for more information.

The documentation for this class was generated from the following files: