Reading sensor data via bluetooth with Arduino

I want to read sensor data via Arduino bluetooth, there are 8 sensor data, but I could not print the data to the part I wanted, is there a method, how can I do it?


Hello BAHA_Aydin, welcome to the forum.

In your Arduino Sketch, assemble to values into a delimited string, like so:

val1|val2|val3|val4|val5|val6|val7|val8

You can do that using Sprintf() which closes the string with a line feed char - ASCII 10, but note, there are only approx 20 bytes available in a Bluetooth packet for your data, so make sure you do not exceed that limit with your string (you can test a representative string on my website ProfessorCad: Tips & Tricks).

Tell your App what the BT Package delimiter (end of data marker) is:

In your App, as per your current Blocks, split the string received into a List. Next, check that there are 8 values in the List - only if there are, populate your labels/textboxes. You cannot call your List 'List' as this is a reserved word. Give it a meaningful name such as 'DataIn'.

You are not actually receiving all the data sent - 'Bytes available to receive' is just a flag, it is not necessarily the total bytes available. To get the total bytes, use math block -1.

Ensure that:

  1. The period of time to process the data in the App is set to be 20% shorter than the period of time used in the Sketch to send it (The Sketch Loop time).

  2. The Sketch Loop time should be controlled using the elapsed milliseconds method because the delay() function (used by so many examples on the internet :nauseated_face:) stops everything, which will make the sensor readings inaccurate.

Example:

//Fake data stream to demo elapsed milliseconds timing


//vars
unsigned long lgUpdateTime;

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

void loop()
{
	           if(millis() - lgUpdateTime > 1000)           //Loop approx every 1 second
               {
                         lgUpdateTime = millis();

                         //Bluetooth to App
                         Serial.print("Hello");
                         Serial.print("|");      //Value delimiter
                         Serial.print("World");
                         Serial.print("|");
                         Serial.println();        //This empty last line tells App "End of Data" = Ascii LineFeed Char Number 10
               }
}

  1. Do not stream your data too fast - It should be timed to be as slow as possible and satisfy the project goal. Streaming very fast into Labels is pointless because the human eye cannot tolerate such fast changes.

The data comes from the pots connected to the arduino.


I tried this before but it didn't show the data I wanted

(Canned Reply: ABG- Export & Upload .aia)
Export your .aia file and upload it here.
export_and_upload_aia

P.S. We only handle AI2 exports, not Kodular or other distros.

It is not the only thing to check - what about my other tips? If the App is not receiving the data, check the data sender -the Sketch. Do you have the Sketch sending the data from the Arduino correctly? Have you tried sending the raw values to the Arduino Serial Monitor?

What is the model name of your Arduino?

I can see the data on the serial monitor and I have tried the other tips but to no avail can you show me a complete block of code
Arduino Uno (Clone)

Post your .ino file here

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(2, 3);

const int potPin0 = A0;
const int potPin1 = A1;
const int potPin2 = A2;
const int potPin3 = A3;
const int potPin4 = A4;
const int potPin5 = A5;
const int potPin6 = A6;
const int potPin7 = A7;

void setup() {
  Serial.begin(9600);
  bluetooth.begin(9600);

  pinMode(potPin0, INPUT);
  pinMode(potPin1, INPUT);
  pinMode(potPin2, INPUT);
  pinMode(potPin3, INPUT);
  pinMode(potPin4, INPUT);
  pinMode(potPin5, INPUT);
  pinMode(potPin6, INPUT);
  pinMode(potPin7, INPUT);
}

void loop() {
  int potValue0 = analogRead(potPin0);
  int potValue1 = analogRead(potPin1);
  int potValue2 = analogRead(potPin2);
  int potValue3 = analogRead(potPin3);
  int potValue4 = analogRead(potPin4);
  int potValue5 = analogRead(potPin5);
  int potValue6 = analogRead(potPin6);
  int potValue7 = analogRead(potPin7);

  bluetooth.print("Pot 0: ");
  bluetooth.println(potValue0);
  
  bluetooth.print("Pot 1: ");
  bluetooth.println(potValue1);
  
  bluetooth.print("Pot 2: ");
  bluetooth.println(potValue2);
  
  bluetooth.print("Pot 3: ");
  bluetooth.println(potValue3);
  
  bluetooth.print("Pot 4: ");
  bluetooth.println(potValue4);
  
  bluetooth.print("Pot 5: ");
  bluetooth.println(potValue5);
  
  bluetooth.print("Pot 6: ");
  bluetooth.println(potValue6);
  
  bluetooth.print("Pot 7: ");
  bluetooth.println(potValue7);

  delay(100);
}

So there is your issue - you are not sending data to your App arranged the way the App is expecting to receive it - you have not used the "|" delimiter between values and you are decorating the values with names - making the Bluetooth packet too large and the data the App Receives nonsense. You are using delay() too, with a too short time period - so basically you have not followed my notes!

