ArduinoDumbDisplay v0.9.9-r34
DumbDisplay Arduino Library -- https://github.com/trevorwslee/Arduino-DumbDisplay
 
Loading...
Searching...
No Matches
atwifidumbdisplay.h
1
2#ifndef atwifidumbdisplay_h
3#define atwifidumbdisplay_h
4
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) \
7 *** \
8 e.g. STM32F103: PA3 (RX2) ==> TX; PA2 (TX2) ==> RX \
9 #define DD_SERIAL Serial2 \
10 *** \
11 e.g. Pico \
12 UART Serial2(8, 9, 0, 0); // 8: PICO_TX; 9: PICO_RX \
13 #define DD_SERIAL Serial2
14#endif
15
16#include "Arduino.h"
17#include "dumbdisplay.h"
18
19//#define DEGUG_SHOW_STATE
20
21//#define DEBUG_ESP_AT
22//#define FORCE_DEBUG_NO_SILENT
23#include "_loespat.h"
24
25
26// after inclusion, can check DD_USING_WIFI to be sure WIFI is used
27#define DD_USING_WIFI
28
29
32 public:
33 /* ESP chipset as WIFI with AT commands IO mechanism */
34 /* - ssid: WIFI name */
35 /* - passphrase: WIFI password */
36 /* - serverPort: server port */
37 DDATWiFiIO(const char* ssid, const char *passphrase, int serverPort = DD_WIFI_PORT): DDInputOutput(DD_SERIAL_BAUD, false, false) {
38 this->ssid = ssid;
39 this->password = passphrase;
40 this->port = serverPort;
41 this->ip = "";
42 this->linkId = -1;
43 this->data = "";
44 this->dataIdx = 0;
45 this->lastValidateMs = 0;
46 //this->connectionState = '0';
47 }
48 const char* getWhat() {
49 return "ATWIFI";
50 }
51 bool available() {
52 return atCheckAvailable();
53 }
54 char read() {
55 return atRead();
56 }
57 void print(const String &s) {
58 atPrint(s);
59 }
60 void print(const char *p) {
61 atPrint(p);
62 }
63 void write(uint8_t b) {
64 atWrite(b);
65 }
66 void write(const uint8_t *buf, size_t size) {
67 atWrite(buf, size);
68 }
69 bool preConnect(bool firstCall) {
70 if (firstCall) {
71 if (!Serial) Serial.begin(DD_SERIAL_BAUD);
72#if defined(ESP_SERIAL_begin)
73 ESP_SERIAL_begin;
74#else
75 ESP_SERIAL.begin(115200);
76#endif
77 LOEspAt::InitAt();
78 }
79 return atPreConnect(firstCall);
80 }
81 void flush() {
82 atFlush();
83 }
84 void validConnection() {
85// #ifdef LOG_DDWIFI_STATUS
86// Serial.println(" ... validate ...");
87// #endif
88 atCheckConnection(false);
89 }
90 bool canConnectPassive() {
91 return true;
92 }
93 bool canUseBuffer() {
94 return true;
95 }
96 private:
97 inline bool atCheckAvailable() {
98 if (dataIdx < data.length()) {
99 return true;
100 }
101 if (linkId != -1) {
102 if (LOEspAt::ReceiveDataFromClient(linkId, data)) {
103 dataIdx = 0;
104 if (dataIdx < data.length()) {
105 return true;
106 }
107 }
108 }
109 return false;
110 }
111 inline char atRead() {
112 if (dataIdx < data.length()) {
113 return data.charAt(dataIdx++);
114 }
115 return 0;
116 }
117 inline void atPrint(const String &s) {
118 if (linkId != -1) {
119 LOEspAt::SendDataToClient(linkId, s);
120 }
121 }
122 inline void atPrint(const char *p) {
123 if (linkId != -1) {
124 LOEspAt::SendDataToClient(linkId, p);
125 }
126 }
127 inline void atPrint(uint8_t b) {
128 if (linkId != -1) {
129 LOEspAt::SendDataToClient(linkId, b);
130 }
131 }
132 inline void atWrite(uint8_t b) {
133 if (linkId != -1) {
134 LOEspAt::SendDataToClient(linkId, b);
135 }
136 }
137 inline void atWrite(const uint8_t *buf, size_t size) {
138 if (linkId != -1) {
139 LOEspAt::SendDataToClient(linkId, buf, size);
140 }
141 }
142 inline void atFlush() {
143 }
144 bool atPreConnect(bool firstCall) {
145 if (true) {
146 if ((firstCall || connectionState == '0') && linkId == -1) {
147 connectionState = '0';
148 if (!atSetupWIFI()) {
149 Serial.println("failed to initially setup WIFI");
150 return false;
151 }
152 connectionState = ' ';
153 }
154 atCheckConnection(true);
155 return connectionState == 'C';
156 } else {
157 if ((firstCall || connectionState == '0') && linkId == -1) {
158 connectionState = '0';
159 Serial.println("setup AT WIFI");
160 LOEspAt::DisconnectAP();
161 bool failed = false;
162 if (!LOEspAt::CheckAt()) {
163 Serial.println("XXX AT not ready");
164 failed = true;
165 }
166 if (!failed) {
167 if (!LOEspAt::SetStationMode()) {
168 Serial.println("XXX failed to set 'station mode'");
169 failed = true;
170 }
171 }
172 if (!failed) {
173 delay(1000); // delay a bit
174 if (!LOEspAt::ConnectAP(ssid, password, ip)) {
175 Serial.println("XXX failed to start AP???");
176 failed = true;
177 }
178 if (!failed) {}
179 if (!LOEspAt::StartServer(port)) {
180 Serial.println("XXX failed to start server");
181 LOEspAt::DisconnectAP();
182 failed = true;
183 }
184 }
185 if (failed) {
186 Serial.println("failed to setup AT WIFI");
187 atReset();
188 return false;
189 }
190 Serial.println("DONE setup AT WIFI");
191 //WiFi.begin(ssid, password);
192 connectionState = ' ';
193 //stateMillis = 0;
194 }
195 // delay(200);
196 atCheckConnection(true);
197 return connectionState == 'C';
198 }
199 }
200 void atReset() {
201 Serial.println("reseting ...");
202 LOEspAt::Reset();
203 delay(2000);
204 Serial.println("... done reset");
205
206 }
207 void atCheckConnection(bool forPreConnect) {
208 if (true) { // don't validate so frequently
209 // don't validate so frequently
210 long now = millis();
211 if ((now - lastValidateMs) < (forPreConnect ? 500 : 2000)) {
212 return;
213 }
214 lastValidateMs = now;
215 }
216 int state = LOEspAt::CheckState();
217#if defined(DEGUG_SHOW_STATE)
218 Serial.print("[");
219 Serial.print(connectionState);
220 Serial.print("]: ");
221 Serial.println(state);
222#endif
223 if (state == -1) {
224 Serial.println("failed to check AT WIFI state");
225 atReset();
226 //LOEspAt::DisconnectAP();
227 connectionState = ' ';
228 return;
229 }
230 // if (state == 0) {
231 // // 0: ESP32 station has not started any Wi-Fi connection.
232 // if (!LOEspAt::Reset() || !LOEspAt::ConnectAP(ssid, password, ip)) {
233 // Serial.println("XXX failed to start AP during connection check");
234 // }
235 // return;
236 // }
237 if (state == 1 || state == 3) {
238 // 1: ESP32 station has connected to an AP, but does not get an IPv4 address yet.
239 // 3: ESP32 station is in Wi-Fi connecting or reconnecting state.
240 return;
241 }
242 if (connectionState == 'C') {
243 if (state != 2) {
244 // 2: ESP32 station has connected to an AP, and got an IPv4 address.
245 Serial.println("lost AT WIFI ... try bind AT WIFI again ...");
246 Serial.println(state);
247 if (true) {
248 if (!atSetupWIFI()) {
249 Serial.println("failed to setup WIFI again");
250 atReset();
251 return;
252 }
253 } else {
254 // client.stop();
255 // WiFi.disconnect();
256 atDisconnectClient();
257 LOEspAt::DisconnectAP();
258 }
259 connectionState = ' ';
260 //stateMillis = 0;
261 } else if (linkId != -1) {
262 //client.stop();
263 //atDisconnectClient();
264 int checkLinkId = LOEspAt::CheckForClientConnection();
265 if (checkLinkId == -1) {
266 linkId = -1;
267 Serial.println("lost connection ... try connect again ...");
268 connectionState = 'W';
269 //stateMillis = 0;
270 } else {
271 return;
272 }
273 } else {
274 return;
275 }
276 }
277 if (connectionState == ' ') {
278 //uint8_t status = WiFi.status();
279 if (state == 2) {
280 Serial.print("binded AT WIFI ");
281 Serial.println(ssid);
282 //server.begin();
283 connectionState = 'W';
284 //stateMillis = 0;
285 } else {
286 //long diff = millis() - stateMillis;
287 //if (stateMillis == 0 || diff > 1000) {
288 //LOEspAt::StartServer(port);
289 Serial.print("binding AT WIFI ");
290 Serial.print(ssid);
291// #ifdef LOG_DDWIFI_STATUS
292// Serial.print(" ... ");
293// Serial.print(state);
294// #endif
295 Serial.println(" ...");
296 //stateMillis = millis();
297 //}
298 }
299 } else {
300 if (state != 2) {
301 connectionState = ' ';
302 //stateMillis = 0;
303 } else {
304 //long diff = millis() - stateMillis;
305 //if (diff >= 1000) {
306 //IPAddress localIP = WiFi.localIP();
307 //uint32_t localIPValue = localIP;
308// #ifdef LOG_DDWIFI_STATUS
309// Serial.print("via WIFI ... ");
310// Serial.print(LOEspAt::CheckState()/*WiFi.status()*/);
311// Serial.print(" ... ");
312// #endif
313 if (true) {
314 int clientState = LOEspAt::CheckServerState();
315 //Serial.println(clientState);
316 if (clientState != 1) {
317 Serial.println("server not running");
318 atReset();
319 connectionState == ' ';
320 return;
321 }
322 }
323 Serial.print("listening on ");
324 Serial.print(ip);
325 Serial.print(":");
326 Serial.print(port);
327 Serial.println(" ...");
328 //stateMillis = millis();
329 //}
330 if (true) {
331 int checkLinkId = LOEspAt::CheckForClientConnection();
332 if (checkLinkId != -1) {
333 linkId = checkLinkId;
334 connectionState = 'C';
335 //stateMillis = 0;
336 Serial.println("client connected");
337 }
338 } else {
339 if (linkId == -1) {
340 linkId = LOEspAt::CheckForClientConnection();
341 if (linkId != -1) {
342 connectionState = 'C';
343 //stateMillis = 0;
344 Serial.println("client connected");
345 }
346 }
347 }
348 }
349 }
350 }
351 bool atSetupWIFI() {
352 Serial.println("setup AT WIFI");
353 LOEspAt::DisconnectAP();
354 bool failed = false;
355 if (!LOEspAt::CheckAt()) {
356 Serial.println("XXX AT not ready");
357 failed = true;
358 }
359 if (!failed) {
360 if (!LOEspAt::SetStationMode()) {
361 Serial.println("XXX failed to set 'station mode'");
362 failed = true;
363 }
364 }
365 if (!failed) {
366 delay(1000); // delay a bit
367 if (!LOEspAt::ConnectAP(ssid, password, ip)) {
368 Serial.println("XXX failed to start AP when");
369 failed = true;
370 }
371 if (!failed) {}
372 if (!LOEspAt::StartServer(port)) {
373 Serial.println("XXX failed to start server");
374 LOEspAt::DisconnectAP();
375 failed = true;
376 }
377 }
378 if (failed) {
379 Serial.println("failed to setup AT WIFI");
380 atReset();
381 return false;
382 }
383 Serial.println("DONE setup AT WIFI");
384 return true;
385 }
386 void atDisconnectClient() {
387 if (linkId != -1) {
388 LOEspAt::DisconnectClient(linkId);
389 linkId = -1;
390 }
391 }
392 private:
393 const char* ssid;
394 const char* password;
395 int port;
396 char connectionState;
397 //long stateMillis;
398 String ip;
399 int linkId;
400 String data;
401 int dataIdx;
402 long lastValidateMs;
403};
404
405
406
407#endif
Subclass of DDInputOutput.
Definition: atwifidumbdisplay.h:31
Class for DD input/output; you explicitly constructed it, pass in when instantiate DumbDisplay,...
Definition: _dd_io.h:9