how to pass data from a code on Arduino IDE to matlab

39 visualizaciones (últimos 30 días)
Hello,
i'm using arduino Nano 33 IoT to get data from a sensor. So i had to write a code on Arduino IDE because there are no libraries in Matlab, wich help for the calculating. That means i can only get the data using the interface I2C on Arduino Nano 33 IoT. When i use I2C directly on Matlab it doesn't work (problem of sensor libraries ). Now after i have the data on Arduino IDE i want to pass it directly to Matlab. In this Case there is two codes, one in C++ (for Arduino to run the sensor and getting the data) and the other code on matlab for image processing. The problem i'm facing is that i can't combine the two codes.
Can someone help me to figure out how i can run the C++ on Arduino and after that send the Data to Matlab ? The data is a Matrix with 768 pixels. Every 2 seconds i get new Matrix from Arduino in a loop.
Thanks in advance

Respuesta aceptada

Walter Roberson
Walter Roberson el 31 de En. de 2023
Use https://arduinogetstarted.com/reference/serial-println to convert to text to send. Or https://arduinogetstarted.com/reference/serial-write to send as binary (more efficient)
On the MATLAB side, serialport rather than using arduino . To read as text readline and sscanf or str2double . To read as binary read
  8 comentarios
Ibrahim alami merroui
Ibrahim alami merroui el 5 de Feb. de 2023
To be more specific i will add the both codes i wrote. I want to test the comminication by sending to matlab a verktor as string.
The think is, i'm getting no data (ReplyBuffer[])
Matlab Code
uByte = udpport("byte");
echoudp("on",2390)
% uByte is a udpport byte object
configureTerminator(uByte,"CR/LF");
writeline(uByte,"hello","192.168.2.50",2390);
data = readline(uByte)
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 = 55753;
//unsigned int localPort = 2390;
char ReplyBuffer[] = { 2, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
WiFiUDP Udp;
// für Setup sollen alle Prints bleiben für INFOS
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
// einmaliger Gebrauch
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("static IP successful.");
// vielleicht brauchen wir nicht: Zur Sicherheit lassen wir
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() {
// if there's data available, read a packet
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();
}
Walter Roberson
Walter Roberson el 5 de Feb. de 2023
The way you build the echo server works for me when I change the IP address to "localhost". That is, that code for echoudp is for testing "loopback" -- testing that when you write data on the MATLAB end that it is properly going out and being received back as input on port 2390.
You would not use echoudp for the case where you expected the arduino to answer.
Note that the 2390 port that you writeline() to must match the local port number on the arduino end. You commented that out; you are assigning 55753 instead.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by