Reading from SD Card and sending to Smartphone with BLE

Hello, I am using an Arduino Nano 33 BLE Sense and have it connected to an SPI SD card breakout board. The SD card code works as needed. What I am trying to do is take the text read from the SD card and send it to an Android smartphone via BLE.

We are using MIT App Inventor to create a 'BLE Remote' of sorts that the Arduino talks to. When the user presses the 'receive' button on the app, the app sends an arbitrary integer to the Arduino, which then takes this as a signal to send all of the SD card data. More specifically, the data is parsed from the one-byte-at-a-time from the soilFIle.read() function into strings of a size 20bytes. The sdSend characteristic value is updated.

As per current function, the phone only displays the last String value sent. Can anyone with some background in BLE point out what I'm doing wrong here?

Note: The full code for this is quite long and the BLE stuff is the only thing I am having issues with, so I am omitting everything except the most relevant code here since my issue is definitely not some sort of syntax error.

              soilFile = SD.open("SoilFile.txt");
              if (soilFile) {
                while (soilFile.available()) { // read from the file until there's nothing else in it
                  holder = soilFile.read();
                  if (charCount < 20)
                  {
                    test += String(holder);
                    //Serial.println(holder);
                    charCount++;
                  }
                  else
                  {
                    if (test != prevStr)
                    {
                      sdSend.writeValue(test);
                    }
                    prevStr = test;
                    charCount = 0;
                    test = "";
                  }
                }
                if (charCount > 0)
                {
                sdSend.writeValue(test);
                prevStr = test;
                charCount = 0;
                test = "";
                }

MIT App Inventor

Can you get your Nano 33 BLE program to work successfully with a standard phone app like LightBlue or nrfConnect?

For trouble shooting, it will important to separate any Nano 33 BLE issues from app inventor issues with what you developing.

Just tried through NRF Connect; got the same result

I'm a little unclear. Is this the "same result"

the phone only displays the last String value sent

This block would appear to send only one character at a time, and overrides the block trying to build a string of 20 characters.

 if (charCount > 0)
      {
          sdSend.writeValue(test);
          prevStr = test;
          charCount = 0;
          test = "";
      }

What characteristic are you using?
Can you write a simple BLE sketch with a service and characteristic which takes a line of text from the monitor, builds a String character by character and sends it to the phone?

So two major things that improved things: I wasn't using the BLENotify setting in the String characteristic's initialization. Naturally, I didn't share this actually important piece of information in my original post (I'm very smart and would never improperly declare something ;))

This fixed a lot:

BLEStringCharacteristic sdSend("0000ffe2-0000-1000-8000-00805f9b34fb", BLERead | BLENotify, 512);

I also began sending text as chunks of 512 bytes (the max size of a characteristic value). I now know that even though ArduinoBLE sends in packets of 20bytes, it handles those little details in the background, so I'm just sending 512 bytes at a time to the writeValue function. See code below:

          while (central.connected()) {
            if (!flag)
            {
              clearSetLCD();
              lcd.print("Connected");
              flag = 1;
            }

            // if the remote device wrote to the characteristic,
            // use the value to decide what happens
            if (bleRW.written()) {
              soilFile = SD.open("SoilFile.txt");
              if (soilFile && fileFlag == 0) {
                while (soilFile.available()) { // read from the file until there's nothing else in it
                  holder = soilFile.read();
                  hldString += String(holder);
                }
              }
                  fileFlag = 1;
                  chop = hldString.substring(0,513);
                  sdSend.writeValue(chop);
                  hldString = hldString.substring(512, hldString.length());
              
            }
          }

Here's what I ended up using for my app (Sorry I was trying to just paste a picture of the blocks but App Inventor wouldn't cooperate, so here's a Google Drive link to the aia file):

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