AtClient.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2017 by Slavey Karadzhov
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * AtClient.h
8  *
9  ****/
10 
16 #pragma once
17 
18 #include "HardwareSerial.h"
19 #include "FIFO.h"
20 #include "Timer.h"
21 
22 #define AT_REPLY_OK "OK"
23 #ifndef AT_TIMEOUT
24 #define AT_TIMEOUT 2000
25 #endif
26 
27 class AtClient;
28 
34 
40 
41 struct AtCommand {
44  unsigned timeout;
45  unsigned retries;
46  bool breakOnError = true;
49 };
50 
51 enum AtState {
52  eAtOK = 0,
55 };
56 
60 class AtClient
61 {
62 public:
63  AtClient(HardwareSerial* stream);
64 
65  virtual ~AtClient()
66  {
67  }
68 
76  void send(const String& text, const String& altResponse = nullptr, uint32_t timeoutMs = AT_TIMEOUT,
77  unsigned retries = 0);
78 
86  void send(const String& text, AtReceiveCallback onReceive, uint32_t timeoutMs = AT_TIMEOUT, unsigned retries = 0);
87 
95  void send(const String& text, AtCompleteCallback onComplete, uint32_t timeoutMs = AT_TIMEOUT, unsigned retries = 0);
96 
97  // Low Level Functions
98 
105  void send(AtCommand command);
106 
111  void sendDirect(AtCommand command);
112 
118  {
119  return state;
120  }
121 
122  /*
123  * @brief Repeats the execution of the current command
124  * Useful if the current State is not eAtOK
125  */
126  void resend();
127 
128  /*
129  * @brief Replaces the current command with the next on in the queue
130  */
131  void next();
132 
134 
135 protected:
139  virtual void processor(Stream& source, char arrivedChar, uint16_t availableCharsCount);
140 
141 private:
142  FIFO<AtCommand, 10> queue;
143  HardwareSerial* stream = nullptr;
144  Timer commandTimer;
145  AtState state = eAtOK;
146 
150  void ticker();
151 };
152 
virtual ~AtClient()
Definition: AtClient.h:65
AtReceiveCallback onReceive
if set you can process manually all incoming data in a callback
Definition: AtClient.h:47
String response2
alternative successful response
Definition: AtClient.h:43
The String class.
Definition: WString.h:136
Definition: AtClient.h:52
AtCommand currentCommand
The current command.
Definition: AtClient.h:133
Definition: AtClient.h:54
AtState getState()
Returns the current state.
Definition: AtClient.h:117
bool breakOnError
stop executing next command if that one has failed
Definition: AtClient.h:46
#define AT_TIMEOUT
Definition: AtClient.h:24
Definition: AtClient.h:41
Hardware serial class.
Definition: HardwareSerial.h:106
AtState
Definition: AtClient.h:51
unsigned retries
number of retries before giving up
Definition: AtClient.h:45
unsigned timeout
timeout in milliseconds
Definition: AtClient.h:44
Class that facilitates the communication with an AT device.
Definition: AtClient.h:60
Callback timer class.
Definition: Timer.h:255
String text
the actual AT command
Definition: AtClient.h:42
AtCompleteCallback onComplete
if set then you can process the complete response manually
Definition: AtClient.h:48
Base Stream class.
Definition: Stream.h:32
Definition: AtClient.h:53