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