FONAFlashStringPtr

I'm using a FONA board with the standard library and wonder about the used typedefs :

If the FONAFlashStringPtr is defined as const char *
( typedef const char* FONAFlashStringPtr ) and String.c_str() provides a const char * to the internal String structure, why can't I feed a method that uses FONAFlashStringPtr with String.c_str()?

I'm using String to build an XML message that will be send through MQTT so I really would like to know how to do this.....

Post an MRE that demonstrates the problem.

Thank you for your remarks. Took some time and digging to find the guidelines but hopefully this is ok now..

Here is some working code ( Well, should work ) describing my problem...

  • In the platform headers, FONAFlashStringPtr is defined as const char * ( typedef )
  • String::c_str() produces a const char *
    And yet, somehow I'm not allowed to feed pFona->setGPRSNetworkSettings with a const char *

I'm a bit lost here....

#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>

#define FONA_RX    9
#define FONA_TX    8
#define FONA_RST 4

// Doesn't really care. This is just to show the error if c_str is fed...
String apn_name = "My_Apn_Name";
String apn_uname = "My_UserName";
String apn_pword = "My_PassWord";

SoftwareSerial. *pSerial = nullptr;
Adafruit_FONA *pFona = nullptr;

void setup()
{
    Serial.begin( 115200 );

    pSerial = new SoftwareSerial( FONA_TX, FONA_RX );
    pFona = new Adafruit_FONA( FONA_RST );

    pSerial.begin( 4800 );
    if( !pFona.begin( *pSerial ) )
    {
        Serial.println( F( "Couldn't find the FONA Module." ) );
        Serial.println( F( "No point in continuing.. " ) );
        while(1);
    }
    else
    {
        Serial.println( F( "FONA found..." ) );
    }
    // Take 5 seconds to settle the FONA...
    delay( 5000 );

    // Try setting the GPRS Networking params..... This produces a compiler error....
    pFona->setGPRSNetworkSettings( apn_name.c_str(), apn_uname.c_str(), apn_pword.c_str() );
}

void loop()
{
    // Enable the GPRS connection
    while( !pFona->enableGPRS( true ) )
    {
        Serial.println( F( "Failed to turn GPRS on" ) );
    }
    else
    {
        Serial.println( F( "GPRS Connected." ) );
        while( 1 );
    }
    delay( 10000 );
}

When I try to compile this the compiler produces the following error :

Arduino: 1.8.16 (Mac OS X), Board: "Adafruit Feather 32u4"











