How to plot a live graph using bluetooth data from ucontroller

rajughade2006

Member
Licensed User
Longtime User
Hello,
I am trying to develop my first official android application and have undertaken a project for the same. Ive have had fun so far learning b4a nuances.(forgive me if i am being an idiot as regards b4a :sign0144:)

First let me explain my hardware part:
This consists of a ucontroller testing module that will send voltage and current values successively (i believe in ASCII format) with a word tag at the end like so- 'number'V 'number'I (next iteration) 'number'V 'number'I (next iteration) and so on...
The ucontroller i am using is pic18f46xx
This will happen only when i press a button on my app which will send a signal to ucontroller to begin that particular subroutine and send data only after that.

What ive done and know so far:
I have been able to study existing programs and using tutorials like Asyncstream and bluetooth chat i am able to receive the data on my phone and see it on the phone in the format i mentioned above. I also understand how to plot a graph using 'plot' and 'canvas' as well as i have help from existing tutorials.

Heres what i havent been able to do:
My basic aim is to plot these values on a graph, it can be live or passive(meaning data is stored first and then plotted or otherwise).
The beef being that i have no idea how to use the data coming from bluetooth or store it by separating the word tags 'v' or 'c' (in an array or such) and then use it to plot my graph. I have read all related tutorials for inspiration as well as other creations but to no avail.

:sign0163:
I request urgent help regarding this and appreciate any and all inputs i can get.
:sign0085:

Thanks and regards,
Raj.
 

rajughade2006

Member
Licensed User
Longtime User
Thank you for the prompt reply Erel,

My followup query is how do i convert this text i receive to separate the numbers into an array to plot, correctly, on a graph. i am unable to find a command for that.
Or should i just make the ucontroller send numbers only in ASCII ? Will it be easier to separate that way? I will give a healthy interval between numbers if needed.

The oscilloscope example you told me is very helpful, although i have used it as a reference while learning to plot graphs as i mentioned earlier, thanks for that either way.
 
Upvote 0

rajughade2006

Member
Licensed User
Longtime User
Absolutely,

I may have to explain this so plz bear with me,
In the ucontroller i use an int(hex) to string converter which gives me an ascii converted number of length 15 bits.
ex. 8.4683627549e-123(this is just a random example but this is pretty much how it looks on my phone)

Then i add on a string 'v' or 'i' at the end so that i don't confuse the strings.
There is a universal transfer mechanism adding 'line feed' and 'carriage return' at the end of each transfer which is not visible on screen but helps regulate transfer.

In effect, i see ' 8.4683627549e-123V ' next iteration ' 8.4683627549e-123I ' next iteration and so on...
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is a universal transfer mechanism adding 'line feed' and 'carriage return' at the end of each transfer which is not visible on screen but helps regulate transfer.
This is good. You can use AsyncStreamsText class to properly receive the messages.

You can use this code to convert your string to a number:
B4X:
Dim d As Double = YourString.Substring2(0, YourString.Length - 1)
 
Upvote 0

rajughade2006

Member
Licensed User
Longtime User
Awesome that is just what i needed. Thank you very much.

Does the ".length-1" remove the characters in the end?(ex. 'vbo' at the end would mean "length-3" ?)

Since i will be getting alternate streams of I, V, I, V,... how do you suggest i separate those? I am just concerned of the problems with speed of bluetooth delivery, which may throw the numbers off, hence the doubt.

