NodeMCU sketch is crashing due to "WiFi.localIP(); "

0

My code was previously working, but today when I ran it it began crashing as soon as it runs on the device. I noticed that the serial monitor printed IP Address: right before the crash, so that led me to play around and isolate the line WiFi.localIP(); as the culprit for the crash. If I remove it, the sketch runs. If I relocate it before or after lines which print to the serial monitor, I will see those printed statements before the error.

Error Trace:

.
IP Address: 
Exception (28):
epc1=0x4020a96c epc2=0x00000000 epc3=0x00000000 excvaddr=0x00004298 depc=0x00000000

ctx: cont 
sp: 3ffffd50 end: 3fffffd0 offset: 01a0

>>>stack>>>
3ffffef0:  20676e69 6f686353 fe006c6f feefeffe  
3fffff00:  69646f43 4e40676e 00545845 00000000  
3fffff10:  40104ab2 000032f3 00000100 00000003  
3fffff20:  3ffe8a98 00000000 3ffef798 00000003  
3fffff30:  3ffe8a98 3ffef728 3ffef798 40206a60  
3fffff40:  3ffe9060 3ffef798 3ffef728 40206949  
3fffff50:  00004298 3fffff80 3ffef728 40206cf0  
3fffff60:  c001a8c0 00ffffff 3ffef798 40206cd8  
3fffff70:  3fffdad0 3ffef728 3ffef798 40202eaa  
3fffff80:  40208160 c001a8c0 feefeffe feefeffe  
3fffff90:  feefeffe feefeffe feefeffe feefeffe  
3fffffa0:  feefeffe feefeffe feefeffe 3ffef858  
3fffffb0:  3fffdad0 00000000 3ffef852 4020781c  
3fffffc0:  feefeffe feefeffe 3ffe863c 40100739  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(1,6)


 ets Jan  8 2013,rst cause:4, boot mode:(1,6)

wdt reset

Portion of Sketch:

 #include <ESP8266WiFi.h>
#include <WebSocketsClient.h>
#include <ArduinoJson.h>
#include <EEPROM.h>

// Initialize pins
int redpin = D0;
int greenpin = D2;
int bluepin = D4;

//// Connecting to the internet
const char* ssid = "********";
const char* password = "********";

// Setting up the websocket client
WebSocketsClient webSocket;

// Set up the WiFi client;
WiFiClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);


  delay(10);
  WiFi.begin(ssid, password);
  while(WiFi.status()!= WL_CONNECTED) {
    Serial.print(".");
    delay(500);  
  }
  Serial.println("");
  Serial.print("IP Address: ");
  Serial.print(WiFi.localIP() + "\n");
  Serial.print(WiFi.macAddress() + "\n");
}

I expect the program to run without error as it was previously doing that and I did not change the code at all. I do not technically need the IP address for my sketch to function, but I am quite confused as to why it is suddenly raising an error and would like to understand.

c++
arduino
esp8266
nodemcu
arduino-esp8266
asked on Stack Overflow Jan 18, 2019 by emijune

1 Answer

0

WiFi.localIP() returns an IPAddress, not a String (see its reference page). You need to turn it into a String before you concatenate anything to it.

So instead of:

Serial.print(WiFi.localIP() + "\n");

You need

Serial.print(String(WiFi.localIP()) + "\n");

Even better, use String.println() and avoid the concatenation and String object construction:

Serial.println(WiFi.localIP());

This works because there are versions of the print() and println() methods which take an IPAddress as an argument and know how to convert it into text.

Similarly, WiFi.macAddress() fills a 6 byte array holding the MAC address of the WiFi interface and does return not a String (see its reference page). You can't easily construct a String from it and the print methods won't know how to handle it.

So instead of:

Serial.print(WiFi.macAddress() + "\n");

You'll need to do something like:

byte mac_address[6];

WiFi.macAddress(mac_address);
Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
               mac_address[0], mac_address[1], mac_address[2],
               mac_address[3], mac_address[4], mac_address[5]);
answered on Stack Overflow Jan 18, 2019 by romkey • edited Jan 18, 2019 by romkey

User contributions licensed under CC BY-SA 3.0