Why is my ESP8266 WiFi connect debug message not shown?

I am scratching my head on this…
The sketch is working all right but when set to run in STA mode the expected debug message when it connects to the network is not output and I don’t understand why.
Here is the section of code from setup() that is misbehaving:

String MsgDbg = "";
int sta_connected = 0;
...
    WiFi.begin(ESPConf.sta_ssid, ESPConf.sta_passwd);
    if(WiFi.waitForConnectResult() == WL_CONNECTED)
    {
        MsgDbg = "STA ADDR = " + WiFi.localIP();
        sta_connected = 1;
    }
    else
        MsgDbg = "WiFi Connection to " + String(ESPConf.sta_ssid) + " failed! Starting SoftAP instead";
    
    SerialDebug.println(MsgDbg); //This should print my message....
    StartSerialServer();

When I start up the sketch what is printed to the debug console is this:

HOSTNAME = TCP_UART
bcn 0
del if1
usl
mode : sta(b4:e6:2d:03:d6:6f)
add if0
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 6
cnt 

connected with xxxxx_Net, channel 4
dhcp client start...
ip:10.0.3.56,mask:255.255.255.0,gw:10.0.3.1
[17]
Swapping pins for Serial, use 7/10 for Rx/Tx!
Serialserver ready!

The first line is printed from a call to InitConfig() immediately preceding the WiFi startup code in setup().
The last 2 lines are debug messages from the StartSerialServer() function.

Why is the STA connect message not printed?
Note that if the if fails the code would automatically switch to SoftAP mode, so it really must get into the if section…

don't post snippets...

how is SerialDebug defined?

don’t post snippets…
how is SerialDebug defined?

I wanted to show the code that is actually executed for STA mode to keep things simple.
But below please find the complete setup() and StartSerialServer() functions.

The total sketch code is pretty big and it contains settings for STA and AP operation.
ESPConf is a struct read from and written to EEPROM. It is used on startup to set up the device parameters inside InitConfig(), which is called at the top of setup().
This works pretty well since about 6 months.
But I want to weed out all irregularities and now I came to the debug printing.

The defines are made in a header file so I could change them easily during debugging:

  #define SerialDebug Serial1   // Debug goes out on GPIO02
  #define SerialSS    Serial    // Device connected to the ESP UART

Notice that inside StartSerialServer() I am swapping the SerialSS pins to the ones actually connected to the controlled device which cannot have spurious data sent to it during WiFi board startup…

void setup()
{
	String MsgDbg = "";  //Used for debug messages
	IPAddress local_ip;
	IPAddress gateway;
	IPAddress netmask (255,255,255,0);
	IPAddress opendns1 (208,67,220,220);
	IPAddress opendns2 (208,67,222,222);
	int sta_connected = 0;

	memset (&cnftxbuf, 0, sizeof(cnftxbuf));
	// !!! Debug output goes to GPIO02 unless we swap pins for Tx between Serial0 and Serial1!!!
	SerialDebug.begin(115200);

	InitConfig(); //Initialize handling of configuration via EEPROM

	if (ESPConf.mode == WIFI_STA)
	{
		WiFi.mode((WiFiMode)ESPConf.mode);  //WIFI_AP, WIFI_STA, WIFI_AP_STA, WIFI_OFF
		if (ESPConf.fixedaddress == 1) //Do not use DHCP, set fixed address
		{
			//The 4 addresses below are (ip, gateway, subnet, DNS1, DNS2) and must be calculated from the used IP address
			local_ip = ESPConf.addr;
			gateway = ESPConf.addr;
			gateway[3] = 1; //Force fake gateway as the first address in the network and use opendns

			WiFi.config(local_ip, gateway, netmask, opendns1, opendns2);
		}
		else
		{
			local_ip = IPAddress(0,0,0,0);
			WiFi.config(local_ip, local_ip, local_ip);	//Reset to use DHCP
		}
		WiFi.hostname(ESPConf.host); //This is the hostname that should be supplied to the DHCP server
	    WiFi.begin(ESPConf.sta_ssid, ESPConf.sta_passwd);
	    if(WiFi.waitForConnectResult() == WL_CONNECTED)
	    {
			MsgDbg = "STA ADDR = " + WiFi.localIP();
			sta_connected = 1;
	    }
	    else
	    	MsgDbg = "WiFi Connection to " + String(ESPConf.sta_ssid) + " failed! Starting SoftAP instead";
	    
	    SerialDebug.println(MsgDbg);
	}
	if ((ESPConf.mode == WIFI_AP) || (!sta_connected)) //Mode WIFI_AP or failed to connect WIFI_STA so start mode WIFI_AP
	{
		WiFi.mode(WIFI_AP);  //WIFI_OFF, WIFI_STA, WIFI_AP, WIFI_AP_STA
		WiFi.softAPConfig((IPAddress)ESPConf.addr, (IPAddress)ESPConf.addr, netmask); //IPAddress(255, 255, 255, 0));
		if (ESPConf.wichannel == 0)
			WiFi.softAP(ESPConf.ap_ssid, ESPConf.ap_passwd);
		else if (ESPConf.hidden == 0)
			WiFi.softAP(ESPConf.ap_ssid, ESPConf.ap_passwd, ESPConf.wichannel);
		else
			WiFi.softAP(ESPConf.ap_ssid, ESPConf.ap_passwd, ESPConf.wichannel, true);
	}
	StartSerialServer();
	StartConfigServer();

	if (ESPConf.mode == WIFI_STA)
		local_ip = WiFi.localIP();
	else
		local_ip = WiFi.softAPIP();

	SerialDebug.print("Use ");
	SerialDebug.print(local_ip);
	SerialDebug.print(':');
	SerialDebug.print(ESPConf.tcpport);
	SerialDebug.println(" to connect serial");
}

