Strings to Bytes

Hi Friends,
So, I'm trying to receive a string from an Arduino UNO with an HC-05 module. Basically, based on a certain condition, the Arduino will send either "opened" or "closed" as a string to the MITAI app. I want it to display the string that comes through in a label, but I can't figure out how to receive the string because it's asking me for the number of bytes, and I don't know what that is...
image
Thank you,
:heart:

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.

1 Like

Thank you so much for your reply!
However, I'm 16, I've never been properly educated in things like this, and I don't know what a lot of what you said means, I'm sorry for being really annoying about it, but I need an explanation in really dumb terms. Also, I looked at the article, and the part 2 of the article, and the second part is the part that I need, but it was extremely overwhelming because there was so much to look at at the same time and even reading the explanations was not helping me understand what it meant. I think I have what I need for the sending information to the Arduino, now I need to receive a string of letters from the Arduino and based on what it sends back will change what the app does. I don't know how bytes work and I don't even know where to look on google for help. I've tried, at this point, I'm ready to give up and just fail the project. But, I really don't want to do that seeing as I've been working on this for almost two years and I feel like I'm so close.

Post your Arduino code here.

Also,

Here is an updated blocks sample illustrating these ideas ...

BlueTooth_delimiter_sample.aia (3.4 KB) global message

1 Like

/*
BTArduino sketch
By Edward Mitchell
http://appinventor.pevest.com

Description:
Uses the JY-MCU Bluetooth module to enable an Arduino board to communicate over Bluetooth
wireless to an Android app written in MIT App Inventor 2, running on an Android device.

Credit:
This Arduino code based very loosely on the original Arduino code for the JY-MCU Bluetooth module tutorial at
http://robotosh.blogspot.com/2012/07/arduino-jy-mcu-bluetooth.html

A tutorial for using this code with App Inventor 2, is available at the appinventor.pevest.com
web site. Please refer to the tutorial for information and source code for the Android side of this sketch.

*/
#include <SoftwareSerial.h>

// Declarations
int cmd = 0; // Stores the next byte of incoming data, which is a "command" to do something
char Str1 = "closed"; // Stores the 2nd byte, which is the command parameter
char Str2 = "opened";
int input = 0;
int x = 100;

SoftwareSerial bluetooth(10, 9);

// Initialization
void setup() {
bluetooth.begin(9600); // start bluetooth communication at 9600bps
//pinMode(switch1, INPUT_PULLUP);
Serial.begin(9600);
}

// Arduino Execution Loop
void loop() {
input = (analogRead(A0)) / x;
if ( bluetooth.available() ) // if data is available to read
{
cmd = bluetooth.read(); // read it and store it in 'cmd'
}
if (cmd == "1")
{
if (input > '5') {
bluetooth.write (Str1);
}
else {
bluetooth.write (Str2);
}
}
Serial.println(input);
delay(5000);
};

Thank you

Replace

with

if (input > '5') {
bluetooth.println (Str1);
}
else {
bluetooth.println (Str2);
}

to get line feeds after your messages.

For your simple text messages all you need is

1 Like

So, here is the AI2 code, as well, it's still not working, but the label changed from its default to just blank, which is more than it was doing before. So thank you so much for what you've done for me already.
image
image
image
image
image

Upload your new .ino file and your new .aia export here.

AI2 isn't letting me export it as a .aia file, it automatically makes it a pdf and pdf doesn't support it, so the best I have is screenshots

#include <SoftwareSerial.h>

// Declarations
int cmd = 0; // Stores the next byte of incoming data, which is a "command" to do something
char Str1 = "closed"; // Stores the 2nd byte, which is the command parameter
char Str2 = "opened";
int input = 0;
int x = 100;

SoftwareSerial bluetooth(10, 9);

// Initialization
void setup() {
bluetooth.begin(9600); // start bluetooth communication at 9600bps
Serial.begin(9600);
}

// Arduino Execution Loop
void loop() {
input = (analogRead(A0)) / x;
if ( bluetooth.available() ) // if data is available to read
{
cmd = bluetooth.read(); // read it and store it in 'cmd'
}
if (cmd == "1")
{
if (input > '5') {
bluetooth.println (Str1);
}
else {
bluetooth.println (Str2);
}
}
Serial.println(input);
delay(5000);
};

Go into your Web browser settings to make it ask where to save downloads.
Which web browser do you use?

The Delimiter setting of your BlueTooth component in the Designer is important, and your blocks don't show it.

Also, try eliminating the spaces before (str1) and (str2) in

if (input > 5) {
bluetooth.println(Str1);
}
else {
bluetooth.println(Str2);
}

I am not a C expert, but that '5' could be replaced with 5 to avoid switching that > comparison from a numeric comparison to a text comparison.

Real_Project_copy.aia (6.4 KB)
Ok, I think this is the right thing.

I can't vouch for your C code.

You are mixing data types in a lot of places, and I am unwilling to bear the burden of learning C data type coercion. Let some one with more C battle scars delve into that.

Regarding your AI2 input stream processing, I suggest changing


into

to avoid obscure AI2 update rules involving screen components.

Ok!
Will do👍