ENC28J60 modules conflicting <solved>

I have a PC and 2 Arduino Mega 2560 boards with ethernet modules, all connected to a central router / hub.

There appears to be some conflict in the modules, as I am not getting reliable comms.

I have tried swapping the boards, and also the ethernet modules, cables, etc.

If I disconnect either one of the 2 modules from the hub, the remaining unit PINGs from the PC perfectly. As soon as I connect the other module, the PING becomes erratic, with 'destination host unreachable'

I am using the UIPethernet library.

I assume that the MAC address for each module is set in the code ( or do I have to read the MAC from the attached module ? ) and I have specified separate IP addresses for each board.

Any suggestions please ?

Extract from the code :

char XamppOnPC[] = "192.168.1.100";
IPAddress gateway(192,168,1,100);  // IP of the PC running xAMPp as the server
IPAddress subnet(255,255,255,0); //subnet mask
IPAddress dnsServerIP(192,168,1,100);  // IP of the PC running xAMPp as the server
EthernetServer server(80);
EthernetClient client;

IPAddress myIP1(192,168,1,115);
byte mac1[] = { 0xDE, 0xAD, 0xEE, 0xBB, 0xBB, 0xED };

IPAddress myIP2(192,168,1,116);
byte mac2[] = { 0xDE, 0xAD, 0xEE, 0xFF, 0xAA, 0xED };

if(DeviceType == 1) {    // this is set from a digital read on a pin. No Gnd for unit 1, Gnd for unit 2
    Serial.println("Device Type = Unit 1");
    UIPEthernet.begin(mac1, myIP1, dnsServerIP, gateway, subnet); 
}

if(DeviceType == 2) {
    Serial.println("Device Type = Unit 2");
    UIPEthernet.begin(mac2, myIP2, dnsServerIP, gateway, subnet); 
}

char XXbuffer[200]; /* local buffer into which strings are encoded and from which they're output'd */
sprintf (XXbuffer, "SUGCtxStart=1");
Serial.println ("Sending Data to PC :");
Serial.println (XXbuffer);
if (client.connect(XamppOnPC, 80)) {
    client.print("GET http://192.168.1.100/testCode/RXard.php?");
    client.print(XXbuffer);
    client.println(" HTTP/1.1");
    client.println("Host: http://192.168.1.100");
    client.println( "Connection: close" );
    client.println();
    delay(250);
    client.stop();
    Serial.println ("Ethernet Operation Completed");
}else{
    Serial.println("Not Connected to PC Control Unit");
}

OK. Just found this on google.

I ran a ping on the PC : ping 192.168.1.100 then typed : arp -a

Although the 2 arduino boards are powered off, it shows the 2 IP addresses ( 192.168.1.115 and 192.168.1.116 ) but both with the same MAC address.

Is there a something wrong with the code, or is there a fault in the library ?

I know the conditional 'if' statement in the code is working, because the 2 IP addresses are being shown.

Also, is it possible that the EthernetServer server(80) on both Arduinos could cause a problem ?

Do they each need unique port numbers ?

I assume that the MAC address for each module is set in the code ( or do I have to read the MAC from the attached module ? ) and I have specified separate IP addresses for each board.

Which Arduino is that code for? You can't be running the same code on each one, and expect a specific Mega to know which MAC address to use.

That "extract" just wasted my time. Don't do that again. Post ALL of your code.

Hi PaulS.

Thanks for the reply and apologies for wasting your time - not the intention.

The code is quite long, so I will have to strip down until it contains just the relevant code and still produces the error.

The selection of the "which Arduino" I am trying to do with a Ground connected to input pin 23 on the second board only, so if the digitalread on pin 23 = high, then it is unit 1, and 23 = LOW is unit 2.

Since posting, I have separated the sketches and hard coded the MAC address and IP address different for each.

I am now seeing to different addresses, but still not PINGing reliably.

So I am thinking that maybe I can't have 2 boards running as servers on the same network with the same port 80, or it could be a router firewall issue.

Regards

The code is quite long, so I will have to strip down until it contains just the relevant code and still produces the error.

See that Additional Options... link down below the post/reply entry area? Use the Browse button to find your code, and post it as an attachment.

OK. I have stripped down the code as much as I can, and have a ‘working’ faulty sketch.

At the moment, I only have unit 1 connected to the ethernet, but am still getting erratic results.

I have added the IP and MAC addresses into the router ‘Static ARP’ table, and also added the MAC addresses to the router firewall ‘MAC Filter’ as ‘always on’

Ping from the PC is now giving constant replies, mostly < 2ms

Only when the Mega is receiving data from the PC and replying back to the PC does the ping time increase to around 500ms ( just for 1 ping )

Although the ping is still working, the test that I am running from the PC browser ( first URL commented in the code ) is still erratic. Sometimes it works in about 2 seconds, other times it runs for over a minutes and the browser gives up.

It seems like the sketch is busy doing something and not listening to the incoming data.

Am I missing something simple here ?

a02_basic.ino (8.56 KB)

	digitalWrite(DevicePin, HIGH); // activate the pull-up
	pinMode(DevicePin, INPUT);

