using UDP to receive ESP32 sensor data by json format

16 次查看(过去 30 天)
I want to using matlab to receive Esp32 data by UDP , UDP is fast.
I just code a test program which I send command to esp32 to return a sine wave , freq and sample is sent by matlab to esp32. My program works well when the total sample is under 160, it gives me the result I expected. But when I add the sample number, the UDP communication failed.
esp32 works seems correctly, I monitered the data by serial data port .
My program is following:
%% initialise UDP
% ESP32 IP and port
global ALLOVER;
global buffer;
global bufferTotal;
esp32IP = '192.168.1.52'; % ESP32 IP
esp32Port = 12345;
udpObj = udpport("datagram", "IPV4");
configureCallback(udpObj,"datagram",1,@readMyData)
disp('UDP start');
% send command to esp32
Freq = 10; % frequency
LengthSample = 150; % total sample number
command = struct('command', 'collect_data', 'length', LengthSample, 'frequency', Freq);
jsonCommand = jsonencode(command);
write(udpObj, jsonCommand, "string", esp32IP, esp32Port);
disp(['command sent out: ', jsonCommand]);
% receiving data
disp("reveive data...");
buffer = ""; % empty buffer
bufferTotal=0;
ALLOVER=false;
while (true)
if (ALLOVER==false)
pause(0.1);
else
break;
end
end
%
% delete the "END"
buffer = erase(buffer, "END");
% get sample from JSON
try
receivedData = jsondecode(buffer);
value = receivedData.values;
% draw sine wave
figure(1);
samplingInterval = 5; % ms
time = (0:length(value)-1) * samplingInterval;
plot(time, value, 'LineWidth', 1.5);
xlabel('Time (ms)');
ylabel('Amplitude');
title('Received Signal');
grid on;
disp('draw finish');
catch ME
disp('JSON fail:');
disp(ME.message);
end
function readMyData(u, ~)
% UDP
global buffer;
global ALLOVER;
global bufferTotal;
bufferTotal=bufferTotal+1;
packet = read(u, 1, "string");
buffer = buffer + string(packet.Data); % over 1 package
disp("receive data:");
disp(packet.Data); %
% find "END"
if contains(buffer, "END")
disp("Found 'END',whole data ");
ALLOVER=true;
end
end
esp32 program is following:
#include <WiFi.h>
#include <AsyncUDP.h>
#include <ArduinoJson.h>
const char* ssid = "ws0922902";
const char* password = "13201604223";
AsyncUDP udp;
void setup() {
Serial.begin(115200);
// WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("conneting with WiFi ...");
}
Serial.println("WiFi is connected");
Serial.println(WiFi.localIP());
// UDP
if (udp.listen(12345)) {
Serial.println("UDP has started");
udp.onPacket([](AsyncUDPPacket packet) {
Serial.print("command received: ");
Serial.println((char*)packet.data());
// JSON
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, packet.data());
if (error) {
Serial.println("JSON format failure");
return;
}
String command = doc["command"];
int length = doc["length"];
int frequency = doc["frequency"];
if (command == "collect_data") {
// generate sine wave
float data[length];
for (int i = 0; i < length; i++) {
data[i] = sin(2 * PI * frequency * (i * 0.005)); // 5 ms sample
}
// send by JSON
StaticJsonDocument<2048> response;
//JsonArray timeArray = response.createNestedArray("time");
JsonArray valuesArray = response.createNestedArray("values");
for (int i = 0; i < length; i++) {
valuesArray.add(data[i]); //
}
String responseData;
serializeJson(response, responseData);
responseData += "END";
packet.printf(responseData.c_str());
Serial.println("data has sent out");
Serial.printf(responseData.c_str());
}
});
}
}
void loop() {
// nothing
}

采纳的回答

jun
jun 2024-11-29
I have found the problem that is when esp32 send the UDP package over 1500, some data is lost, so the END will never be detected in matlab . rewrite esp32 be sure max number of sent is less than 1400, if over, send several times.
...................................
const int MAX_PACKET_SIZE = 1400; // Max payload size to fit within MTU
............................
responseData += "END";
// Send the response in smaller packets
int totalLength = responseData.length();
Serial.println("Sending data in chunks...");
for (int offset = 0; offset < totalLength; offset += MAX_PACKET_SIZE) {
String chunk = responseData.substring(offset, offset + MAX_PACKET_SIZE);
packet.printf(chunk.c_str(), chunk.length());
delay(10); // Allow receiver to process
}
Serial.println("Data sent successfully.");
Serial.printf(responseData.c_str());
}

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Analog Data Acquisition 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by