I have already done those things since I posted and it works fine
markmazel:
I have a 12V 1000mA DC supply, can I power it with that while still communicating over USB Serial?
yes
I have already done those things since I posted and it works fine
You may be running out of memory, or you may be writing beyond the end of some array(s).
Are you really sending 200 character URLs to the server?
PaulS:
You may be running out of memory, or you may be writing beyond the end of some array(s).Are you really sending 200 character URLs to the server?
Its a 200 char buffer but the actual URL length is shorter than that, this should be fine though because of the null terminator that is placed at the end when I strcpy the HTTP version to the end of the URL/Request.
This is example output that occurs:
Start.
Attempting to connect to SSID: TPLINK25
Connected to wifi
SSID: TPLINK25
IP Address: 192.168.251.102
signal strength (RSSI):-72 dBm
READY
GET /barcode.php?code=57018
Starting connection to server...
connected to server
HTTP/1.1 200 OK
Date: Tue, 08 Aug 2017 18:39:32 GMT
Content-Type: textStart.
Attempting to connect to SSID: TPLINK25
Connected to wifi
SSID: TPLINK25
IP Address: 192.168.251.102
signal strength (RSSI):-80 dBm
READY
You can see that at the following line the Arduino seems to restart and I cannot figure out why this is happening:
Content-Type: textStart.
You can see that at the following line the Arduino seems to restart and I cannot figure out why this is happening:
That usually happens when you run out of memory.
Since you KNOW that the URL is not 200 characters, try reducing the size of the array to 100. See if you then get more of the server response before the restart happens.
I tried with 100 as the buffer size for the barcode/URL and it did not help the behavior, actually it made the Arduino restart earlier into the HTTP response.
I F() macro'd all possible literals in print statements and after compiling the IDE reports the following memory usage:
Sketch uses 17844 bytes (55%) of program storage space. Maximum is 32256 bytes.
Global variables use 1244 bytes (60%) of dynamic memory, leaving 804 bytes for local variables. Maximum is 2048 bytes.
I rewrote the code to not even save the scanned barcode but start the web communication once a code is scanned (except now it is always sending the same constant request for /queue.txt).
The reported memory usage is as follows:
Sketch uses 17640 bytes (54%) of program storage space. Maximum is 32256 bytes.
Global variables use 1032 bytes (50%) of dynamic memory, leaving 1016 bytes for local variables. Maximum is 2048 bytes.
Still, the same strange restart occurs...
Below is the output to the Serial monitor from such a run:
Start.
Attempting to connect to SSID: TPLINK25
Connected to wifi
SSID: TPLINK25
IP Address: 192.168.251.102
signal strength (RSSI):-68 dBm
READY
Starting connection to server...
connected to server
HTTP/1.1 200 OK
Date: Tue, 08 Aug 2017 19:35:16 GMT
Content-TyStart.
Attempting to connect to SSID: TPLINK25
Connected to wifi
SSID: TPLINK25
IP Address: 192.168.251.102
signal strength (RSSI):-66 dBm
READY
I have found something even stranger, after removing all the internet communication code and simply using the following code, the same issue arises.
#include <hidboot.h>
#include <usbhub.h>
#define OFFSET 18
char barcode[128]; //global barcode array
int index = OFFSET; //global index
int process = 0;
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)
{
barcode[index] = '\0';
index++;
Serial.println(barcode);
delay(1000);
Serial.print(F("."));
delay(1000);
Serial.print(F("."));
delay(1000);
Serial.print(F("."));
delay(1000);
Serial.print(F("."));
delay(1000);
Serial.println(F("."));
process = 1;
}
else
barcode[index] = (char)key;
index++;
}
};
USB Usb;
HIDBoot<USB_HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
KbdRptParser Prs;
void setup()
{
Serial.begin( 115200 );
#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("OSC did not start.");
Serial.println(F("Start"));
pinMode(2, INPUT);
delay( 200 );
strcpy(barcode, "/barcode.php?code=");
HidKeyboard.SetReportParser(0, &Prs);
}
void loop()
{
Usb.Task();
if(process)
{
int i;
for(i = OFFSET; i < sizeof(barcode); i++)
{
barcode[i] = '\0';
}
index = OFFSET;
process = 0;
}
}
However if lines 34-43 are removed, it works perfectly fine. Is there something about the Task() function that I am not understanding?
However if lines 34-43 are removed, it works perfectly fine.
So, you've discovered that KbdRptParser::OnKeyPressed is an interrupt service routine, and that delay() can't be used in an ISR.
Big surprise, there. Not.
In sketches for Uno Wifi you don't need to init the wifi connection in the sketch. ESP connects to WiFi at start with parameters from web panel and you can use this connection. I use only this in setup
delay(5000); //ESP init
while (WiFi.status() != WL_CONNECTED);
PaulS:
So, you've discovered that KbdRptParser::OnKeyPressed is an interrupt service routine, and that delay() can't be used in an ISR.Big surprise, there. Not.
If i do the delay in loop() not in the interrupt, same thing occurs... Sorry I should have done it outside of it to demonstrate, my bad.
Your code does not compile so I made a few changes. I used a keyboard instead of a barcode scanner. The console output looks like this. No crashes.
Maybe power problem? USB keyboards draw <= 100 mA. I am not sure how much current your bar code scanner draws. Try a USB keyboard and see what happens.
Start
/barcode.php?code=;laksjdf;lkajsdf;lkajsdf;lkjasd;flk
.....
/barcode.php?code=;aklsdjf;lkajsdfl;kajsdf
.....
/barcode.php?code=
.....
/barcode.php?code=;aklsdjf;laksdjf;laksjdf;lkja;dlfkja;lskdfj;laksdjf;klasjdf;lkajsdfl;kj
.....