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