Wiznet makers

jaden

Published September 30, 2022 ©

76 UCC

18 WCC

49 VAR

0 Contests

0 Followers

0 Following

Original Link

Communication between two Arduino

Communication between two Arduino via Ethernet

COMPONENTS Hardware components

Arduino - Arduino UNO

x 1


Arduino - Arduino Ethernet Shield 2

x 1


PROJECT DESCRIPTION
two arduino led switch button

author defined simple protocol:

TCP connection is created betwwen Arduino #1 and Arduino #2

this is Arduino #1 code

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/communication-between-two-arduino
*/

// ARDUINO #1: TCP CLIENT + A BUTTON/SWITCH
#include <ezButton.h>
#include <SPI.h>
#include <Ethernet.h>

const int BUTTON_PIN = 7;
const int serverPort = 4080;
ezButton button(BUTTON_PIN);  // create ezButton that attach to pin 7;

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03};
IPAddress serverAddress(192, 168, 0, 180);
EthernetClient TCPclient;

void setup() {
 Serial.begin(9600);
 button.setDebounceTime(50); // set debounce time to 50 milliseconds

 Serial.println("ARDUINO #1: TCP CLIENT + A BUTTON/SWITCH");

 // Initialize Ethernet Shield:
 if (Ethernet.begin(mac) == 0)
   Serial.println("Failed to configure Ethernet using DHCP");

 // connect to TCP server (Arduino #2)
 if (TCPclient.connect(serverAddress, serverPort))
   Serial.println("Connected to TCP server");
 else
   Serial.println("Failed to connect to TCP server");
}

void loop() {
 button.loop(); // MUST call the loop() function first

 if (!TCPclient.connected()) {
   Serial.println("Connection is disconnected");
   TCPclient.stop();

   // reconnect to TCP server (Arduino #2)
   if (TCPclient.connect(serverAddress, serverPort))
     Serial.println("Reconnected to TCP server");
   else
     Serial.println("Failed to reconnect to TCP server");
 }

 if (button.isPressed()) {
   TCPclient.write('1');
   TCPclient.flush();
   Serial.println("- The button is pressed,  sent command: 1");
 }

 if (button.isReleased()) {
   TCPclient.write('0');
   TCPclient.flush();
   Serial.println("- The button is released, sent command: 0");
 }
}
 

 

and this is Arduino #2 code

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/communication-between-two-arduino
*/

// ARDUINO #2: TCP SERVER + AN LED
#include <SPI.h>
#include <Ethernet.h>

const int LED_PIN = 7;
const int serverPort = 4080;

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
EthernetServer TCPserver(serverPort);

void setup() {
 Serial.begin(9600);
 pinMode(LED_PIN, OUTPUT);

 Serial.println("ARDUINO #2: TCP SERVER + AN LED");

 // Initialize Ethernet Shield:
 if (Ethernet.begin(mac) == 0)
   Serial.println("Failed to configure Ethernet using DHCP");

 // Print your local IP address:
 Serial.print("TCP Server IP address: ");
 Serial.println(Ethernet.localIP());
 Serial.println("-> Please update the serverAddress in Arduino #1 code");

 // Listening for a TCP client (from Arduino #1)
 TCPserver.begin();
}

void loop() {
 // Wait for a TCP client from Arduino #1:
 EthernetClient client = TCPserver.available();

 if (client) {
   // Read the command from the TCP client:
   char command = client.read();
   Serial.print("- Received command: ");
   Serial.println(command);

   if (command == '1')
     digitalWrite(LED_PIN, HIGH); // Turn LED on
   else if (command == '0')
     digitalWrite(LED_PIN, LOW);  // Turn LED off

   Ethernet.maintain();
 }
}
 

 

Documents
Comments Write