Even when im using "BTclient_test6.aia" by @ChrisWard to try connecting my android phone to HC-05, i still get the error 507 : Unable to connect. Is the device turned on?". Help:((
That project was specific to someone else's setup. Possibly included code not suited to your setup. Tell us what your setup is.
Make, Model, Version of sending device
Make, Model, Version of receiving device
What data are you sending/receiving and how often
i am making an app that used as a control interface and receive trash weight data. The app has 2 screen where the first screen is for connection and 2nd screen is for the control interface and weight data shown.
Switching Screens breaks Bluetooth connections.
Instead of multiple screens, use stacked Vertical Arrangements, only one visible at a time.
Post your sketch code for good measure.
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:
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.
...
This is my updated block and my sketch code.
UFTC (1).aia (14.6 KB)
#include <AFMotor.h>
#include <SoftwareSerial.h>
#include "HX711.h"
// HC-05 on A0/A1
SoftwareSerial BT(A1, A0); // RX, TX
// Motor shield
AF_DCMotor motor1(1); // Left wheel
AF_DCMotor motor2(2); // Right wheel
AF_DCMotor motor3(3); // Conveyor
// HX711 pins
#define HX711_DOUT_PIN A2
#define HX711_SCK_PIN A3
HX711 scale;
// Speeds & timing
const int moveSpeed = 200;
const int conveyorSpeed = 180;
const int rampDelay = 10;
// ─────────────────────────────────────────────────
// *** ADJUST this calibration factor to match your cell! ***
float calibration_factor = 2280.0;
// ─────────────────────────────────────────────────
void setup() {
Serial.begin(9600);
BT.begin(9600);
Serial.println("System Ready.");
// Init load cell
scale.begin(HX711_DOUT_PIN, HX711_SCK_PIN);
scale.set_scale(calibration_factor);
scale.tare();
Serial.println("HX711 tared.");
// Conveyor on at startup
motor3.setSpeed(conveyorSpeed);
motor3.run(FORWARD);
Serial.println("Conveyor running.");
stopMovement();
}
void loop() {
// ——————— Bluetooth input ———————
if (BT.available()) {
String btData = BT.readStringUntil('\n');
btData.trim();
Serial.print("BT: "); Serial.println(btData);
if (btData == "REQUEST_DATA") {
sendWeight();
} else if (btData.length() > 0) {
handleCommand(btData.charAt(0));
}
}
// ——————— Serial Monitor input ———————
if (Serial.available()) {
String inData = Serial.readStringUntil('\n');
inData.trim();
Serial.print("Serial: "); Serial.println(inData);
if (inData == "REQUEST_DATA") {
sendWeight();
} else if (inData.length() > 0) {
handleCommand(inData.charAt(0));
}
}
}
// ——————— Command dispatcher ———————
void handleCommand(char cmd) {
switch (cmd) {
case 'F': moveForward(); break;
case 'B': moveBackward(); break;
case 'L': turnLeft(); break;
case 'R': turnRight(); break;
case 'S': stopAll(); break;
default: Serial.println("Invalid command"); break;
}
}
// ——————— Send weight over Bluetooth/Serial ———————
void sendWeight() {
float w = scale.get_units(10); // average of 10 readings
BT.print ("WEIGHT=");
BT.println(w, 2);
Serial.print ("WEIGHT=");
Serial.println(w, 2);
}
// ——————— Movement routines ———————
void moveForward() {
motor1.run(FORWARD); motor2.run(FORWARD);
for (int s = 0; s <= moveSpeed; s += 10) {
motor1.setSpeed(s);
motor2.setSpeed(s);
delay(rampDelay);
}
}
void moveBackward() {
motor1.run(BACKWARD); motor2.run(BACKWARD);
for (int s = 0; s <= moveSpeed; s += 10) {
motor1.setSpeed(s);
motor2.setSpeed(s);
delay(rampDelay);
}
}
void turnLeft() {
motor1.run(FORWARD); motor2.run(RELEASE);
for (int s = 0; s <= moveSpeed; s += 10) {
motor1.setSpeed(s);
delay(rampDelay);
}
}
void turnRight() {
motor1.run(RELEASE); motor2.run(FORWARD);
for (int s = 0; s <= moveSpeed; s += 10) {
motor2.setSpeed(s);
delay(rampDelay);
}
}
void stopMovement() {
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void stopAll() {
stopMovement();
motor3.run(RELEASE);
Serial.println("All motors & conveyor stopped.");
}
This is from my phone, hours until I get to my PC.
You have duplicate events that you need to be combined.
Look for the red X marks.
You test bytes available wrong.
You show incoming data wrong.
Here is a simple BlueTooth text receiver sample, for single value per line:
In addition to ALL that @ABG and @ChrisWard have already said, please be sure that the pins A0 and A1 are suitable for the serial (BT) transmission. Typically the pins that can be used for serial transmission are those with PWM capabilities, like pins 2,3 or 9,10 in a UNO Arduino board.
Another hint is: be sure that you have crossed the Tx and Rx pins between the Arduino board and the HC05.
Lastly, before getting crazy with the AI2 code, try to use the Serial Bluetooth Terminal app (free on Playstore). By using that app you'll be capable to debug the complete Arduino side and, once you are done with that, you can focus your attention to the AI2 app.
Best wishes.
i also want to ask is my bluetooth connection part of the block is correct?
At a first glance, In your AI2 app you have two clocks, and this is wrong: you need only one. Also take care that when you send a BT command from app to Arduino (Hc05) the answer isn't immediate. So in the same set of blocks that sends the command you cannot wait for the answer. Typically it's better to allocate the command sending to a button, and leave the receiving of the answers to the clock.
Furthermore as already said by @ABG, be aware that when you switch between screens, the BT comm's ceases to work.
At the end, my suggestion, apart of using the Serial BT Terminal to have only one variable in your system (i.e. the SBTerminal works for sure, therefore the uncertainty remains allocated only into the Arduino+Hc05 side) is to follow, as a second step, @ABG's example, which is simple and higjly reliable.
Best wishes.