Skip to content

Commit 0b5fc0f

Browse files
committed
Change char array to vector for wifiClient read
1 parent 4e9de7e commit 0b5fc0f

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

src/Wifi_Helper.cpp

+23-34
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,7 @@ namespace Wifi_Helper
148148
// Begin WiFi
149149
WiFi.begin(wifi_ssid, wifi_password);
150150

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
165152

166153
Serial.println("Creating Wifi Update Task");
167154
/* Task function. */
@@ -213,24 +200,21 @@ namespace Wifi_Helper
213200
Serial.println("-- End of Wifi initialisation --");
214201
}
215202

216-
namespace
203+
void Update(void *pvParameters)
217204
{
205+
const int readBufferMax = 64;
206+
vector<char> readBuffer;
207+
readBuffer.reserve(readBufferMax);
208+
218209
unsigned long previousMillisWifi = 0;
219210
unsigned long previousMillisServer = 0;
220211
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;
224215
unsigned long currentMillisWifi = 0;
225216
unsigned long currentMillisServer = 0;
226217
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];
234218

235219
for (;;)
236220
{
@@ -281,8 +265,10 @@ namespace Wifi_Helper
281265
previousMillisTeleplot = currentMillisTeleplot;
282266
}
283267
}
284-
else
268+
269+
if(WiFi.status() != WL_CONNECTED && Printer::teleplotUDP.IsInitialized())
285270
{
271+
// We need to un-init in case of wifi lost
286272
Printer::teleplotUDP.~Teleplot();
287273
}
288274

@@ -291,23 +277,26 @@ namespace Wifi_Helper
291277
while (wifiClient.available() > 0)
292278
{
293279
char tmpChar = wifiClient.read();
294-
if (indexBuffer < readBufferMax)
280+
if (readBuffer.size() < readBuffer.capacity())
295281
{
296-
readBuffer[indexBuffer++] = tmpChar;
282+
readBuffer.push_back(tmpChar);
297283
if (tmpChar == '\n')
298284
{
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();
301290
// Read and extract Commands
302-
ESP32_Helper::BufferReadCommand(readBuffer, indexBuffer);
303-
indexBuffer = 0;
291+
ESP32_Helper::BufferReadCommand(readBuffer.data(), readBuffer.size());
292+
readBuffer.clear();
304293
}
305294
}
306295
else
307296
{
308297
wifiClient.print("Read Buffer Overflow : ");
309-
wifiClient.println(indexBuffer);
310-
indexBuffer = 0;
298+
wifiClient.println(readBuffer.size());
299+
readBuffer.clear();
311300
}
312301
}
313302
}

0 commit comments

Comments
 (0)