How to send data from Arduino Nano 33 IoT to Matlab using UDP over WIFI
15 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 11 Feb 2023
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"
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!