Help with Ethernet and Wifi code

Description of the Situation:

Hello, I'm doing a car with Arduino, which is controlled by a Analog Thumbstick attached to another Arduino. I already have the code to read the analog values and send a character to the car, because previously it used to be by Bluetooth. Now, we are asked to do it by Wifi and Ethernet. The Thumbsticks will be attached to the Arduino with the Ethernet Shield and this one, connected directly to a Router. This Router creates a Wifi network. The car is over an Arduino Uno with a Wifi Wizfi210 Shield. I already made the Wifi shield connect to the network created by the Router, so the 2 Arduinos are in the same network and always with a unique IP setted by me on the Router's config.

Problem:

What I want is the code for each Arduino to communicate between them. On the Ethernet, just send a Char to the Wifi... And in the Wifi, read the Ethernet's Char. Is only that, but I have almost no knowledge about creating servers or clients.

Thanks.

What I want is the code for each Arduino to communicate between them.

The first thing that you need to do is to define what role each Arduino should implement. Should the car be a client (making requests) or should it be a server (responding to requests)? Should the joystick device be a server (responding to requests) or should it be a client (making requests)?

Seems to me that the car should be a server (Hey, car, can you turn left?) and that the joystick device should be a client (Hey, you idiot, I said go left!).

Once you know what role each device will play, making requests and responding to requests is (nearly) trivial.

PaulS:
Seems to me that the car should be a server (Hey, car, can you turn left?) and that the joystick device should be a client (Hey, you idiot, I said go left!).

Mmm yes, I suppose. So the car would be a WifiServer and the Joystick an EthernetClient.

Btw, I already added the Ethernet's MacAddress to the Router's Adress reservation and setted an IP, but when I print the LocalIp from the Ethernet's device on the Serial Monitor, it prints a different one. Any idea?

Maybe these things I'm saying are too basic for you, but I'm new in networking :stuck_out_tongue:
Thanks :slight_smile:

Javi97:
Btw, I already added the Ethernet's MacAddress to the Router's Adress reservation and setted an IP, but when I print the LocalIp from the Ethernet's device on the Serial Monitor, it prints a different one. Any idea?

I finally made the Arduinos get a DHCP IP for every time thet connect to the router. Now, I need the code to communicate them between UDP. As it's an only char to send and receive, I think it's the more appropiated way to do it. So, it would be the Joystick sends an UDPpacket via Ethernet and the Car reads it via Wifi. They are different Libraries and I only want that, just send from the Ethernet and read it from the Car, not reply anything... But I don't know how :stuck_out_tongue: I would be grateful if someone could give me an example code of Ethernet's and Wifi's. Thanks.

This would be my Joystick's code:

#include <SPI.h>        
#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[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
  byte remoteIP[]={192, 168, 1, 101};
  unsigned int remotePort=8888;
  char rxChar;
  int val, val2;
  const int potpin=0;
  const int potpin2=1;


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

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

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac);
  Udp.begin(localPort);

}

void loop() {

  val = analogRead(potpin);
  val2= analogRead(potpin2);
  Serial.println("val1=");
  Serial.println(val);
  Serial.println("val2=");
  Serial.println(val2);
  if(val>650)
{ 
    if(val2>650)
    {
      rxChar='I';
      Serial.println('I');
    }
    else if(val2<350)
    {
      rxChar='D';
      Serial.println('D');
    }
    else
    {
      rxChar='F';
      Serial.println('F');
    }
}
  else if (val<350)
{
    if(val2>650)
    {
      rxChar='J';
      Serial.println('J');
    }
    else if(val2<350)
    {
      rxChar='K';
      Serial.println('K');
    }
    else
    {
      rxChar='B';
      Serial.println('B');
    }
}
  else if(val2>650)
{
    if(val>650)
    {
      rxChar='I';
      Serial.println('I');
    }
    else if(val<350)
    {
      rxChar='J';
      Serial.println('J');
    }
    else
    {
      rxChar='L';
      Serial.println('L');
    }
}
  else if (val2<350)
{
    if(val>650)
    {
      rxChar='D';
      Serial.println('D');
    }
    else if(val<350)
    {
      rxChar='K';
      Serial.println('K');
    }
    else
    {
      rxChar='R';
      Serial.println('R');
    }
}
  else
  {
    rxChar='S';
    Serial.println('S');
  }
  
  Udp.beginPacket(remoteIP,remotePort);
    Udp.write(rxChar);
    Udp.endPacket();

}

