ArduinoDumbDisplay v0.9.9-r34
DumbDisplay Arduino Library -- https://github.com/trevorwslee/Arduino-DumbDisplay
 
Loading...
Searching...
No Matches
wifidumbdisplay.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#if defined(ARDUINO_UNOR4_WIFI)
10 #include <WiFiS3.h>
11#elif defined(ESP8266)
12 #include <ESP8266WiFi.h>
13#else
14 #include <WiFi.h>
15#endif
16
17
18
19//#define LOG_DDWIFI_STATUS
20
21
24 public:
25 /* WiFI IO mechanism */
26 /* - ssid: WIFI name (pass to WiFi) */
27 /* - passphrase: WIFI password (pass to WiFi) */
28 /* - serverPort: server port (pass to WiFiServer) */
29 DDWiFiServerIO(const char* ssid, const char *passphrase, int serverPort = DD_WIFI_PORT): DDInputOutput(DD_SERIAL_BAUD, false, false),
30 server(serverPort) {
31 this->ssid = ssid;
32 this->password = passphrase;
33 this->port = serverPort;
34 //this->logToSerial = logToSerial;
35 //Serial.begin(DD_SERIAL_BAUD);
36 }
37 const char* getWhat() {
38 return "WIFI";
39 }
40 // experimental ... not quite working
41 bool availableAdditionalClient(WiFiClient& additionalClient) {
42 if (client.connected()) {
43 WiFiClient cli = server.available();
44 if (cli) {
45 additionalClient = cli;
46 return true;
47 }
48 }
49 return false;
50 }
51 bool available() {
52 return client.available() > 0;
53 }
54 char read() {
55 return client.read();
56 }
57 void print(const String &s) {
58 client.print(s);
59 }
60 void print(const char *p) {
61 client.print(p);
62 }
63 void write(uint8_t b) {
64 client.write(b);
65 }
66 void write(const uint8_t *buf, size_t size) {
67 if (false) {
68 Serial.print("*** write ");
69 Serial.print(size);
70 Serial.print(" ... ");
71 size_t written = client.write(buf, size);
72 Serial.println(written);
73 } else {
74 client.write(buf, size);
75 }
76 }
77 bool preConnect(bool firstCall) {
78 if (firstCall) { // since 2023-08-10
79 if (!Serial) Serial.begin(DD_SERIAL_BAUD);
80#if defined(ARDUINO_UNOR4_WIFI)
81 if (WiFi.status() == WL_NO_MODULE) {
82 Serial.println("XXX communication with WiFi module failed");
83 }
84 String fv = WiFi.firmwareVersion();
85 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
86 Serial.println("XXX please upgrade the firmware");
87 } else {
88 Serial.println("* WIFI_FIRMWARE_LATEST_VERSION=" + String(WIFI_FIRMWARE_LATEST_VERSION));
89 }
90#endif
91 }
92 if (true) { // since 2023-08-16
93 if (firstCall && !client.connected()) {
94 connectionState = '0';
95 }
96 checkConnection();
97 return connectionState == 'C';
98 } else if (true) { // since 2023-08-15
99 if (firstCall && !client.connected()) {
100 Serial.println("setup WIFI");
101 WiFi.disconnect();
102 WiFi.begin(ssid, password);
103 connectionState = ' ';
104 stateMillis = 0;
105 }
106 checkConnection();
107 return connectionState == 'C';
108 } else if (true) { // since 2023-06-05
109 if (firstCall) {
110 if (true) {
111 if (client.connected()) {
112 Serial.println("stop client for new setup");
113 client.stop();
114 delay(2000); // wait a bit
115 }
116 WiFi.disconnect();
117 } else if (true) { // since 2023-06-03
118 client.stop();
119 WiFi.disconnect();
120 }
121 Serial.println("setup WIFI");
122 WiFi.begin(ssid, password);
123 connectionState = ' ';
124 stateMillis = 0;
125 }
126 // delay(200);
127 checkConnection();
128 return connectionState == 'C';
129 } else {
130 if (firstCall) {
131 // if (logToSerial)
132 // Serial.begin(serialBaud);
133 if (true) { // since 2023-06-03
134 client.stop();
135 WiFi.disconnect();
136 }
137 WiFi.begin(ssid, password);
138 connectionState = ' ';
139 stateMillis = 0;
140 } else {
141 checkConnection();
142 delay(200);
143 }
144 return connectionState == 'C';
145 }
146 }
147 void flush() {
148 client.flush();
149 }
150 void validConnection() {
151#ifdef LOG_DDWIFI_STATUS
152 Serial.println(" ... validate ...");
153#endif
154 checkConnection();
155 }
156 bool canConnectPassive() {
157 return true;
158 }
159 bool canUseBuffer() {
160 return true;
161 }
162 private:
163// bool _connectToNetwork() {
164// WiFi.begin(ssid, password);
165// long last = millis();
166// while (true) {
167// uint8_t status = WiFi.status();
168// if (status == WL_CONNECTED)
169// break;
170// delay(200);
171// long diff = millis() - last;
172// if (diff > 1000) {
173// if (logToSerial) {
174// Serial.print("binding WIFI ");
175// Serial.print(ssid);
176// #ifdef LOG_DDWIFI_STATUS
177// Serial.print(" ... ");
178// Serial.print(status);
179// #endif
180// Serial.println(" ...");
181// }
182// last = millis();
183// }
184// }
185// if (logToSerial) {
186// Serial.print("binded WIFI ");
187// Serial.println(ssid);
188// }
189// return true;
190// }
191// bool _connectToClient(bool initConnect) {
192// if (initConnect) {
193// if (!_connectToNetwork())
194// return false;
195// server.begin();
196// }
197// long last = millis();
198// while (true) {
199// client = server.available();
200// if (client) {
201// break;
202// } else {
203// if (WiFi.status() != WL_CONNECTED)
204// return false;
205// long diff = millis() - last;
206// if (diff >= 1000) {
207// IPAddress localIP = WiFi.localIP();
208// uint32_t localIPValue = localIP;
209// if (logToSerial) {
210// #ifdef LOG_DDWIFI_STATUS
211// Serial.print("via WIFI ... ");
212// Serial.print(WiFi.status());
213// Serial.print(" ... ");
214// #endif
215// Serial.print("listening on ");
216// // #ifdef LOG_DDWIFI_STATUS
217// // Serial.print("(");
218// // Serial.print(localIPValue);
219// // Serial.print(") ");
220// // #endif
221// Serial.print(localIP);
222// Serial.print(":");
223// Serial.print(port);
224// Serial.println(" ...");
225// }
226// last = millis();
227// if (localIPValue == 0)
228// return false;
229// }
230// }
231// }
232// return true;
233// }
234 void checkConnection() {
235 if (connectionState == '0' || connectionState == 'C') { // since 2023-08-16 added check connectionState == '0'
236 if (connectionState == '0' || WiFi.status() != WL_CONNECTED) {
237 if (connectionState == 'C') {
238 Serial.println("lost WiFi ... try bind WiFi again ...");
239 client.stop();
240 }
241 WiFi.disconnect();
242 Serial.println("setup WIFI");
243 WiFi.begin(ssid, password);
244 connectionState = ' ';
245 stateMillis = 0;
246 } else if (!client.connected()) {
247 client.stop();
248 Serial.println("lost connection ... try bind again ...");
249 connectionState = 'W';
250 stateMillis = 0;
251 } else {
252 return;
253 }
254 }
255 if (connectionState == ' ') {
256 uint8_t status = WiFi.status();
257 if (status == WL_CONNECTED) {
258 Serial.print("binded WIFI ");
259 Serial.println(ssid);
260 server.begin();
261 connectionState = 'W';
262 stateMillis = 0;
263 } else {
264 long diff = millis() - stateMillis;
265 if (stateMillis == 0 || diff > 1000) {
266 Serial.print("binding WIFI ");
267 Serial.print(ssid);
268#ifdef LOG_DDWIFI_STATUS
269 Serial.print(" ... ");
270 Serial.print(status);
271#endif
272 Serial.println(" ...");
273 stateMillis = millis();
274 }
275 }
276 } else {
277 uint8_t status = WiFi.status();
278 if (status != WL_CONNECTED) {
279 connectionState = ' ';
280 stateMillis = 0;
281 } else {
282 long diff = millis() - stateMillis;
283 if (diff >= 1000) {
284 IPAddress localIP = WiFi.localIP();
285 uint32_t localIPValue = localIP;
286#ifdef LOG_DDWIFI_STATUS
287 Serial.print("via WIFI ... ");
288 Serial.print(WiFi.status());
289 Serial.print(" ... ");
290#endif
291 Serial.print("listening on ");
292 Serial.print(localIP);
293 Serial.print(":");
294 Serial.print(port);
295 Serial.println(" ...");
296 // if (true) {
297 // // testing for ESP
298 // Serial.println("*** sleep ...");
299 // esp_sleep_enable_timer_wakeup(10 * 1000); // 10ms
300 // esp_light_sleep_start();
301 // Serial.println("*** ... up");
302 // }
303 stateMillis = millis();
304 }
305 WiFiClient cli = server.available();
306 if (cli) {
307 client = cli;
308 connectionState = 'C';
309 stateMillis = 0;
310 Serial.println("client connected");
311 } else {
312 //Serial.print("~");
313 }
314 }
315 }
316 }
317 private:
318 const char* ssid;
319 const char *password;
320 int port;
321 //bool logToSerial;
322 char connectionState;
323 long stateMillis;
324 WiFiServer server;
325 WiFiClient client;
326};
327
328
329
330
331#endif
Class for DD input/output; you explicitly constructed it, pass in when instantiate DumbDisplay,...
Definition: _dd_io.h:9
Subclass of DDInputOutput.
Definition: wifidumbdisplay.h:23