First time with the Ethernet shield

I’m currently trying to use my Arduino with an Ethernet shield to act as a simulator for a networked device in my university Junior Project. We’re currently running the simulation on a laptop, but just for kicks, I wanted to try to make it work on the Arduino. Basically, we’re writing an iPad app capable of communicating with a vehicle over a network for diagnostics and such, and the Arduino is going to simulate the vehicle side of things. Basically it’s running as a server, receiving commands from the iPad, and shipping out data packets at regular intervals describing the current state of the vehicle (gas/battery levels, rpm, speed, etc.). Currently, I have the iPad able to establish a connection with the Arduino and recognize that it receives at least the initial data packet (I have not yet added the code for simulating changes in the data like gas/battery levels decreasing over time, change in speed, etc.), but at some point, it just freezes up. I didn’t write the iPad code (other members of my team were responsible for that), but here is my current Arduino code. It’s my first time doing networking with the Arduino, so I’m sure I just messed up something simple. Also, the use of a statically allocated struct and memcpy for my payload is a hack, I know, but the I couldn’t find any decent method of serializing the data, and the String class is useless, so it’s the least of the evils I could devise, and according to Wireshark, it’s working, so that’s good enough for me. I have a momentary pushbutton between pin 52 and GND, and for now it’s just toggling a single value so I can see if it’s working or not (it isn’t. pushing the button freezes the Arduino until I release it, and if I hold it too long it never resumes). Any help is greatly appreciated.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <TimerOne.h>

#define CLOCK 1000000

// Define MAC address and destination port
byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0x92, 0xFB };
IPAddress broadcastIP;
unsigned int localPort = 55555;

float fuel_level = 1.0;
float battery_level = 1.0;
int speed = 0;
int electric_rpm = 0;
int ic_rpm = 0;
bool electric_on = 0;
bool ic_on = 0;
int battery_temp = 0;

<?xml version="1.0" encoding="UTF-8"?>
struct {
  char t1[50];
  char fl[8];
  char t2[10];
  char bl[8];
  char t3[10];
  char sp[8];
  char t4[11];
  char emr[8];
  char t5[12];
  char icr[8];
  char t6[23];
  char emto[1];
  char t7[14];
  char icto[1];
  char t8[12];
  char bt[8];
  char t9[26];
} udpPayload;

char buffer[10];

EthernetUDP Udp;
EthernetServer server = EthernetServer(localPort);
EthernetClient client;

boolean connected = false;

void setup() {
  pinMode(13, OUTPUT);
  pinMode(52, INPUT);
  pinMode(53, OUTPUT);
  digitalWrite(52, HIGH);
  // give the Ethernet shield a second to initialize:
  Serial.print("Local IP Address: ");
  broadcastIP = Ethernet.localIP();
  broadcastIP[3] = 255;
  memcpy(udpPayload.t1, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<data>\n<fl>", 50);
  memcpy(udpPayload.t2, "</fl>\n<bl>", 10);
  memcpy(udpPayload.t3, "</bl>\n<sp>", 10);
  memcpy(udpPayload.t4, "</sp>\n<emr>", 11);
  memcpy(udpPayload.t5, "</emr>\n<icr>", 12);
  memcpy(udpPayload.t6, "</icr>\n<ti></ti>\n<emto>", 23);
  memcpy(udpPayload.t7, "</emto>\n<icto>", 14);
  memcpy(udpPayload.t8, "</icto>\n<bt>", 12);
  strcpy(udpPayload.t9, "</bt>\n<bs></bs>\n<ss></ss>");
  Timer1.initialize(CLOCK); // Set up a timer at the defined resolution
  Timer1.attachInterrupt(tick); // Attach the tick function

void loop() {
  // if an incoming client connects, there will be bytes available to read:
  client = server.available();
  if (client) {
      Serial.println("Connected to client.");
      connected = true; 
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:
    while(client.available()) {
      char buf =;
    battery_temp = digitalRead(52);

void tick() {

void updatePayload() {
  dtostrf(fuel_level, 3, 6, buffer);
  memcpy(udpPayload.fl, buffer, 8);
  dtostrf(battery_level, 3, 6, buffer);
  memcpy(, buffer, 8);
  sprintf(buffer, "%8d", speed);
  memcpy(udpPayload.sp, buffer, 8);
  sprintf(buffer, "%8d", electric_rpm);
  memcpy(udpPayload.emr, buffer, 8);
  sprintf(buffer, "%8d", ic_rpm);
  memcpy(udpPayload.icr, buffer, 8);
  sprintf(buffer, "%1d", electric_on);
  memcpy(udpPayload.emto, buffer, 8);
  sprintf(buffer, "%1d", ic_on);
  memcpy(udpPayload.icto, buffer, 8);
  sprintf(buffer, "%8d", battery_temp);
  memcpy(, buffer, 8);

void broadcastPayload() {
  Udp.beginPacket(broadcastIP, localPort);
  Udp.write((uint8_t *)&udpPayload, sizeof(udpPayload));