Bluetooth receive text from Arduino

I'm using HC-05 bluetooth with Arduino and want to show values from sensors. Using this code on Arduino.

//#include <SPI.h>
#include <Wire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Addresses of 4 DS18B20s Change these to match temp sensors used

uint8_t sensor1[8] = { 0x28, 0x73, 0x7D, 0x7D, 0x08, 0x00, 0x00, 0x4E };//engine------------sensor #29
uint8_t sensor2[8] = { 0x28, 0x11, 0xAD, 0x6D, 0x08, 0x00, 0x00, 0x85 };//transmission #1----sensor #28
uint8_t sensor3[8] = { 0x28, 0xFF, 0x5F, 0x98, 0xA1, 0x15, 0x04, 0x60 };//transmission #2
uint8_t sensor4[8] = { 0x28, 0xFF, 0x11, 0x57, 0x73, 0x15, 0x01, 0xE1 };//outside
int lvl;
void setup() {
Serial.begin(9600);
#define TEMPERATURE_PRECISION 10
sensors.begin();
sensors.setResolution(sensor1, TEMPERATURE_PRECISION);
sensors.setResolution(sensor2, TEMPERATURE_PRECISION);
// void printTemperature(DeviceAddress deviceAddress)
}

void loop() {

//++++++++++++++++++++++++++++Temperature

float temperature1 = sensors.getTempF(sensor1);
float temperature2 = sensors.getTempF(sensor2);
float temperature3 = sensors.getTempF(sensor3);
float temperature4 = sensors.getTempF(sensor4);

//++++++++++++++++++++++++++++Pressure
int sensorVal1 = analogRead(A0);//air pressure 1
int sensorVal2 = analogRead(A1);//oil pressure

float voltage1 = (sensorVal1 * 5.0) / 1024.0;
float voltage2 = (sensorVal2 * 5.0) / 1024.0;

float pressure_pascal1 = (3.0 * ((float)voltage1 - 0.47)) * 1000000.0;
float pressure_pascal2 = (3.0 * ((float)voltage2 - 0.47)) * 1000000.0;

float pressure_psi1 = pressure_pascal1 * 0.00001450377;
float pressure_psi2 = pressure_pascal2 * 0.00001450377;

//++++++++++++++++++++++++++++Pressure Print

if (pressure_psi1 <= 1) {
Serial.print("0");
} else

Serial.print(pressure_psi1, 1);

//Serial.println(" psi 1");
//Serial.print("|");

Serial.print("|");

if (pressure_psi2 <= 1) {
Serial.print("0");
} else
Serial.print(pressure_psi2, 1);
Serial.print("|");
//Serial.println(" psi 2");

//++++++++++++++++++++++++++++Temperature Print
sensors.requestTemperatures();
Serial.print(temperature1, 0);
Serial.print("|");
Serial.print(temperature2, 0);
Serial.print("|");
Serial.print(temperature3, 0);
Serial.print("|");
Serial.println(temperature4, 0);
// Serial.println("|");
// Serial.println(lvl);
delay(2000);
}
Everything works as it should on the Arduino serial monitor. Values change according to sensor changes. I have no problem connecting to bluetooth, I've done it many times.
Using these blocks.


aia for above
From_2_sensorsWebsite.aia (8.3 KB)
When I open the app I get the readings, but only once, it doesn't change with the readings from the Arduino serial screen.
Sometimes I get this error.

Sometimes I get more readings than there are boxes for

This looks like the standard BlueTooth Delimiter problem, so here is the standard advice:

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. 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.

After again reading the suggestions I've changed the arduino code to this

//++++++++++++++++++++++++++++Pressure Print

if (pressure_psi1 <= 1) {
Serial.println("0");
} else
Serial.println(pressure_psi1, 1);

if (pressure_psi2 <= 1) {
Serial.println("0");
} else
Serial.println(pressure_psi2, 1);

Serial.println(pressure_psi3, 1);

//++++++++++++++++++++++++++++Temperature Print
sensors.requestTemperatures();
Serial.println(temperature1, 0);
Serial.println(temperature2, 0);
Serial.println(temperature3, 0);
Serial.println(temperature4, 0);

delay(2000);
}
And getting this on the serial screen.
serial

and using these blocks.
I now show no data


aia
From_2_sensorsWebsite_3.aia (9.1 KB)

I see in your AI2 code you are checking your BlueTooth input for the literal text constants

  • "pressure_psi1"
  • "pressure_psi2"
  • ... "temperature4"

but I don't see any place in your transmission code where you actually send those pieces of text to label your data. You only send the numbers.

In the bottom case of your if/then/elseif ladder, you assign the variable list to a Label, but I don't see in your AI2 code any place where you load that variable from the incoming data, so you always show nothing.

1 Like

We need to know the model of the Arduino Richard. It most likely needs to use the SoftwareSerial Library to send data to the App.

Can you upload your Sketch? Change the ext from .ino to .txt

Well actually, try this basic setup. It sends the four temperatures to both the PC Arduino Serial Monitor via USB and the App via Bluetooth HC05. Note, I do not know which Arduino you are using nor which pins the HC05 is connected to (typically 10, 11). So you may need to change those two values in the Sketch.

The App Inventor Project includes a test to ensure Bluetooth is switched on. Depending on your Android version (10+), Location will need to be turned on too (before running the App). Test the App as an APK, it will not work in the Companion. What is going on should be clear from the comments in both documents.

Temperatures.txt (3.0 KB)

BT_Receive_4_Vals.aia (8.8 KB)

ABG and Chris, it turns out the problem was my HC-05 module.
Chris, your

temperatures

aia file doesn't really work, the values never change. Also the

aia file only shows values in the first two boxes.

What did work for me, with a different HC-05 module, is this arduino file. These are the first things I posted with some slight appearance changes. I'm using a Nano which is basically a Uno.
MyAppInventorApp2.txt (2.6 KB)
with this aia
MyAppInventorApp2.aia (9.8 KB)
Probably some safety things missing but, values change as they should and show in the correct boxes.

Thank you, D_ck

(The Sketch, not aia) It does require to be matched-up to the hardware.......

That is an issue from the Arduino/Sketch end.

Well that's what we want to hear at the end of the day! :grin: