Bluetooth data between Arduino and App

Hey ! I'm doing a project for school. I have a Grove SoundSensor and I need to send some data to my App Inventor. Here is my Arduino code :

#include <Wire.h>
#include "rgb_lcd.h"
#include <SoftwareSerial.h>
rgb_lcd lcd;
SoftwareSerial bluetooth(10, 11); // RX, TX pins
const int pinSound = A1;
const int pinLed = 2;
int thresholdValue = 80;

void setup()
{
  pinMode (pinLed, OUTPUT);
  Serial.begin(9600);
  bluetooth.begin(9600);
  lcd.begin(16, 2);
  lcd.setRGB(0,255,0);
}

void loop()
{
    int sensorValue = analogRead(pinSound);
    int mappedValue = map(sensorValue, 0, 1023, 0, 255);
    Serial.println(mappedValue);
    
    // Envoyer les données via Bluetooth
    bluetooth.print(mappedValue); // Utilisez print() au lieu de write() pour envoyer une chaîne de caractères
    
    if (sensorValue > thresholdValue)
    {
      digitalWrite(pinLed, HIGH);
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print(mappedValue); // Affichez correctement la valeur sur l'écran LCD
    }
    else
    {
      digitalWrite(pinLed, LOW);
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print(mappedValue); // Affichez correctement la valeur sur l'écran LCD
    }

    if (sensorValue > 100)
    {
      bluetooth.print("STOP");
    };
    delay(1000);
}

So I need to send the sound values to my App. And then it will display on the screen the sound values. And when the value is bigger than 100 the phone need to vibrate. So I started to try something and I have this :

But I have 2 problems. Firstly, I don't have the first digit of sound values displayed (e.g. : I have 161 but it only display 61, I have 45 it only display 5). Secondly, my phone don't vibrate when the sound values are bigger than 100.

Could someone help me about this please ?

Thank you,

Arthur

Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

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.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.

1 Like

Dear @arthur_j, in addition to ALL @abg has already said: i.e. use the 0x0A as terminator, use println(); to send data from Arduino to the app and change accordingly the receiving block
image
with
image
Moreover, please pay attention to the comparison block:
image
The blue blocks are for mathematical operations, while you are comparing texts, therefore the correct one is:
image

Bonne chance! :hugs:

2 Likes

Hey ! Thanks you for your help it worked for me !

1 Like

Heureux qu'il marche nickel pour toi !
A la prochaine.
:+1:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.