从显示的函数中打印库值

Print library value from function on display

我正在尝试从库 ESP8266WiFi. But I'm getting an error. For the display I'm using the SSD1306 库中打印 return 值(WiFi.localIP)。

这样做的目的是在显示屏上打印ESP8266的IP地址。

错误:

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]

代码:

#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();
  
}

2 件事:

  • 您正在影响一个指针来初始化一个非指针变量(使用“&”)
  • 编译器无法在字符串中转换对象 IPAddress。

使用函数“to_string”会更好

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

当然,如果这个函数不存在,你必须编写代码 ;)

使用了 ESP8266WiFi library 中的 toString() 方法(第 162-173 行)。

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