How to send data from Arduino Nano 33 IoT to Matlab using UDP over WIFI

15 次查看(过去 30 天)
Hey,
I'm trying to send data from Arduino Nano 33 IoT (has WIFI chip) to Matlab using UDP. The gateway is the router. I wrote two codes for Arduino and Matlab but i'm geetting not data (ReplyBuffer).
IP-Adress from Arduino:
SSID: MagentaWLAN-6XU4
signal strength (RSSI):-62 dBm
Local IP: 192.168.2.50
Subnet Mask: 255.255.255.0
Gateway IP: 192.168.2.1
Matlab Code
remPort=8888;
host='192.168.2.50';
locPort=50001;
u = udp(host,'RemotePort',remPort,'LocalPort',locPort);
fopen(u)
A = fread(u);
fclose(u)
Arduino Code:
#include <WiFi.h>
#include <WiFiUdp.h>
int status = WL_IDLE_STATUS;
char ssid[] = "MagentaWLAN-6XU4"; // your network SSID (name)
char pass[] = "11980331210951165236"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
unsigned int localPort = 8888;
char ReplyBuffer[] = { 2, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
WiFiUDP Udp;
void setup() {
Serial.begin(115200);
// einmaliger Gebrauch
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, keyIndex, pass); // Das habe ich eingefügt
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Udp.begin(localPort);
}
void loop() {
int packetSize = Udp.parsePacket();
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// da werden dann unsere Daten an Matlab geschikt
Udp.write(ReplyBuffer);
Udp.endPacket();
}
The error I'm getting in Matlab:
Warning: udp will be removed in a future release. Use udpport instead.
Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period.
'udp' unable to read any data. For more information on possible reasons, see UDP Read Warnings.
and the Data (A) is empty.
Does someone see where is the Problem in the code?
  7 个评论
Walter Roberson
Walter Roberson 2023-2-11
I would not worry about the firmware thing at the moment; the code is written in such a way that it would also complain if you had a newer firmware.
Have you tried using the packet generation program mentioned in https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/udp-examples.html to see whether it can see a response? We need to distinguish between "no reply packet is really being generated" and "reply packet is generated but is sent to wrong place" and "reply packet is being correctly generated but MATLAB specifically is having problems with it"
Ibrahim alami merroui
thanks for the reply.
I couldn't upload the code you shared, but i have used this code Send and Receive UDP String | Arduino Documentation | Arduino Documentation. I think it does the same work. I downloaded the Packet Sender App for Windows. After I uploaded the sketch on Arduino IDE I sent directly a messag from the App and I got the reply.
Serial Monitor:
Starting connection to server...
Received packet of size 13
From 192.168.2.41, port 53988
Contents:
hello ibrahim
The problem i see that i can't make any communication beetween matlab and packet sender app. I used this code:
remPort=18;
host='192.168.2.50';
locPort=19;
u = udp(host,'RemotePort',remPort,'LocalPort',locPort)
fopen(u)
A = fread(u);
write('hey');
fclose(u)
I think i should use udpport() instead of udp. With this code i'm getting not packets or receiving it from the app.
If I use the port 53988 as remPort to send data, matlab says:
Error using icinterface/fopen
Unsuccessful open: Address already in use: Cannot bind
In this case the reply packet is generated but is sent to wrong place and matlab can't receive anything.

请先登录,再进行评论。

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by