I want to use my Arduino Mega (with a sensor shield) and an ENC28J60 Ethernet module (directly connected to my PC) to send and receive UDP from a flight simulator (X-Plane 11, which is capable of sending UDP via the network).
The network module: http://www.ebay.de/itm/281353516180?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
I can't manage to write the received UDP data into the serial monitor however. I am not even sure I really do receive UDP.
The connection between Ethernet module and PC seems to be fine, as the green LED of the Ethernet module's I/O is permanently on, and the yellow one is blinking as soon as I start my flight sim and send UDP from there.
I have tried both, standard Ethernet cable and crossover.
I have have tried 2 ways of connecting the ENC28J60 Ethernet module to the Arduino Mega sensor shield, according to 2 different guides.
Standard wiring for the Arduino Uno
- Enc28j60 SO to Arduino pin 12
- Enc28j60 SI to Arduino pin 11
- Enc28j60 SCK to Arduino pin 13
- Enc28j60 CS to Arduino pin 10
- Enc28j60 VCC to Arduino 3V3 pin
- Enc28j60 GND to Arduino Gnd pin
Recommended wiring for the Arduino Mega
https://en.code-bude.net/2013/06/22/how-to-use-enc28j60-ethernet-shield-with-arduino-mega-2560/
- GND to GND
- 3.3 to 3.3V
- SO to Pin50
- SI to Pin51
- SCK to Pin52
- CS to Pin53
I also tried several libs:
EtherCard: It is recommended to set the cs pin to 53 in the library files, which I did. Plus one should use this line of code in the sketch (which did not compile. Error is with the use of
sizeof
in combination withEthernet::buffer
)ether.begin(sizeof Ethernet::buffer, mac, 53)
UIPEthernet (I assume I could use standard wiring here, as it is said that this lib uses standard Ethernet shield settings?)
None of the combinations made it possible to have any output in the serial monitor.
One of the sketches I have tried is the following:
#include <Dhcp.h>
#include <Dns.h>
#include <ethernet_comp.h>
#include <UIPClient.h>
#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPUdp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 6); // local IP - address of my Arduino
unsigned int localPort = 49001; // local port to listen - default X-Plane port
byte buf = 00; // buffer for UDP packet (BYTE, not char)
EthernetUDP Udp; // An EthernetUDP instance to send and receive packets over UDP
//-------------------------------------------------------------------------------
void setup()
{
Ethernet.begin(sizeof Ethernet::buffer, mac, 53)
Ethernet.begin(mac,ip); // start the Ethernet
Udp.begin(localPort); //..and UDP:
Serial.begin(9600); // init serial port
}
void loop() {
int packetSize = Udp.parsePacket(); // Checks for the presence of a UDP packet, and returns its size
if(packetSize) // UDP packet was received and its size defined
{
Serial.println();
Serial.print("Packet size: ");
Serial.println(packetSize); // Packet Size in bytes
// When Udp.read used without parameters, it returns next char (byte in this case) :
Serial.println("Xplane Data:");
for (int i =0; i/<packetSize; i++)
{
buf = Udp.read();
Serial.print(buf);
Serial.print("-");
}
}
delay(10);
}
So my questions are:
What is the easiest way to test both connections:
PC -> Ethernet module and
Ethernet module -> Arduino?Do I need to set the used Arduino pins in my sketch or does the lib do that?
Is the sketch supposed to work properly?