Search code examples
c++gpslora

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 char 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 'operator+'

Where am I going wrong?


Solution

  • 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