@@ -148,20 +148,7 @@ namespace Wifi_Helper
148
148
// Begin WiFi
149
149
WiFi.begin (wifi_ssid, wifi_password);
150
150
151
- // ! Connection to server will be done later in the Update task
152
-
153
- // time to connect to wifi
154
- // delay(3000);
155
-
156
- // Connect to server
157
- /* if (wifiClient.connect(wifi_server_ip.c_str(), wifi_server_port))
158
- {
159
- Serial.println("Connected to server !");
160
- }
161
- else
162
- {
163
- Serial.println("Connection to server failed");
164
- }*/
151
+ // ! Connection with wifiClient to server will be done later in the Update task
165
152
166
153
Serial.println (" Creating Wifi Update Task" );
167
154
/* Task function. */
@@ -213,24 +200,21 @@ namespace Wifi_Helper
213
200
Serial.println (" -- End of Wifi initialisation --" );
214
201
}
215
202
216
- namespace
203
+ void Update ( void *pvParameters)
217
204
{
205
+ const int readBufferMax = 64 ;
206
+ vector<char > readBuffer;
207
+ readBuffer.reserve (readBufferMax);
208
+
218
209
unsigned long previousMillisWifi = 0 ;
219
210
unsigned long previousMillisServer = 0 ;
220
211
unsigned long previousMillisTeleplot = 0 ;
221
- unsigned long intervalWifi = 5000 ;
222
- unsigned long intervalServer = 5000 ;
223
- unsigned long intervalTeleplot = 5000 ;
212
+ const unsigned long intervalWifi = 5000 ;
213
+ const unsigned long intervalServer = 5000 ;
214
+ const unsigned long intervalTeleplot = 5000 ;
224
215
unsigned long currentMillisWifi = 0 ;
225
216
unsigned long currentMillisServer = 0 ;
226
217
unsigned long currentMillisTeleplot = 0 ;
227
- }
228
-
229
- void Update (void *pvParameters)
230
- {
231
- uint16_t indexBuffer = 0 ;
232
- const int readBufferMax = 64 ;
233
- char readBuffer[readBufferMax];
234
218
235
219
for (;;)
236
220
{
@@ -281,8 +265,10 @@ namespace Wifi_Helper
281
265
previousMillisTeleplot = currentMillisTeleplot;
282
266
}
283
267
}
284
- else
268
+
269
+ if (WiFi.status () != WL_CONNECTED && Printer::teleplotUDP.IsInitialized ())
285
270
{
271
+ // We need to un-init in case of wifi lost
286
272
Printer::teleplotUDP.~Teleplot ();
287
273
}
288
274
@@ -291,23 +277,26 @@ namespace Wifi_Helper
291
277
while (wifiClient.available () > 0 )
292
278
{
293
279
char tmpChar = wifiClient.read ();
294
- if (indexBuffer < readBufferMax )
280
+ if (readBuffer. size () < readBuffer. capacity () )
295
281
{
296
- readBuffer[indexBuffer++] = tmpChar;
282
+ readBuffer. push_back ( tmpChar) ;
297
283
if (tmpChar == ' \n ' )
298
284
{
299
- wifiClient.print (" Received : " );
300
- wifiClient.write (readBuffer, indexBuffer);
285
+ wifiClient.print (" Received " );
286
+ wifiClient.print (readBuffer.size ());
287
+ wifiClient.print (" : " );
288
+ wifiClient.write (readBuffer.data (), readBuffer.size ());
289
+ wifiClient.println ();
301
290
// Read and extract Commands
302
- ESP32_Helper::BufferReadCommand (readBuffer, indexBuffer );
303
- indexBuffer = 0 ;
291
+ ESP32_Helper::BufferReadCommand (readBuffer. data (), readBuffer. size () );
292
+ readBuffer. clear ();
304
293
}
305
294
}
306
295
else
307
296
{
308
297
wifiClient.print (" Read Buffer Overflow : " );
309
- wifiClient.println (indexBuffer );
310
- indexBuffer = 0 ;
298
+ wifiClient.println (readBuffer. size () );
299
+ readBuffer. clear () ;
311
300
}
312
301
}
313
302
}
0 commit comments