Sending realtime data EMG to my Android via BluetoothLE

Your Arduino BLE must send a String every second, example:
245,767
282,789
...

Your App must receive that String in a Label, example:
245,767
282,789

Do I have to activate the clock first, sir?

You don't need a Clock in the App, just ensure that your Sketch (Loop) sends the values every second. Use elapsed milliseconds like this:

//ArduinoToApp.ino  02/03/2021 02:38:48

//Fake data stream to demo elapased milliseconds timing


//vars
unsigned long lgUpdateTime;

void setup()
{
               Serial.begin(9600);
               lgUpdateTime = millis();
}

void loop()
{
	           if(millis() - lgUpdateTime > 1000)           //Loop approx every 1 second
               {
                         lgUpdateTime = millis();

                         //Bluetooth to App
                         Serial.print("Hello");
                         Serial.print("|");      //Value delimiter
                         Serial.print("World");
                         Serial.print("|");
                         Serial.println();        //This empty last line tells App "End of Data" = Ascii LineFeed Char Number 10
               }
}

Your content is entirely different, this just shows how to use elapsed milliseconds.

ACTUATOR_COBAK6a.ino (8.3 KB)
How about this one, sir?


I just type println, is that not enough, sir?

image
I have tried sir, but an error appears like that
and this is my blocks

Don't draw, just present the data in a Label
234,765

Don't use delay() it will interrupt your hardware and thus start producing erroneous results.

if (switchCharacteristic.written()) .... it is never written!
The Serial comms is with your Arduino Serial Monitor, not the App.

Put the "if(millis() - lgUpdateTime > 1000)" within your "while (central.connected())"

switchCharacteristic.write(nilai_sensor); //To App
switchCharacteristic.write(nilai_sensor2); //To App

Note, the "nilai" vars should be shorts, not ints.

Actually, I just noticed that Juan's chart wants a string - so change your Characteristic from Short to String:

BLEStringCharacteristic switchCharacteristic(CHARACTERISTIC_UUID, BLERead | BLEWriteWithoutResponse);

//Send to App:
char sSensorVals[16];
sprintf(sSensorVals, "%u,%u\0", nilai_sensor, nilai_sensor2);
switchCharacteristic.writeValue(sSensorVals);

is this what i should write in my sketch?

Yes - also, sorry, but to use sprinf() the values from the sensors should be type unsigned int.

If it's an unsigned int, can it be accepted by the app, sir?

And if i use the above, do i use this too?

It's an unsigned int for sprintf(), which creates a string (char[16]) to send to the App.

Example result of Sprintf(): 234, 765

Formatted this way, it can populate a Label in the App as directed by Juan.

It does mean, in the App, you should register for and read Strings instead of Shorts.

A large cup of coffee might help at this point :upside_down_face:

1 Like

Nope, you are now sending each pair of values as one String.


there is an error sir, is my sketch wrong?

Can't tell from a Screenshot.........

ACTUATOR_COBAK6a.ino (8.0 KB)
I'm sorry sir, this is it

Well, it's a good idea to declare your variables before using them :thinking:

Collect the values, then combine them into a String:

I haven't tested the rest of your code, my Arduino compiler isn't launching.

Edit: I have now tested your code. I can see it is supposed to activate the motors but currently the code isn't going to do that, the Arduino is not receiving commands from the App.

Staying with the issue as per the Topic Title, try this Sketch:
ACTUATOR_COBAK6c.ino (8.1 KB)

I assume you have changed your Project Blocks accordingly to receive Strings and then set each String in a Label as per Juan's instructions. Note, we are using a 'C' String which is actually a special array of chars. There is also a 'C++' String compatible with Arduino but not compatible with all libraries.

So how do I get my sketch to receive commands from the app as well as transmit data?