Pages: [1]   Go Down
Author Topic: Is there a similar "socket_connect" to Arduino?  (Read 621 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are you guys?

I already did Arduino comunicating with my webserver just using PHP> socket_connect($sock,$Arduino_IP, 8081); and a simple read/write
But now I want my Arduindo comunicating with another Arduino, and I didn't find a "socket_connect" to Arduino, Is there a similar "socket_connect" to Arduino?

Thanks
« Last Edit: June 18, 2013, 08:13:18 am by jgarcia » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From File->Examples->Etherenet->TelnetClient:

Code:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Enter the IP address of the server you're connecting to:
IPAddress server(1,1,1,1);

EthernetClient client;

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac);
 
  // give the Ethernet shield a second to initialize:
  delay(1000);

  // if you get a connection, report back via serial:
  if (client.connect(server, 10002)) {
    // Conected
  }
  else {
    // Failed
  }
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did some progress, using TelnetClient exemple, thank you johnwasser.

BUT, I didn't comunicate two arduinos.

My console shows:

connecting...
connected

disconnecting.

- - -

"client.write("B2222C08");" isn't read in by the other Arduino...
TelnetClient exemple don't use client.write, just client.read. Do you have a working code for both sides?

Thanks
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's my exemple code, the Arduino Client connect the socket but Arduino Serve don't get the message.
Why is not working?

Arduino Server
Code:
#include <SPI.h>
#include <Ethernet.h>

char msg[100];

//change the MAC
byte mac[] = { 0xDA, 0xAA, 0xBA, 0xEF, 0xFE, 0xED };
byte ip[] = { 10,20,30,79 };
byte gateway[] = { 10,20,30, 1 };  
byte subnet[] = { 255, 255, 255, 0 };

EthernetServer server(8081);

EthernetClient client;

void setup()
{      
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop()
{
  client = server.available();
  if(client){
      for(int i = 0; client.available() > 0; i++)
          msg[i] = client.read();
  
      client.write(msg);
  }
  else
      client.write("NO MSG READ");
  delay(1000);    
}

- - -

Arduino Client
Code:
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,20,30,99);
IPAddress serverIP(10,20,30,79);

EthernetClient client;

void setup() {
  Ethernet.begin(mac, ip);
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  Serial.println("connecting socket...");
  if (client.connect(serverIP, 8081)) {
    Serial.println("connected");
  }
  else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if(client){
    client.write("TEST MSG");
    Serial.print("TEST MSG");  
    delay(2000);
  }
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar);
    }
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    while(!client.connect(serverRep, 8081)){
      delay(10000);
      Serial.println("Trying to connect");
    }
    Serial.println("connected");
  }
}
« Last Edit: June 21, 2013, 02:12:57 pm by jgarcia » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The MAC address is supposed to be globally unique and MUST be unique on the LAN.

There are websites that will generate a random MAC address for you, or you could make something up. Just don't use the same address on two machines.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perfect!

I did many diferent tests, with diferent codes, and always remembered to never put the same MAC!
Under this last teste!!!

And that was the problem, thank you John Wasser again and again and again!
Logged

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi question is there a way for the server to know which of the possible 4 clients connection its talking to?
  Could the sever send a message just to one of the connected clients only?
Im working with udp and wondering can I a message to
All ofvthe connected clients?

I've been looking over most of the examples there using get/response webserver type.
Thanks
Don
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi question is there a way for the server to know which of the possible 4 clients connection its talking to?
  Could the sever send a message just to one of the connected clients only?
Im working with udp and wondering can I a message to
All ofvthe connected clients?

I've been looking over most of the examples there using get/response webserver type.
Thanks
Don

The server.available() function will only return a client that has data ready to read.  The client must send first.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can work with that. I was planning to send a call sign with the server would compare and allow it to communicate with the server. If it didn't match then issue a client.stop (); function to disconnect.
Thanks Paul I'm looking for project id3as and learn more about ethernet library and the classes in it.
Don
Logged

Pages: [1]   Go Up
Jump to: