My Android phone keeps crashing whenever I try to connect to MIT by using APK file

sketch_aug27a.ino (1.3 KB)
REAL_DEAL.aia (4.8 KB)

These are the codes for both MIT and Arduino. Ide C++. this is for an upcoming competition, help is really appreciated :slight_smile: (I have already doubled check my Esp's connection, it seems to connect fine to my phone)

On first glance, replace


with

Clock1.Timer has loads of mistakes

Your sketch:

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

// Ultrasonic sensor pins
const int trigPin = 26;
const int echoPin = 25;

// Speed of sound in cm/µs
const float SOUND_SPEED_CM_US = 0.0343;

// Detection distance in cm
const float DETECTION_THRESHOLD_CM = 7.0;

String lastSentState = "";

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32_Ultrasonic");  // Bluetooth name
  Serial.println("Bluetooth Started. Waiting for connection...");

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Send a 10 µs pulse to trigger pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure echo pulse duration
  long duration = pulseIn(echoPin, HIGH);

  // Convert duration to distance
  float distanceCm = (duration * SOUND_SPEED_CM_US) / 2.0;

  // Determine object state
  String currentState;
  if (distanceCm > 0 && distanceCm <= DETECTION_THRESHOLD_CM) {
    currentState = "Object detected";
  } else {
    currentState = "No object";
  }

  // Send only if the state changes
  if (currentState != lastSentState) {
    SerialBT.println(currentState);  // Send text to MIT app
    Serial.println("Sent to MIT App: " + currentState);
    lastSentState = currentState;
  }

  delay(500); // Wait before next reading
}

Thank you for your reply! may I know where's the mistake so I can make changed to my block code?

Your sketch sends text messages several words long.

Your app expects characters 1 or 0.

You left an empty socket.

Who wrote the sketch?

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.

Here is a simple BlueTooth text receiver sample, for single value per line:
blocks
initialize global message to

...

thank you so much! My app is working fine now and my phone is not crashing anymore

This is my first time using MIT so I'm still trying to get the hang of it :slight_smile:

For the /n problem

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