Error when sending strings with Xbee

I am trying to use Arduino to use string type data via Xbee.

In a segment of my code in void loop,

I have:

    if (XBee.available())  
   {
          char c = XBee.read();
           if (c == 'q')
   {
      delay(1000);
      XBee.write('A');
      Serial.println("A sent");
    }
   }

The above works and 'A' is sent.

However, when I try to modify the code to send a variable (generated by a light sensor) as shown below, I get 'no matching function for call to 'SoftwareSerial::write(String&) error' for the line 'Xbee.write(data)'.

    if (XBee.available())  
   {
          char c = XBee.read();
           if (c == 'q')
   {
      delay(100);
      String data;
      data=String(event.light);
      Serial.println(data);
      delay(1000);
      XBee.write(data);
      Serial.println("value sent");
    }
   }

Why do I get this error and how can I successfully modify my code to send variables?

It appears that SoftwareSerial does not support the write() method for Strings but you could, however, use the print() method instead

Thanks UKHeliBob, that worked! Seems like I'll just stick to Xbee.print from now on. Also I'm just wondering if there are situations where using xbee print might be more advantageous?

More advantageous than what ?

more advantageous than using xbee.write?

pyren:
more advantageous than using xbee.write?

Considering that you cannot use xbee.write() it must be more advantageous

Oops, I asked my question the wrong way :sweat_smile:

So what is the actual question ?

In general, write() outputs bytes and print() outputs chars

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R