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"));
}