1

I'm finishing up an Sketch with the Arduino IDE and to be able to run it on UNO, I have to work with C char[]. Which has not been easy for me and I already asked something regarding this a short while ago.

I need to create a longer char[] by concatenating smaller parts. So far I've been able to do it by something like this:

//Declarations 

    #define TARGET_IP "184.106.153.149"

//Methods

    void randomMethod(){

     strcpy(foo, "AT+CIPSTART=\"TCP\",\"");
     strcat(foo, TARGET_IP);
     strcat(foo, "\",80\r\n");
     Serial.println(foo);

    }

However in my code I have a lot of static string which eat up most of the 2K RAM memory, so I would like to make use of the PROGMEM So I tried the following:

//Declarations

#define TARGET_IP "184.106.153.149"

static const char targetIp[] PROGMEM = TARGET_IP;


//Methods

void randomMethod(){

         strcpy(foo, "AT+CIPSTART=\"TCP\",\"");
         strcat(foo, targetIp);
         strcat(foo, "\",80\r\n");
         Serial.println(foo);

        }

When printing on the serial monitor the first snippet works just fine whereas the second does not.

I've been trying to google it, but to no avail.

Any further education from you guys and gals will be most appreciate it. Thanks in advance!

user2736738
  • 30,591
  • 5
  • 42
  • 56
Marcal
  • 1,371
  • 5
  • 19
  • 37
  • 2
    Where is `foo` defined? – dbush Nov 30 '17 at 17:56
  • [First search engine hit for PROGMEM](https://www.arduino.cc/reference/en/language/variables/utilities/progmem/) explains that you have to use special functions to access progmem memory. You can't just treat them like regular variables. – Raymond Chen Nov 30 '17 at 17:58
  • On the top of the Sketch, with all the other definitions. It is well defined, as char foo[400]; – Marcal Nov 30 '17 at 17:58
  • @RaymondChen I swear to you I had this page on a tab on my browser... I'm going to see to it – Marcal Nov 30 '17 at 18:21
  • 2
    Rather than concatenate 3 strings, perhaps print with `Serial.` 3 times? – chux - Reinstate Monica Nov 30 '17 at 18:56
  • Yeah, you'll have to write your own PROGMEM versions of strcpy, strcat, etc. (These are very simple functions, so it shouldn't be difficult). Or try to figure out how to accomplish your goal with less memory. – Lee Daniel Crocker Nov 30 '17 at 19:13

0 Answers0