Arduino Keeps Restarting Setup()

I am working on a scanner that will communicate with my server, but every time that a code is scanned once the web request begins, the arduino restarts for some reason. I am using an Arduino Uno Wifi with a USB Shield. Code is attached below:

#include <hidboot.h>
#include <usbhub.h>
#include <WiFiLink.h>

#define OFFSET 22

char barcode[200]; //global barcode array
//int index = 0; //global index
int index = OFFSET; //global index
//const char* connector = "rest";
//const char* server = "surprisebot.000webhostapp.com";
//const char* method = "GET";
//const char* resource = "/barcode.php?code=IOFF281343";
int process = 0;
int startWeb = 0;

char ssid[] = "TPLINK25"; // your network SSID (name)
char pass[] = "Wireless1609"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
//char server[] = "www.google.com"; // name address for Google (using DNS)
char server[] = "surprisebot.000webhostapp.com"; // name address for Server (using DNS)

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;

class KbdRptParser : public KeyboardReportParser
{
  protected:

    void OnKeyDown  (uint8_t mod, uint8_t key);
    void OnKeyPressed(uint8_t key);
};


void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
  uint8_t c = OemToAscii(mod, key);

  if (c)
    OnKeyPressed(c);
}

void KbdRptParser::OnKeyPressed(uint8_t key)
{
  if((int)key == 19)
  {
    //Serial.println();
    barcode[index] = '\0';
    index++;
    Serial.println(barcode);
    process = 1;
  }
  else
  {
    //Serial.print((char)key);
    //Serial.print("added ");
    //Serial.println(index);
    barcode[index] = (char)key;
    index++;
  }
  
};

USB     Usb;
//USBHub     Hub(&Usb);
HIDBoot<USB_HID_PROTOCOL_KEYBOARD>    HidKeyboard(&Usb);

KbdRptParser Prs;

void setup() {
 //Initialize serial and wait for port to open:
 Serial.begin(115200);
 while (!Serial) {
 ; // wait for serial port to connect. Needed for native USB port only
 }

 Serial.println(F("Start."));

//USB SETUP

#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif

  if (Usb.Init() == -1)
    Serial.println(F("OSC did not start."));

// WIFI SETUP
 //Check if communication with wifi module has been established
 if (WiFi.status() == WL_NO_WIFI_MODULE_COMM) {
 Serial.println(F("Communication with WiFi module not established."));
 while (true); // don't continue:
 }

 // attempt to connect to Wifi network:
 while (status != WL_CONNECTED) {
 Serial.print(F("Attempting to connect to SSID: "));
 Serial.println(ssid);
 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
 status = WiFi.begin(ssid, pass);

 // wait 2 seconds for connection:
 delay(2000);
 }
 Serial.println(F("Connected to wifi"));
 printWifiStatus();

 Serial.println(F("\n\tREADY\n"));

 strcpy(barcode, "GET /barcode.php?code=");

 HidKeyboard.SetReportParser(0, &Prs);
}

void loop() {
  if(process)
  {
    if(!startWeb)
    {
       // FINISH SANITIZING BARCODE
       strcat(barcode, " HTTP/1.1");
       
       Serial.println("\nStarting connection to server...");
       // if you get a connection, report back via serial:
       if (client.connect(server, 80)) {
         Serial.println("connected to server");
         // Make a HTTP request:
         //client.println("GET /queue.txt HTTP/1.1");
         client.println(barcode);
         client.println("Host: surprisebot.000webhostapp.com");
         client.println("Connection: close");
         client.println();
       }
       startWeb = 1;
    }
    else
    {
       // if there are incoming bytes available
       // from the server, read them and print them:
       while (client.available()) {
         char c = client.read();
         Serial.write(c);
       }
      
       // if the server's disconnected, stop the client:
       if (!client.connected()) {
         Serial.println();
         Serial.println("disconnecting from server.");
         client.stop();
         
         int i;
         for(i = OFFSET; i < sizeof(barcode); i++)
         {
           barcode[i] = '\0';
         }
         index = OFFSET;
      
         process = 0;
         startWeb = 0;
       }
    }
  }
  else
  {
    Usb.Task();
  }
}


void printWifiStatus() {
 // print the SSID of the network you're attached to:
 Serial.print(F("SSID: "));
 Serial.println(WiFi.SSID());

 // print your WiFi shield's IP address:
 IPAddress ip = WiFi.localIP();
 Serial.print(F("IP Address: "));
 Serial.println(ip);

 // print the received signal strength:
 long rssi = WiFi.RSSI();
 Serial.print(F("signal strength (RSSI):"));
 Serial.print(rssi);
 Serial.println(F(" dBm"));
}

First thing comes to mind, brownout. How are powering the Uno and other devices?

It's powered by USB Serial from my Laptop but once completed will be powered by 12V 100mA DC supply as described here.

What is a brownout btw?

markmazel: What is a brownout btw?

Not enough power

Brownout is when the power to the Controller falls below a preset level causing the controller to go into reset. Are you powering all your devices from the Uno +5 and how much current do they draw?

Not sure how much current they draw, how would I be able to check?

markmazel: Not sure how much current they draw, how would I be able to check?

Probably easier just to list everything you have connected to your Arduino that draws power from it. Be specific. And don't forget things like a simple LED that's only on once in a while.

These folks know their stuff. They'll be able to get an idea if your pulling too much. If not, start searching for datasheets. Or get a good a good multimeter.

  if ((int)key == 19)

Is 19 (decimal) the correct code which terminates a message? Which scanner is sending this info?

Pete

Full list of pieces:

  1. Arduino Uno Wifi
  2. Arduino USB Host Shield
  3. Wired Handheld USB Automatic Laser Barcode Scanner Reader With USB Cable

markmazel:
Full list of pieces:

  1. Arduino Uno Wifi
  2. Arduino USB Host Shield
  3. Wired Handheld USB Automatic Laser Barcode Scanner Reader With USB Cable

2 and 3 use a combined 193mA from what I can find in what little research I just did. I think the Uno only outputs 200mA max. If I’m wrong, someone will correct me. You may want to try powering the scanner with its own source and see if that helps. Can get a cheap 5v wal-wart most anywhere these days.

Doesn't WiFi demand quite a bit of juice, also? I remember Parallax had to reengineer their Activity Board because Xbee pulled too hard on the power rail.

I have a 12V 1000mA DC supply, can I power it with that while still communicating over USB Serial?

markmazel: I have a 12V 1000mA DC supply, can I power it with that while still communicating over USB Serial?

If you mean to hook the 12v to the board? It won't help, IMO, due to the regulator on the board. It will knock it down to (I believe) 5v 500mA and still only output 200mA. Someone with more experience with power issues will have to help you out. I'm just going off memory. And it's not too good!

Are we even sure it's a power issue?

markmazel: Are we even sure it's a power issue?

Try disconnecting the sensor. The program should still run, right? It just won't get info from the sensor. If your Arduino, barring any possible permanent damage, stays powered up like it's supposed to, then you have a power issue. Or, you had the sensor wired up wrong.

It works fine just as soon as internet communications start, it restarts

I see more problems in your sketch but they are not responsible for the reset. Try a simplified version of the skech without the usb shield and scaner.

Juraj:
I see more problems in your sketch but they are not responsible for the reset. Try a simplified version of the skech without the usb shield and scaner.

Could you elaborate as to what problems? Without the shield and scanner it works fine but obviously will not go through the loop as it’ll keep hitting Usb.Task() without a response.

modify the sketch so you can test without the scanner sending some constant string.

and you should remove comments they not apply, because it's confusing.