Hello
I am working on this
What I want to get is the label 3 with the value received from arduino
The output i am having is the label 3 with more than a value
/*
solo trasmissione a modulo BT
test di comunicazione con telefono via BT e app testing
*/
#include <SoftwareSerial.h>//per la comuncazione con modulo BT
//definisco pin RX e TX da Arduino per modulo BT
#define BT_RX_PIN 4 // "RXD" Pin on BT HC05
#define BT_TX_PIN 5 // "TXD" Pin on BT HC05
SoftwareSerial BTSerial(BT_RX_PIN, BT_TX_PIN);//(RxD, TxD) modulo BT
unsigned long time_1; // will store last time LED was updated
unsigned long time_2; // will store last time LED was updated
long interval1;
long interval2;
byte count;
void setup()
{
Serial.begin(9600);
BTSerial.begin(9600);
interval1 = 1500;//verde
interval2 = 6000;//rosso
count = 1;
delay(2000);
}
void loop()
{
if (millis() >= time_1 + interval1)
{
time_1 += interval1;
count++;
BTSerial.println('0');
BTSerial.println(count);
Serial.println("0 to BT");
// Serial.println(count);
}
if (millis() >= time_2 + interval2)
{
time_2 += interval2;
count++;
BTSerial.println('1');
BTSerial.println(count);
Serial.println("1 to BT");
// Serial.println(count);
}
if(count >= 100)
{
count = 2;
}
}
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.
Hello S_Z. Just briefly looked at your codes. You are sending two values to the App, either a '0' and a 'count' or a '1' and a'count' which is why the data received arrives 'wonky'. Do you actually want the App to receive the count value?
In the attached modification of your Sketch I have simple changed it to only send the '0' or the '1' correctly. I have changed your App code to receive the data correctly. Both can be changed to include the count value if required.