Skip to content

Commit 6dea617

Browse files
committed
Use JsonDocument stream API
Don't use an intermediate buffer for UDP packets, rather just stream into a JsonDocument.
1 parent 94d05e2 commit 6dea617

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

src/WeatherFlowUdp.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,13 @@ void WeatherFlowUdp::update() {
3838
// Process all waiting packets
3939
int packetSize = 0;
4040
while(packetSize = weatherUDP.parsePacket()) {
41-
char* buffer = (char*)malloc(packetSize+1);
42-
int bufferLen = weatherUDP.read(buffer, packetSize);
43-
44-
// Null terminate the buffer, just in case.
45-
buffer[bufferLen] = 0;
46-
bufferLen++;
47-
#ifdef DEBUG
48-
Serial.println(F("Received WeatherFlow packet"));
49-
Serial.println(buffer);
50-
#endif
51-
// Free the buffer if it wasn't handled.
52-
if (processPacket(buffer) != 0) {
53-
Serial.println(F("Packet not handled"));
54-
Serial.println(buffer);
55-
free(buffer);
56-
}
41+
DynamicJsonDocument tempDoc(600);
42+
DeserializationError err = deserializeJson(tempDoc, weatherUDP);
43+
if (err) {
44+
Serial.print(F("deserializeJson() failed of UDP packet: "));
45+
Serial.println(err.c_str());
46+
}
47+
processJsonDocument(tempDoc);
5748
}
5849
}
5950

0 commit comments

Comments
 (0)