TCP/IP communication between the Yun and Matlab

I am trying to set up a WiFi communication between my Arduino Yun and my PC, I have to use Matlab /simulink for that. I already connected the Yun to my WiFi network, it has the Ip Address 192.168.2.105, my Pc has the IP Adress 192.168.2.103. I found this example for a TCP/IP communication in this forum, which I am running on the Yun:

#include <Bridge.h>
#include <YunClient.h>

#define PORT 80

// Define our client object
YunClient client;

void setup()
{
  // Bridge startup
  Bridge.begin();
  
  Serial.begin(9600);

  while (!Serial); // wait for a serial connectionM
}

void loop()
{
  // Make the client connect to the desired server and port
  IPAddress addr(192, 168, 2, 103);
  
  // Or define it using a single unsigned 32 bit value
  // IPAddress addr(0xc0a8sab9); // same as 192.168.42.185
  
  // Or define it using a byte array
  // const uint8 addrBytes = {192, 168, 42, 185};
  // IPAddress addr(addrBytes);

  client.connect(addr, PORT);
  
  // Or connect by a server name and port.
  // Note that the Yun doesn't support mDNS by default, so "Yun.local" won't work
  // client.connect("ServerName.com", PORT);

  if (client.connected())
  {
    Serial.println("Connected to the server.");
    
    // Send something to the client
    //client.println("Something...");
    client.println(3);
    // Cheap way to give the server time to respond.
    // A real application (as opposed to this simple example) will want to be more intelligent about this.
    delay (250);
  
    // Read all incoming bytes available from the server and print them
    while (client.available())
    {
      char c = client.read();
      Serial.print(c);
    }
    Serial.flush();

    // Close the connection
    client.stop();
  }
  else
    Serial.println("Could not connect to the server.");  
    
  // Give some time before trying again
  delay (10000);
}

In Simulink I use the TCP/IP receive und TCP/IP send blocks from the Instrument toolbox in the model attached, but I am always getting the error message

Error evaluating registered method 'Start' of MATLAB S-Function 'stcpiprb' in 'host_receive/TCP//IP Receive'.
Caused by:
Unsuccessful open: Connection timed out: connect

I would expect, that the sketch and simulink would create a echo, starting with 2 and getting doubled every time.

I don't know a lot about Wifi and TCP/IP communication, can someone help me please to set up a communication?

@Ard_Yun,
This is all nice information. What Operating system are you running on your PC?
Jesse

jessemonroy650:
@Ard_Yun,
This is all nice information. What Operating system are you running on your PC?
Jesse

I am running Windows 7 on my PC.

Does someone know if the problem is in the arduino code or a simulink problem?

Does someone know if the problem is in the arduino code or a simulink problem?

Since it is simulink that is having a problem, the answer to that seems obvious.

PaulS:
Since it is simulink that is having a problem, the answer to that seems obvious.

And the Arduino always sends to the console, "Could not connect to the server." so it can be an arduino problem too, but one pbolem is, that both, the yun and simulink, are clients and one needs to be a server. I will try to change that first.

A UDP communication would also work, is it possible to send UDP messages with the bridge library, because I can't find anything about that in the documentation?

Ard_Yun:
both, the yun and simulink, are clients and one needs to be a server.

Yes, that would certainly be a problem. Look at the YunServer class - it listens for incoming connections, and creates a YunClient object when the remote system makes a connection.

I've helped several people to do this, one of the examples with code is here: Arduino Forum Be sure to read all the way to the end, there is an important fix in the last post.

is it possible to send UDP messages with the bridge library

I'm not aware of any way to do a UDP connection through the Bridge Library. You would have to manage such a connection on the Linux side of the Yun.

Any fix to this problem?