TCP/IP communication between Matlab - Arduino. Stuck at fopen.

I have a TCP/IP communication between Matlab - Arduino. For some reason, I stuck at fopen even when Arduino indicated a connection has been established.

Here’s my Arduino code:

#include <WiFi101.h>

// To connect to the server on laptop
char ssid[] = "EEELAB";
char pass[] = "@adelaide";
int status = WL_IDLE_STATUS;

IPAddress server(129,127,225,25);
WiFiClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  
  // Setup wifi connection
  Serial.println("Connecting to wifi.");
  while (status != WL_CONNECTED) {
    Serial.println("Can't connect to wifi, try again.");
    status = WiFi.begin(ssid, pass);
  }

  delay(10000); // If don't include this, it won't work
                // don't know why, can anyone figure out??
  
  // Setup server connection
  Serial.println("Connecting to server.");
  int j = client.connect(server, 1234);
  while (j != 1)  {
    Serial.println();
    Serial.println(j);
    Serial.println("Can't connect to server, try again.");
    j = client.connect(server, 1234);
  }
  Serial.println("Done connecting to server");

}

void loop() {
  // put your main code here, to run repeatedly:
  if (client.available()) {
    int a = client.read();
    Serial.print("I got this from client: ");
    Serial.println(a);
    Serial.println();
  }
}

And my Matlab code:

t = tcpip('129.127.225.25',1234,'NetworkRole', 'server')
fopen(t)

On the Arduino, I can see on Serial Monitor that it has “Done connecting to server”, but fopen still stuck at a loop and not returning. Any idea?

Please fire up wireshark nd look at what happens on the line.

In the matlab code use '0.0.0.0' for the IP address.

Ooops .. just saw that you use Arduino as server and Matlab as server. One of them has to be client :slight_smile:

zwieblum:
Ooops .. just saw that you use Arduino as server and Matlab as server. One of them has to be client :slight_smile:

I don't think I use Arduino as a server. The function "server" next to IPAdress is to convert that IP adress to something the library can use later.

gbafamily:
In the matlab code use '0.0.0.0' for the IP address.

It won't connect that way. I also tried 'localhost', but it produces the same result as my initial post.

Maybe you should send some data from arduino to pc after connecting?