Switch statement from one Arduino board to another. One Client and one Server

I have two arduino boards, one with Wifi and one with Ethernet shield. I need some advice on getting the Wifi Arduino board to send the Switch statement to the Ethernet board that will be a web server. I have made some Wifi code, but I do not know what the code is to send the switch statement to the Webserver.

It Is not a big task for those who know the code, but for me It is. I want to press the switch button, and the server will output “Button pressed” or something, to indicate that the statement go from the Arduino board with Wifi to the Arduino board with ethernet and will work as a Web server. Please help.

Here is the code for the Client.

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

char ssid[] = "Network";      // your network SSID (name) 
char pass[] = "Password";   // your network password
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 0, 0, 20); // IP address, may need to change depending on network

WiFiServer server(80);  // create a server at port 80
int status = WL_IDLE_STATUS;

void setup() {
  //Initialize serial and wait for port to open:
  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:
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to 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:
  // you're connected now, so print out the status:

  pinMode(3, INPUT);  // input pin for switch

void loop()
    WiFiClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connnection: close");
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<title>Arduino Read Switch State</title>");
                    client.println("<meta http-equiv=\"refresh\" content=\"1\">");
                    client.println("<p>State of switch is:</p>");
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)

void GetSwitchState(WiFiClient cl)
    if (digitalRead(3)) {
    else {
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(" dBm");

Have you considered UDP instead of TCP? It is faster and if you design the software correctly, just as reliable. I find it easier to send command statements also.

Have the WiFi "client" send a packet to the "server" and listen for a response. Have the ethernet "server" listen for packets, and when it receives one, send a response packet back to the client.

Yeah, but I will use TCP because I will use it through the internet too later. Exactly that I want, but I need some help with the code to do that! :) Would be great If somebody can write some code for me so I understand :)

UDP will work through a router from a localnet to the internet. Don't you use NTP on your localnet computers? That uses UDP.

I guess. But would you help me with the code for the server/client? Would be great to have some guidance from someone who knows how to code this. :)

I recommend using the UDP examples to get started. That is what I did. I used the UdpNtpClient (for the "client") and UDPSendReceiveString (for the "server") examples in the IDE. Cut them down to the basic send and receive stuff to test them. Once they are communicating, it should be obvious how to send commands. If not, you may need to do some more study on UDP.

edit: How this works on a localnet "client" to an internet "server" is a function in the router called connection tracking. When a UDP "client" sends a packet to a "server", the router will track that "connection" for a few seconds. If the "server" sends a return packet within a few seconds, the router will take it. After that, it will not.

I really would like to use TCP. Can you please help me on the server side, so I can send a simple switch statement to the server?

zoomkat has better code than I have. Search for "server zoomkat" above. He might show up and give you a hand.