Sketch doesn't work after Arduino reset

Let me clarify:

When I upload a sketch initially, I have the AC adaptor plugged into the Arduino, as well as the USB cord. The sketch works. I unplug the USB cord, everything works. I unplug the AC adaptor (removing the power source), plug it back in, and the sketch breaks, e.g. the MQTT portion of sending data fails (my red LED indicator flashes). If I press the manual reset button, the sketch works again. Why does this happen?

#include <Ethernet.h>
#include <PubSubClient.h>

//generic mac address for arduino.  dead beaf-eater?
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  
//static ip needs to be assigned
byte ip[] = { 
  192, 168, 10, 121 };
  
//broker ip
byte mqserver[] = { 
  XXX, XXX, XXX, XXX };

//mqtt
void callback(char* topic, byte* payload,int length) {
}

PubSubClient mqclient(mqserver, 1883, callback);

//initialize leds
int greenPin = 3;
int redPin = 2;

//large enough to hold ten chars plus the terminating zero byte
char mqttCode[11];

void setup() {
  Ethernet.begin(mac, ip);
  pinMode(greenPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  mqclient.connect("arduino");
  //everything is done at 9600 whopping baud
  Serial.begin(9600);                           
}

char bintohexascii(byte x) {
    char hex[16] = {
        '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
    };
    return hex[x & 0x0f];
}

void loop () {
  byte i = 0;
  byte val = 0;
  byte code[6];
  byte checksum = 0;
  byte bytesread = 0;
  byte tempbyte = 0;
  int j = 0;
  int counter = 0;

  if(Serial.available() > 0) {
    //check for header 
    if((val = Serial.read()) == 2) {                  
      bytesread = 0; 
      //read 10 digit code + 2 digit checksum
      while (bytesread < 12) {                        
        if( Serial.available() > 0) { 
          val = Serial.read();
          //if header or stop bytes before the 10 digit reading
          if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { 
            //stop reading 
            break;         
            digitalWrite(redPin, HIGH);   
            delay(500);
            digitalWrite(redPin, LOW);              
          }

          //do ascii/hex conversion:
          if ((val >= '0') && (val <= '9')) {
            val = val - '0';
       
          } 
          else if ((val >= 'A') && (val <= 'F')) {
            val = 10 + val - 'A';
            
          }

          //every two hex-digits, add byte to code:
          if (bytesread & 1 == 1) {
            //make some space for this hex-digit by
            //shifting the previous hex-digit with 4 bits to the left:
            code[bytesread >> 1] = (val | (tempbyte << 4));
            //if we're at the checksum byte
            if (bytesread >> 1 != 5) {            
              //calculate the checksum... (xor)    
              checksum ^= code[bytesread >> 1];       
            };
          } 
          else {
            //store the first hex digit first
            tempbyte = val;                           
          };
          //ready to read next digit
          //mqttCode[bytesread]=val;
          bytesread++;                                
        } 
      } 

      //output to Serial:
      //if 12 digit read is complete
      if (bytesread == 12) {           
        digitalWrite(greenPin, HIGH);   
        delay(1000);
        digitalWrite(greenPin, LOW);
        Serial.print("5-byte code: ");
        for (i=0; i<5; i++) {
          byte hinibble = (code[i] >> 4) & 0x0f;
          byte lonibble = code[i] & 0x0f;
          Serial.print(hinibble, HEX);
          Serial.print(lonibble, HEX);
          Serial.print(' ');
          mqttCode[j++] = bintohexascii(hinibble);
          mqttCode[j++] = bintohexascii(lonibble);
        }
        mqttCode[j] = '\0'; 
        //test what is getting sent through mqtt
        Serial.print("\nHere is the string: ");
        Serial.println(mqttCode);
        Serial.println();
        if (mqclient.connected()) {
            digitalWrite(greenPin, HIGH);
            mqclient.publish("company/project/arduino/DEADBEEFFEED/FacebookBadge", mqttCode);
            delay(1000);
            digitalWrite(greenPin, LOW);  
        } else {   
              mqclient.connect("arduino");
              if (mqclient.connected()) {
                digitalWrite(greenPin, HIGH);
                mqclient.publish("company/project/arduino/DEADBEEFFEED/FacebookBadge", mqttCode);
                delay(1000);
                digitalWrite(greenPin, LOW);
                counter++; 
              } else {
                digitalWrite(redPin, HIGH);   
                delay(1000);
                digitalWrite(redPin, LOW); 
                //restart, doesn't seem to help
                void (*restart)(void) = 0;
                restart();
              }
        }
        Serial.println();
        Serial.print("Checksum: ");
        Serial.print(code[5], HEX);
        Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
        Serial.println();
        delay(1000);                  
      }

      bytesread = 0;
    }
  }
}

The Ethernet shield draws a lot of current, so it is extremely wise of you to not depend on the USB power.

However "wall wards" come in different qualities. It seems that your power adapter needs a few milliseconds to build up the needed voltage. This can cause campatibility issues between components that alread work and components that are not yet ready.

I don't know by heart whether "brown-out detection" is enabled in the standard Arduino fuse setting. It it is not you should do this and maybe it can help...

Another work-around might be to delay(500) as first instruction in the init()