El Stevo El Stevo - 1 year ago 124
C++ Question

invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'

Using an Adafruit Feather M0 board with LoRa radio, I want to send GPS position to a receiver. When trying to create a data packet with an ISO 8601 timestamp and lat/long GPS values I am using the following code to create a

array and then send it:

char radiopacket[40] = {GPS.year + "-" + GPS.month + "-" + GPS.day + "T" + GPS.hour + ":" + GPS.minute + ":" + GPS.seconds + "Z" + "," + GPS.latitude + "," + GPS.longitude};
rf95.send((uint8_t *)radiopacket, 40);

I keep getting the error:

invalid operands of types 'const char*' and 'const char [2]' to binary

Where am I going wrong?

Answer Source

You can't concatenate strings like that in C. Try something like

char radiopacket[40];
sprintf(radiopacket, "%04d-%02d-%02dT%02d:%02d:%02dZ,%f,%f", GPS.year, GPS.month, GPS.day, GPS.hour, GPS.minute, GPS.seconds, GPS.latitude, GPS.longitude); 
rf95.send((uint8_t *)radiopacket, 40);

see here for some documentation on the format string (the "%04d-...") within sprintf

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download