ArduinoDumbDisplay v0.9.9-r50
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 SSID / name; NULL if WiFi explicitly connected */
27 /* - passphrase: WIFI password; can be NULL if Wifi explicitly connected */
28 /* - serverPort: server port */
29 DDWiFiServerIO(const char* ssid = NULL, const char *passphrase = NULL, int serverPort = DD_WIFI_PORT):
30 DDInputOutput(DD_SERIAL_BAUD, false, false), 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 (firstCall && !client.connected()) {
93 connectionState = '0';
94 }
95 checkConnection();
96 return connectionState == 'C';
97 }
98 void flush() {
99 client.flush();
100 }
101 void validConnection() {
102#ifdef LOG_DDWIFI_STATUS
103 Serial.println(" ... validate ...");
104#endif
105 checkConnection();
106 }
107 bool canConnectPassive() {
108 return true;
109 }
110 bool canUseBuffer() {
111 return true;
112 }
113 private:
114// bool _connectToNetwork() {
115// WiFi.begin(ssid, password);
116// long last = millis();
117// while (true) {
118// uint8_t status = WiFi.status();
119// if (status == WL_CONNECTED)
120// break;
121// delay(200);
122// long diff = millis() - last;
123// if (diff > 1000) {
124// if (logToSerial) {
125// Serial.print("binding WIFI ");
126// Serial.print(ssid);
127// #ifdef LOG_DDWIFI_STATUS
128// Serial.print(" ... ");
129// Serial.print(status);
130// #endif
131// Serial.println(" ...");
132// }
133// last = millis();
134// }
135// }
136// if (logToSerial) {
137// Serial.print("binded WIFI ");
138// Serial.println(ssid);
139// }
140// return true;
141// }
142// bool _connectToClient(bool initConnect) {
143// if (initConnect) {
144// if (!_connectToNetwork())
145// return false;
146// server.begin();
147// }
148// long last = millis();
149// while (true) {
150// client = server.available();
151// if (client) {
152// break;
153// } else {
154// if (WiFi.status() != WL_CONNECTED)
155// return false;
156// long diff = millis() - last;
157// if (diff >= 1000) {
158// IPAddress localIP = WiFi.localIP();
159// uint32_t localIPValue = localIP;
160// if (logToSerial) {
161// #ifdef LOG_DDWIFI_STATUS
162// Serial.print("via WIFI ... ");
163// Serial.print(WiFi.status());
164// Serial.print(" ... ");
165// #endif
166// Serial.print("listening on ");
167// // #ifdef LOG_DDWIFI_STATUS
168// // Serial.print("(");
169// // Serial.print(localIPValue);
170// // Serial.print(") ");
171// // #endif
172// Serial.print(localIP);
173// Serial.print(":");
174// Serial.print(port);
175// Serial.println(" ...");
176// }
177// last = millis();
178// if (localIPValue == 0)
179// return false;
180// }
181// }
182// }
183// return true;
184// }
185 void checkConnection() {
186 if (connectionState == '0' || connectionState == 'C') { // since 2023-08-16 added check connectionState == '0'
187 if (connectionState == '0' || WiFi.status() != WL_CONNECTED) {
188 if (connectionState == 'C') {
189 Serial.println("lost WiFi ... try bind WiFi again ...");
190 client.stop();
191 }
192 if (ssid != NULL && WiFi.status() != WL_CONNECTED) { // since 2025-03-10
193 WiFi.disconnect();
194 Serial.println("setup WIFI");
195 WiFi.begin(ssid, password);
196 } else {
197 Serial.println("assume explicitly connected WIFI");
198 }
199 connectionState = ' ';
200 stateMillis = 0;
201 } else if (!client.connected()) {
202 client.stop();
203 Serial.println("lost connection ... try bind again ...");
204 connectionState = 'W';
205 stateMillis = 0;
206 } else {
207 return;
208 }
209 }
210 if (connectionState == ' ') {
211 uint8_t status = WiFi.status();
212 if (status == WL_CONNECTED) {
213 Serial.print("binded WIFI ");
214 Serial.println(ssid);
215 server.begin();
216 connectionState = 'W';
217 stateMillis = 0;
218 } else {
219 long diff = millis() - stateMillis;
220 if (stateMillis == 0 || diff > 1000) {
221 Serial.print("binding WIFI ");
222 Serial.print(ssid);
223#ifdef LOG_DDWIFI_STATUS
224 Serial.print(" ... ");
225 Serial.print(status);
226#endif
227 Serial.println(" ...");
228 stateMillis = millis();
229 }
230 }
231 } else {
232 uint8_t status = WiFi.status();
233 if (status != WL_CONNECTED) {
234 connectionState = ' ';
235 stateMillis = 0;
236 } else {
237 long diff = millis() - stateMillis;
238 if (diff >= 1000) {
239 IPAddress localIP = WiFi.localIP();
240 //uint32_t localIPValue = localIP;
241#ifdef LOG_DDWIFI_STATUS
242 Serial.print("via WIFI ... ");
243 Serial.print(WiFi.status());
244 Serial.print(" ... ");
245#endif
246 Serial.print("listening on ");
247 Serial.print(localIP);
248 Serial.print(":");
249 Serial.print(port);
250 Serial.println(" ...");
251 // if (true) {
252 // // testing for ESP
253 // Serial.println("*** sleep ...");
254 // esp_sleep_enable_timer_wakeup(10 * 1000); // 10ms
255 // esp_light_sleep_start();
256 // Serial.println("*** ... up");
257 // }
258 stateMillis = millis();
259 }
260 WiFiClient cli = server.available();
261 if (cli) {
262 client = cli;
263 connectionState = 'C';
264 stateMillis = 0;
265 Serial.println("client connected");
266 } else {
267 //Serial.print("~");
268 }
269 }
270 }
271 }
272 private:
273 const char* ssid;
274 const char *password;
275 int port;
276 //bool logToSerial;
277 char connectionState;
278 long stateMillis;
279 WiFiServer server;
280 WiFiClient client;
281};
282
283
284
285
286#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