0

Can anyone confirm this for me although I have tested it myself but when I send a string of data from my android app and send it to arduino say for example data = bluetoothPort.read(); inside an if statement then when I try to use the data in another if statement for example Serial.print(data); inside another if statement it will only show a blank line which let me believe that none of the data are saved. also when a receiving a string of data for example a string of abcde and send it to arduino for example data = bluetoothport.read() and then Serial.print(data) the serial monitor shows abcde as expected but what really happens is data has the value of a then prints then replace by b then print then replace by c then print then replace by d then print then replace by e. Am I wrong or somehow right? are there any work around or parts I need that will make me store data as a whole so that I can use it for other thread or a better code to store string of data?

#include <IRremote.h>
#include <SoftwareSerial.h>



SoftwareSerial bluetoothPort(4,5);
const int RECV_PIN = 12;
const int SEND_PIN = 13;

IRsend irsend;
IRrecv irrecv(RECV_PIN);

decode_results results;
char data;


void setup() 
{
   bluetoothPort.begin(9600);
   Serial.begin(9600);
   irrecv.enableIRIn();
}

void loop() 
{
   if (bluetoothPort.available() > 0)
   {
       data = bluetoothPort.read(); //received bluetooth data from android
   }

   if(irrecv.decode(&results))
   {
       irreceived(); //received IR code from a remote then sends it to my android
       irsend.sendNEC(data, 32)
   }
 }



 void irreceived()
 {
     Serial.println(results.value, HEX);   //receive value of 489010   
     bluetoothPort.println(results.value, HEX); //send value to android
     irrecv.resume();
 }

so the above is a simple sketch that receive data from a remote controller then send it to my android app then when I resend it back to my arduino I would like the data to be able to receive all the data instead of saving it per letter/number and also be able to store it for other statement to use.

1 Answer 1

0

Serial communications only consist of one character. That is what serial is - the sending of one character at a time.

How you then deal with those characters, combining them into strings, interpreting them as numbers, whatever, is completely up to you to program.

There is no such thing as a string in serial communications. There is only the character.

The Arduino API has a few (badly implemented IMHO) helper functions that can get a whole string (kinda), but it is much better (both from a learning perspective and an implementation perspective) to do it yourself.

I have a little tutorial here that may help you get to grips with it.

6
  • This works great for me at best even and I love it. I got one question though... I am developing a universal remote control Arduino paired with Android app and thanks to your help I was able to receive the code sent from a remote control and send to the android app and on a push of a button the Arduino received(thanks to your help) and sends using IR emitter... the usual way to send was irsend.sendNEC(0x40BFEA15, 32); but when I do this irsend.sendNEC(buffer, 32); the buffer has 0x40BFEA15 value it does not work? any idea or tips or work around on how to make it work?
    – User9124
    Jan 26, 2017 at 14:11
  • You have a string, and you need a 32-bit unsigned integer. You need to parse the string and convert it to an integer. strtoul() may help you there.
    – Majenko
    Jan 26, 2017 at 14:33
  • Through trial and error I have manage to figure out that I need when I do this long number = 0x40BFEA15; then this irsend.sendNEC(number, 32) it works so the next question is... how do I convert the received Bluetooth data on a char buffer` that contains 0x40BFEA15 into a long that will retain the value of buffer..? or is there other way to receive bluetooth data as a long instead of a char.? silly question but I know I am near to finish with this one.
    – User9124
    Jan 26, 2017 at 15:31
  • I told you the function to use just now.
    – Majenko
    Jan 26, 2017 at 16:25
  • just did this long data = strtoul(buffer, NULL, 16); then Serial.print(val); and it gives me a combination of numbers so I did this Serial.print(val, HEX); and gives me 40BFEA15 and was expecting the result to be 0x40BFEA15, it omits the first two character :"0x" am I doing it wrong?
    – User9124
    Jan 26, 2017 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.