Mit app inventor bluetooth transfer data problems

Hi.

i have a problem with receiving data on my phone via bluetooth HC 05. I'm using arduino uno with proximty sensor. Code is written and it gives a value 0 or 1, depends on output value. It could be also stop or go. I use bluetoothclient ReceiveTextNumberOfBytes function, but there is no errror and no data. Any ideas?

thanks,
Stojan

Arduino code :

void setup()
{
  pinMode(13,OUTPUT);
  pinMode(3,INPUT);
  Serial.begin(9600);
}
void loop()
{
  if (digitalRead(3)== LOW)
  {
    digitalWrite(13,HIGH);
    Serial.println("0");  
    delay(10);
  }
  else 
  {
    
    digitalWrite(13,LOW);
    Serial.println("1"); 
    delay(10);
    
  }
  
}

Show your (relevant) blocks.

Hi

  1. Do not use "ReceiveTextNumberOfBytes"
  2. Do not use "delay()" inside loop(), that essentially pauses everything and the sensor data may become garbage.

Edit:

1a) As you are only receiving one value, you can do that manually via a button or periodically via a Clock Timer:

2a) The best way to "delay" inside the loop() is to use elapsed milliseconds:

//ArduinoToApp.ino  Chris Ward 15/10/2021 10:23:03

//Fake data stream to test App. Classic Bluetooth
//Modified from 1 to 2 vars

//vars
unsigned long lgUpdateTime;
int   iVal = 50;
float fVal = 3.14159265358979

void setup()
{
         Serial.begin(9600);
         lgUpdateTime = millis();
}

void loop()
{
	     if(millis() - lgUpdateTime > 6000)              //Loop approx every 6 seconds
         {
               lgUpdateTime = millis();

               if (Serial.connected())
               {
                     //To App via Bluetooth
                                  iVal = iVal + 2;
                     Serial.print(iVal);
                     Serial.print('|');               //Value Seperator
                                  fVal = fVal + 0.2;
                     Serial.print(fVal,4);
                     Serial.println();                //This tells App "End of Data"
                                                      //= Ascii LineFeed Char Num 10
               }
         }
}
1 Like

Dear @Stojan_Humar,
there are many things that we need to know before being in degree to help you.
@Anke has already asked the relevant blocks, as far as the AI2 is concerned, in addition, also in your Arduino code I have some doubts.
The HC05 is connected to pins 0 and 1 ? Because you use the Serial.println() function that addresses those pins. Have you reversed the pins between HC05 and Uno ? (i.e. Uno Tx pin to HC05 Rx pin and visa versa). Are you sure that the HC05 baudrate is effectively 9600 baud ? Are you aware that your Arduino code is transmitting every 10 millisecond a '1', unless the sensor generates an input on pin 3 ? Do you really need such fast updating rate: once you'll be sure that your Arduino code is working fine, the AI2 app will have to be capable to withstand with such speed!
So, before going deeper with your app, please verify that your Arduino board and code are working as you expect. To this purpose install on your Android device a free app capable to receive everything on the BT line such as "Serial Bluetooth Terminal". You can download it free from google playstore. Once done with that then you are sure that your Arduino is sending the correct data and you can then focus on the app.

PS while I was writing my answer I've seen that also @ChrisWard has given some suggestions :grimacing: :grimacing: :grimacing:

1 Like

.. Also, make sure your App knows which character is used to indicate the end of the incoming data (annoyingly known as the Delimiter Byte in App Inventor):

The easiest to use is Ascii LineFeed = Char Num 10

Thanks for all advices. Problem was with Rx and Tx connections. Now i have to optimize code as ChrisWard said, to avoid delay inside loop.

Regards,
Stojan