Sending data from arduino to app via bluetooth


I would like to ask why my application is not working as I intended. The buzzer I am using is already on when the alarm button is pressed, but the labels for temperature and humidity in the application do not change according to the temperature read from the sensor. Additionally, my servo does not move when the open and close buttons are pressed. Can someone help me?

Here is the design of the application:

Here are the code blocks:

And this is the program for Arduino that I am using:

#include <DHT.h>
#include <DHT_U.h>
#include <SoftwareSerial.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

SoftwareSerial bt(8, 9); // RX, TX

// Definisi pin
#define BUZZER 3
#define SERVO 2

// Definisi sensor DHT22
#define DHTPIN 12
#define DHTTYPE DHT11

// Definisi LCD dengan modul I2C
#define I2C_ADDR 0x20 // Alamat LCD sesuai dengan yang Anda sebutkan
#define LCD_COLS 16   // Jumlah kolom LCD
#define LCD_ROWS 2    // Jumlah baris LCD
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);

// Inisiasi variabel
String text;
Servo myservo;

void setup() {
  pinMode(BUZZER, OUTPUT);
  myservo.attach(SERVO); // Menginisialisasi servo pada pin yang sesuai
  // Inisialisasi LCD

void loop() {
    // Membaca suhu dan kelembapan dari sensor DHT22
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Debugging: cek apakah pembacaan sensor berhasil
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    lcd.setCursor(6, 0);
    lcd.setCursor(10, 1);
  } else {
    // Menampilkan nilai suhu dan kelembapan pada LCD
    lcd.setCursor(0, 0);
    lcd.print(" C");
    lcd.setCursor(0, 1);

    bt.print(temperature); //send distance to MIT App
    bt.print(humidity); //send distance to MIT App 
  // Mengecek input serial Bluetooth
  while (Serial.available()) {
    char c =;
    text += c;

  if (text.length() > 0) {

    if (text == "OPEN") {
      // Untuk memutar servo 90 derajat ke kanan

    if (text == "CLOSE") {
      // Untuk memutar servo 90 derajat ke kiri

    if (text == "MALING") {
      // Untuk menyalakan buzzer
      digitalWrite(BUZZER, HIGH);
    } else {
      digitalWrite(BUZZER, LOW);

    text = "";
  delay(2000); // Delay untuk pengambilan data yang lebih lambat

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

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 @Varano_Tuga,
in addition to all that @ABG has already said, I would also suggest a couple of things.
First: in your Arduino code you check for the exact commands "OPEN" or "CLOSE" and so on: please be aware that the app is sending texts, therefore each command is terminated with a Linefeeed (0x0A), and this could foolish your check because the text composed with a += will contain also the Linefeed.
Second: instead of receiving 4 bytes (or more, depending on the float representation in Arduino) I normally convert the float value into a string, before transmitting it to the app. For example take a look here:

Obviously you have to change the receiving method into your app, by using the blocks able to do so
The -1 means that the app waits the incoming text until the Linefeed is received (as @Abg has already said) thus your last, and only the last, text transmitted by Arduino shall be made by using a println(). To accomplish this, the terminator character on app side shall be set to 0x0A i.e.10 decimal.
The second point is explained in detail in @abg canned response.

(If something is not clear) You can find a huge amount of such examples by digging the forum.
Best wishes.