Go Down

Topic: Arduino to Arduino communication over Ethernet issues (Read 255 times) previous topic - next topic

Dexter101

Arduino Uno R3, wiznet w5100 ethernet shield on both ends.

Have used the Webserver example "https://www.arduino.cc/en/Tutorial/WebServer" to be able to read the analog inputs from the 1st Arduino and send them via Ethernet so they can be read by a client which I had hoped to make a 2nd Arduino to drive the PWM outputs.

The example does a good job of exploring the ability to display the data but not send data.  Any ideas how best to accomplish the task of reading the analog inputs (A0 thru A5) from the 1st Arduino, send via ethernet, read them at the 2nd Arduino and output them to the PWM pins (3,5,6)?


I have been reading the pins in the following way:

...
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(5);

  valx = analogRead(X_pin);           
  valx = map(valx, 0, 1023, 0, 180);     
  myservox.write(valx);
  delay(5);

  valy = analogRead(Y_pin);           
  valy = map(valy, 0, 1023, 0, 180);     
  myservoy.write(valy);
  delay(15);
...

which is a great test for local control but have not been able to replicate the results over ethernet.

Thanks in advance for your help and consideration.

Dexter101

Have been attempting to use the UDP protocall, seems a much more efficient way to deliver data.  Has anyone else moved down this road, any tips or tricks when using UDP?

Juraj

UDP sends messages. TCP creates a channel.
 you can use TCP without HTTP. see the ChatServer example
for UDP see the UDP example

With TCP on first Arduino you start an EthernetServer on 2323. On second Arduino you create a EthernetClient, connect it to IP address of the first Arduino and port 2323. The EthernetServer then returns an EthenetClient object and the EtherntClients are then connected and everything you print to one EthernetClient you read on the other EthernetClient.



pert

Start with a simple "hello world" sketch that simply sends some arbitrary data from one Arduino to another over Ethernet. Once you have confirmed that you can get communication, you can then add in the analog read and PWM parts of your project. Right now, they are just unnecessarily complicating matters.

Dexter101

Thank you both for the input, I have managed to get two sketches working.  One sends the test msg using UDP and the second receives the msg on the other end. 

***Write Arduino****

#include <Ethernet.h>
#include <EthernetUdp.h>

// MAC address and IP addresses.
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
IPAddress remoteIP(192, 168, 1, 200);   // Receiver IP address
unsigned int localPort = 8888;          // local port to listen on UNUSED
unsigned int remotePort = 8888;         // Remote port to transmit on

// buffer for sending data
char SendBuffer[] = "Test Hello";        // a string to send

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
 
  // start the Ethernet
  Ethernet.begin(mac, ip);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // start UDP
  Udp.begin(localPort);
}

void loop() {

    //Serial.println(remoteIP);
    //Serial.println(remotePort);

    // send data to the remote IP address and port
    Udp.beginPacket(remoteIP, remotePort);
    Udp.write(SendBuffer);
    Udp.endPacket();
    Serial.println(SendBuffer);

  delay(1000);
}

***Receiver Arduino****

#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xDE
};
IPAddress ip(192, 168, 1, 200);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";        // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {

  // start the Ethernet
  Ethernet.begin(mac, ip);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start UDP
  Udp.begin(localPort);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
   
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.print("Contents:");
    Serial.println(packetBuffer);
  }
  delay(10);
}

********************

So the "write" Arduino sends a UDP packet to "Receiver" Arduino which works perfectly.

The new task is to find a data type compatible with the analogreads of the A0-A5 on the "Write" Arduino and fit that into an appropriate format that can be sent via the UDP packet. 

I first tried an array, which on the "Write" Arduino is easy:

***
  valx = analogRead(X_pin);         
  valx = map(valx, 0, 1023, 0, 180);     
  PWMArray[0]=valx;

  valy = analogRead(Y_pin);           
  valy = map(valy, 0, 1023, 0, 180);   
  PWMArray[1]=valy;

  psw = digitalRead(SW_pin);           
  PWMArray[2]=psw;

When printed to serial gives the desired values.
***

How to get this array sent via UDP or am I just not seeing how to do that?

Go Up