/Applications/Arduino.app/Contents/Java/arduino-builder -dump-prefs -logger=machine -hardware /Applications/Arduino.app/Contents/Java/hardware -hardware /Users/pgroen/Library/Arduino15/packages -tools /Applications/Arduino.app/Contents/Java/tools-builder -tools /Applications/Arduino.app/Contents/Java/hardware/tools/avr -tools /Users/pgroen/Library/Arduino15/packages -built-in-libraries /Applications/Arduino.app/Contents/Java/libraries -libraries /Users/pgroen/Documents/Arduino/libraries -fqbn=adafruit:avr:feather32u4 -ide-version=10816 -build-path /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_build_128016 -warnings=all -build-cache /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_cache_64839 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -prefs=runtime.tools.avrdude.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -prefs=runtime.tools.avr-gcc.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -verbose /Users/pgroen/Documents/Arduino/sketch_nov02a/sketch_nov02a.ino
/Applications/Arduino.app/Contents/Java/arduino-builder -compile -logger=machine -hardware /Applications/Arduino.app/Contents/Java/hardware -hardware /Users/pgroen/Library/Arduino15/packages -tools /Applications/Arduino.app/Contents/Java/tools-builder -tools /Applications/Arduino.app/Contents/Java/hardware/tools/avr -tools /Users/pgroen/Library/Arduino15/packages -built-in-libraries /Applications/Arduino.app/Contents/Java/libraries -libraries /Users/pgroen/Documents/Arduino/libraries -fqbn=adafruit:avr:feather32u4 -ide-version=10816 -build-path /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_build_128016 -warnings=all -build-cache /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_cache_64839 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -prefs=runtime.tools.avrdude.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -prefs=runtime.tools.avr-gcc.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=/Applications/Arduino.app/Contents/Java/hardware/tools/avr -verbose /Users/pgroen/Documents/Arduino/sketch_nov02a/sketch_nov02a.ino
Using board 'feather32u4' from platform in folder: /Users/pgroen/Library/Arduino15/packages/adafruit/hardware/avr/1.4.14
Using core 'arduino' from platform in folder: /Applications/Arduino.app/Contents/Java/hardware/arduino/avr
Detecting libraries used...
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10816 -DARDUINO_AVR_FEATHER32U4 -DARDUINO_ARCH_AVR -DUSB_VID=0x239A -DUSB_PID=0x800C "-DUSB_MANUFACTURER=\"Adafruit\"" "-DUSB_PRODUCT=\"Feather 32u4\"" -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino -I/Users/pgroen/Library/Arduino15/packages/adafruit/hardware/avr/1.4.14/variants/feather32u4 /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_build_128016/sketch/sketch_nov02a.ino.cpp -o /dev/null
Alternatives for Adafruit_FONA.h: [Adafruit_FONA_Library@1.3.10]
ResolveLibrary(Adafruit_FONA.h)
  -> candidates: [Adafruit_FONA_Library@1.3.10]
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10816 -DARDUINO_AVR_FEATHER32U4 -DARDUINO_ARCH_AVR -DUSB_VID=0x239A -DUSB_PID=0x800C "-DUSB_MANUFACTURER=\"Adafruit\"" "-DUSB_PRODUCT=\"Feather 32u4\"" -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino -I/Users/pgroen/Library/Arduino15/packages/adafruit/hardware/avr/1.4.14/variants/feather32u4 -I/Users/pgroen/Documents/Arduino/libraries/Adafruit_FONA_Library /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_build_128016/sketch/sketch_nov02a.ino.cpp -o /dev/null
Alternatives for SoftwareSerial.h: [SoftwareSerial@1.0]
ResolveLibrary(SoftwareSerial.h)
  -> candidates: [SoftwareSerial@1.0]
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10816 -DARDUINO_AVR_FEATHER32U4 -DARDUINO_ARCH_AVR -DUSB_VID=0x239A -DUSB_PID=0x800C "-DUSB_MANUFACTURER=\"Adafruit\"" "-DUSB_PRODUCT=\"Feather 32u4\"" -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino -I/Users/pgroen/Library/Arduino15/packages/adafruit/hardware/avr/1.4.14/variants/feather32u4 -I/Users/pgroen/Documents/Arduino/libraries/Adafruit_FONA_Library -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SoftwareSerial/src /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_build_128016/sketch/sketch_nov02a.ino.cpp -o /dev/null
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10816 -DARDUINO_AVR_FEATHER32U4 -DARDUINO_ARCH_AVR -DUSB_VID=0x239A -DUSB_PID=0x800C "-DUSB_MANUFACTURER=\"Adafruit\"" "-DUSB_PRODUCT=\"Feather 32u4\"" -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino -I/Users/pgroen/Library/Arduino15/packages/adafruit/hardware/avr/1.4.14/variants/feather32u4 -I/Users/pgroen/Documents/Arduino/libraries/Adafruit_FONA_Library -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SoftwareSerial/src /Users/pgroen/Documents/Arduino/libraries/Adafruit_FONA_Library/Adafruit_FONA.cpp -o /dev/null
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10816 -DARDUINO_AVR_FEATHER32U4 -DARDUINO_ARCH_AVR -DUSB_VID=0x239A -DUSB_PID=0x800C "-DUSB_MANUFACTURER=\"Adafruit\"" "-DUSB_PRODUCT=\"Feather 32u4\"" -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino -I/Users/pgroen/Library/Arduino15/packages/adafruit/hardware/avr/1.4.14/variants/feather32u4 -I/Users/pgroen/Documents/Arduino/libraries/Adafruit_FONA_Library -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SoftwareSerial/src /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SoftwareSerial/src/SoftwareSerial.cpp -o /dev/null
Generating function prototypes...
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10816 -DARDUINO_AVR_FEATHER32U4 -DARDUINO_ARCH_AVR -DUSB_VID=0x239A -DUSB_PID=0x800C "-DUSB_MANUFACTURER=\"Adafruit\"" "-DUSB_PRODUCT=\"Feather 32u4\"" -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino -I/Users/pgroen/Library/Arduino15/packages/adafruit/hardware/avr/1.4.14/variants/feather32u4 -I/Users/pgroen/Documents/Arduino/libraries/Adafruit_FONA_Library -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SoftwareSerial/src /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_build_128016/sketch/sketch_nov02a.ino.cpp -o /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_build_128016/preproc/ctags_target_for_gcc_minus_e.cpp
/Applications/Arduino.app/Contents/Java/tools-builder/ctags/5.8-arduino11/ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_build_128016/preproc/ctags_target_for_gcc_minus_e.cpp
Compiling sketch...
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega32u4 -DF_CPU=8000000L -DARDUINO=10816 -DARDUINO_AVR_FEATHER32U4 -DARDUINO_ARCH_AVR -DUSB_VID=0x239A -DUSB_PID=0x800C "-DUSB_MANUFACTURER=\"Adafruit\"" "-DUSB_PRODUCT=\"Feather 32u4\"" -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino -I/Users/pgroen/Library/Arduino15/packages/adafruit/hardware/avr/1.4.14/variants/feather32u4 -I/Users/pgroen/Documents/Arduino/libraries/Adafruit_FONA_Library -I/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SoftwareSerial/src /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_build_128016/sketch/sketch_nov02a.ino.cpp -o /var/folders/8k/p7scffkj4cddyrrm_bv_z_fh0000gn/T/arduino_build_128016/sketch/sketch_nov02a.ino.cpp.o
/Users/pgroen/Documents/Arduino/sketch_nov02a/sketch_nov02a.ino: In function 'void setup()':
sketch_nov02a:38:91: error: no matching function for call to 'Adafruit_FONA::setGPRSNetworkSettings(const char*, const char*, const char*)'
     pFona->setGPRSNetworkSettings( apn_name.c_str(), apn_uname.c_str(), apn_pword.c_str() );
                                                                                           ^
