2#ifndef atwifidumbdisplay_h
3#define atwifidumbdisplay_h
5#if !defined(ESP_SERIAL)
6 #error Must define the macro ESP_SERIAL and optionally ESP_SERIAL_begin (a function call or a code block) \
8 e.g. STM32F103: PA3 (RX2) ==> TX; PA2 (TX2) ==> RX \
9 #define DD_SERIAL Serial2 \
12 UART Serial2(8, 9, 0, 0);
17#include "dumbdisplay.h"
37 DDATWiFiIO(
const char* ssid,
const char *passphrase,
int serverPort = DD_WIFI_PORT):
DDInputOutput(DD_SERIAL_BAUD,
false,
false) {
39 this->password = passphrase;
40 this->port = serverPort;
45 this->lastValidateMs = 0;
48 const char* getWhat() {
52 return atCheckAvailable();
57 void print(
const String &s) {
60 void print(
const char *p) {
63 void write(uint8_t b) {
66 void write(
const uint8_t *buf,
size_t size) {
69 bool preConnect(
bool firstCall) {
71 if (!Serial) Serial.begin(DD_SERIAL_BAUD);
72#if defined(ESP_SERIAL_begin)
75 ESP_SERIAL.begin(115200);
79 return atPreConnect(firstCall);
84 void validConnection() {
88 atCheckConnection(
false);
90 bool canConnectPassive() {
97 inline bool atCheckAvailable() {
98 if (dataIdx < data.length()) {
102 if (LOEspAt::ReceiveDataFromClient(linkId, data)) {
104 if (dataIdx < data.length()) {
111 inline char atRead() {
112 if (dataIdx < data.length()) {
113 return data.charAt(dataIdx++);
117 inline void atPrint(
const String &s) {
119 LOEspAt::SendDataToClient(linkId, s);
122 inline void atPrint(
const char *p) {
124 LOEspAt::SendDataToClient(linkId, p);
127 inline void atPrint(uint8_t b) {
129 LOEspAt::SendDataToClient(linkId, b);
132 inline void atWrite(uint8_t b) {
134 LOEspAt::SendDataToClient(linkId, b);
137 inline void atWrite(
const uint8_t *buf,
size_t size) {
139 LOEspAt::SendDataToClient(linkId, buf, size);
142 inline void atFlush() {
144 bool atPreConnect(
bool firstCall) {
146 if ((firstCall || connectionState ==
'0') && linkId == -1) {
147 connectionState =
'0';
148 if (!atSetupWIFI()) {
149 Serial.println(
"failed to initially setup WIFI");
152 connectionState =
' ';
154 atCheckConnection(
true);
155 return connectionState ==
'C';
157 if ((firstCall || connectionState ==
'0') && linkId == -1) {
158 connectionState =
'0';
159 Serial.println(
"setup AT WIFI");
160 LOEspAt::DisconnectAP();
162 if (!LOEspAt::CheckAt()) {
163 Serial.println(
"XXX AT not ready");
167 if (!LOEspAt::SetStationMode()) {
168 Serial.println(
"XXX failed to set 'station mode'");
174 if (!LOEspAt::ConnectAP(ssid, password, ip)) {
175 Serial.println(
"XXX failed to start AP???");
179 if (!LOEspAt::StartServer(port)) {
180 Serial.println(
"XXX failed to start server");
181 LOEspAt::DisconnectAP();
186 Serial.println(
"failed to setup AT WIFI");
190 Serial.println(
"DONE setup AT WIFI");
192 connectionState =
' ';
196 atCheckConnection(
true);
197 return connectionState ==
'C';
201 Serial.println(
"reseting ...");
204 Serial.println(
"... done reset");
207 void atCheckConnection(
bool forPreConnect) {
211 if ((now - lastValidateMs) < (forPreConnect ? 500 : 2000)) {
214 lastValidateMs = now;
216 int state = LOEspAt::CheckState();
217#if defined(DEGUG_SHOW_STATE)
219 Serial.print(connectionState);
221 Serial.println(state);
224 Serial.println(
"failed to check AT WIFI state");
227 connectionState =
' ';
237 if (state == 1 || state == 3) {
242 if (connectionState ==
'C') {
245 Serial.println(
"lost AT WIFI ... try bind AT WIFI again ...");
246 Serial.println(state);
248 if (!atSetupWIFI()) {
249 Serial.println(
"failed to setup WIFI again");
256 atDisconnectClient();
257 LOEspAt::DisconnectAP();
259 connectionState =
' ';
261 }
else if (linkId != -1) {
264 int checkLinkId = LOEspAt::CheckForClientConnection();
265 if (checkLinkId == -1) {
267 Serial.println(
"lost connection ... try connect again ...");
268 connectionState =
'W';
277 if (connectionState ==
' ') {
280 Serial.print(
"binded AT WIFI ");
281 Serial.println(ssid);
283 connectionState =
'W';
289 Serial.print(
"binding AT WIFI ");
295 Serial.println(
" ...");
301 connectionState =
' ';
314 int clientState = LOEspAt::CheckServerState();
316 if (clientState != 1) {
317 Serial.println(
"server not running");
319 connectionState ==
' ';
323 Serial.print(
"listening on ");
327 Serial.println(
" ...");
331 int checkLinkId = LOEspAt::CheckForClientConnection();
332 if (checkLinkId != -1) {
333 linkId = checkLinkId;
334 connectionState =
'C';
336 Serial.println(
"client connected");
340 linkId = LOEspAt::CheckForClientConnection();
342 connectionState =
'C';
344 Serial.println(
"client connected");
352 Serial.println(
"setup AT WIFI");
353 LOEspAt::DisconnectAP();
355 if (!LOEspAt::CheckAt()) {
356 Serial.println(
"XXX AT not ready");
360 if (!LOEspAt::SetStationMode()) {
361 Serial.println(
"XXX failed to set 'station mode'");
367 if (!LOEspAt::ConnectAP(ssid, password, ip)) {
368 Serial.println(
"XXX failed to start AP when");
372 if (!LOEspAt::StartServer(port)) {
373 Serial.println(
"XXX failed to start server");
374 LOEspAt::DisconnectAP();
379 Serial.println(
"failed to setup AT WIFI");
383 Serial.println(
"DONE setup AT WIFI");
386 void atDisconnectClient() {
388 LOEspAt::DisconnectClient(linkId);
394 const char* password;
396 char connectionState;
Subclass of DDInputOutput.
Definition: atwifidumbdisplay.h:31