我试图从库ESP8266WiFi打印我的显示器上的返回值(WiFi.localIP)。但是我得到了一个错误。对于显示,我使用SSD1306库。
这样做的目的是在显示器上打印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:UserslauwyDocumentsArduinoTestsketchProjectCodeWEBV4.2V4.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();
}
ESP8266WiFi库中使用的toString()
方法(第162-173行)。
String ipstat = WiFi.localIP().toString();
2件事:
- 你正在影响一个指针来初始化一个无指针变量(带有'&')
- 编译器不能强制转换String对象的IPAddress
to_string"函数的使用会更好。
String ipstat = to_string(WiFi.localIP());
当然,如果这个函数不存在,你必须编写这个函数;)