Update Switch status in app after switching LED on and off using arduino button

hello
Dear, I am quite fresh in creating of app in MIT and I stopped on issue linked with update switch status in app after switching the LED on or off on Arduino.
I am using for communication HC05 and from my point of view everything should be ok but it is not.....
using below code I can receive from Arduino String but it is not possible to use it for button status changing.
I can see the text which I send from Arduino appear in texbox but when I put it to the function it is not working. please take a look below on MIT block and arduino code - maybe someone can catch point where I am making the mistake. thank you very much in advance for support in solving this problem

Arduino code:

#define button A3
int lastbuttonstate;
String bt;
void setup() {
Serial1.begin(9600);
pinMode(button, INPUT_PULLUP);
}

void loop() {
int reading = digitalRead(button);  
if (reading != lastbuttonstate){
  if(reading == 1){
bt = "on";
Serial1.println("off");
}
if(reading == 0){
  bt = "off";
Serial1.println("on");
}
delay(100);
}
lastbuttonstate = reading;
}

You issued 4 ReceiveText commands, so you used up different input stream items.

Instead, in your Clock Timer just issue a single ReceiveText command, and save the result in a variable (like temp.) Then just apply your tests to that variable, and don't ask for more data from BlueTooth in that Clock Timer cycle.

That will avoid loss of data from your incoming data stream.

Also, you need to take advantage of Line Feed (decimal 10) Delimiters in your BlueTooth component and blocks.

Here is my standard advice for that (paste)

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.
BlueToothClient1_Properties
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.

(end of paste)

1 Like

hello
Dear Abraham, thank you very, very much much for helpful tutorial - using your tips right now status of switch in app is changing like it was my idea, but I found that I sometimes I have some problem with transmision - after relesing button app do not get appropriate data from Arduino and status is not changing - please taka a look on below picture from the phone:
point 1 - shows textbox which dispalys message which is not completed and base on this status despite this that phisically button is released (should show off in textbox instead of only ff and function do not work and finally do not change switch status - like you can see is still in position on - point 2
in clock I set TimerInterval to 100, I change DelimiterByte to 10 like you proposed but still something is wrong - please take a look on photo and final version of blocks - thank you very much in advance for any tips how to solve this problem

It's necessary to see the Transmission side code to diagnose the missing leading character.
Also show the Bluetooth client attributes from the Designer.

in case of this problem I must mention that it happened when button is pressed and released quite fast .....
BT attribute:
image
clock atribute:
image

arduino code:
#define button A3
int lastbuttonstate;
String bt;
void setup() {
Serial1.begin(9600);
pinMode(button, INPUT_PULLUP);
}

void loop() {
int reading = digitalRead(button);
if (reading != lastbuttonstate){
if(reading == 1){
bt = "off";
Serial1.println(bt);
}
if(reading == 0){
bt = "on";
Serial1.println(bt);
}
delay(20);
}
lastbuttonstate = reading;
}

I suspect you need debouncing in your transmission code.

thanks, I will study this point and I will come back to you with results

... little note, TimeAlwaysFires should be false. The timer should only be enabled when it is required and it must be disabled when the App has finished/closed.

Dear, I have one blocking point in my app which I can not solve: to remeber the position of switches and sliders I am using tinyDB whne app is closed and open one more time to come back to previous settings but it is not so good because in meantime somebody else can change something and values will not be actual. please let me know if it is possible to use general summarry of switch status which I have in the arduino program - when we call function statusm from app to arduino we will get the list with 10 positions which gives information about actual status of all phisical switches - is there posibility to use each line for each slider and update status of sliders in app for example after each screen initialize, thank that always after opening an app I will have always fresh and actual data? thank you very much for your support :
image

You can use that technique in the Sketch, but do not try to send a long string like that to the App - Bluetooth data packets are small. It is best to send the app integers only 1 for ON, 0 for OFF.

If you can switch the LED on/off on the Arduino, what is the point of having the App?

thank you very much for information linked with size of sended message - I will correct it in sketch.
to clarify situation: I will not switch led on Arduino, I can do this only via bluetooth, but there are posibilities that connection can be done from more than one device (not parallel of course) and here come the problem - when I switch something on one device and I will disconnect app it will remember lasr settings, but when in meantime somebody else will connect and change something I will not have real status despite I have last settings remeber, that's why I am looking for solution which will update app according actual status of arduino leds during app screen initialization.

maybe I can use cloudDB instead tinyDB - in my opinion it can solve the problem - what do you think?

A good choice for sharing yes. Any hardware failure on the microcontroller side of things may need to be taken into account too.

taking care about hardware will be like a second point, right now I have problem because I do not know in which way I can replace tinyDB block to cloudDB block - they have different extensions .... :frowning:

thanks in advance for support

It's almost a straight swap:

Edit: Include connection:

Snap094

thank you very much for support, if I understand good when I want to skip get data button it will work in below form?
image
and for rest of sliders the same way?
but one question - from where program will know that block get value will have data from Switch_mc63 and not from Switch_mc64 for example? on this time this is black magic for me but I go step by smal step forward, especially thanks for your help :slight_smile:

combination for Switchmc63 is working but in which way I should make the same for label ....
image

I wouldn't put the Get Value Block in Screen Initialise - it could fail there. Call a Procedure, as the last Block in Screen Initialise.

Via the Tag.

Can you use App Inventor's Blocks Image capture please - your snapshots are difficult to read!

Right-click in the Blocks work space and select "Download Blocks as Image".

download blocks