Also, usually UNO only provides analogRead() via pins A0 to A5......

So, try the sketch attached. Before you do, set Clock1 timer to 800 milliseconds and fix ALL App Inventor code as I have shown/noted above. Make sure App Inventor tests the length of the List before trying to populate the Labels/Textboxes.

GetData.ino (2.1 KB)

Actually, this code was my trial code, this is my actual code.

#include <SoftwareSerial.h> // Bluetooth modülü bağlantısı için tanımlamalar
#include <LiquidCrystal.h> //Ekran için tanımlama
#include <Servo.h> //Servo
#include <TinyGPS++.h> // TinyGPS++ kütüphanesi eklenmeli
LiquidCrystal lcd_1(18, 19, 20, 21, 22, 23); //Ekran için pin tanımlama
SoftwareSerial bluetoothSerial(24, 25); // RX, TX pimleri olarak belirtilen dijital pinler

// Röle kontrolü için tanımlamalar
const int role1 = 2;
const int role2 = 3;
const int role3 = 4;
const int role4 = 5;
const int role5 = 6;
const int role6 = 7;
const int role7 = 8;
const int role8 = 9;
const int role9 = 10;
const int role10 = 11;
const int role11 = 12;
const int role12 = 13;
const int role13 = 14;
const int role14 = 15;
const int role15 = 16;
const int role16 = 17;

// GPS modülünün bağlı olduğu pimler
const int gpsTxPin = 24; // GPS modülünün TX pini
const int gpsRxPin = 25; // GPS modülünün RX pini

// Potansiyometre bağlantı pini(Direksiyon)
const int potPin = A0;

// Servo nesnesi
Servo servo1;

// gps nesnesi oluşturulur
TinyGPSPlus gps;

void setup() {
  // Röle kontrol pini çıkış olarak ayarlanır
  pinMode(role1, OUTPUT);
  pinMode(role2, OUTPUT);
  pinMode(role3, OUTPUT);
  pinMode(role4, OUTPUT);
  pinMode(role5, OUTPUT);
  pinMode(role6, OUTPUT);
  pinMode(role7, OUTPUT);
  pinMode(role8, OUTPUT);
  pinMode(role9, OUTPUT);
  pinMode(role10, OUTPUT);
  pinMode(role11, OUTPUT);
  pinMode(role12, OUTPUT);
  pinMode(role13, OUTPUT);
  pinMode(role14, OUTPUT);
  pinMode(role15, OUTPUT);
  pinMode(role16, OUTPUT);

  // Bluetooth haberleşme hızı 9600 baud olarak ayarlanır
  // GPS modülü için seri iletişim başlatılır
  Serial.begin(9600);
  bluetoothSerial.begin(9600);

  // Servo pin bağlantısı
  servo1.attach(A1);

  // LCD başlatma
  lcd_1.begin(16,2);

  // Yükleme ekranı gösterimi(SİSTEM)
  lcd_1.print("Sistem Basliyor");
  lcd_1.setCursor(0, 1);
  for (int i = 0; i < 16; i++) {
    lcd_1.print("-");
    delay(100);
  }
  lcd_1.clear();
  delay(1000);
  lcd_1.print("Servolar");
  lcd_1.setCursor(0, 1);
  lcd_1.print("Ayarlaniyor...");
  // Servo başlangıç konumu
  servo1.write(90);

  delay(2000);

  // LCD ekranı temizleme
  lcd_1.clear();
  lcd_1.print("Sistem");
  lcd_1.setCursor(0, 1);
  lcd_1.print("Baslatildi");

  delay(2000);

  // LCD ekranı temizleme
  lcd_1.clear();
}