void StartSerialServer()
{
	//start UART. Be sure to set the speed to match the speed of whatever is
	//connected  to the UART.
	SerialSS.begin(ESPConf.baud);

	SerialDebug.println("Swapping pins for Serial, use 7/10 for Rx/Tx!");
	SerialSS.swap();	//Put the serial pins on 7 (Rx) and 10 (Tx) to avoid boot data to reach SS

	// Instantiate and start TCP serial server on port TCP_PORT
	tcpServer = new WiFiServer(ESPConf.tcpport);
	tcpServer->begin();
	tcpServer->setNoDelay(true);
	SerialDebug.println("Serialserver ready!");
}

Additional new question:
I have now moved to another location (the office) with another WiFi network so therefore I started up my device in AP mode so I could reconfigure the WiFi STA connect data.
But now it constantly resets on connection!
Here is the debug log output starting at the end of the configuration data output:

SENSORS = 0
INTERVAL = 0
HOSTNAME = TCP_UART
bcn 0
del if1
usl
mode : sta(b4:e6:2d:03:d6:6f)
add if0
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 12
cnt 

connected with xxxxWireless, channel 4
dhcp client start...
ip:10.0.0.38,mask:255.255.255.0,gw:10.0.0.1  <== Seems to work OK so far, but then:

 ets Jan  8 2013,rst cause:4, boot mode:(3,7) <== Unexpected output from here!

wdt reset
load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v614f7c32
~ld

SDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1)

Starting TCP <-> UART bridge
...
 ets Jan  8 2013,rst cause:4, boot mode:(3,7)  <== Again, repeats idefinitely..

What could have happened to cause this behavior?

Is this below what is killing it (converting the LocalIP to string)?

	    if(WiFi.waitForConnectResult() == WL_CONNECTED)
	    {
			MsgDbg = "STA ADDR = " + WiFi.localIP(); // <== Questionable??
			sta_connected = 1;
	    }

But it did work without crash on the home network…

try
MsgDbg = String("STA ADDR = ") + WiFi.localIP();

to have some String instance which implements + operator as String concatenation.
but I am not sure if it can work with IPAddress

Concerning the String instance, the variable used is declared as such at the top of setup():

	String MsgDbg = "";  //Used for debug messages

It is confusing, because close to the end of setup() I have this code:

	if (ESPConf.mode == WIFI_STA) //USE STATION
	  local_ip = WiFi.localIP();
	else
	  local_ip = WiFi.softAPIP();

	SerialDebug.print("Use "); SerialDebug.print(local_ip);
	SerialDebug.print(':');	SerialDebug.print(ESPConf.tcpport);
	SerialDebug.println(" to connect serial");

Here there is no problem, but I have separated the text and IP printing into two sentences and also used an intermediate variable local_ip of type IPAddress. And of course using the serial.print() method...

The output from this is:
Use 10.0.0.38:2101 to connect serial

it is: object operator= object operator+ object.
the operator is executed by the left object. first + then = of course

OK, I changed it like this using a local IPAddress variable:

        if(WiFi.waitForConnectResult() == WL_CONNECTED)
        {
            local_ip = WiFi.localIP(); //Temp var
 	    SerialDebug.print("1) STA ADDR = ");
	    SerialDebug.println(local_ip);
            MsgDbg = "2) STA ADDR = " + String(local_ip);
            sta_connected = 1;
        }
        else
            MsgDbg = "WiFi Connection to " + String(ESPConf.sta_ssid) + " failed! Starting SoftAP instead";
        SerialDebug.println(MsgDbg);

This works in a way but prints the wrong way when converting into the MsgDbg string and correctly if using the println() method of the SerialDebug port:

1) STA ADDR = 10.0.0.38
2) STA ADDR = 637534218

The digital value shown is actually 0x2600000A and it represents byte values 38, 0, 0, 10 for IP address 10.0.0.38.
So how can I format an IPAddress variable for inclusion into a string as its normal view?
This seems to be what is left to solve this problem.

This is part of the code I use to print the IP address.
WiFi.localIP().toString().c_str()
Leo..

Serial.printf("Connected to %s IP address %s strength %d%%\r\n", WiFi_SSID, WiFi.localIP().toString().c_str(), 2 * (WiFi.RSSI() + 100));