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