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();
^
I didnt understand. I need to pass the variables to another function... When i do it like this, using global names, the values get lost when i query the variable from another function.
This way wont work for me because if i create another function called "sendInfo", and calls it, this is what happens:
(As i said previously, i will need to create a function that gets the string definitions in networkInfo() function. "Why?" You may ask. Order and organization - I will use multiple libraries on this project, so if i do everything in the same function it will get weird and confusing.):
Global Variables can be called from anywhere, Local Variables are only called from withing functions (They are created not outside of functions.)
I was able to solve this tho... I didnt really understand the logic, but here it is (Im on this for many hours):
Declared MAC and IP Address:
Yes, with * in it (So i believe that it became a pointer ?)
String *g_IP;
String *g_MAC;
Initialized Serial (I believe this is why it was showing blank stuff on Serial Monitor)
void initSerial(); // Se n iniciar a Serial n consegue usar o Serial Monitor
void initWifi();
void setup()
{
// Functions that will be executed
initSerial();
initWiFi();
}
void loop()
{
// Empty. No functions here.
}
void initSerial()
{
Serial.begin(115200); // Baudrate do NodeMCU
}
PS: I removed some of the code on initWifi Func, since it isnt relevant. It just connects to WiFi..
I left the part that i was able to get the IP and MAC address (It worked perfectly)
void initWifi()
{
// Obs: Without new String it gives Error.
// ets Jan 8 2013,rst cause:4, boot mode:(1,6)
// wdt reset
// Why? IDK
g_IP = new String; // Is this needed??
g_MAC = new String; // Removing new String returns error above
*g_IP = WiFi.localIP().toString();
*g_MAC = WiFi.macAddress();
Serial.print("\nIP Obtained: ");
Serial.println(*g_IP);
Serial.print("MAC Address of Device: ");
Serial.println(*g_MAC);
}
Output (A little different because i translated when posting and removed unnecessary lines):
I will keep studying pointers and what u guys said here on this post since im still a little confused, but thanks for all your help!