I would like to output the local IP address of my device to the lcd with a WidgetLCD print but it returns an unexpected 10 digit number with no “.” ie:
So would it be correct to say that WiFi.localIP() returns the IP in “long decimal” format and that print.terminal() and print.serial() seem to automatically convert but lcd.print() needs a little help from toString() ?
Thanks for your help by the way. My IP address is now displaying correctly. This method is far neater than splitting the ip address into a 2 dimensional array like what is described elsewhere.
Although it’s not necessary to achieve the goal of displaying the IP address on the display I have a related question about storing the IP address in a 1 dimensional array:
I would have expected this code to work given Costas reply:
@grieve2774 IPAddress is a unique data format made up of four octets in an array. Serial Monitor for the following sketch:
IP address in IPAddress format: 192.168.10.236
IP address in unsigned long format: 3960121536 [octet values are transposed]
long back to IP, for sanity check: 192.168.10.236
Octet by octet: 192.168.10.236
Sketch that shows you the array format:
// IPAddress.ino prepared by Costas 22/8/16.
// The ESP IPAddress data type used toCharArray() for a few hours, 21st Aug 2015.
// It was then changed to use toString() and the format is a 4 byte array in octets.
// There are also some special print functions fr IPAddress data type.
// Converter http://www.smartconversion.com/unit_conversion/IP_Address_Converter.aspx
// Converter http://www.ipaddressguide.com/ip
// see https://github.com/esp8266/Arduino/blob/master/cores/esp8266/IPAddress.cpp
// see https://github.com/esp8266/Arduino/blob/master/cores/esp8266/IPAddress.h
#include <ESP8266WiFi.h>
const char* ssid = "xxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxx";
void setup() {
Serial.begin(115200);
Serial.println();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
IPAddress myip = WiFi.localIP();
unsigned long myiplong = WiFi.localIP();
Serial.print("IP address in IPAddress format: ");
Serial.println(myip);
Serial.print("IP address in unsigned long format: ");
Serial.print(myiplong);
Serial.println(" [octet values are transposed]");
IPAddress reversedIP = myiplong;
Serial.print("long back to IP, for sanity check: ");
Serial.println(reversedIP);
Serial.print("Octet by octet: ");
Serial.print(myip[0]);
Serial.print(".");
Serial.print(myip[1]);
Serial.print(".");
Serial.print(myip[2]);
Serial.print(".");
Serial.println(myip[3]);
}
void loop() {
delay(1);
}
@elanozturk if you don’t want to work with the data type IPAddress, which is a bit cumbersome, try changing:
lcd.print(0 ,1,myip);
to:
lcd.print(0, 1, WiFi.localIP());
Your sketch compiles with this change but I haven’t connected it up to a virtual LCD. We generally use Terminal and terminal.println(WiFi.localIP()) works fine so it should be OK for the LCD.
This works nicely if each octet is 3 digits long like 192.168.100.123 but leaves unsightly gaps for an IP address such as 10.10.10.1, which would display as 10 .10 .10 .1
Not a major issue but I’ll try to join the array into a single string that will skip the gaps.
I have a number of WeMos D1 mini devices around the house, I update them via OTA . . .I added a Labeled value widget to each project to display the local IP address - makes it easy to identify each device from the app rather than maintaining a list of IP addresses.
Thnx for your simple solution (I had the same formatting issues as OP)