App is not working properly. can anyone pls help?

BT_Control_4Load.aia (294.4 KB)

WE ARE MAKING SMART EXTENSION BOARD. THROUGH APP INVENTOR, WE ARE MAKING THE APP TO CONTROL THE BLUETOOTH MODULE HCO5. BUT THE APP IS NOT WORKING, THE APP IS GETTING CONNECTED TO THE BLUETOOTH MODULE, BUT THE FUNCTIONS ARE NOT WORKING, PLS HELP US AS OUR SUBMISSION DATE IS NEARBY.

Post your sketch also, so we can verify it agrees with the AI2 app.

In the meantime see

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.

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.

#include <EEPROM.h>
#include <SoftwareSerial.h>
SoftwareSerial BT_Serial(2, 3); // RX, TX

#define Relay1 4 // Load1 Pin Out
#define Relay2 5 // Load2 Pin Out
#define Relay3 6 // Load3 Pin Out
#define Relay4 7 // Load4 Pin Out

char bt_data; // variable to receive data from the serial port
int load1, load2, load3, load4, power;

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

pinMode(Relay1, OUTPUT); digitalWrite(Relay1, 1);
pinMode(Relay2, OUTPUT); digitalWrite(Relay2, 1);
pinMode(Relay3, OUTPUT); digitalWrite(Relay3, 1);
pinMode(Relay4, OUTPUT); digitalWrite(Relay4, 1);

load1 = EEPROM.read(1);
load2 = EEPROM.read(2);
load3 = EEPROM.read(3);
load4 = EEPROM.read(4);

power = EEPROM.read(5);
delay(500);
}

void loop() {
if(BT_Serial.available()>0){bt_data = BT_Serial.read();}

if(bt_data == 'A'){load1=0;EEPROM.write(1, load1);}
if(bt_data == 'a'){load1=1;EEPROM.write(1, load1);}

if(bt_data == 'B'){load2=0;EEPROM.write(2, load2);}
if(bt_data == 'b'){load2=1;EEPROM.write(2, load2);}

if(bt_data == 'C'){load3=0;EEPROM.write(3, load3);}
if(bt_data == 'c'){load3=1;EEPROM.write(3, load3);}

if(bt_data == 'D'){load4=0;EEPROM.write(4, load4);}
if(bt_data == 'd'){load4=1;EEPROM.write(4, load4);}

if(bt_data == 'E'){power=0;EEPROM.write(5, power);}
if(bt_data == 'e'){power=1;EEPROM.write(5, power);}

bt_data = '0';

if(power==1){
digitalWrite(Relay1, 1);
digitalWrite(Relay2, 1);
digitalWrite(Relay3, 1);
digitalWrite(Relay4, 1);
}else{
digitalWrite(Relay1, load1);
digitalWrite(Relay2, load2);
digitalWrite(Relay3, load3);
digitalWrite(Relay4, load4);
}

BT_Serial.print(power); //send distance to MIT App
BT_Serial.print(";");
BT_Serial.print(load1); //send distance to MIT App
BT_Serial.print(";");
BT_Serial.print(load2); //send distance to MIT App
BT_Serial.print(";");
BT_Serial.print(load3); //send distance to MIT App
BT_Serial.print(";");
BT_Serial.print(load4); //send distance to MIT App
BT_Serial.println(";");

delay(500);
}

Your sketch is sending LF delimited messages, and each message separates it data with ';'.

That's good.

Now go apply my Delimiter advice in the app.