Go Down

Topic: Is there a similar "socket_connect" to Arduino? (Read 719 times) previous topic - next topic

jgarcia

Jun 18, 2013, 02:07 pm Last Edit: Jun 18, 2013, 03:13 pm by jgarcia Reason: 1
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

johnwasser

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

Code: [Select]

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
  }
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

jgarcia

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

jgarcia

#3
Jun 21, 2013, 03:52 pm Last Edit: Jun 21, 2013, 09:12 pm by jgarcia Reason: 1
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: [Select]
#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: [Select]
#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");
 }
}

johnwasser

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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

jgarcia

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!

Frisky

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

johnwasser


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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Frisky

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

Go Up