Receiving strings in Arduino - problem -

Hi everybody!!
I have some problems with my code when I receive a string from my app in Android. I need to send some string (utf8 format) from my App to Arduino (over bluetooth BLE), and get data in a SdCard (in Arduino). The strings are two and the first string is:

U\n

and the last one:

0 20/11/25 18:18:46\n

The Arduino code is:

SoftwareSerial HM10BLE(8, 9);            
char appData = ' ';
String inData = "";

void loop() {
      HM10BLE.listen(); 
      while (HM10BLE.available() > 0){
        appData = HM10BLE.read();
        inData = inData+appData;
        if (appData == '\n'){
          Serial.print("StringReceived:");
          Serial.println(inData);
          inData="";
        }
      }
    }

The output for Serial.print(“String:”); print U (the result that I expect), but only the first time… and next doesn’t print anything…
When I need to send the last one string I only receive trash… But if I forward the last string again, now the string received it’s Ok!!
I have also sent the first string (the string received is Ok) and next restart Arduino and finally send the last string and the string received ( the last one) is Ok!! :upside_down_face:
What do I need to do to solve this problem?
Thanks in advance! :grin:


Please export your project and post it here.

Hi ABG!!
Here is the project BLE_Test_ABG_V1.aia (181.7 KB) and this is the problem from my monitor:

Thanks ABG! :+1:

Your detailed error symptoms should make it easy for a real BLE expert to diagnose your problem.
In the meantime, all I have for you are some observations and hypotheses …

  • Your BLE version is current.
    BLE Properties

    • NullTerminateStrings – Instructs the BluetoothLE component to terminate strings with a null byte (true) or not (false) when sending string data to a connected device.
      I don’t see any place in your arduino code where you check for 0 as a null terminator. Instead you use \n. By not including any expected null terminator, are you forcing the arduino to keep accepting data until its gullet (buffer) overflows? (Are you guilty of torturing your arduino?)
  • The transmission blocks …
    write strings 2 write strings 1

  • There is room for slipshod type conversion here. I hope the AI2 block is not fouling up interpretation of the long text string as a list. On the other hand, the errors are not consistent, so that points more to a timing problem.

  • Timing issues?
    register for strings
    You use the same UUIDs for sending and receiving, thus sharing a channel with the Arduino. Is this a good practice? Might the Arduino be interrupting the AI2’s transmissions randomly? (I don’t know.)

  • I don’t remember seeing in your Arduino code any inclusion of a BLE library, or calls to procedures with BLE type functions. Might you be missing something on that end?

That should get you started, until a BLE expert arrives.

1 Like

Hi ABG!!

You're right!! I used both :upside_down_face: that was the problem!!
I changed my Arduino code (replace '\n' to '\0') to:

SoftwareSerial HM10BLE(8, 9);            
char appData = ' ';
String inData = "";

void loop() {
      HM10BLE.listen(); 
      while (HM10BLE.available() > 0){
        appData = HM10BLE.read();
        inData = inData+appData;
        if (appData == '\0'){
          Serial.print("StringReceived:");
          Serial.println(inData);
          inData="";
        }
      }
    }

And I also changed the strings deleting '\n' y Mit App!
The communication between Mit App and BLE works fine!
Thank you very much ABG you are very kind! :slightly_smiling_face:
Greetings!!

Javier

Thanks for telling us what worked!

I am weak in C.

So ‘\0’ is the char literal for hex 00 ?
(I had not seen that before.)

Unfortunately I don’t have much experience to affirm that…
But yes! Thats worked for me! :grinning: