Pages: [1]   Go Down
Author Topic: WIFI shield TCP connection problem  (Read 709 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Yes, we can (solder)!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to set up my WIFI shield to send and receive messages over TCP sockets. There's a Max MSP patch as well with a TCP client object that seems to work well. Yet I cannot get the two, the WIFI shield and Max to talk to each other over TCP. What confuses me is that which IP address should I provide in the Arduino code? The router's, the shield's or the localhost IP?

Code:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <SPI.h>


char ssid[] = "nwtwork";
//password of your WPA Network
char pass[] = "password";

WiFiClient client;
IPAddress server(); //shield IP address
void setup()
{
  Serial.begin(9600);
  Serial.println("** Scan Networks **");
  byte numSsid = WiFi.scanNetworks();
  Serial.print("SSID List:");
  Serial.println(numSsid);
  WiFi.begin(ssid, pass);
  //delay(5000);
  Serial.println("Connected to wifi");
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("Shield IP Address: ");
  Serial.println(ip);
  client.connect(server,7120);
  if(client.connected()){
    Serial.println("CLIENT AVAILABLE");
  }
  else {
    Serial.println("CLIENT NOT AVAILABLE");
  }
}

void loop () {
  client.write("test");
  delay(1000);
  int in = client.read();
  Serial.println(in);
}

Also, do the port number on both sides have to match?


Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5974
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is probably not going to work. If the WiFi shield is the client, the variable server should be set to the localnet ip of the other unit (MAX MSP?). Something like this:
Code:
IPAddress server( 192,168,0,2); //MAX MSP IP address
The ports do not need to match, and normally do not. Insure your wireless settings in the router allow clients access to other localnet clients. This is "default-forwarding=yes" in my router.
Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Yes, we can (solder)!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

THanks! I've tried the local IP address (192.168.0.2) and different ports yet I still cannot see the two connecting.  The Max patch is a client too, could that be the problem?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5974
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want to use TCP, then one must be a server. Even UDP requires one to be a "server" (listen for UDP packets.
Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Yes, we can (solder)!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks very much, I managed to setup a server on the Arduino. I can broadcast messages however when the Max patch sends integers (formatted like this: send "23" ), they appear as random set of characters on the server side. How are TCP messages formatted?
Logged

Pages: [1]   Go Up
Jump to: