0

I'm working with VirtualWire and I'm trying to get a string value out of the Serial.println in the loop. I've tried create a string and add the message[i] , but surprisingly message[i] becomes an int when added. Serial.println outputs the string correctly, but the string that I get from message[i] is just numbers...

#include <VirtualWire.h>
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
String sensorname;
void setup(){
  Serial.begin(9600);
  Serial.println("Device is ready");
  // Initialize the IO and ISR
  vw_setup(2000); // Bits per sec
  vw_rx_start(); // Start the receiver
  }
void loop(){

  if (vw_get_message(message, &messageLength)){
    Serial.print("Received: ");
    for (int i = 0; i < messageLength; i++){
      Serial.write(message[i]);
      sensorname += message[i];
      }
      Serial.println();
      Serial.pintln("Name of sensor: " + sensorname);
      sensorname="";  //Reestart the string
      }
}

I'm quite new with VirtualWire so I don't know if there is command to get the message and pass it directly to string...Any help will be appreciated!

EDIT I think I dind't express myself well. In my original question I didn't add the String sensor name

The Response I get in the Serial is the following:

Device is ready    
SensorA
Name of sensor: 8310111011511111465

My aim is to instead of having as sensorname=8310111011511111465, have sensorname=SensorA

Alvaro
  • 137
  • 10

1 Answers1

0

Those numbers are the numerical ASCII representation of the characters.

83 = S
101 = e
110 = n
... etc ...

(Find the whole list here: http://www.asciitable.com/)

The reason you are ending up with numbers is because of the type of variable you are receiving the data into - a byte.

A byte stores a number between 0 and 255. When you add that byte to a String the compiler assumes you want to append a number, since it is a numerical type that you are using.

If you instead tell it to use a char type then the compiler should realise "Ah, this is the numeric representation of a character - I will add the character instead".

You can either change the whole array type:

char message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages

Or cast the variable to be the right type when you append it:

  sensorname += (char)message[i];

Or better still, don't use the String class since it is really too heavyweight and prone to memory issues for use on a little microcontroller. Stick to C strings. They take a little more getting used to, but are so much more microcontroller friendly.

Majenko
  • 105,095
  • 5
  • 79
  • 137
  • Thanks, Majeko, that did the trick! In my case I want to use this value to post it into a webserver, but what you suggestest can't be applied because I have to post as a string no? Thanks again! – Alvaro Sep 11 '15 at 09:54
  • A "String" is just a "C string" with dynamic allocation and lots of helper functions. It's rare for a library author to only ever allow you to use String objects. C strings are far more commonly supported than String objects. – Majenko Sep 11 '15 at 10:16