void loop() {
 // Gelen veriyi saklamak için bir değişken
  char receivedData;
    // Bluetooth verisi var mı diye kontrol edilir
    if (bluetoothSerial.available()) {
      // Bir karakter alınır ve receivedData değişkenine atanır
      receivedData = bluetoothSerial.read();
      // Alınan veriye göre röleyi kontrol eder
      switch (receivedData) {
        case 'A':
          digitalWrite(role1, HIGH); // Röleyi açar 1
          break;
        case 'B':
          digitalWrite(role1, LOW); // Röleyi kapatır 1
          break;
        case 'C':
          digitalWrite(role2, HIGH); // Röleyi Açar 2
          break;
        case 'Ç':
          digitalWrite(role2, LOW); // Röleyi kapatır 2
          break;
        case 'D':
          digitalWrite(role3, HIGH); // Röleyi Açar 3
          break;
        case 'E':
          digitalWrite(role3, LOW); // Röleyi kapatır 3
          break;
        case 'F':
          digitalWrite(role4, HIGH); // Röleyi Açar 4
          break;
        case 'G':
          digitalWrite(role4, LOW); // Röleyi kapatır 4
          break;
        case 'İ':
          digitalWrite(role5, HIGH); // Röleyi Açar 5
          break;
        case 'J':
          digitalWrite(role5, LOW); // Röleyi kapatır 5
          break;
        case 'K':
          digitalWrite(role6, HIGH); // Röleyi Açar 6
          break;
        case 'L':
          digitalWrite(role6, LOW); // Röleyi kapatır 6
          break;
        case 'M':
          digitalWrite(role7, HIGH); // Röleyi Açar 7
          break;
        case 'N':
          digitalWrite(role7, LOW); // Röleyi kapatır 7
          break;
        case 'O':
          digitalWrite(role8, HIGH); // Röleyi Açar 8
          break;
        case 'P':
          digitalWrite(role8, LOW); // Röleyi kapatır 8
          break;
        case 'R':
          digitalWrite(role9, HIGH); // Röleyi Açar 9
          break;
        case 'S':
          digitalWrite(role9, LOW); // Röleyi kapatır 9
          break;
        case 'T':
          digitalWrite(role10, HIGH); // Röleyi Açar 10
          break;
        case 'U':
          digitalWrite(role10, LOW); // Röleyi kapatır 10
          break;
        case 'V':
          digitalWrite(role11, HIGH); // Röleyi Açar 11
          break;
        case 'Y':
          digitalWrite(role11, LOW); // Röleyi kapatır 11
          break;
        case 'X':
          digitalWrite(role12, HIGH); // Röleyi Açar 12
          break;
        case 'W':
          digitalWrite(role12, LOW); // Röleyi kapatır 12
          break;
        case 'Z': //boş
          digitalWrite(role13, HIGH); // Röleyi Açar 13
          break;
        case 'a': //boş
          digitalWrite(role13, LOW); // Röleyi kapatır 13
          break;
        case 'b': //boş
          digitalWrite(role14, HIGH); // Röleyi Açar 14
          break;
        case 'c': //boş
          digitalWrite(role14, LOW); // Röleyi kapatır 14
          break;
        case 'd': //boş
          digitalWrite(role15, HIGH); // Röleyi Açar 15
          break;
        case 'e': // boş
          digitalWrite(role15, LOW); // Röleyi kapatır 15
          break;
        default:
          break;
        }
      }
  // Potansiyometre değerini oku
  int potValue = analogRead(potPin);

  // Potansiyometre değerini 0-180 arasında ölçekle
  int servoAngle = map(potValue, 0, 1023, 0, 180);

  // Servo motoru güncelle
  servo1.write(servoAngle);

  // Servo açısını ekrana yazdırma
  lcd_1.setCursor(0, 0);
  lcd_1.print("Pot Degeri: ");
  lcd_1.print(potValue);
  //Bluetooth to App
  bluetooth.print("potValue");  //Bluetooth to App send
  bluetooth.print("|");         //Value delimiter

  
  lcd_1.setCursor(0, 1);
  lcd_1.print("Donus Acisi: ");
  lcd_1.print(servoAngle);
  //Bluetooth to App
  bluetooth.print("servoAngle"); //Bluetooth to App send
  bluetooth.print("|");

  delay(50); // Biraz gecikme ekleyerek okumaları daha stabil hale getiriyoruz.

    // GPS verilerini okuma
  while (Serial.available() > 0) {
    if (gps.encode(Serial.read())) {
      // GPS verisi başarıyla okundu
      float latitude = gps.location.lat();
      float longitude = gps.location.lng();
      float altitude = gps.altitude.meters();
      float speed = gps.speed.kmph();

      // Ekranda gösterme
      lcd_1.println(latitude);//lat
      lcd_1.println(longitude);//long
      lcd_1.println(altitude);//ALt
      lcd_1.println(speed);//long
      lcd_1.print(speed, 0, 0);

      //Bluetooth to App send
      bluetooth.print("latitude");
      bluetooth.print("|");         //Value delimiter
      bluetooth.print("longitude");
      bluetooth.print("|");
      bluetooth.print("altitude");
      bluetooth.print("|");         //Value delimiter
      bluetooth.print("speed");
      bluetooth.println(); //end data
    }
  }
}

At the bottom there are data sends, the previous code was just a test to see if I could get the data correctly.
I think this code is correct
I will test my system soon, hopefully the data will come in

Well it's not correct, but you can fix it by observing the .ino file I wrote for you. That Sketch, 'actual code' is expecting data/commands from the App?

I created it by examining the .ino file you wrote. The purpose is to perform operations and send data according to data reception.

The .ino file that I wrote controls the time period (length of time between sends).

I can understand this, but can you show me how to send data temporarily in code?

You can use my GetData.ino as-is. Modify your App Blocks as I have shown - the List length should be 6. That will get you up and running with something that can be modified later.

1 Like

i will try

1 Like

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