Hardware components | ||||||
| × | 2 | ||||
| × | 2 | ||||
| × | 2 | ||||
| × | 1 | ||||
| × | 4 | ||||
Software apps and online services | ||||||
| ||||||
Hand tools and fabrication machines | ||||||
| ||||||
| ||||||
|
This project came from the necessity of monitoring a diesel generator, installed in a remote site, from a data center.
This is a rather unique project, because people are using the smart phone or the computer to talk to Arduino, but I don’t need another screen, that’s why I wanted to make two Arduino talk to each other over the local area network.
So what this project does, it takes the input pins of an Arduino (named Server), and replicate their status to another Arduino (the Client), activating the same pins as output. The transmission media is the local area network and they work independently, without the need for a computer, though you can monitor the web-page generated by the Server, by typing in its IP address. Also, the page has to be refreshed manually or with the help of a browser plug-in.
I used the Arduino Nano and the ENC28J60 Ethernet Shield, making the whole assembly very compact. The code for both the server and the client is very simple and self-explanatory. You just have to change the IP address according to your network.
Right now I only transfer 6 dry contacts in the digital status of A0 to A5 pins, but this can be extended with D3 to D9. The rest of the pins are taken. With more work, it is possible to transfer also analog values to PWM outputs, but not in this project
/* This code outputs a web-page with pin status in the form of 0 or 1, like: 00100010
* It acts as a web server.
* I chose to monitor Analog pins A0-A5 because most of the digital pins are taken over by the Ethernet shield
* =========================================================
* Viorel Racoviteannu /
* https://www.youtube.com/@Racov
* https://racov.ro
* YO3RAK@gmail.com
* =========================================================
* I cannot take any responsibility for missuse of this code or any kind of damage it may occur from using this code.
* =========================================================
---Version history---
* Jul. 2023 - first try :)
*/
// UIPEthernet library is used for Arduino Nano Ethernet Shiel
// It is not needed if you are using Arduino UNO/Duemilanove/Mega/etc.
#include <UIPEthernet.h> // Used for Ethernet
// **** ETHERNET SETTING ****
// byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xEE };
byte mac[] = {0x01,0x02,0x03,0x04,0x05,0x06};
IPAddress ip(192, 168, 1, 115);
EthernetServer server(80);
char Pin[9]=""; // string to keep the pin status
char WebWrite[40]=""; // string to be displayed on the web
int val;
void setup() {
// pin declaration
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
Serial.begin(9600);
// display splash screen:
Serial.println("Viorel Racoviteannu / Jul. 2023");
Serial.println("https://www.youtube.com/@Racov");
Serial.println("https://racov.ro");
Serial.println("YO3RAK@gmail.com");
Serial.println("This code creates a web-page with pin status in the form of 0 or 1, like: 00100010");
Serial.println("I chose to monitor Analog pins A0-A5 because most of the digital pins are taken over by the Ethernet shield \n\r");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
delay(1000);
server.begin();
Serial.print("local IP: ");
Serial.println(Ethernet.localIP());
Serial.println(" \n\r");
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client)
{
Serial.print("New Connection. ");
Serial.print("Sending-> ");
Serial.print(Pin);
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected())
{
// read the status of the pins and create the string
val = digitalRead(A0);
if(val == 0 ) { Pin[0] = '0'; }
else { Pin[0] = '1'; }
val = digitalRead(A1);
if(val == 0 ) { Pin[1] = '0'; }
else { Pin[1] = '1'; }
val = digitalRead(A2);
if(val == 0 ) { Pin[2] = '0'; }
else { Pin[2] = '1'; }
val = digitalRead(A3);
if(val == 0 ) { Pin[3] = '0'; }
else { Pin[3] = '1'; }
val = digitalRead(A4);
if(val == 0 ) { Pin[4] = '0'; }
else { Pin[4] = '1'; }
val = digitalRead(A5);
if(val == 0 ) { Pin[5] = '0'; }
else { Pin[5] = '1'; }
// creating the line with pin values
strcpy (WebWrite ,"<body><h3>");
strcat (WebWrite , Pin);
strcat (WebWrite , "</h3></body>");
if (client.available())
{
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank)
{
client.print("<html><title>Web Control</title>");
client.println(WebWrite);
client.println("<body><h3>A0=>A5</h3></body>");
client.println("<body><h3>Viorel Racoviteanu 2023</h3></body>");
client.println("<body><h3>https://racov.ro || https://www.youtube.com/@Racov</h3></body>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r')
{
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(100);
// close the connection:
client.stop();
Serial.println(" <- Disconnected");
}
}
/* This code reads a web-page with pin status in the form of 0 or 1, like: 00100010
* It acts as a web client and activates its coresponding pins. See the server code...
* I chose to monitor Analog pins A0-A5 because most of the digital pins are taken over by the Ethernet shield
* =========================================================
* Viorel Racoviteannu /
* https://www.youtube.com/@Racov
* https://racov.ro
* YO3RAK@gmail.com
* =========================================================
* I cannot take any responsibility for missuse of this code or any kind of damage it may occur from using this code.
* =========================================================
* ---Version history---
* Jul. 2023 - first try :)
*/
// UIPEthernet library is used for Arduino Nano Ethernet Shiel
// It is not needed if you are using Arduino UNO/Duemilanove/Mega/etc.
#include <UIPEthernet.h>
// **** ETHERNET SETTING ****
byte mac[6] = {0x01,0x02,0x03,0x04,0x05,0x07};
IPAddress ip(192, 168, 1, 116);
EthernetClient client;
signed long next;
void setup() {
// pin declaration
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
Serial.begin(9600);
// display splash screen:
Serial.println("Viorel Racoviteannu / Jul. 2023");
Serial.println("https://www.youtube.com/@Racov");
Serial.println("https://racov.ro");
Serial.println("YO3RAK@gmail.com");
Serial.println("Reads a web-page with pin status in the form of 0 or 1, like: 001001");
Serial.println("It acts as a web client and activates its coresponding pins");
Serial.println("I chose to monitor Analog pins A0-A5 because most of the digital pins are taken over by the Ethernet shield \n\r");
digitalWrite(A0, LOW); // trigger some sort of alarm
digitalWrite(A1, LOW); // for COMM FAIL
// start the Ethernet connection
Ethernet.begin(mac, ip);
delay(1000);
Serial.print("local IP: ");
Serial.println(Ethernet.localIP());
Serial.println("");
next = 0;
}
void loop() {
if (((signed long)(millis() - next)) > 0)
{
next = millis() + 5000;
Serial.print("Connecting to server ->");
if (client.connect(IPAddress(192, 168, 1, 115),80))
{
Serial.print("-> Connected. ");
client.println("GET HTTP/1.1");
client.println("Connection: close");
client.println();
while(client.available()==0)
{
if (next - millis() < 0)
goto close;
}
int size;
while((size = client.available()) > 0)
{
String result = client.readString();
Serial.print("Received-> ");
//Serial.print(result); // print the entire message for debuging
for (int i = 42; i <= 47; i++) { // print only the important bits (A0->A5)
Serial.print(result.charAt(i));
}
// extracting each bit and activating the corresponding pin
char val1; int val2;
val1 = result.charAt(42); // extract the pin value of the (remote) server
val2 = val1 - '0'; // conversion from 'char' to 'int'
digitalWrite(A0, val2); // activate the (local) client respective pin
val1 = result.charAt(43);
val2 = val1 - '0';
digitalWrite(A1, val2);
val1 = result.charAt(44);
val2 = val1 - '0';
digitalWrite(A2, val2);
val1 = result.charAt(45);
val2 = val1 - '0';
digitalWrite(A3, val2);
val1 = result.charAt(46);
val2 = val1 - '0';
digitalWrite(A4, val2);
val1 = result.charAt(47);
val2 = val1 - '0';
digitalWrite(A5, val2);
}
close:
//disconnect client
Serial.println(" <-Disconnected.");
client.stop();
}
else {
Serial.println(" <-Connection failed.");
}
}
}
Comments