You are using a New Line Delimiter, but your AI2 App lacks the Clock Timer to repeatedly check for incoming messages and post them.
Standard Delimiter 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.
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.
I have another problem is that if i send the text "T" from app inventor to arduino
When i print that text "T" from out by serial print, the serial monitor gives "W" instead of "T".
How can I fix this??
thank you for helping
You are not helping us to help you! Questions are asked for good reason - the devil is in the technical detail. Without knowing precisely what you want your App to do and the exact set-up, we can only guess.
I attach an improved Sketch, but I don't expect it to work without modification:
Your temperature sensor make, model (type) are unknown
The Sketch must be singing the same song as the App (ABG's notes).
#include<SoftwareSerial.h> //If you are using an Uno, it only has 1 port
char dataIn;
unsigned long lgUpdateTime;
int sensorValue;
void setup()
{
pinMode(A0, OUTPUT);
Serial.begin(9600); //Receive data from App
Serial1.begin(9600); //Write to Serial Monitor
lgUpdateTime = millis();
}
void loop()
{
//Excute loop every 1000 milliseconds, faster than the App sends to avoid buffer over-run
if ((millis() - lgUpdateTime) > 1000)
{
lgUpdateTime = millis();
if (Serial.available() > 0)
{
dataIn = Serial.read();
Serial1.println(dataIn);
}
switch (dataIn)
{
case 'T':
sensorValue = analogRead(A0);
Serial1.println('T');
Serial1.println(sensorValue);
break;
case 'W':
Serial1.println('W');
break;
}
}
}
Dear Chris,
if Cindy is using an UNO board as far as I know the PC monitor works only on the hardware serial, therefore on "Serial". Then, before being allowed to use the secondary line (i.e. Serial1) one shall instantiate the library as follows:
SoftwareSerial Serial1 = SoftwareSerial(10, 11); // Rx on pin 10 and Tx on pin 11 toward the (supposed ) BT shield.
Consequently the initialization should be something like here below:
Serial1.begin(9600); //Receive data from App
Serial.begin(9600); //Write to Serial Monitor
Apart of that @Cindy_tong in effect the information you have given are too few.
Have a great 2021 to all.
Ugo.
Sorry i missed some questions, my whole sketch are on the comment before (i don't know why i cannot upload .ino file
i uploaded my app inventor blocks as aia file at the above comment
my arduino board is MEGA2560
Bluetooth used is HC-06 model
my phone is android , Samsung S10+
Dear Cindy,
annexed you'll find two files:
the .aia contains a sample app that features two buttons whose value is sent via BT to an Arduino Mega board.
The .txt contains the respective receving code on the Arduino Mega. (As Chris said: .ino files are not allowed, if you want to upload them you shall convert to txt before).
I have developed and tested them for another guy of the forum (Ruben) so they work for sure.
Don't care if the core job of this couple of files is to switch on/off a relay: what you can easily do is to verify that everything is working, as far as the communication is concerned, then you can modify all that you want.
What you need to do before all is, anyway, to change in the app the physical address of the HC06 because the one that is written in my code is that of my HC06.
Also please be aware to put a resistor partition between the Tx pin of the Mega and the Rx pin of the HC06 in order to reduce the voltage to 3.3 V approx.
The third file contains some useful hints on BT shields.
Enjoy !!!
Ugo.
Thank you everyone for helping me
It turns out to be the problem of the Bluetooth HC06, everything works normally after I buy a new one are replace it.