Hi I appreciate help to use buttons on arduino or esp32 board and send data by bluetooth that I can use it in MIT app inventor.
It is no issue to send data from esp32 by bluetooth periodically, eg. every few seconds and then receive it by bluetoothClient in MIT app, even in my case is possible to display values assinged to button press.
But I can't find any way to keep that data as variable I can use (eg. compare text or logic functions). It is strange that I can see on the label field in MIT app, data sent from esp32 by bluetooth, but can't store that value for next use.
OK, lots of things not quite right in your App Code. Can you upload the Arduino Sketch (rename from .ino to .txt) please.
The time interval of 500 milliseconds per Bluetooth read is ambitious - is it necessary? I ask because I once had a student whose App was controlling the watering of flowers. That was being done every few seconds when once a day might have been too often
hi, there will be few variables to send them from esp32 every 30sec., I can see those values on the labels in MIT app but can't store them,
another thing is to send button position status from esp32 to MIT app, and let set change the screeen to another one when press particular button, or make some changes in the app, for now have no idea how to keep those values, I would like to use 5-7buttons and about 5 variables to send them by bluetooth, so I thought I could send different strings by SerialBT from esp32 and then decoding them on the MIT app and store/use them
You are not using line delimiters properly ...
Standard Advice:
Please see the Delimiter article in FAQ
Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.
In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.
In your Clock Timer, you should check
Is the BlueTooth Client still Connected?
Is Bytes Available > 0?
IF Bytes Available > 0 THEN
set message var to BT.ReceiveText(-1)
This takes advantage of a special case in the ReceiveText block:
ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.
If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.
P.S. Most devices use 9600 bps, don't ask for too much speed.
That would disconnect Bluetooth. If you want BT to stay connected, the other Screens can be Virtual Screens.
When we define virtual screens, we use one 'real' App Inventor Screen (most often Screen1). Screen-sized Vertical Arrangements on it are displayed/hidden as required - they are the Virtual Screens. This is generally a better approach for multi-screen Apps, they share data without having to "pass" it between screens and it also reduces code duplication, making the App more efficient and the code easier to follow if you have to return to it at a later date.
So, instead of separate "houses", virtual screens are "rooms" of the same "house".
Currently, you don't have the button position status in the main loop, but your idea of sending a code ('A', 'B' etc) is good, since the App has to determine what data is being sent (button status or sensor data).
So I assume you want to send several sensor values, then send a button value which tells the App what to do with the data?
I can put together an example for you - easier than listing all the things that are currently wrong. Please be patient with me as I have a few other matters to deal with first, so most likely you won't see anything until tomorrow (Chinese New Years Eve).
Using TinyDb to store the data is good and it's easy when you know how.
I'm going to assume that when the App is Exited, the Data does not need to be stored. If it does need to be stored, I can show you how to store and retrieve with TinyDB.
I have to trust that your Sketch generally works, I see you have based it on code published on Git Hub (DF Robot have similar code).
However, using delay() in the main loop can adversely affect the sensor values, so I have replaced that with a time interval test instead:
So here is a basic App Inventor Project which uses Virtual Screens.
I don't know what you intend to do with the sensor data (a chart?). In this example it is streamed onto the Main Screen and If Virtual ScreenB is opened, the data populates a ListView (Which could instead be anything you require). Each sensor value received is added to a comma separated string and the display refreshed.
Given that Bluetooth and Location must be on, they are dealt with by the code.
The Data is set to be read and processed every 1600 milliseconds. That's already faster than the human eye can tolerate.
I'm unable to conduct tests here as my test 'lab' is mothballed, but the Project is based on my template that many people have successfully customised to suit their requirements. An exception being the enabling of Bluetooth and Location, previously done via Activity Starter but the latest App Inventor version has that covered (hopefully!).
Edit: Not being prompted for BT or Loc on my phone
Thank you so much Chris, I will test your code and app, and let you know how it work for my app.
btw. I am ok with virtual screens, just I need to use physical buttons because I want to use my app at bike, where I use a gloves and would much easier for me to use buttons for controlling my app than use phone screen.
Chris, I did check your code and app,
It looks like you can change screens by buttons on the phone screen,
but still can't change them by phisical esp32 buttons,
your app display values from buttons on the Data in label but not changing screens like your B or C screen buttons in the MITapp
Do you mean, when an esp32 button is pressed, the result of that button press is displayed in the App? Not good, because the code should detect that it's a command and not data......
Let's try a Sketch that only has the button code (no sensor read) and an App that just puts whatever it receives into the label 'Label_Data_Received_VirtualScreenA' . Then we will know if "B" and "C" actually get sent to the App.
The test App does not do anything other than put everything it receives into a Label, nothing else. How about a screenshot?
Edit: Or swap the Label Component with a TextBox, which will allow you to copy the result to the clipboard - then you can paste it into an email to yourself, then on your PC copy the content of the email to this Forum Topic
app does not do anything other than shows information on the label, thats it,
no screen change, and I also tried to catch that value from serialBT, but can't
put that into new variable
text box only shows the same values, A or B, depend what I press