EDIT: issues are resolved. Please read Edit section.
Firstly, I am very sorry for the long post. And then...
I have been working on this project for quite some time now. It is part of a bigger final project that I am working on with my three co-students for our graduation. The project consists of a hardware part (Arduino Ethernet Rev.3 + a lot of components) and a .NET solution containing a MSSQL database, ASP.NET Web API 2 and an ASP.NET MVC website. In its whole, the project is a system to register and bill travellers in public transportation services like bus, train etc.
The hardware part is almost done, but I am struggling with a rather obscure programming issue which I desperately need help to overcome. Therefore I turn to you my fellow Arduino gurus hoping that some of you might help me. Just as a disclaimer, I should say that I am not particularly advanced in Arduino/C/C++ and my knowledge in microcontrollers is still on a rather novice level, although I am deeply interested in this. The sketch I am working with is my cook up of different web-client, RFID and RTC examples, which I have adapted to our purpose.
Following, I will explain the way my setup works (or at least how it should be working), and then I will describe the impending issues. I have provided the full source code in the link below.
Arduino source code GIST (Updated with new code revision.)
How it works
On start up, we will receive an IP-address from DHCP and indicate success by turning on the green RGB LED. After getting the IP-address we send a HTTP request (CheckApiConnection()) to a controller on my test API, which returns nothing but a "200 OK" response. This is done solely to verify that we can connect to the API and on success we indicate by switching the green RGB LED to blue. After this process the system is in "Ready" state.
When reading a RFID tag the process is as follows:
-
The tag is read and converted (ReadRFIDTag()) into a string containing the tag ID in HEX format.
-
A JSON string is built (BuildJSONObject()) by concatenating different values in the following format: {"TerminalId":10, "TagId":"", "Timestamp":"<timestamp from GetTimeStamp()>"}.
-
While building the JSON string we call GetTimeStamp() which is returning a string containing date and time in MSSQL DateTime datatype format (YYYY-MM-DD hh:mm:ss[.nnn]).
-
Once the JSON string is built, we send a HTTP request (SendRequest()) to a controller on the test API. This request is containing the JSON string which the API then acts upon.
-
After sending the HTTP request we wait for the response (ReadResponse()). The received response is then decoded for content which is just a string returned from the API controller ("NEW", "CHECKIN" or "CHECKOUT") indicating which signal (sound and LED) the Arduino is supposed to give.
-
After the above process, the Arduino is again in "Ready" state, waiting to read another tag.
The problem
NOW here comes the issues which is driving me crazy:
On start up, when we call CheckApiConnection() to check if we can connect to the API:
-
First of all, sometimes (maybe 3 times out of 10) I don't get a response from the API. And by the nature of Arduino it is hard to debug this process, so I am not sure whether this is caused by the Arduino not sending the request properly OR the API not responding. This is a minor issue though.
-
The issue which concerns me the most is when CheckApiConnection() was run on start up, and I get a succesful response, the system goes into a really obscure state. Now when I scan a tag and the JSON string is being built something is breaking. I have circled in on the issue and found that when GetTimeStamp() is called from within BuildJSONObject() the program hangs right there, printing out inexplicable text (like maybe half of the JSON string or nothing at all) to the Serial Monitor instead of the JSON string I expect.
Now if I Serial.println() GetTimeStamp() in the Setup() function after calling CheckApiConnection(), this works fine. The problem occurs only when GetTimeStamp() is called from within BuildJSONObject() AFTER running CheckApiConnection(), which I think is really strange and unexpected behavior. If I comment out the CheckApiConnection() the whole program runs as it should and I am able to scan multiple tags and sending requests to the API without problems.
WHAT could possibly cause this strange behavior? The only thing that comes to my mind is memory register failure (data overwriting each other unexpectedly), which should not be happening since my sketch is only using 64% of program storage space and 61% of dynamic memory. Could this be it? Does anyone have a clue. Any help is highly appreciated.
Thank you in advance.
EDIT (10-26-2016):
After studying the replies to this thread and getting help from other parts, I have been doing a major refactoring of the source code (The GIST above is updated). Besides from renaming a lot of functions and variables I have been doing a little memory optimizing. This actually solved the described issues I was facing.
To make it short: what did the trick was to move around with some String variables to make sure they are falling out of scope when not needed anymore as well as getting rid of some other variables. I managed to reduce the usage of dynamic memory by 13%. This apparently did the trick. Though I still don't understand the underlying problem. I wouldn't have thought that using 61% of dynamic memory could make the Arduino crash.
P.S. Be aware that a lot of function and variable names has changed so some of the names mentioned earlier in the thread are no longer applicable.