I have a ENC28J60 ethernet shield on a Nano and trying to get it to work – as in receive data – with UDP packet input. The sketch below works when I use a Mega 2560 with its w5100 shield with changes only for the different mac and IP addresses and the different Ethernet libraries each uses. Otherwise the setup and inputs are identical.
I actually have a string of LEDs on pin 7 of the Arduino in both cases but the failure to communicate is obvious from the serial monitor where the Mega prints out a sample of the data whereas the Nano/ENC28J60 does not. That and the GND is the extent of the circuit. I have swapped all the hardware and addresses around to no affect and I also tried the web server example sketch that is included in the library. Both devices worked OK with that.
I have used the package processing part of the sketch below in several Mega and Uno based controllers but I have quite a number of them to make and cannot afford that form of unit.
Can anyone suggest how I can get this working with UDP?
#include <SPI.h>
//#include <EthernetENC.h> // for ENC28J60 on Nano
#include <Ethernet.h> //for Mega2560 with W5100 shield <---------------
#include <EthernetUdp.h>
#include "FastLED.h"
#define DMX_SUBNET 0
#define DMX_UNIVERSE 4
#define DATA_PIN 7
#define ETHERNET_BUFFER 540
#define CHANNEL_COUNT 300
#define NUM_LEDS 100
#define UNIVERSE_COUNT 2
#define LEDS_PER_UNIVERSE 50
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC }; // DD for Nano; EC for Mega <----
IPAddress ip(192,168,1,206); // 207 for Nano; 206 for Mega <-----
EthernetUDP Udp;
CRGB leds[NUM_LEDS];
unsigned char packetBuffer[ETHERNET_BUFFER];
void setup() {
FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
Ethernet.begin(mac,ip);
Udp.begin(5568);
//ledTest();
}
void loop() {
int packetSize = Udp.parsePacket();
if(packetSize){
Udp.read(packetBuffer,ETHERNET_BUFFER);
int count = checkACNHeaders(packetBuffer, packetSize);
if (count) {
sacnDMXReceived(packetBuffer, count);
}
}
}
void sacnDMXReceived(unsigned char* pbuff, int count) {
if (count > CHANNEL_COUNT) count = CHANNEL_COUNT;
byte b = pbuff[113]; //DMX Subnet
if ( b == DMX_SUBNET) {
b = pbuff[114]; //DMX Universe
if ( b >= DMX_UNIVERSE && b <= DMX_UNIVERSE + UNIVERSE_COUNT ) {
if ( pbuff[125] == 0 ) { //start code must be 0
int ledNumber = (b - DMX_UNIVERSE) * LEDS_PER_UNIVERSE;
for (int i = 126;i < 126+count;i = i + 3){
byte charValueR = pbuff[i];
byte charValueG = pbuff[i+1];
byte charValueB = pbuff[i+2];
leds[ledNumber].setRGB(charValueR,charValueG,charValueB);
ledNumber++;
}
FastLED.show();
}
}
}
}
int checkACNHeaders(unsigned char* messagein, int messagelength) {
if ( messagein[1] == 0x10 && messagein[4] == 0x41 && messagein[12] == 0x37) {
int addresscount = (byte) messagein[123] * 256 + (byte) messagein[124];
return addresscount -1;
}
return 0;
}
Thanks, I'll have a go. I assume I can set the two TCP settings to zero since this device is receiving UDP packets only. How do I edit the file. Is that done from the IDE somehow?
My assumption was wrong. I tried changing the value of UIP_SOCKET_NUMPACKETS from 3 to 0 and UIP_CONF_MAX_CONNECTIONS from 4 to 0 but it wouldn't compile (same sketch as before). Changed then back and forth a couple of times to confirm my edit was responsible for the error below. That takes me out of my depth.
In file included from \Arduino\sketches\libraries\EthernetENC\src/Ethernet.h:32:0,
from \Arduino\sketches\libraries\EthernetENC\src/EthernetENC.h:1,
from \Arduino\2024_Lights\sketches\ENC28J60_tester\ENC28J60_tester.ino:2:
\Arduino\sketches\libraries\EthernetENC\src/EthernetClient.h:48:57: error: too many initializers for 'memhandle [0] {aka unsigned char [0]}'
memhandle packets_in[UIP_SOCKET_NUMPACKETS] = {NOBLOCK};
^
\Arduino\sketches\libraries\EthernetENC\src/EthernetClient.h:49:58: error: too many initializers for 'memhandle [0] {aka unsigned char [0]}'
memhandle packets_out[UIP_SOCKET_NUMPACKETS] = {NOBLOCK};
^
I installed the MemoryFree Library and ran the example sketch. Not sure what to do with it then so I added to my sketch, subject of this post, and ran freeMemory() in Setup as in the example. It prints out
Free RAM =
2294
The upload print out says:
Sketch uses 18778 bytes (61%) of program storage space. Maximum is 30720 bytes.
Global variables use 1648 bytes (80%) of dynamic memory, leaving 400 bytes for local variables. Maximum is 2048 bytes.
Another week of struggling to get ENC28J60 on a Nano to work has passed with no success. I went back to basics and tried the examples with the Ethernet.h library but including EthernetENC.h instead. Only the Webserver example worked. The others will not compile because of multiple redefinition errors. Dozens of lines like this:
In file included from c:\Users\Donald\OneDrive\mycode\Arduino\sketches\libraries\E131/E131.h:36:0,
from C:\Users\Donald\AppData\Local\Temp.arduinoIDE-unsaved2024430-4092-fv9noi.ozhy\Ethernet_Test\Ethernet_Test.ino:22:
C:\Users\Donald\AppData\Local\Arduino15\libraries\Ethernet\src/Ethernet.h:62:6: error: multiple definition of 'enum EthernetHardwareStatus'
enum EthernetHardwareStatus {
^~~~~~~~~~~~~~~~~~~~~~
The error says that Ethernet.h library is duplicated but also that the version used and the version not used have exactly the same path. I tried uninstalling the EthernetENC library and searching for Ethernet.h. I have no other version installed so I guess the other version is bundled into the IDE. I thought the library could deal with multiple includes and it should not be a problem. Can anyone give me a steer on how to deal with this?
I am using Version 2.32 of the IDE. I have removed E131 library referenced in above error message from IDE and PC and got an even longer error message - last 10% copied below with sketch code. Not to waste your time, I am only looking at this sketch more in hope than expectation that I can find whatever is blocking the ethernet card from working - grasping at straws. What I really want is to get the E1.31 controller sketch in same post working so if today's problem is not relevant pls just drop it. I have got the E1.31 sketch working with a W5100 based card on a Mega2560 but it is only good for 1 maybe 2 universes before I get memory problems. I have a prop which has 500 LEDs so a single ethernet card will not hack it. If I can get ENC28J60 to work that offers an affordable route to multiplying the controllers.
The sketch I am working with today is straight from the library examples:
/*
DHCP-based IP printer
This sketch uses the DHCP extensions to the Ethernet library
to get an IP address via DHCP and print the address obtained.
using an Arduino WIZnet Ethernet shield.
Circuit:
Ethernet shield attached to pins 10, 11, 12, 13
created 12 April 2011
modified 9 Apr 2012
by Tom Igoe
modified 02 Sept 2015
by Arturo Guadalupi
*/
#include <SPI.h>
#include <EthernetENC.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
void setup() {
// 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
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
} else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// no point in carrying on, so do nothing forevermore:
while (true) {
delay(1);
}
}
// print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
switch (Ethernet.maintain()) {
case 1:
//renewed fail
Serial.println("Error: renewed fail");
break;
case 2:
//renewed success
Serial.println("Renewed success");
//print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
break;
case 3:
//rebind fail
Serial.println("Error: rebind fail");
break;
case 4:
//rebind success
Serial.println("Rebind success");
//print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
break;
default:
//nothing happened
break;
}
}
Extract from error message:
(.text+0x0): multiple definition of `EthernetClient::availableForWrite()'
C:\Users\Donald\AppData\Local\Temp\arduino\sketches\1D2EE7A5466D8D15383B482E55764F5B\libraries\Ethernet\EthernetClient.cpp.o (symbol from plugin):(.text+0x0): first defined here
EthernetClient.cpp.o (symbol from plugin): In function `EthernetClient::connected()':
(.text+0x0): multiple definition of `EthernetClient::read(unsigned char*, unsigned int)'
C:\Users\Donald\AppData\Local\Temp\arduino\sketches\1D2EE7A5466D8D15383B482E55764F5B\libraries\Ethernet\EthernetClient.cpp.o (symbol from plugin):(.text+0x0): first defined here
EthernetClient.cpp.o (symbol from plugin): In function `EthernetClient::connected()':
(.text+0x0): multiple definition of `EthernetClient::stop()'
C:\Users\Donald\AppData\Local\Temp\arduino\sketches\1D2EE7A5466D8D15383B482E55764F5B\libraries\Ethernet\EthernetClient.cpp.o (symbol from plugin):(.text+0x0): first defined here
c:\users\donald\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\stdint.h:130:22: note: type 'uint32_t' should match type 'long int'
typedef unsigned int uint32_t __attribute__ ((__mode__ (__SI__)));
^
C:\Users\Donald\AppData\Local\Arduino15\libraries\Ethernet\src\Ethernet.h:152:7: note: type 'struct EthernetUDP' itself violates the C++ One Definition Rule
class EthernetUDP : public UDP {
^
c:\Users\Donald\OneDrive\mycode\Arduino\sketches\libraries\EthernetENC\src\EthernetUdp.h:50:7: note: the incompatible type is defined here
class EthernetUDP : public UDP
^
collect2.exe: error: ld returned 1 exit status
Multiple libraries were found for "Ethernet.h"
Used: C:\Users\Donald\OneDrive\mycode\Arduino\sketches\libraries\EthernetENC
Not used: C:\Users\Donald\OneDrive\mycode\Arduino\sketches\libraries\EthernetENC
exit status 1
Compilation error: exit status 1
OK thanks. I will google the One Drive Problem. I finally had some success getting ENC28J60 to do something. For my own record, I think problem was Vixen continuously sends packets when the app is open. The RJ45 led is solid on. V has to be offline when I want to use network for controllers. I will put one or other on separate network. I may have used a pin allocated to ethernet card. I tried to manage more than 2 universes and card is not capable - nor is Nano really- now I will use more than 1 controller per prop. Useful redundancy. Most likely is Vixen only offered Multicast output and I wonder if that is incomptible. By exporting sequence to RaspPi with FPP and transmitting unicast I am getting an output.
UIP_CONF_MAX_CONNECTIONS is a maximum number of connected EthernetClient connections. The default value s 4. You can set it to 0 if you don't use EthernetClient TCp connections. It will save a lot of dynamic memory.