How to display local IP address in lcd widget?

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:

void updateLCD() { IPAddress strWiFiIP = WiFi.localIP(); lcd.clear(); lcd.print(0, 0, "IP address:"); lcd.print(0, 1, strWiFiIP); }

expected output: 192.168.1.239
output: 4009863360

What is the correct way to achieve this?

This works, just tried it:

lcd.print(0, 1, WiFi.localIP().toString());

4 Likes

Handly little IP to long and long to IP converter at http://www.smartconversion.com/unit_conversion/IP_Address_Converter.aspx

4009863360 shows 239.1.168.192 (192.168.1.239 in reverse).

2 Likes

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:

char localIPaddr[16]; void setup() { localIPaddr[] = WiFi.localIP().toString(); } updateLCD() { lcd.print(0, 1, localIPaddr); }

But it gives a compile error. What is the best way to store the local IP address in a character array ?

I think the format is actually IPAddress which Serial and terminal are ok with but LCD will only accept Strings.

I think the IPAddress value is actually an array of chars, but I’m not sure.

@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);
}
1 Like

@Costas any idea about how to get it work ip address on lcd widget?

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SPI.h>

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
extern "C" {
  uint16 readvdd33(void);
}

WidgetLCD lcd(V10);
long rssi;
IPAddress myip = WiFi.localIP();
int Led1=D4;
int Led2=D5;

SimpleTimer timer;

void myTimerEvent()
{
  Blynk.virtualWrite(V11, millis() / 60000);                           // Uptime
  Blynk.virtualWrite(V12,readvdd33());                                // Voltage
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  unsigned long maxMillis=millis() + 8000;
  while ((Blynk.connect() == false) && ( millis() <= maxMillis)) {
  }
  Blynk.notify("NodeMCU Started!");
  timer.setInterval(60000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run();
  rssi=WiFi.RSSI();
  myip=WiFi.localIP();
}

BLYNK_WRITE(V4)                                                 // Wifi strength
{
 int pinData = param.asInt();
 if(pinData==1)
 {
   lcd.clear();
   lcd.print(0 ,0,"Wifi Strength");
   lcd.print(0 ,1,rssi);
 }else
 {
   lcd.clear();
 }
}

BLYNK_WRITE(V5)                                                    // IP Address
{
 int pinData = param.asInt();
 if(pinData==1)
 {
   lcd.clear();
   lcd.print(0 ,0,"IP ADDRESS:");
   lcd.print(0 ,1,myip);

 }else
 {
   lcd.clear();
 }
}

@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.

Thank you @Costas tried but no luck,i’m getting same value on lcd widget,should i use terminal widget instead of?

@elanozturk no you don’t need to switch to Terminal.

Just setting up a virtual LCD and then I’ll get back to you.

@elanozturk back to using IPAddress datatype the following works on my virtual LCD:

lcd.print(0, 1, myip[0]);
lcd.print(3, 1, “.”);
lcd.print(4, 1, myip[1]);
lcd.print(7, 1, “.”);
lcd.print(8, 1, myip[2]);
lcd.print(11, 1, “.”);
lcd.print(12, 1, myip[3]);

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.

This will look better on your LCD for all IP addresses:

   String fullip = String(myip[0]) + "." + myip[1] + "." + myip[2] + "." + myip[3];
   lcd.print(0, 1, fullip);

Just to add the correct structure for using the IP address is not in loop() as the IP doesn’t change after setup() so:

IPAddress myip; // declare the variable

myip = WiFi.localIP(); // in setup() after Blynk connection and nothing in loop for IP

Thank you @Costas ,i am trying to place codes to correct places in my sketch :slight_smile:

declaration of the variable goes after your libraries.

myip = … goes after the while statement in setup.

In V5:

String fullip = String(myip[0]) + “.” + myip[1] + “.” + myip[2] + “.” + myip[3];
lcd.print(0, 1, fullip);

Finally i succeed with your help,thank you @Costas very appreciated. :grinning: :+1:

This also works with Blynk virtual write to widgets.

Blynk.virtualWrite(V2, WiFi.localIP().toString());

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)

cul
billd

2 Likes