ArduinoDumbDisplay v0.9.9-r40
DumbDisplay Arduino Library -- https://github.com/trevorwslee/Arduino-DumbDisplay
 
Loading...
Searching...
No Matches
amb82wifidumbdisplay.h
1#ifndef wifidumbdisplay_h
2#define wifidumbdisplay_h
3
4// after inclusion, can check DD_USING_WIFI to be sure WIFI is used
5#define DD_USING_WIFI
6
7#include "dumbdisplay.h"
8
9#include <WiFi.h>
10
11
12
13#define LOG_DDWIFI_STATUS
14//#define WRITE_BYTE_BY_BYTES
15
16
20 public:
21 /* WiFI IO mechanism */
22 /* - ssid: WIFI name (pass to WiFi) */
23 /* - passphrase: WIFI password (pass to WiFi) */
24 /* - serverPort: server port (pass to WiFiServer) */
25 DDAmb82WiFiServerIO(char* ssid, char *passphrase, int serverPort = DD_WIFI_PORT): DDInputOutput(DD_SERIAL_BAUD, false, false),
26 //server(serverPort) {
27 server(serverPort, tProtMode::TCP_MODE, tBlockingMode::NON_BLOCKING_MODE) {
28 this->ssid = ssid;
29 this->password = passphrase;
30 this->port = serverPort;
31 //this->logToSerial = logToSerial;
32 //Serial.begin(DD_SERIAL_BAUD);f
33 //this->server.setNonBlockingMode();
34 }
35 const char* getWhat() {
36 return "WIFI";
37 }
38 // // experimental ... not quite working
39 // bool availableAdditionalClient(WiFiClient& additionalClient) {
40 // if (client.connected()) {
41 // WiFiClient cli = server.available();
42 // if (cli) {
43 // additionalClient = cli;
44 // return true;
45 // }
46 // }
47 // return false;
48 // }
49 bool available() {
50 int count = client.available();
51 if (false) { // TODO: disable
52 if (count > 0) {
53 Serial.print("- available: ");
54 Serial.println(count);
55 }
56 }
57 return count > 0;
58 }
59 char read() {
60 char ch = client.read();
61 if (false) { // TODO: disable
62 Serial.print("- read:[");
63 Serial.print(ch);
64 Serial.println("]");
65 }
66 return ch;
67 }
68 void print(const String &s) {
69#ifdef WRITE_BYTE_BY_BYTES
70 for (size_t i = 0; i < s.length(); i++) {
71 write(s.charAt(i));
72 }
73#else
74 // if (true) { // TODO: disable debug
75 // Serial.print("* print: [");
76 // Serial.print(s);
77 // Serial.println("]");
78 // }
79 client.print(s);
80 if (true) { // TODO: forced delay since 2024-11-04
81 delay(200);
82 }
83#endif
84 }
85 void print(const char *p) {
86#ifdef WRITE_BYTE_BY_BYTES
87 while (true) {
88 char c = *(p++);
89 if (c == 0) {
90 break;
91 }
92 write(c);
93 }
94#else
95 if (false) { // TODO: disable debug
96 Serial.print("* print: [");
97 Serial.print(p);
98 Serial.println("]");
99 }
100 client.print(p);
101 // if (true) {
102 // client.flush();
103 // }
104 if (true) { // TODO: forced delay since 2024-11-04
105 delay(200);
106 }
107#endif
108 }
109 void write(uint8_t b) {
110#ifdef WRITE_BYTE_BY_BYTES
111 while (true) {
112 size_t count = client.write(b);
113 if (count > 0) {
114 break;
115 }
116 delay(200);
117 }
118#else
119 client.write(b);
120#endif
121 }
122 void write(const uint8_t *buf, size_t size) {
123#ifdef WRITE_BYTE_BY_BYTES
124 for (size_t i = 0; i < size; i++) {
125 write(buf[i]);
126 }
127#else
128 if (false) {
129 Serial.print("*** write ");
130 Serial.print(size);
131 Serial.print(" ... ");
132 size_t written = client.write(buf, size);
133 Serial.println(written);
134 } else {
135 client.write(buf, size);
136 }
137 if (false) { // TODO: forced delay since 2024-11-04
138 delay(200);
139 }
140#endif
141 }
142 bool preConnect(bool firstCall) {
143 if (firstCall) { // since 2023-08-10
144 if (!Serial) Serial.begin(DD_SERIAL_BAUD);
145// #if defined(ARDUINO_UNOR4_WIFI)
146// if (WiFi.status() == WL_NO_MODULE) {
147// Serial.println("XXX communication with WiFi module failed");
148// }
149// String fv = WiFi.firmwareVersion();
150// if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
151// Serial.println("XXX please upgrade the firmware");
152// } else {
153// Serial.println("* WIFI_FIRMWARE_LATEST_VERSION=" + String(WIFI_FIRMWARE_LATEST_VERSION));
154// }
155// #endif
156 }
157 if (true) { // since 2023-08-16
158 if (firstCall && !client.connected()) {
159 connectionState = '0';
160 }
161 checkConnection();
162 return connectionState == 'C';
163 } else if (true) { // since 2023-08-15
164 if (firstCall && !client.connected()) {
165 Serial.println("setup WIFI");
166 WiFi.disconnect();
167 WiFi.begin(ssid, password);
168 connectionState = ' ';
169 stateMillis = 0;
170 }
171 checkConnection();
172 return connectionState == 'C';
173 } else if (true) { // since 2023-06-05
174 if (firstCall) {
175 if (true) {
176 if (client.connected()) {
177 Serial.println("stop client for new setup");
178 client.stop();
179 delay(2000); // wait a bit
180 }
181 WiFi.disconnect();
182 } else if (true) { // since 2023-06-03
183 client.stop();
184 WiFi.disconnect();
185 }
186 Serial.println("setup WIFI");
187 WiFi.begin(ssid, password);
188 connectionState = ' ';
189 stateMillis = 0;
190 }
191 // delay(200);
192 checkConnection();
193 return connectionState == 'C';
194 } else {
195 if (firstCall) {
196 // if (logToSerial)
197 // Serial.begin(serialBaud);
198 if (true) { // since 2023-06-03
199 client.stop();
200 WiFi.disconnect();
201 }
202 WiFi.begin(ssid, password);
203 connectionState = ' ';
204 stateMillis = 0;
205 } else {
206 checkConnection();
207 delay(200);
208 }
209 return connectionState == 'C';
210 }
211 }
212 void flush() {
213 if (false) {
214 client.flush(); // flush is not the expected -- just flush send buffer
215 }
216 }
217 void validConnection() {
218#ifdef LOG_DDWIFI_STATUS
219 Serial.println(" ... validate ...");
220#endif
221 checkConnection();
222 }
223 bool canConnectPassive() {
224 return false; // TODO: make it passive
225 }
226 bool canUseBuffer() {
227 return false; // buffering might make it fails to send (and marked disconnected)
228 }
229 private:
230// bool _connectToNetwork() {
231// WiFi.begin(ssid, password);
232// long last = millis();
233// while (true) {
234// uint8_t status = WiFi.status();
235// if (status == WL_CONNECTED)
236// break;
237// delay(200);
238// long diff = millis() - last;
239// if (diff > 1000) {
240// if (logToSerial) {
241// Serial.print("binding WIFI ");
242// Serial.print(ssid);
243// #ifdef LOG_DDWIFI_STATUS
244// Serial.print(" ... ");
245// Serial.print(status);
246// #endif
247// Serial.println(" ...");
248// }
249// last = millis();
250// }
251// }
252// if (logToSerial) {
253// Serial.print("binded WIFI ");
254// Serial.println(ssid);
255// }
256// return true;
257// }
258// bool _connectToClient(bool initConnect) {
259// if (initConnect) {
260// if (!_connectToNetwork())
261// return false;
262// server.begin();
263// }
264// long last = millis();
265// while (true) {
266// client = server.available();
267// if (client) {
268// break;
269// } else {
270// if (WiFi.status() != WL_CONNECTED)
271// return false;
272// long diff = millis() - last;
273// if (diff >= 1000) {
274// IPAddress localIP = WiFi.localIP();
275// uint32_t localIPValue = localIP;
276// if (logToSerial) {
277// #ifdef LOG_DDWIFI_STATUS
278// Serial.print("via WIFI ... ");
279// Serial.print(WiFi.status());
280// Serial.print(" ... ");
281// #endif
282// Serial.print("listening on ");
283// // #ifdef LOG_DDWIFI_STATUS
284// // Serial.print("(");
285// // Serial.print(localIPValue);
286// // Serial.print(") ");
287// // #endif
288// Serial.print(localIP);
289// Serial.print(":");
290// Serial.print(port);
291// Serial.println(" ...");
292// }
293// last = millis();
294// if (localIPValue == 0)
295// return false;
296// }
297// }
298// }
299// return true;
300// }
301 void checkConnection() {
302 if (connectionState == '0' || connectionState == 'C') { // since 2023-08-16 added check connectionState == '0'
303 if (connectionState == '0' || WiFi.status() != WL_CONNECTED) {
304 if (connectionState == 'C') {
305 Serial.println("lost WiFi ... try bind WiFi again ...");
306 client.stop();
307 }
308 WiFi.disconnect();
309 Serial.println("setup WIFI");
310 WiFi.begin(ssid, password);
311 connectionState = ' ';
312 stateMillis = 0;
313 } else if (!client.connected()) {
314 client.stop();
315 Serial.println("lost connection ... try bind again ...");
316 connectionState = 'W';
317 stateMillis = 0;
318 } else {
319 return;
320 }
321 }
322 if (connectionState == ' ') {
323 uint8_t status = WiFi.status();
324 if (status == WL_CONNECTED) {
325 Serial.print("binded WIFI ");
326 Serial.println(ssid);
327 server.begin();
328 connectionState = 'W';
329 stateMillis = 0;
330 } else {
331 long diff = millis() - stateMillis;
332 if (stateMillis == 0 || diff > 1000) {
333 Serial.print("binding WIFI ");
334 Serial.print(ssid);
335#ifdef LOG_DDWIFI_STATUS
336 Serial.print(" ... ");
337 Serial.print(status);
338#endif
339 Serial.println(" ...");
340 stateMillis = millis();
341 }
342 }
343 } else {
344 uint8_t status = WiFi.status();
345 if (status != WL_CONNECTED) {
346 connectionState = ' ';
347 stateMillis = 0;
348 } else {
349 long diff = millis() - stateMillis;
350 if (diff >= 1000) {
351 IPAddress localIP = WiFi.localIP();
352 //uint32_t localIPValue = localIP;
353#ifdef LOG_DDWIFI_STATUS
354 Serial.print("via WIFI ... ");
355 Serial.print(WiFi.status());
356 Serial.print(" ... ");
357#endif
358 Serial.print("listening on ");
359 Serial.print(localIP);
360 Serial.print(":");
361 Serial.print(port);
362 Serial.println(" ...");
363 // if (true) {
364 // // testing for ESP
365 // Serial.println("*** sleep ...");
366 // esp_sleep_enable_timer_wakeup(10 * 1000); // 10ms
367 // esp_light_sleep_start();
368 // Serial.println("*** ... up");
369 // }
370 stateMillis = millis();
371 }
372 WiFiClient cli = server.available();
373 // if (cli) {
374 // if (!cli.connected()) { // TODO: unexpected ... debugging
375 // Serial.println("??? client not connected");
376 // cli.stop();
377 // return;
378 // }
379 // }
380 if (cli && cli.connected()) { // TODO: for some reason, under lying _sock an be 0xFF, which seems to indicated "try again"
381 // if (true)
382 // delay(2000); // wait a bit
383 //cli.setNonBlockingMode();
384 client = cli;
385 connectionState = 'C';
386 stateMillis = 0;
387 Serial.println("client connected");
388 } else {
389 //Serial.print("~");
390 }
391 }
392 }
393 }
394 private:
395 char* ssid;
396 char *password;
397 int port;
398 //bool logToSerial;
399 char connectionState;
400 long stateMillis;
401 WiFiServer server;
402 WiFiClient client;
403};
404
405
406
407
408#endif
Definition: amb82wifidumbdisplay.h:19
Class for DD input/output; you explicitly constructed it, pass in when instantiate DumbDisplay,...
Definition: _dd_io.h:9