Generally, one sets the mode first, and then enables the internal pullup resistor. Your code works because the default mode is input.

				for(int vvS = 1; vvS <= 20; vvS++){
					VVsets[vvS] = strtok (NULL, "&");   // to print the split values :   printf ("%s\n",VVsets[0]);
				}

Breaking out of the loop when strtok() returns a NULL would be a good idea.

				for (int vv1 = 0; vv1 <= 20; vv1++) {
					printf ("Examining VV set : %c\n",VVsets[vv1]);  // echo the variable and value to the Serial Monitor
					char *VVinner[2];
					VVinner[0] = strtok(VVsets[vv1], "=");
					VVinner[1] = strtok (NULL, "=");
					Serial.print("Data Received : -"); Serial.print(VVinner[0]); Serial.print("- --> -"); Serial.print(VVinner[1]); Serial.println("-");

Passing a NULL to strtok() the first time is NOT a good idea. You really should iterate only the number of times that is equal to the number of tokens extracted on the first pass. Keep in mind that the first pass should probably be copying the data pointed to. The address of the token that you are storing remains the same. The pointed to data changes. So, you are making up to 20 copies of the same pointer. I doubt that that is what you want to do.

					String TempST1 = "";
					String TempST2 = "";
					TempST1 = String(VVinner[0]);
					TempST2 = String(VVinner[1]);

Pissing away resources is definitely something you do NOT want to do. There is no reason to make Strings out of strings. NONE!

			char XXbuffer[200]; /* local buffer into which strings are encoded and from which they're output'd */
			sprintf (XXbuffer, "U1txStart=1&U1millis=%ld&U1txEnd=1", millis());

It’s worth taking your shoes off, if you need to, and count the number of characters you really need in the array. 200 is far too many. You don’t have the resources to waste.

				client.print("GET http://192.168.1.100/test/RXard.php?");

You’ve already defined the server to connect to. There is only one protocol supported. This is not the usual way to format a GET request.

Hi PaulS

Thanks for the detailed reply and the length that you have gone to in your reply. I say this in the most sincere way and must add that your input on the forum, together with the other highly experienced members, is so greatly appreciated and valued by those of us still trying to learn what you have already forgotten.

Looking at your reply, the points you mentioned don't seem to have relevance to the ethernet problem, unless you are saying that they would cause a memory problem that would break the sketch ?

Either way, your points are taken, and I will spend some time trying to better understand the code and how to make it more 'friendly'.

Your last point about the GET command : OK, so it may not be the 'usual way' ( and I am still to find the correct way to do this ), but my question is that if this code is working some of the time, it would appear to work as it is in the sketch, what could be causing the sketch to break ?

Finally with some pointers and direction from the guru’s, I have a working solution that appears stable after 2 hours of testing.

My mistake was to assume that, because I was running xampp on my PC as a web server so that I could host php files on my PC and have the Arduinos interact with the PC based data, that the IP address of the xampp web server ( in my case 192.168.1.100 ) should be used as the Gateway and DNS server when setting the UIPEthernet.begin

I changed these to the IP address of the router ( 192.168.1.254 ) and guess what – worked ! I can’t believe I didn’t think of trying that earlier. It was only after an earlier reply from PaulS, that I stripped the code down to the bare minimum, and went so far as to remove the code that he mentioned was wasting space / resources. The problem still existed, but at least I was dealing with a lot less code to try and find the cause in.

Once I had each of the Arduinos communicating with the PC individually, I connected them both to the ethernet hub, and found out something else :

In the code :
I had to change .print to .println at the end before the delay, and
I also had to add the ’ User-Agent’ line, otherwise it just would not transmit.

				client.print("GET /test/RXard.php?");
				client.println(XXbuffer);
				client.println("HTTP/1.1");
				client.println("Host: http://192.168.1.100");
                                client.println("User-Agent: arduino-ethernet");
				client.println("Connection: close");
				client.println();
				delay(250);
				client.stop();

I still have to re-examine the code that extracts the keys and values from the incoming http data ( as PaulS suggested ), but in case anyone else can make use of this, the working code that I have at the moment is in the attachment.

a02_basic.ino (8.75 KB)

Hi PaulS

Thinking about your comment on the size of the char :

PaulS: char XXbuffer[200]; /* local buffer into which strings are encoded and from which they're output'd */ sprintf (XXbuffer, "U1txStart=1&U1millis=%ld&U1txEnd=1", millis());

It's worth taking your shoes off, if you need to, and count the number of characters you really need in the array. 200 is far too many. You don't have the resources to waste.

would it not be better to use a String that will expand as more data is added to it ?

I have seen code online that includes this :

String txData =""; // This is the Data that will be sent to the PHP page. It will have variable names and data in it.

txData = "DataToSend1=" + (String ( '__some integer or long value__' )) + "&DataToSend2=" + (String ( '__some integer or long value__' ));

client.print(txData);    // send the String to the php script

and if so, would it make any difference to the resources after the data is sent to the php script, if the String was reset, say with :

String txData ="";