Folks,
I'm using an MKR ZERO plus an MKR ETH to repeatedly send 128 bytes to an IP address. There is no software listening at that destination address, but I don't think that should affect a UDP transmission.
The destination computer is directly connected to the MKR (no intervening switch or other network equipment, other than a cable), but again, I don't think UDP cares.
I'm using the 1.8.9 Arduino IDE to compile, and I'm using the 2.0.0 version of the built-in Ethernet library.
Arduino/Reference/Ethernet
In the MKR ZERO, the EthernetUDP.endPacket() invocation for each of my 128 byte transmissions (all zeros) is taking 1.6 seconds to complete.
That's absurdly long/slow: ( 128 * 8 ) bits / 1.6 seconds = 640 bits per second.
I'm curious if anyone can spot a mistake in my simple program, or can describe to me why a short message would take so long to send (surely there isn't that much overhead (> 1 second) per message).
Here's the code, and some sample results. In the results, the numbers are the times spent executing the endPacket() invocations, in milliseconds.
I have my fingers crossed that the answer is that I'm doing something boneheaded, or that there are some simple SPI or other tricks for speeding things up.
#include "Ethernet.h"
#include "EthernetUdp.h"
#include "IPAddress.h"
//=======================================================================================//
//============================================================================//
EthernetUDP _udp;
byte _myMacAddress[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
IPAddress _myIpAddress = IPAddress{192, 168, 192, 223};;
int _myPort = 14531;
IPAddress _heartbeatIp = IPAddress(192, 168, 192, 232);
int _heartbeatPort = 17142;
byte _outboundPacketBuffer[128];
//=======================================================================================//
//=======================================================================================//
void setup()
{
Serial.begin(9600);
delay(5000);
Serial.println("\nDoing UDP Setup");
Serial.print(" My MAC: "); printHex(_myMacAddress, 6);
Serial.print(" My IP: "); printDottedDecIp(_myIpAddress);
Serial.print(" My Port: "); Serial.println(_myPort);
Serial.println("The default External Computer (laptop) address is");
Serial.print(" Ext IP: "); printDottedDecIp(_heartbeatIp);
Serial.print(" Ext Port: "); Serial.println(_heartbeatPort);
Serial.print("MAX OUTBOUND MESSAGE BUFFER length = "); Serial.print(sizeof(_outboundPacketBuffer)); Serial.println(" bytes\n");
Ethernet.begin(_myMacAddress, _myIpAddress);
_udp.begin(_myPort);
if (Ethernet.hardwareStatus() == EthernetW5500)
Serial.println("W5500 Ethernet controller detected.\n");
if (Ethernet.linkStatus() == LinkON)
Serial.println("Link status: ON. This implies an Ethernet port exists and a useable cable is plugged into it\n");
}
void loop()
{
memset(_outboundPacketBuffer, 0, sizeof(_outboundPacketBuffer));
_udp.beginPacket(_heartbeatIp, _heartbeatPort); // The remoteIp and remotePort calls only work after doing a parsePacket call
_udp.write(_outboundPacketBuffer, sizeof(_outboundPacketBuffer));
unsigned long heartStartMicros = micros();
_udp.endPacket(); // This actually causes the packet to be sent
double heartSendingMillis = 0.001 * (micros() - heartStartMicros);
Serial.println(heartSendingMillis);
delay(3000);
}
//=======================================================================================//
// MISC UTILITIES
//=======================================================================================//
void printHex(const byte * data, const uint32_t numBytes)
{
uint32_t szPos;
for (szPos = 0; szPos < numBytes; szPos++)
{
// Serial.print("0x");
// Append leading 0 for small values
if (data[szPos] < 0x10)
Serial.print("0");
Serial.print(data[szPos], HEX);
if ((numBytes > 1) && (szPos != numBytes - 1))
{
Serial.print(' ');
}
}
Serial.println();
}
void printDottedDecIp(uint32_t ip) // Expects the IP Address to be stored with the last Octet in the MSByte.
{
Serial.print ((byte)(ip >> 0)); Serial.print('.');
Serial.print ((byte)(ip >> 8)); Serial.print('.');
Serial.print ((byte)(ip >> 16)); Serial.print('.');
Serial.println((byte)(ip >> 24));
}
//=======================================================================================//
//=======================================================================================//
Doing UDP Setup
My MAC: 01 02 03 04 05 06
My IP: 192.168.192.223
My Port: 14531
The default External Computer (laptop) address is
Ext IP: 192.168.192.232
Ext Port: 17142
MAX OUTBOUND MESSAGE BUFFER length = 128 bytes
W5500 Ethernet controller detected.
Link status: ON. This implies an Ethernet port exists and a usable cable is plugged into it
1617.90
1617.76
1617.77
1617.67
1617.76
1617.62
1617.80
1617.65