In file included from /Users/pgroen/Documents/Arduino/sketch_nov02a/sketch_nov02a.ino:1:0:
/Users/pgroen/Documents/Arduino/libraries/Adafruit_FONA_Library/Adafruit_FONA.h:128:8: note: candidate: void Adafruit_FONA::setGPRSNetworkSettings(FONAFlashStringPtr, FONAFlashStringPtr, FONAFlashStringPtr)
   void setGPRSNetworkSettings(FONAFlashStringPtr apn,
        ^~~~~~~~~~~~~~~~~~~~~~
/Users/pgroen/Documents/Arduino/libraries/Adafruit_FONA_Library/Adafruit_FONA.h:128:8: note:   no known conversion for argument 1 from 'const char*' to 'FONAFlashStringPtr {aka const __FlashStringHelper*}'
Using library Adafruit_FONA_Library at version 1.3.10 in folder: /Users/pgroen/Documents/Arduino/libraries/Adafruit_FONA_Library 
Using library SoftwareSerial at version 1.0 in folder: /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SoftwareSerial 
exit status 1
no matching function for call to 'Adafruit_FONA::setGPRSNetworkSettings(const char*, const char*, const char*)'

Please fix your post to properly put the code in Code Tags as instructed in the Forum Guidlines:

Also, copy and post the COMPLETE compiler error message.

Don't know what library you're using, but at https://github.com/adafruit/Adafruit_FONA, FONAPlatStd.h has the typedef:

typedef const __FlashStringHelper *FONAFlashStringPtr;

That's not the same as:

typedef const char* FONAFlashStringPtr;

I did find it here :
https://www.codenong.com/37679684/ but my Mandarin is horrible..... As is my Russian..

But this says you're using the Adafruit library:

#include "Adafruit_FONA.h"

So, the typedef is:

typedef const __FlashStringHelper *FONAFlashStringPtr;

And that agrees with the error message:

no matching function for call to 'Adafruit_FONA::setGPRSNetworkSettings(const char*, const char*, const char*)'

Yep. Thanks for pointing that out :smiley: My problem doesn't get nearly close to being solved however, in fact the rabbit hole goes deeper....

I'm reading the docs on PROGMEM at the moment, which only is more confusing...

I don't really understand why Arduino is D*cking around with strings this way. I do understand the whole memory issue but why not have the compiler-optimiser do this. ( If declared const, store in Flash. If not, don't ) Why creating all kinds of confusing macro's, typedefs and datatypes....

Because 'const' is not a directive for the object NOT to be moved from flash to RAM at boot / run time.

'const' is your promise to the C++ compiler that you won't try to change the variable's value.