0

I'm trying to make android applications with the hc 05 module and 3 ultrasonic sensors. It consists in the fact that each sensor sends information about the distance from the obstacle to the application, which is divided into 3 textviews and each of them displays information on how many cm are left. And here comes my problem that I have no idea how I could divide the data sent by the module to the application into 3 independent textview.

// arduino code
int LtriggerPin = 13;
int LechoPin = 12;  

int RtriggerPin = 11;
int RechoPin = 10;  

int CtriggerPin = 9;
int CechoPin = 8;  

int info = 0;
int state = 0;

void setup() { 

 Serial1.begin(9600);  

 pinMode(LtriggerPin, OUTPUT); 
 pinMode(LechoPin, INPUT);

 pinMode(RtriggerPin, OUTPUT); 
 pinMode(RechoPin, INPUT);

 pinMode(CtriggerPin, OUTPUT); 
 pinMode(CechoPin, INPUT);

 }

 void loop(){ 

    sensor();
 }




 void sensor() { 

 int durationL, distanceL;  
 int durationR, distanceR;   
 int durationC, distanceC;        

 digitalWrite(LtriggerPin, HIGH); 
 delay(10);
  digitalWrite(LtriggerPin, LOW);
 durationL = pulseIn(LechoPin, HIGH); 
 distanceL = (durationL/2) / 29.1; 

 digitalWrite(RtriggerPin, HIGH); 
 delay(10);
 digitalWrite(RtriggerPin, LOW);
 durationR = pulseIn(RechoPin, HIGH); 
  distanceR = (durationR/2) / 29.1; 

 digitalWrite(CtriggerPin, HIGH); 
 delay(10);
 digitalWrite(CtriggerPin, LOW);
 durationC = pulseIn(CechoPin, HIGH); 
 distanceC = (durationC/2) / 29.1; 


 Serial1.print("Left Sensor "); 
 Serial1.print((String) distanceL + " cm" ); 
 delay(500);   
 Serial1.println(" ");  

 Serial1.print("Right Sensor "); 
 Serial1.print((String) distanceR + " cm" ); 
 delay(500);   
 Serial1.println(" ");  

 Serial1.print("Center Sensor "); 
 Serial1.print((String) distanceC + " cm" ); 
 delay(500);   
 Serial1.println(" ");  
  Serial1.println(" "); 
 Serial1.println(" "); 
  }

/ / / / / Android Studio Code

 handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg){
            if(msg.what == MESSAGE_READ){
                String readMessage = null;
                readMessage = new String((byte[]) msg.obj, StandardCharsets.UTF_8);
                TvL.setText(readMessage);
                TvR.setText(readMessage);
                TvC.setText(readMessage);

            }

            if(msg.what == CONNECTING_STATUS){
                char[] sConnected;
                if(msg.arg1 == 1)
                    Tv3.setText(getString(R.string.BTConnected) + msg.obj);
                else
                    Tv3.setText(getString(R.string.BTconnFail));
            }
        }


    };





    @Override
public void run() {
    byte[] buffer = new byte[1024];  // buffer store for the stream
    int bytes; // bytes returned from read()
    // Keep listening to the InputStream until an exception occurs
    while (true) {
        try {
            // Read from the InputStream
            bytes = mmInStream.available();
            if(bytes != 0) {
                buffer = new byte[1024];
                SystemClock.sleep(100); //pause and wait for rest of data. Adjust this depending on your sending speed.
                bytes = mmInStream.available(); // how many bytes are ready to be read?
                bytes = mmInStream.read(buffer, 0, bytes); // record how many bytes we actually read
                hesler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget(); // Send the obtained bytes to the UI activity
            }
        } catch (IOException e) {
            e.printStackTrace();

            break;
        }
    }
}
Jezdi
  • 21
  • 6

1 Answers1

0

The problem with BT transmission is that the thread that receives the characters can deliver the message in arbitrary bursts. Therefore, it is necessary for the sender to mark the end of the message somehow. Here it can be the "\n" character. So I suggest change the sending of data from the arduino, for example, as follows:

Serial1.print("Left Sensor "); 
Serial1.println(distanceL); 
delay(500);

Serial1.print("Right Sensor "); 
Serial1.println(distanceR); 
delay(500);   

Serial1.print("Center Sensor "); 
Serial1.println(distanceC); 
delay(500);     

The program for receiving data from BT (which is not included) must be modified so that it stores the received characters in the buffer until the "\n" character arrives. Only when the complete line arrives should it be sent to the main thread, which will find out what information was sent by searching for keywords (Left Sensor, Right Sensor, Center Sensor) and then display it in the appropriate textbox

Update:

Modification your run method:

public void run() {
    byte[] buffer = new byte[1024];  // buffer store for the stream
    int c; // one byte from BT or -1 if end
    int position =0; //position in the line
    // Keep listening to the InputStream until an exception occurs
    while (true) {
        try {
            // Read from the InputStream
            c = mmInStream.read(); //read one byte from stream
            if(c == -1) {  //if end of stream
                break; // stop receive
            }
            if (c=='\n'){ //if end of line char arrives
                byte[] copyBuffer = Arrays.copyOf(buffer, position+1); //make copy of received line
                hesler.obtainMessage(MainActivity.MESSAGE_READ, position+1, -1, copyBuffer)
                        .sendToTarget(); // Send the obtained bytes to the UI activity
                position=0; //start save chars again from beginning 
            }
            buffer[position]=(byte) c; //store char to buffer
            if (position<1024) position++; //advance position for next char if not overflow buffer
            

        } catch (IOException e) {
            e.printStackTrace();

            break;
        }
    }
}
Peter Plesník
  • 524
  • 1
  • 2
  • 7