Greetings!
I have been working on Nordic NRF52840 USB dongle and after trying couple of things I decided to give up and use it just as BLE UART device which sends the data it receives from mobile app to arduino mega on UART at 115200 baudrate. My friend who made this app and me mutually agreed to send this data from app in a format prescribed like <1,2,3,6,4,7,0,asf,23,hg> likewise I took this idea from the arduino example for serial communication basics to avoid any data loss. Everything was working fine until we were required to send an image and audio file.
The app sends Image as base 64 with a really long string of data same is the case for audiio. When we tried to send this to the NRF52840 connected to CP2102 module we could see all those base 64 stuff being received on to the tag in serial monitor, so we later decided to connect it to arduino mega so that it can be stored in SD card. We also kept the serial monitor commands which helps us to view whats exactly happening there. The problem is that Arduino doesn't receive complete string for image and neither for audio. SO my question is there any limit on how long string of data I can receive over UART? Im sharing the code that I use on arduino Mega
/*
* This example accepts data from nrf52840 and sends it to SD card
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
#include <SPI.h>
#include <SD.h>
File myFile;
const byte numChars = 100;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
mySerial.begin(115200);
Serial.println("Demonstrate data parsing received from mobile app");
Serial.println();
if (!SD.begin(53))
{
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
delay(5000);
GetDataFromSDCard();
}
//============
void loop()
{
recvWithStartEndMarkers();
if (newData == true)
{
strcpy(tempChars, receivedChars);
SendTOSDCard();
newData = false;
}
}
//============
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (mySerial.available() > 0 && newData == false) {
rc = mySerial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void SendTOSDCard()
{
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile)
{
Serial.println("Writing to test1.txt...");
Serial.println(tempChars);
myFile.println(tempChars);
myFile.close();
Serial.println("done.");
}
else
{
Serial.println("error opening test.txt");
}
}
//============
void GetDataFromSDCard()
{
delay(5000);
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available())
{
mySerial.print("{<");
mySerial.write(myFile.read());
mySerial.print(">}");
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
mySerial.println("{<1,0,,,,,,,,,,,,,,,,,>}");
}
}
this is the output I got in serial Monitor
21:26:26.412 -> Demonstrate data parsing received from mobile app
21:26:26.479 ->
21:26:26.479 -> initialization done.
21:27:08.163 -> Writing to test1.txt...
21:27:08.198 -> 1,0,Sharma,Shruti,Anuja,5556,fgh,F,16,09,1998,0,bomber,11,03,2020,21,27,07
21:27:08.267 -> done.
21:27:47.004 -> Writing to test1.txt...
21:27:47.038 -> 3,1,Laceration,0,Fall,11,03,2020,21,27,45
21:27:47.073 -> done.
21:27:55.421 -> Writing to test1.txt...
21:27:55.421 -> 4,1,0,11,03,2020,21,27,54
21:27:55.454 -> done.
21:28:08.460 -> Writing to test1.txt...
21:28:08.495 -> 5,1,0,5566,11,03,2020,21,28,07
21:28:08.530 -> done.
21:28:20.526 -> Writing to test1.txt...
21:28:20.560 -> 6,1,0,Intact,5,11,03,2020,21,28,19
21:28:20.595 -> done.
21:28:33.278 -> Writing to test1.txt...
21:28:33.325 -> 7,1,Analgesics,Fentanyl,55669,0,11,03,2020,21,28,32
21:28:33.378 -> done.
21:28:44.221 -> Writing to test1.txt...
21:28:44.275 -> 8,1,0,56909,0,11,03,2020,21,28,43
21:28:44.322 -> done.
21:29:03.367 -> Writing to test1.txt...
21:29:03.402 -> 9,1,hello there, it's testing 1,2,3,11,03,2020,21,29,02
21:29:03.437 -> done.
21:33:21.767 -> Writing to test1.txt...
21:33:21.767 -> 12,1,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDACgcHiMeGSgjISMtKygwPGRBPDc3PHtYXUlkkYCZlo+A
21:33:21.887 -> jIqgtObDoKrarYqMy
21:33:21.887 -> done.
21:34:18.303 -> Writing to test1.txt...
21:34:18.338 -> 13,1,AUDIO_20200311_213348_.3gp,AAAAGGZ0eXAzZ3A0AAAAAGlzb20zZ3A0AAADgG1vb3YAAABsbXZoZAAAAADajrxs2o6
21:34:18.439 -> done.
what we noticed is after the last line appears on serial monitor the app exits itself and the BLE disconnects from the app. But Im pretty sure that my mega does not reset to make this happen. what I probably think of is I need to increase the number defined for numChars which is currently 100, but I'm not quite sure about it as well. If it had to be increased what should be the number what is the maximum number it can hold? the image is supposed to be of nearly 200Kbs.
What should be done? any suggestions?
Thanks in advance