ENC28J60 (EtherCard Library) Data Intermittent

Hi All,

I have an issue with a data logging sketch, the data is sending intermittently.

Most of the time it works, but occasionally (maybe 1 in 5 times, intermittently) it seems to corrupt the URL in the GET request. I have confirmed this with a packet sniffer.

Is there any reason this might be happening?

#include <Wire.h>
#include <SD.h>
#include <EtherCardMicro.h>
#include <avr/wdt.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 
  0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[500];
static uint32_t timer;
const int chipSelect = 4;
String dataString;
String sendstring;
char sendchar[35];
int fileindex = 0;
char itoabuffer[12];
char website[] PROGMEM = "www.google.com";  // URL Changed for privacy!
boolean trysend = 0;
boolean trysuccess = 0;
static uint32_t trytimer;
////////////////////////////////////////////////////////////////////////////
void setup () {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(chipSelect, OUTPUT);
  digitalWrite(chipSelect, HIGH);
  pinMode(17, OUTPUT);
  digitalWrite(17,HIGH);
  if (ether.begin(sizeof Ethernet::buffer, mymac, 17) == 0) 
  {
    //Serial.println("a");
  }
  if (!SD.begin(chipSelect))
  {
    //Serial.println("e");
    software_Reboot(4);
  }
  int totalfiles = 0;
  while (SD.exists(itoa(totalfiles, itoabuffer, 10)))
  {
    totalfiles++;
  }
  fileindex = totalfiles - 1;
  if(fileindex < 0) {
    fileindex = 0; 
  }
  Wire.begin(4);
  Wire.onReceive(receiveEvent);
  if (!ether.dhcpSetup())
  {
    //Serial.println("b");
    software_Reboot(2);
  }
  if (!ether.dnsLookup(website))
  {
    //Serial.println("c");
    software_Reboot(3);
  }
  else
  {
    ether.printIp(ether.hisip);
  }
}
////////////////////////////////////////////////////////////////////////////
void loop ()
{
  ether.packetLoop(ether.packetReceive());
  if (millis() > timer)
  {
    char sdreading[15];
    char sendchar[35];
    timer = millis() + 10000;
    if(SD.exists(itoa(fileindex, itoabuffer, 10)))
    {
      File MyFile = SD.open(itoa(fileindex, itoabuffer, 10), FILE_READ);
      for(int i = 0; MyFile.available(); i++)
      {
        if(i < sizeof(sdreading))
        { 
          sdreading[i] = MyFile.read();
        }
        else { 
          MyFile.read();
        } 
      }
      MyFile.close();
      int y = 0;
      int x = 0;
      for(x = 0; x < (sizeof(sdreading) + 1); x++)
      {
        if(sdreading[x] == '.')
        {
          y = x;
          x = sizeof(sdreading) + 1;
        }
      }
      sendstring = "srv.php?d=1&p=pass&v=";
      for(x = 0; x < y; x++)
      { 
        //Serial.println(x);
        sendstring += sdreading[x];
      }
      sendstring.toCharArray(sendchar, sizeof(sendchar));
      Serial.println(sendstring);
      trysend = 1;
      trytimer = millis() + 4000;
      ether.browseUrl(PSTR("/ard/"), sendchar, website, my_callback);
      //Serial.println(fileindex);

      if(trysuccess == 1)
      {
        if (fileindex > 0)
        {
          SD.remove(itoa(fileindex, itoabuffer, 10));
          fileindex--;
        } 
        else
        {
          while (SD.exists(itoa(fileindex, itoabuffer, 10)))
          {
            SD.remove(itoa(fileindex, itoabuffer, 10));
          }
        }
        trysuccess = 0;
      }
    }
  }
  if(trysend == 1 && millis() > trytimer)
  {
    software_Reboot(5);
  }
}
////////////////////////////////////////////////////////////////////////////
void receiveEvent(int howMany)
{
  dataString = "";
  while(0 < Wire.available())
  {
    //Serial.println("h");
    byte sensor = Wire.read();
    int z = 0;
    z = Wire.read() << 8;
    z |= Wire.read();
    dataString += String(sensor);
    dataString += ",";
    dataString += String(z);
    dataString += ".";

    while (SD.exists(itoa(fileindex, itoabuffer, 10)))
    {
      fileindex++;
    }
    File dataFile = SD.open(itoa(fileindex, itoabuffer, 10), FILE_WRITE);
    if(dataFile)
    {
      Serial.println(dataString);
      //Serial.println(fileindex);
      dataFile.println(dataString);
      dataFile.close();
    }
  }
}
////////////////////////////////////////////////////////////////////////////
static void my_callback (byte status, word off, word len) {
  Ethernet::buffer[off+300] = 0;
  Serial.print((const char*) Ethernet::buffer + off);
  if(status == 0)
  {
    trysend = 0;
    trysuccess = 1;
  }
}
////////////////////////////////////////////////////////////////////////////
void software_Reboot(int count)
{
  while(count != 0)
  {
    digitalWrite(13,HIGH);
    delay(1000);
    digitalWrite(13,LOW);
    delay(500);
    count--;
  }
  wdt_enable(WDTO_15MS);
  while(1)
  {
  }
}

To clarify, when the data send successfully I have “GET /ard/srv.php?d=1&p=pass&v=8,43”, however whenever it fails it always returns “GET /ard/srv.php?d=1&p=pass\001\266”

The “8,43” are the sensor values which do change, but oddly whenever it corrupts the data it is always sending “\001\266”?