Not receiving value from arduino

Hello.
I used HC-06 at Arduino to code to send the value to App Inventor.
HC-06 is experiencing problems connecting normally but not delivering values.
Can you tell me the problem by looking at the code?
Arduino Code
#include <SoftwareSerial.h>

#include <HX711.h>

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

const int LOADCELL_DOUT_PIN_1 = 2;

const int LOADCELL_SCK_PIN_1 = 3;

const int LOADCELL_DOUT_PIN_2 = 4;

const int LOADCELL_SCK_PIN_2 = 5;

HX711 scale_1;

HX711 scale_2;

LiquidCrystal_I2C lcd(0x27, 16, 2);

SoftwareSerial hc06(7, 8);

void setup() {

Serial.begin(115200);

Serial.println("HX711 Demo");

lcd.init();

lcd.backlight();

hc06.begin(9600);

Serial.println("Initializing the scales");

scale_1.begin(LOADCELL_DOUT_PIN_1, LOADCELL_SCK_PIN_1);

scale_1.set_scale(-10600.00);

scale_1.tare();

scale_2.begin(LOADCELL_DOUT_PIN_2, LOADCELL_SCK_PIN_2);

scale_2.set_scale(-10600.00);

scale_2.tare();

}

void loop() {

float weight_kg_1 = scale_1.get_units() * 0.453592;

float weight_kg_2 = scale_2.get_units() * 0.453592;

Serial.print("Scale 1 reading:\t");

Serial.println(weight_kg_1, 1);

Serial.print("Scale 2 reading:\t");

Serial.println(weight_kg_2, 1);

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Scale 1: ");

lcd.print(weight_kg_1, 1);

lcd.print(" kg");

lcd.setCursor(0, 1);

lcd.print("Scale 2: ");

lcd.print(weight_kg_2, 1);

lcd.print(" kg");

hc06.println(weight_kg_1);

hc06.println(weight_kg_2);

delay(1000);

}




image

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.

Thank you for your answer.
However, I reply because there are some things that I don't understand and some difficulties.
I was told to use println at the end when you answered first, but I didn't understand exactly how to put it in.
If there are two values to be transmitted from Hc-06 to AI2,
hc06.print(weight_kg_2);
hc06.println(weight_kg_1);
I'm asking you again because it's ambiguous if you asked me to write it like this.
Also, check on ClockTimer I wanted to double-check if it was wrong because the block you showed me seemed to be in the block I wrote.
Can you show me an example?

a1_copy.aia (68.0 KB)
First, I will upload the file I wrote as well.

A canned response

If you want to send your temperature and humidity on separate lines, you could alternatively send tag:value pairs, like
T:98.6\n
H:100\n
which would arrive individually if you use Delimiter 10 (\n).
Your AI2 logic would look like

if BT.bytesAvailable > 0 then
  set local message to BT.ReadText(-1)   (to get only 1 line)
  if contains(message, ":") then
      set local splits to split message at ":"
      If (select item 1 of splits) = "T" then
         set LabelTemperature.Text to (select item 2 of splits)
      else if (select item 1 of splits) = "H" then
         set LabelHumidity.Text to (select item 2 of splits)
     else set LabelWhatHappenned to local message
end if

Should be:
hc06.print(weight_kg_1);
hc06.print(',');
hc06.println(weight_kg_2);

if you want to use the comma-delimitted pair message format.
Each message arriving at AI2 will contain both readings, with a comma between them.

These blocks would work with that message format:


blocks

I did not bother propagating the values to your other global variables, since their single letter names are a mystery.

Thank you for your help!
But my problem was something else. I made a new project file, and using the same code made communication easier.
Maybe it's just a file problem
Have a nice day. :slight_smile:

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