And one last thing, i have been testing my code all day, but am not positive about this.
B4X:
Sub AStream_NewData (Buffer() As Byte)
   LogMessage("MC", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub
So the Yourstring in the code you suggested would be my 'Buffer', right?
 
Upvote 0

rajughade2006

Member
Licensed User
Longtime User
Also, will the new 'd' as a double that i create will be an array?
Please tell me about this, thanks.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Does the ".length-1" remove the characters in the end?(ex. 'vbo' at the end would mean "length-3" ?)
Yes. You can easily test it.

So the Yourstring in the code you suggested would be my 'Buffer', right?
No. It will be the result of BytesToString. You should save the result in a string variable.

Also, will the new 'd' as a double that i create will be an array?
No.
 
Upvote 0

rajughade2006

Member
Licensed User
Longtime User
Thank you Erel for all the help, i will post my creation and the problems i came across as soon as i'm done with this.

Regards,
Raj
 
Upvote 0

rajughade2006

Member
Licensed User
Longtime User
Dear Erel,
I really need your help on this one,
I have been trying hard to no avail, i am unable to plot the data on a graph,
as you can see, ive tried to adapt the three activity example and bluetooth mc program in my code.

The nature of data arriving in my code is 'V4.1234562e-2' and 'I5.1231345e-2' and this continues serially by alternating between V and I values
but in the log i see it all broken up.
I can change the position of 'V' or 'I' and add or remove CRLF if required in my data to make my plot work.

Uploading my program as is, please suggest or make any modifications to my program to help me plot a graph of V vs I. I am really at my wits end!
 

Attachments

  • semiconductorcurvetracer.zip
    12.8 KB · Views: 355
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I had a look at your project.
But, unfortunately I don't really understant the logic behind it.

Could you explain more in detail what exactly you want to do.
Do you get concrete values from the ucontroller ?

I could help you with the graphic but only when knowing what data and how you get it and how you want it to be displayed.

From what I understood from your code you seem to get the data in one activity and try to draw it directly in another activity.
If this is the case this won't work.
If you want to draw the graph dynamicaly you need to have both, data acquisition and display in the same activity.
If you want a 'remote' or 'delayed' drawing you could memorize the data in one activity and when the acquisition is finished draw in another activity.

Do you have an example of the data you get from the ucontroller?
If you had a file with example data, I could then have a look for the graph.

If you are the only user to get these data I don't see the need to have 'V' and 'I' as a prefix for the values.
You know that 'V' is the first one and 'I' is the second one.

Best regards.
 
Last edited:
Upvote 0

rajughade2006

Member
Licensed User
Longtime User
Thank you for the help klaus,
yes, i am the only user receiving data.
The data comes to me over bluetooth.
I use the prefix to help differentiate between data.
I simply wish to plot a simple point to point line graph of volt-current characteristics for different devices, which is what ive tried to do in my code.
Plz forgive me if im being an idiot, b4a is quite new for me.
the data i receive is voltage and current, from a microcontroller in the nature 'V4.1234562e-2' and 'I5.1231345e-2', then again v then i then so on,...
I get about 15 sets of these readings. and i wish to plot them on a graph in any way possible ie store and plot or receive and plot, im happy either way.
also, i can change the way the microcontroller transmits data , with or without prefixes to help ease the plotting of graph on android

Thanks and Regards,
Raj

PS: i could use prefix too if required, plz tell me what should go into my prefix bytes, as in, what value i should put in the first 4 prefix bytes
considering the nature of my data as i have mentioned above
 
Last edited:
Upvote 0

rajughade2006

Member
Licensed User
Longtime User
I could upload a video of the data i receive if you want, but i feel that my description should give u a good idea. I also use crlf after each voltage or i data and have been experimenting with this for a while, any insight is greatly appreciated.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
It seems that you are getting the data.
So you should either put the data as Doubles into one two dimensional array or two arrays one for the voltage and one for the current.
With these arrays it's possible to draw the plot.
If you could get an example of a data acquisition and store it in a file, I could look for the graph. It's easier to work with concrete data instead of inventing any.
What kind of graph are you thinking of ?
Dimensions, grid, scaling etc.

Best regards.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find a small class with a test program allowing to plot one curve.
It's the first step, I will contnue developping it.
Current limitations:
- Only one curve per plot.
- No automatic scales

Best regards.
 

Attachments

  • Y_X_Plot.png
    Y_X_Plot.png
    35 KB · Views: 451
  • Y_X_Plot.zip
    8.3 KB · Views: 428
Last edited:
Upvote 0

ferdztech

Member
Licensed User
Longtime User
Attached you find a small class with a test program allowing to plot one curve.
It's the first step, I will contnue developping it.
Current limitations:
- Only one curve per plot.
- No automatic scales

Best regards.

Sir Klaus!,

I found your Y_X Plot.zip very useful. How to reset the graph/invalidate/cls , It is possible to plot without using array XVal2(), YVal2() just plot it directly when i got values for x and y for real time.

My objective is to plot into values of x,y using timer like a live series graph.

for example: x=1-1000 maxdata ,scale is 1
y=5 scale .5 actually its a voltage

my goal is to plot a signal like an ECG


Hope you can help me sir..thank you and god bless.
 

Attachments

  • X_Y_ECG.zip
    8.7 KB · Views: 382
Upvote 0

klaus

Expert
Licensed User
Longtime User
So what you need is a Y-Time Plot or Y-Index Plot class.
Do you get single point samples (Volt / time) or records of a given number of samples.
What is the sample frequency or record update frequency ?

Did you have a look at the Oscilloscope project, it could also be an inspiration for your needs.
 
Upvote 0

ferdztech

Member
Licensed User
Longtime User
Sir Klaus,

Y-time plot. Realtime. single point samples (Volt / time)

-I need to plot x,y directly everytime i received data, not using array of records it must be real time. in plot class i don't know how to modify it
-I need to Clear the plot every time i reach the maximum samples which is the x, for ex: 1000 , if reach 1000. start to 0 or 1 again
-For frequency just set for an example.
-My value for y range 0-5 or 0-255
-i had use plot2.setscale(0,1000,0,5) something like in previous example.
-I tested the Oscilloscope i had a hard time to dissect so i referred to your x_y samples which is very simple. my problem only how could i plot it
without accumalating data into array records for x and y.and how to reset the plot samples.

Thank you so much Sir.
 
Upvote 0
Top