Hi Everybody,
This is my first post here. I spent some hours looking for a solution to this and was unable to find a configuration where there were 3 (more than one) listening servers and 1 client.
The client sends out information to one of the 3 servers depending on where in a machine it is. The client gets its information over I2C and then converts it to a LAN and then after it is received, it converts back to I2C. I was really struggling with long I2C lines and all that is bad about that.
This solution works but in the morning when the machine is started, one of the servers fails to connect to the switch. If I open the cover and hit the reset button, it will connect and work. I tried to add in a watchdog to reboot if there is a disconnected cable but after a failed boot, a hard reset is the only thing that works. I have thought about moving to a more robust watchdog but I really want to understand the root cause.
I have tried assigning a different port to each server but that was no better.
Here is a giff of the connection led (on the right sheild):
And my Server Code:
/*
DHCP Chat Server WILL RECEIVE INFO FROM CLIENT
A simple server that distributes any incoming messages to all
connected clients. To use, telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 21 May 2011
modified 9 Apr 2012
by Tom Igoe
modified 02 Sept 2015
by Arturo Guadalupi
Based on ChatServer example by David A. Mellis
*/
#include <avr/wdt.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 187);
//IPAddress myDns(192, 168, 1, 1);
//IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23
EthernetServer server(23); //port 23 is default for telnet services
boolean gotAMessage = false; // whether or not you got a message from the client yet
int StationNumber = 0;
int UOSgoSteps = 0;
int UGSgoSteps = 0;
int LOSgoSteps = 0;
int LGSgoSteps = 0;
byte byteRcvd0 = 0;
byte byteRcvd1 = 0;
byte byteRcvd2 = 0;
byte byteRcvd3 = 0;
byte byteRcvd4 = 0;
void reboot() {
wdt_disable();
wdt_enable(WDTO_15MS);
while (1) {}
}
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(4, OUTPUT);
digitalWrite(4,HIGH); //Turn off SD Card
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
// start the Ethernet connection:
Ethernet.begin(mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
// print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
Serial.println("Waiting for Print Cylinder information...");
// start listening for clients
server.begin();
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
Serial.println("Before Reboot");// reset the Arduino
reboot();
Serial.println("After Reboot");
}
// read the incoming from the client:
if (client) {
byteRcvd0 = client.read();
Serial.print("byteRcvd0 : ");Serial.println(byteRcvd0);
byteRcvd1 = client.read();
Serial.print("byteRcvd1 : ");Serial.println(byteRcvd1);//UOSgoSteps
byteRcvd2 = client.read();
Serial.print("byteRcvd2 : ");Serial.println(byteRcvd2);
byteRcvd3 = client.read();
Serial.print("byteRcvd3 : ");Serial.println(byteRcvd3);//UOSgoSteps
byteRcvd4 = client.read();
Serial.print("byteRcvd4 : ");Serial.println(byteRcvd4);//UOSgoSteps
StationNumber = byteRcvd4;
UOSgoSteps = ((unsigned char)byteRcvd1 << 8 | (unsigned char)byteRcvd0); // Combine the two 8 bit numbers to make a 16 bit one.
UGSgoSteps = ((unsigned char)byteRcvd1 << 8 | (unsigned char)byteRcvd0); // Combine the two 8 bit numbers to make a 16 bit one.
LGSgoSteps = ((unsigned char)byteRcvd3 << 8 | (unsigned char)byteRcvd2); // Combine the two 8 bit numbers to make a 16 bit one.
LOSgoSteps = ((unsigned char)byteRcvd3 << 8 | (unsigned char)byteRcvd2); // Combine the two 8 bit numbers to make a 16 bit one.
// UOSgoSteps = 7500; //debug
// UGSgoSteps = 7500; //debug
//LGSgoSteps = 1300; //debug - Need to make a formula to calculate this
//LOSgoSteps = 1300; //debug - Need to make a formula to calculate this
Serial.print("Station Number : ");Serial.println(StationNumber);
Serial.print("Upper Steps : ");Serial.println(UOSgoSteps);
Serial.print("Lower Steps : ");Serial.println(LOSgoSteps);
Serial.println("=====================================");
Wire.beginTransmission(StationNumber);
const unsigned char Stepval1 = UOSgoSteps & 0xFF;
const unsigned char Stepval2 = (UOSgoSteps >> 8) & 0xFF;
const unsigned char Stepval3 = LOSgoSteps & 0xFF;
const unsigned char Stepval4 = (LOSgoSteps >> 8) & 0xFF;
Wire.write(Stepval1);
Wire.write(Stepval2);
Wire.write(Stepval3);
Wire.write(Stepval4);
Wire.endTransmission();
int StationNumber = 0;
int UOSgoSteps = 0;
int UGSgoSteps = 0;
int LOSgoSteps = 0;
int LGSgoSteps = 0;
byte byteRcvd0 = 0;
byte byteRcvd1 = 0;
byte byteRcvd2 = 0;
byte byteRcvd3 = 0;
byte byteRcvd4 = 0;
}
}
Thanks for any help you may have.
Gary