Home > Net >  Print library value from function on display
Print library value from function on display

Time:10-26

I'm trying to print the return value(WiFi.localIP) on my display from the library ESP8266WiFi. But I'm getting an error. For the display I'm using the SSD1306 library.

The purpose of this is to print the IP adress of the ESP8266 on the display.

ERROR:

Arduino: 1.8.16 (Windows 10), Board: "LOLIN(WEMOS) D1 R2 & mini, 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache   32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 115200"


C:\Users\lauwy\Documents\Arduino\Testsketch\Project\Code\WEB\V4.2\V4.2.ino: In function 'void connectToWifi()':

V4.2:38:32: error: taking address of rvalue [-fpermissive]

   38 |   String ipstat = &WiFi.localIP();

      |                    ~~~~~~~~~~~~^~

V4.2:38:19: error: conversion from 'IPAddress*' to non-scalar type 'String' requested

   38 |   String ipstat = &WiFi.localIP();

      |                   ^~~~~~~~~~~~~~~

exit status 1

taking address of rvalue [-fpermissive]

CODE:

#include <Wire.h>  
#include "SSD1306.h"
SSD1306  display(0x3C, D2, D5);

#include <ESP8266WiFi.h>
const char* ssid     = "*****************"; //Enter network SSID 
const char* password = "*****************"; //Enter network PASSWORD 

WiFiServer server(80);

void connectToWifi(){
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  server.begin();

  String ipstat = &WiFi.localIP();
 
  display.init();
  display.flipScreenVertically();
  display.drawString(0, 0, ipstat);
  display.display();
  
}

CodePudding user response:

Used toString() method from the ESP8266WiFi library (line 162-173).

String ipstat = WiFi.localIP().toString();

CodePudding user response:

2 things:

  • You are affecting a pointer to initialize a no-pointer variable (with the '&')
  • The compiler does not manage to cast an object IPAddress in String.

The use of a function "to_string" would be better.

String ipstat = to_string(WiFi.localIP());

Of course you have to code this function if it does not exist ;)

  • Related