Android Question Receive data from ARDUINO (HC05/06 or others) to B4A ESPECIALLY in bluetooth

Henrig

Member
Licensed User
Longtime User
Hello
For an RC project I would like be able exchange BLUETOOTH data between ARDUINO and B4A, other direction(B4A --> arduino) works very well,
My arduino software is written for a long time and I use severals ADAFRUIT libraries (Adafruit_PWMServoDrive . . . and others) I'm not sure that B4R allows me to keep theses libraries and the futures ones to come, so I want to stay on real ARDUINO language, the one I know well, write fast enough.
I communicate very well from B4A to ARDUINO with HC 05 or 06, BUT I don't know how to receive my data from ARDUINO to B4A.
For several days I have been reading TUTORIALS, CODE SNIPPETS, SHARE YOUR CREATIONS from ANYWHERE SOFTWARE pages without finding anything as examples or lines I can use,
Who can help me, or give me a few lines of code to receive my data from the real ARDUINO software?
Are there better components than HC 05/06 that will make my job easier, more efficient?
Thanks all for help.
Henri
 
Last edited:

Henrig

Member
Licensed User
Longtime User
In my case (Henrig) the AT commands are correct and the software HC-05 LED ON/OFF works,
But just from my smartphone to Arduino
Henri
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I'm not sure that B4R allows me to keep theses libraries and the futures ones to come, so I want to stay on real ARDUINO language, the one I know well, write fast enough.

I am not clear about whether your problem is in coding the Arduino or in coding your Android device, but I assume that the Arduino code is done. Whatever code you use in the Arduino the data is coming from the HC-05 as an asynchronous stream. You do not show the code that you are using at the Android end but I presume you have set up an AsyncStreams object. If not then that is where to start.

The problem with asyncstreams is determining the message length - you need some protocol to do this. The easiest protocol is fixed length messages, and ASCII messages are also easy. For binary data you need something more. Does your Arduino code apply any protocol or does it send raw data? In the latter case you might need to invent your own protocol.

At the moment you are providing very little information about anything so it is hard to know what help you need.
 
Upvote 0

Henrig

Member
Licensed User
Longtime User
I printed this farm tractor model: this and i want to drive it with Andriod interface,
I started with a btarduino.zip, a model found on https://www.b4x.com/.
I modified it a lot and it works very well now BUT I want to improve it
I need to control the voltage and the intensity of the battery, I also want to measure the distance when I approach an object with two Ultrasonic Sensor HC-SR04 (and maybe other things)
The apps are not yet clean, still under study,
I post all my work (still dirty writing) on the subject:trac01.ino and BTArduino2.zip,
My goal is to retrieve data from Arduino,
Thank you for your help
Henri
 
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
Bonjour, personnellement, j'envoie des données de B4A vers B4R via le système Bluetooth de mon Smartphone en passant par un module HC05 (Mode esclave) installé sur le véhicule Arduino télécommandé... Dans ce premier cas, les données vont de B4A vers B4R.

Dans un autre projet, j'ai également utilisé une télécommande B4R pour envoyer des données (Module HC05 en mode maître) vers le véhicule Arduino ou B4R (Autre module HC05 en mode esclave). Dans ce second cas, les données vont de B4R vers B4R.

Cordialement.
 
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
Ceci est la partie anglaise du forum. Veuillez écrire ANGLAIS ici.
Alternativement, vous pouvez créer un fil dans le forum français pour y parler en français.
Sorry, I'm in the French West Indies, far away from home and my computer. I just try to answer ...
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I know little about Arduino, but I have sent data both ways between a 'phone and a microprocessor using Bluetooth. I suggest that you forget about the tractor for the time being and instead write a new Arduino sketch as a testbed for Arduino/Android communication. When you have sorted out how to transfer data you can decide what data you want to transfer. Start with ASCII strings.

You say elsewhere that you can send data from your 'phone to the Arduino. Write some Arduino code that waits for a specific incoming single ASCII character and responds by transmitting a short ASCII string. Terminate the string with a LF (chr(10)).

I assume that in your Android code you have already set up a serial connection with statements something like these ...
B4X:
    Dim link as Serial
    Dim stream As AsyncStreams
    ... ...
            
    link.Initialize("link")
    ... ...
    
    link.Connect(MAC)
    ... ...
    
    stream.Initialize(link.InputStream, link.OutputStream, "link")


Also define a global string variable ...
B4X:
    Dim data as String

Receive and handle the incoming data from the HC-05 with this code ...

B4X:
Sub link_NewData (Buffer() As Byte)
    Dim msg As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    If (Buffer.length > 0) Then data = data & msg
    If (data.Contains(Chr(10))) Then
        Log(data)
        data = ""
    End If
End Sub

Send the defined single character from your 'phone to the Arduino. Data is going to arrive in bursts. You need to collect the data until you receive an "end-of-line" (LF) character. It really is quite simple.
 
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
Sorry, I'm away from home and my computer, I'm on vacation in the French West Indies, I'll get back to you after my return to France
 
Upvote 0

Henrig

Member
Licensed User
Longtime User
Hello Brian,
I grope in all directions (too much) to try,
I can't put my files on this website,
I have to find another place BTArduino2.zip is 11.9 MB (12,483,225 bytes) and trac01.ino is 5.31 KB (5,440 bytes)
I can't put them here,
the website tells me TOO HEAVY
I put them on my personal website (and I can download them):
http://henrig.free.fr/trac01.ino
http://henrig.free.fr/BTArduino2.zip
Thank you for your help and (important) advice.
Henri
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Hi @Henrig . I have downloaded your app and, in my opinion, it is working! I have a breadboard circuit with a microprocessor and an HC-05. The microprocessor code simply responds whenever it receives a message. As soon as I pair your app with the HC-05 I can see the responses from my breadboard displayed in your app - a little mixed up, but they are there. So there is no doubt that there is a two-way connection.

If you are not seeing this then we have to suspect an error in your Arduino sketch - is it really transmitting any data? I am attaching my HC-05 testbed. Much of the coding is similar to yours and I suspect that we both got it from the same place on this forum. My testbed sends single ASCII characters to the HC-05. The "On" and "Off" keys send "p" and "q" respectively - the number keys send their equivalent ASCII character. Write a new Arduino program - it should only be a few lines - that waits for an incoming character and sends a short ASCII message in return. You should see the return messges displayed in the testbed and in its logger. If you do not, then your Arduino code is incorrect.

By the way, my testbed is only a test bed - it may contain bugs, but it does a job.

Edit : There is another factor that you need to check. Last year I was having problems with HC-05 communication very much like yours. After many weeks I discovered that the problem was in the HC-05 modules. Not all HC-05 modules are the same - some do not work or only partially work. If you have a terminal program and a USB-TTL converter then use that to try them out.
 

Attachments

  • Testbed.zip
    11.5 KB · Views: 324
Last edited:
Upvote 0

Henrig

Member
Licensed User
Longtime User
Hello Brian,
You pulled me out of the ditch
Last night I adapted your lines and OOH MIRACLE I received my data
We were very close to the goal, REALLY THANK YOU,
I can continue my application, but above all keep these lines for my next projects,
I will also try your file now.
Thanks and let's keep in touch
Henri
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…