Arduino Android handshaking for data transfer

Hello,
I have to create a txt file by the name of a subject (person) in the sd card interfaced to the board. The name will be entered by the user in the android application. This name will be then received by my hardware board (adafruit feather M0 express interfaced with SD card) via bluetooth low energy which is configured with UART pins. I am able to receive the name character by character and see it on the serial monitor but i have to input the complete name like "Shivam Trivedi" and store in an array or string and then use this string or array to name the file. So i am not able to see the complete array when i am inputting the complete array at once. Here is my code that i am using.
Thanks,
Shivam

#include <SPI.h>
#include <SD.h>
#include <Arduino.h>   // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function
#include <U8x8lib.h>
#include <PCF8563.h>
PCF8563 pcf;
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define PIN      8
#define NUMPIXELS 1
#define SERIAL_BUFFER_SIZE 256
//char incomingData[50] ;   // for incoming serial data
//int incomingSerialDataIndex = 0; 
Uart Serial2 (&sercom1, 11, 10, SERCOM_RX_PAD_0, UART_TX_PAD_2);
void SERCOM1_Handler()
{
  Serial2.IrqHandler();
}
char Filename[30];
File dataFile;
String Data;
void setup() 
{
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
        Serial2.begin(9600);
        Wire.begin();
        pinPeripheral(10, PIO_SERCOM);
        pinPeripheral(11, PIO_SERCOM);
        delay(5000);
        Serial.println("Enter Subject Name");
        delay(1500);
        unsigned long int time2;
        time2 = millis();
        while (time2<90000)
          {
            time2 = millis();
            Data = Serial2.readStringUntil('\n'); // Add the incoming byte to the array
            //incomingData[incomingSerialDataIndex] = Serial2.read(); // Add the incoming byte to the array
            //incomingSerialDataIndex++; // Ensure the next byte is added in the next position
            //Serial2.flush();
          }
        Serial.println ("Finished");
        delay(2000);
        Serial.println(Data);
       
}

void loop() 
{

}

Before you go any further consider what the maximum length of a filename is when using the SD library.

You will find that it must be in the 8.3 format, hence many names will not fit

For starting point we can work on names within the limit.

Or is there any chance we can go to sd library and increase the length of the name. For starting point let us use only initials of the name. That means i will pass initials from android to my custom hardware via BLE and i want that these initials should be used to give the file its name. For example if name is John Dickson then the txt file storing the incoming uart data by the name JD. The filename should be JD.txt

If you are going to pass JD from Android to use as the filename then I see no need to alter the SD library.

8.3 is the maximum filename length, not the mandatory filename length, hence JD.txt is a perfectly good filename

For passing JD from Android via ble what should be my steps.

As you receive each character put it in an array of chars and put a '\0' in the next array position to indicate the end of the received string

When the end of message character is received, probably a newline character, but you will know what you are sending, use the string in the array of chars as the filename

Note that I am referring to C style strings not objects of the String library

Okay, is the above method of doing it in the code a correct way of doing this thing?
My goal is to ask for the filename everytime i create a file so whether it should be in loop function or setup?

If you want to use Strings (uppercase S) then you can use readStringUntil() to get user input but the way that you have used it wrapped in a while loop won't work properly because the input will not be responded to for 90 seconds even after you press return. You also cannot use such a while loop in the loop() function because the value of millis() will always be greater than 90000 after 90 seconds

I was suggesting using C style strings (lowercase s) with the filename read character by character in a section of free running code in the loop() function

See Serial input basics - updated

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.