And this my WiFi's code:

#include <SPI.h>        
#include <WiFiUdp.h>
#include<WiFi.h>

  byte remoteIP[]={192, 168, 1, 102};
  unsigned int remotePort=8888;
  char rxChar;
const int motorPin = 2;
const int motorPin2 = 3;
const int motorPin3 = 4;
const int motorPin4 = 5;
const int ledrojo = 1;
const int ledsadelante= 12;
const int ledsatras= 13;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
unsigned int localPort = 8888;      // local port to listen on
WiFiUDP Udp;

void setup() {
  pinMode(motorPin, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(ledrojo, OUTPUT);
  pinMode(ledsadelante, OUTPUT);
  pinMode(ledsatras, OUTPUT);
  
  char ssid[] = "pp123";      // your network SSID (name)
  char pass[] = "asdf1234";   // your network password
  int keyIndex = 0;                 // your network key Index number (needed only for WEP)
  int status = WL_IDLE_STATUS;
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  Udp.begin(localPort);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }
}

void loop() {

  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    rxChar=Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  }
  switch (rxChar)
    {
      case 'F':
      digitalWrite(motorPin,HIGH);
      digitalWrite(ledrojo, LOW);
      digitalWrite(ledsadelante, HIGH);
      digitalWrite(ledsatras, LOW);
      break;
      case 'L':
      digitalWrite(motorPin3,HIGH);
      digitalWrite(ledrojo, LOW);
      digitalWrite(ledsadelante, LOW);
      digitalWrite(ledsatras, LOW);
      break;
      case 'R':
      digitalWrite(motorPin4,HIGH);
      digitalWrite(ledrojo, LOW);
      digitalWrite(ledsadelante, LOW);
      digitalWrite(ledsatras, LOW);
      break;
      case 'B':
      digitalWrite(motorPin2,HIGH);
      digitalWrite(ledrojo, LOW);
      digitalWrite(ledsadelante, LOW);
      digitalWrite(ledsatras, HIGH);
      break;
      case 'I':
      digitalWrite(motorPin,HIGH);
      digitalWrite(motorPin3,HIGH);
      digitalWrite(ledrojo, LOW);
      digitalWrite(ledsadelante, HIGH);
      digitalWrite(ledsatras, LOW);
      break;
      case 'D':
      digitalWrite(motorPin,HIGH);
      digitalWrite(motorPin4,HIGH);
      digitalWrite(ledrojo, LOW);
      digitalWrite(ledsadelante, HIGH);
      digitalWrite(ledsatras, LOW);
      break;
      case 'J':
      digitalWrite(motorPin2,HIGH);
      digitalWrite(motorPin3,HIGH);
      digitalWrite(ledrojo, LOW);
      digitalWrite(ledsadelante, LOW);
      digitalWrite(ledsatras, HIGH);
      break;
      case 'K':
      digitalWrite(motorPin2,HIGH);
      digitalWrite(motorPin4,HIGH);
      digitalWrite(ledrojo, LOW);
      digitalWrite(ledsadelante, LOW);
      digitalWrite(ledsatras, HIGH);
      break;
      default:
      digitalWrite(motorPin,LOW);
      digitalWrite(motorPin2,LOW);
      digitalWrite(motorPin3,LOW);
      digitalWrite(motorPin4,LOW);
      digitalWrite(ledrojo,HIGH);
      digitalWrite(ledsadelante, LOW);
      digitalWrite(ledsatras, LOW);
      break;
    } 
    
}

It obviously doesn't work, but is how I Imagined :stuck_out_tongue: I think the Ethernet's one is fine, but i have my doubts about the Wifi one... Please help!
Thanks