Jump to content
43oh

What's wrong with strings addition in Energia?


Recommended Posts

What's wrong with strings addition in Energia?

 

Super basis; both SSID and PASS area #defineds previously

Serial.println("AT+CWMODE=1");
String cmd = "AT+CWJAP=\"";
cmd += SSID;
cmd += "\",\"";
cmd += PASS;
cmd += "\"";
Serial.println(cmd);

cmd equals null????

Thanks

Link to post
Share on other sites

@@spirilis Thanks for the quick reply. The code is from ubidots:

 

http://ubidots.com/docs/devices/ESP8266.html

 

I'm using MSP430 G2553

 

I'm finding that String cmd = "anything" doesn't work at all? I guess I am missing fundamentals with that?

 

Basically String cmd = "anything" will return cmd to be null? I am puzzled?

Thank you

Link to post
Share on other sites

A few days ago a similar issue raised with stings. Apparently, the C++ runtime cannot do free space allocations, try using c style strings instead:

Serial.println("AT+CWMODE=1");
char cmd[50] = ""; // use any large enough value, but as small as possible
sprintf(cmd, "AT+CWJAP=\"%s\",\"%s\"", SSID, PASS);
Serial.println(cmd);

Or alternatively, use multiple small print stamements:

Serial.println("AT+CWMODE=1");
Serial.print("AT+CWJAP=\"");
Serial.print(SSID);
Serial.print("\",\"");
Serial.print(PASS);
Serial.println("\"");
Link to post
Share on other sites

@@roadrunner84 Thank you for the suggestion. I suspected something is broken. Forget my own example and problems. I discovered strange stuff. Take the sketch book examples that come with Energia. Pick the string addition example or simply use the on-line Energia tutorial:

 

http://energia.nu/Tutorial_StringConstructors.html

 

It would work; then simply include the SoftSerial library; I mean just include it; no calls to functions no nothing -- it breaks the example; half the code produces totally different results than when compiled without the library though in both cases no compile errors occur??

Link to post
Share on other sites

My hunch is RAM overflow.  String requires dynamic allocation/free'ing and if the heap is already full of stuff + stack is doing its thing, it's possible the dynamic allocations blow into the stack.

 

Would be interesting to see if this all works properly on the F5529LP or FRAM (FR5969) LaunchPads.

Link to post
Share on other sites

Fwiw, back when I used the Arduino I remember the seasoned folks on the arduino.cc forums always urgently recommending that NOBODY USE String, just use C strings and the standard C-library str* functions to manipulate them since heap/memory fragmentation can occur quickly with the limited amount of SRAM on those chips (and those ATmega328P's had 2KB SRAM to work with...)

Link to post
Share on other sites

@@bubba198 If the inclusion of a library causes different behaviour, you can make a safe bet that there is just too few RAM left to do everything required. Your compiler wouldn't know, because String uses free space, not static or stack space. Free space (a.k.a. heap) can only be determined by the run-time (or Lint possibly), but not by the compiler. So your problems will go undetected at compile/upload time.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...