The ESP-AT MQTT library project
A simple wrapper to the MQTT functionality found in the ESP-AT interpreter
AT.h
Go to the documentation of this file.
1 /*
2  * ----------------------------------------------------------------------------
3  * _ _ _
4  * (_) | | |
5  * _| | __ _| |__ ___
6  * | | | / _` | '_ \/ __|
7  * | | |___| (_| | |_) \__ \
8  * |_|______\__,_|_.__/|___/
9  *
10  * ----------------------------------------------------------------------------
11  Copyright (c) 2022 iLabs - Pontus Oldberg
12 
13  Permission is hereby granted, free of charge, to any person obtaining a copy
14  of this software and associated documentation files (the "Software"), to deal
15  in the Software without restriction, including without limitation the rights
16  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17  copies of the Software, and to permit persons to whom the Software is
18  furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included in all
21  copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29  SOFTWARE.
30  * ----------------------------------------------------------------------------
31  */
32 
35 #include <inttypes.h>
36 #include <Arduino.h>
37 
38 #ifndef _H_AT_COM_
39 #define _H_AT_COM_
40 
48  ESP_AT_SUB_OK = 0x00000000,
49  ESP_AT_SUB_COMMON_ERROR = 0x01010000,
50  ESP_AT_SUB_NO_TERMINATOR = 0x01020000,
51  ESP_AT_SUB_NO_AT = 0x01030000,
55  ESP_AT_SUB_PARA_INVALID = 0x01070000,
57  ESP_AT_SUB_UNSUPPORT_CMD = 0x01090000,
58  ESP_AT_SUB_CMD_EXEC_FAIL = 0x010a0000,
61  ESP_AT_SUB_CMD_OP_ERROR = 0x010c0000,
62  ESP_AT_SUB_CMD_ERROR = 0x010d0000,
63  ESP_AT_SUB_CMD_TIMEOUT = 0x010e0000,
67  ESP_AT_SUB_CMD_LAST_COMMAND
68 };
69 
70 #define AT_ERROR(x) (x & 0x00ff0000)
71 
77 typedef uint32_t at_status_t;
78 
79 
90 class AT_Class {
91 public:
92  AT_Class(HardwareSerial* = &ESP_SERIAL_PORT);
93 
94  size_t readLine();
95  at_status_t waitReply(const char *asynch, uint32_t timeout);
96  at_status_t sendCommand(const char *cmd, const char *param, char **result,
97  const char *asynch = NULL, uint32_t timeout=2000);
98  at_status_t waitPrompt(uint32_t timeout);
99  at_status_t waitString(const char *str, uint32_t timeout);
100  at_status_t sendString(const char *str, size_t len);
101  at_status_t sendString(char *str, size_t len);
102  at_status_t sendString(const char *str);
103  char read();
104  int available();
105 
106  char *getBuff();
107 private:
108  HardwareSerial* _serial;
109  char buff[1024];
110  char cmdBuff[256];
111  char resBuff[128];
112  int wx;
113  int line;
114 };
115 
116 #endif
ESP_AT_SUB_CMD_CONN_SYNCH
@ ESP_AT_SUB_CMD_CONN_SYNCH
MQTT connection was synchronous.
Definition: AT.h:64
AT_Class::AT_Class
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...
Definition: AT.cpp:51
ESP_AT_SUB_NO_TERMINATOR
@ ESP_AT_SUB_NO_TERMINATOR
The At interpretor did not detect a terminator.
Definition: AT.h:50
AT_Class::sendCommand
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.
Definition: AT.cpp:210
AT_Class::wx
int wx
Write pointer to the input buffer while processing an ESP-AT response.
Definition: AT.h:112
ESP_AT_SUB_PARA_PARSE_FAIL
@ ESP_AT_SUB_PARA_PARSE_FAIL
Failed to parse parameter.
Definition: AT.h:56
AT_Class::available
int available()
Checks to see if there are any data available on the serial port.
Definition: AT.cpp:402
AT_Class::buff
char buff[1024]
Serial input buffer.
Definition: AT.h:109
ESP_AT_SUB_PARA_NUM_MISMATCH
@ ESP_AT_SUB_PARA_NUM_MISMATCH
Incorrect number of parameters.
Definition: AT.h:54
at_status_t
uint32_t at_status_t
The return value of an AT operation.
Definition: AT.h:77
ESP_AT_SUB_NO_AT
@ ESP_AT_SUB_NO_AT
Command was not prepended with "AT".
Definition: AT.h:51
ESP_AT_SUB_PARA_TYPE_MISMATCH
@ ESP_AT_SUB_PARA_TYPE_MISMATCH
Wrong parameter type.
Definition: AT.h:53
AT_Class::line
int line
Keeps track of how many lines have been received during the processing of an ESP-AT reply.
Definition: AT.h:113
AT_Class::waitString
at_status_t waitString(const char *str, uint32_t timeout)
Wait for a specific string to arrive on the serial port.
Definition: AT.cpp:285
ESP_AT_SUB_COMMON_ERROR
@ ESP_AT_SUB_COMMON_ERROR
A generic error has occured, no more info is available.
Definition: AT.h:49
AT_Class::waitReply
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...
Definition: AT.cpp:112
status_code_e
status_code_e
Status codes that the AT handler can return.
Definition: AT.h:47
AT_Class::readLine
size_t readLine()
Read exactly one line from the serial port.
Definition: AT.cpp:66
AT_Class::cmdBuff
char cmdBuff[256]
Command buffer for stuff sent to the ESP-AT device.
Definition: AT.h:110
ESP_AT_SUB_CMD_PROCESSING
@ ESP_AT_SUB_CMD_PROCESSING
Error in command processing, this error always occur before a MQTT error happens.
Definition: AT.h:59
ESP_AT_SUB_CMD_ERROR
@ ESP_AT_SUB_CMD_ERROR
Generic command error.
Definition: AT.h:62
ESP_AT_SUB_PARA_INVALID
@ ESP_AT_SUB_PARA_INVALID
Invalid parameter value.
Definition: AT.h:55
AT_Class
EspAT MQTT AT_Class definition.
Definition: AT.h:90
ESP_AT_SUB_UNSUPPORT_CMD
@ ESP_AT_SUB_UNSUPPORT_CMD
Found unsupported command.
Definition: AT.h:57
AT_Class::resBuff
char resBuff[128]
Result buffer for parameter data returned from the ESP-AT device.
Definition: AT.h:111
ESP_AT_SUB_CMD_EXEC_FAIL
@ ESP_AT_SUB_CMD_EXEC_FAIL
Command execution failed.
Definition: AT.h:58
ESP_AT_SUB_CMD_TIMEOUT
@ ESP_AT_SUB_CMD_TIMEOUT
AT_Class timout waiting for a reply from the interpreter.
Definition: AT.h:63
AT_Class::sendString
at_status_t sendString(const char *str, size_t len)
Send a generic string on the serial port.
Definition: AT.cpp:323
ESP_AT_SUB_CMD_CONN_ASYNCH
@ ESP_AT_SUB_CMD_CONN_ASYNCH
MQTT connection was asynchronous, a callback will be issued when the connection is made.
Definition: AT.h:65
AT_Class::read
char read()
Reads a byte from the serial port.
Definition: AT.cpp:385
AT_Class::getBuff
char * getBuff()
Returns a pointer to the internal receive buffer.
Definition: AT.cpp:373
AT_Class::waitPrompt
at_status_t waitPrompt(uint32_t timeout)
Waits for a prompt of the character '>' to arrive on the serial port.
Definition: AT.cpp:257
ESP_AT_SUB_PARA_LENGTH_MISMATCH
@ ESP_AT_SUB_PARA_LENGTH_MISMATCH
Mismatch in the parameter length.
Definition: AT.h:52
ESP_AT_SUB_OK
@ ESP_AT_SUB_OK
All is good in life and no errors have been detected.
Definition: AT.h:48
ESP_AT_SUB_CMD_OP_ERROR
@ ESP_AT_SUB_CMD_OP_ERROR
Error while performing and operation.
Definition: AT.h:61