Firstly i would like to apologize beforehand about this question. I imagine that it must be really straight forward, but i'm a beginner and had nowere to go.
I'm declaring the following pointers, outside all my functions (Global Variable):
#include <ESP8266WiFi.h>
String* IP;
String* MAC;
Then, from inside a function i'm trying to change it's values:
void networkInfo() {
String* IP = WiFi.localIP().toString();
String* MAC = WiFi.macAddress();
}
When i try to compile and execute, it gives me the following error:
error: cannot convert 'String' to 'String*' in initialization
String* IP = WiFi.localIP().toString();
^
error: cannot convert 'String' to 'String*' in initialization
String* MAC = WiFi.macAddress();
^
Please, what am i doing wrong? Thanks...
PS: I would like to state that before asking this question i REALLY did search on StackOverflow, tried many different approachs but none helped for me. (Most were incomplete or just really specific to the OPs question)
If this is a duplicate, i'm sorry. I didnt find the solution for this.
Edit 1:
Changed pointers to normal variables:
// Global Variables
String IP;
String MAC;
Inside the function, changed ways that the variable is assigned:
void networkInfo() {
IP = new String;
MAC = new String;
*IP = WiFi.localIP().toString();
*MAC = WiFi.macAddress();
}
But right here on networkInfo function it returned me a error. Error on equals sign:
****no viable overloaded '='****
Edit 2:
Further tests below.
// Global Variables String *g_IP; String *g_MAC;
void networkInfo() { g_IP = new String; g_MAC = new String; *g_IP = WiFi.localIP().toString(); } void sendInfo() { cout << "Test: " << g_IP; }
When calling sendInfo() function, the output was exactly:
Test: 0x22beeb0
I expected the IP Address.
Edit 3 (Solution): For some weird reason i was able to solve this using g_IP = new String; and then assigning as pointer. Weird, didnt understand why.
// Global Variables
String *g_IP;
String *g_MAC;
// Prototypes - Functions you will use.
void networkInfo();
void sendInfo();
void initSerial();
void setup()
{
// Initialize Functions
initSerial();
networkInfo();
sendInfo();
}
void loop()
{
// Loop Functions
sendInfo();
}
void initSerial()
{
Serial.begin(115200); // Baudrate NodeMCU
}
void networkInfo() {
// Obs: Without new String a Error happens, below.
// ets Jan 8 2013,rst cause:4, boot mode:(1,6)
// wdt reset
// Why? IDK
g_IP = new String; // No idea why this works.
g_MAC = new String;
// Removing this two lines and above gives wdt reset error
*g_IP = WiFi.localIP().toString();
*g_MAC = WiFi.macAddress();
}
void sendInfo() {
delay(500);
Serial.println(*g_IP);
}
First, in void networkInfo()
you are declaring local variables that are hiding the global ones.
String* IP = (value)
Should just be
IP = (value)
in the function if you want IP to be the global version. Some people will use a naming scheme like g_IP
to make it clear that it is a global variable not a local.
Second, a pointer must be assigned the address of (something) not the (something) itself.
IP = new String
Will allocate a persistent string object on the heap and set IP to the address of it (a pointer to it).
Now that IP points to a String, *IP is the string and this will work
*IP = WiFi.localIP().toString();
As @CherryDT notes, you probably only want to allocate storage for the global variables once, in some initialization function. If you keep allocating new objects every time this function is called you will leak memory.