WiFi.SSID(); not displaying on web app properly [SOLVED]

I’m trying to display SSID on my webapp using WiFi.SSID(); but am getting strange characters: A tall hexigon with a question mark inside and a capital “Z”. I suspect this is a datatype issue. I am able to pass a string to my webapp and display it properly. I am also able print the SSID successfully to a terminal over serial. But when I try to pass the SSID value to my web app I get the strange characters. I found in the reference material that WiFi.SSID(); returns a char* datatype and have been trying to convert that to a string before passing it to my webapp but nothing has worked. I found a post on this forum very specific to converting char* to string but couldn’t make it work. Here are snippets of my code that pertain without the datatype conversion attempts.

String SSID = “None”;

void setup() {
Spark.variable(“SSID”, SSID, STRING);

void loop() {
SSID = WiFi.SSID(); // WiFi SSID of connected network

If i print SSID over serial I see the correct value but I figure maybe the print function doesn’t care about the datatype where as maybe the Spark.variable function does.

Anyone see anything obvious I am doing wrong.

The thing is, that Spark.variable() stores a pointer to the variable at the moment when you set it up, but with the String class you have no real control over the actual location of the stored string.
So when you change the value, the location of your string will possibly change and hence your Spark.variable will not reflect the current value anymore.

So your best bet is to use a char SSID[64] with strcpy() instead.

BTW: Even if a class implements a method or an operator that would give you what you want, the important thing is to look at the caller if it actually will make use of that operator/method.

2 Likes

Thanks ScruffR, that worked!

1 Like

I think I have the same problem as this - well, maybe. I am trying to printf my SSID and getting random stuff instead of my AP name. However, Serial1.println(WiFi.SSID()) gives the correct string - however

char outBuff[132];
sprintf(outBuff,“AP: %s”, WiFi.SSID());

when I publish outBuff gives me

AP: �V ���������

SSID is supposed to return a char* so I am not clear on how I solve this?
Thanks for any hints :slight_smile:

How do you do the publishing?
You do show how you place the string into outBuff but is this really what you publish?
Have you tried Serial.println(outBuff) or Serial1.println(outBuff) (directly before and after the publish call)?
Could it be that the buffer gets corrupted somewhere after your sprintf() call?
Also when do you populate outBuff - is WiFi.SSID() already valid at that time?

Hi @ScruffR - thanks for picking this up so quickly.

EDIT: Doh! I have an extra close round bracket on my macs (see below) which was I think terminating the args early and causing the SSID %s to suck in randomness. You’d have thought the compiler would give a missing args error or something like that, but no. Anyway, sorry to have wasted your time. I’ll leave this here though in case anyone else comes this way.

No, this publish happens a long time after startup and other publishes have previously been done, so it’s not a startup race condition or anything like that.

So - the full functions are:

void showNetworkDetails()
{
    myAddr=WiFi.localIP();
	WiFi.macAddress(mac);
    sprintf(outBuff,"CLITE: IP=%d.%d.%d.%d: MAC=%02x:%02x:%02x:%02x:%02x:%02x: AP: %s: WiFI Strength %d%%\n",
	                myAddr[0],
	                myAddr[1],
	                myAddr[2],
	                myAddr[3],
	                mac[5],
	                mac[4],
	                mac[3],
	                mac[2],
	                mac[1],
	                mac[0]),
	                WiFi.SSID(),
	                100-((int8_t) WiFi.RSSI());
	publishInfo(outBuff);
}

That first one runs when a REST command is sent in.
and

void publishInfo(char* theBuffer)
{
	if (Particle.connected() == true) {
		Particle.publish("HH_Info",theBuffer,10,PRIVATE);
	}
}

publishes it - mostly correctly.
outBuff is globally declared like
char outBuff[132];

as is mac
byte mac[6];

and the output I see in the console is

CLITE: IP=192.168.1.154: MAC=29:14:b4:88:3a:08: AP: : WiFI Strength 134898760%

Sometimes the AP name is blank like that, sometimes like I showed before but never what it should be. - forget the wifi % strength thing still trying to get that right - but the SSID doesn’t show properly even with that removed.

1 Like

Although your buffer should be big enough, I'd recommend using snprintf() instead of sprintf() to avoid overshooting the buffer size.

   snprintf(outBuff, sizeof(outBuff)
           , "CLITE: IP=%d.%d.%d.%d: "
             "MAC=%02x:%02x:%02x:%02x:%02x:%02x: "
             "AP: %s: "
             "WiFI Strength %d%%\n"
           , myAddr[0], myAddr[1], myAddr[2], myAddr[3]
           , mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]
           , WiFi.SSID()
           , 100-((int8_t) WiFi.RSSI()
           );

I'll also repeat the suggestion to print the contents of your outBuff before and after the publish.
As an additional debugging step you could also see whether theBuffer and outBuff really are the same (before and after publish).

Which?
Could it be that it is in response to a Particle.subscribe()?

@ScruffR
No, the command comes in via a published function. Anyway, see my edit at the top here - this was all due to just a silly thing I did which the compiler did not seem to pick up. All sorted now and working properly. Sorry to have wasted your time.

1 Like