Question about BT example

rfsingh81

Member
Licensed User
Longtime User
I am trying to play with the BT example and on the other end I have a microcontroller which is receiving the data from this module. I am facing a problem:

My Micro is waiting for decimal number. If the decimal number is 4 it switches on 1 led if 5 then the other. Now My tablet is connected to the module, and when I type 4 on the chat window, I HAVE TO PRESS IT TWICE for the other side to accept it as 4. Same happens with 5.

I would like to ask what happens on the app side. I am more into programming micros and trying to build an app for the first time. I am interested in the following code and thinking if this has something to do with the problem:
B4X:
Sub btnSend_Click
   AStream.Write(txtInput.Text.GetBytes("UTF8"))
   txtInput.SelectAll
   txtInput.RequestFocus
   LogMessage("Me", txtInput.Text)
End Sub

I am sorry but I am not into this programming a lot, so don't know if the above makes perfect sense but I tried to delivery my problem here in a hope that it can be understood. But I welcome any clarification if needed. Thanks
 

Beja

Expert
Licensed User
Longtime User
Hi rfsingh81,

try to increase the delay in the MCU firmware.
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
Hi rfsingh81,

try to increase the delay in the MCU firmware.

Hi, it is not possible as there is no feature for doing that. The code below will explain more:
B4X:
Main:
   Debugin [DEC B0]
   if b0=4 then GoSub TOG ; led 1 toggles
   if b0=5 then GoSub DOG ; led 2 toggles
   Pause 200
Goto Main

B0 is a byte, and the first command loads the byte into B0. I think it is something to do with the way data is sent from the App. Is it possible to send the data as a decimal or a STRING in one go? What changes will I need to do to accomplish that (if possible)?
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Can you post the MCU code that controls the I/O?
The code above tells it may be a high-level interpreted code which is used
in Basic Stamp and similar modules.. you need to edit the MCU firmware.
I still believe this is a sync problem and not from the bt example.
Also try to put the I/O module far from the mobile device.
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
Hi Beja, the code which relates to receiving data and I/O operation is above. There is nothing more to it MCU code wise, yes the language is PICBasic. I have checked the manual and I can confirm not even a single word is mentioned on handling a UTF8 character or conversion of it to decimal or string. So what I wanted to ask is how can he following be changed :
AStream.Write(txtInput.Text.GetBytes("UTF8"))
To send a fixed character as a string or decimal. Lets say for example send an String "a" or decimal equivalent of "a".
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
Ok. I just downloaded serialMon software and found out that UTF8 means plain text. So my question now is - What is the HEX or Decimal Equivalent of UTF8 character 4?

And is there any table of equivalent values?
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
will test the example code in my Atmel and PIC16 boards and get back to you, hopefully soon as I am a little busy now with other things.
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
Thanks

Thanks Beja and GMan. I did it finally :sign0060: . The problem was that every 4 bit nibble of hex showing on my screen was actually a whole byte of 8 bits. So adjusting the variables did the job.

JOB DONE! Cheers for the help.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks,
Would you please share with us your solution? how did the problem solved?
I mean the steps you made.
thanks in advance..
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
Rfsingh81,

You have made an important discovery! The representation of data is fundamental to handling it. You will find different computer languages often handle data types differently. By learning the details, you are well on the way, if not already, to be a great programmer.

Cheers!
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
Rfsingh81,

You have made an important discovery! The representation of data is fundamental to handling it. You will find different computer languages often handle data types differently. By learning the details, you are well on the way, if not already, to be a great programmer.

Cheers!
Thanks M8 for the appreciation.

Beja, I did the following change:
B4X:
Main:
    Debugin [Wait(1), B0]
    if b0=4 then GoSub TOG ; led 1 toggles
    if b0=5 then GoSub DOG ; led 2 toggles
    Pause 200
Goto Main

Now it waits for the 1 to arrive first and then loads the value in B0 - Which has the actual value (HEX type).

Now I am trying to handle the data the opposite way i.e. MCU to tablet. If I get stuck, I will open another thread and hope to get support from you guys. Cheers!
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
Another Question

Hi, I thought it would be more appropriate to ask this question here as some of you already aware of my aims.

Now, I am trying to send data from the MCU to the APP running on my tablet. The MCU Side is like this:

B4X:
debug "a" // Send 'a' as a string

The app side is something like this:
B4X:
Sub AStream_NewData (Buffer() As Byte)
   ToastMessageShow(BytesToString(Buffer, 0, Buffer.Length, "UTF8"),True)
'   LoadMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

THE PROBLEMS:
2 problems I am facing with running this code. a) After MCU sends the data, twice in a row the bluetooth example stops working on the second time
b) No data is toast displayed on my tablet screen.
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
Many experience problems using prefix mode without setting length in first 32 bits. The solution is non prefix mode. There are many posts on this. The system hangs because the "a" is interpreted as a length, and receive waits for more.
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
Attempt 1

Many experience problems using prefix mode without setting length in first 32 bits. The solution is non prefix mode. There are many posts on this. The system hangs because the "a" is interpreted as a length, and receive waits for more.

I tried and found the info about prefix and changed the following line in the example to:
B4X:
AStream.InitializePrefix(Main.serial1.InputStream, False, Main.serial1.OutputStream, "AStream")

I changed the code of my MCU a little to accept the changes the way info will come this time. It Worked FinE.

When the MCU sent the data in return, the message which came on the screen is: 'Connection is Broken' which is a part of error sub.
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
That is still using prefix mode. I do not have, my pc, I seem to remember it is just initialize.

The lost connection may also be hardware issue. Too much distance, power loss or noise.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
set a Boolean variable (flag), to see for new data before trying to act on them.
 
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
The thing is that the serial connection, virtual over bluetooth or otherwise is 'character based' and yes, one character is 8 bits with or without 1 stop bit and with or without 1 parity bit so each character received will be ... a character :)

You just have to remember the old RS232 stuff.
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
set a Boolean variable (flag), to see for new data before trying to act on them.

Thanks. Is there any code example somewhere pertaining to what you mean? It will be easier for me to tweak any existing code than break my head against a wall for doing silly mistakes in writing it myself.
Currently my sub looks like this:
B4X:
Sub AStream_NewData (Buffer() As Byte)
   ToastMessageShow(BytesToString( Buffer, 0, Buffer.Length, "UTF8"),True)
'   LoadMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub
 
Upvote 0
Top