Data type conversion

I have a Wemos D1 board with a OLED display using the u8g2 library.

I am printing my MAC-adress on the serial port & would like to print the same information on the OLED :

#include <ESP8266WiFi.h>
#include <U8g2lib.h>

byte mac[6];

setup(){
 Serial.println(WiFi.macAddress());
 WiFi.macAddress(mac);
 u8g2.drawStr(1,10,WiFi.macAddress());
 u8g2.sendBuffer();
}

This does not work. It prints the MAC-address on the serial port but not on the OLED.

I get : no known conversion for argument 3 from 'String' to 'const char*

This is what u8g2.drawStr expects :

drawStr(u8g2_uint_t x, u8g2_uint_t y, const char *s)

How can I print the MAC-information on my oled using the u8g2-library ??

The problem is that WiFi.macAddress() returns a String, but u8g2.drawStr() does not support that parameter type. The solution is to convert the String to a char*. You can do that using c_str():

u8g2.drawStr(1,10,WiFi.macAddress().c_str());

The solution is to convert the String to a char*. You can do that using c_str():

That does not convert the String to a char *. It exposes the string that the String instance wraps. That will satisfy OP's requirement, but we need to be careful to not say that a function does something that it does not do.

Yes, thank you pert & PaulS, using

u8g2.drawStr(1,10,WiFi.macAddress().c_str());

works just fine (seems like I still have a lot to learn regarding string conversion).

Gurra

Hmmm...the story goes on...

Using my Wemos D1 with Adsfruits ILI9341-library prints :
tft.print(WiFi.localIP()
tft.print(WiFi.subnetMask()
tft.print(WiFi.gatewayIP().toString()
tft.print(WiFi.dnsIP()

with no errors, but my OLED using u8g2-library does not like this at all.
I get type conversion errors.

I was hoping that the solution of the WiFi.macaddress() problem should take care of the other WiFi commands too, but they seem to return a different data type.

How do I get around this ??

You'll have to find out what data type each of those WiFi. calls returns and convert it to something your u8g2-library can handle.

Yes, i have found the Arduino examples for WiFi.localIP(), WiFi.subnetMask(), WiFi.gatewayIP(), and Wifi.dnsIP(), and they all print fine on my ILI9341 TFT-screen with Adafruit ILI9341.h library, but I dont know how to "convert this" into a u8g2 string format for my OLED display.....
.....

These are the arduino examples.

Find & Print local IP WiFi.localIP():

#include <WiFi.h>

char ssid[] = "yourNetwork";      //SSID of your network

int status = WL_IDLE_STATUS;     // the Wifi radio's status

IPAddress ip;                    // the IP address of your shield

void setup()
{
 // initialize serial:
 Serial.begin(9600);

 WiFi.begin(ssid);

  if ( status != WL_CONNECTED) { 
    Serial.println("Couldn't get a wifi connection");
    while(true);
  } 
  // if you are connected, print out info about the connection:
  else {
 //print the local IP address
  ip = WiFi.localIP();
  Serial.println(ip);

  }
}

void loop () {}

Find & Print WiFi Subnet mask WiFi.subnetMask():
:

#include <WiFi.h>
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//SSID of your network 
char ssid[] = "yourNetwork";
//password of your WPA Network 
char pass[] = "secretPassword";

IPAddress ip;
IPAddress subnet;
IPAddress gateway;

void setup()
{
  WiFi.begin(ssid, pass);

  if ( status != WL_CONNECTED) { 
    Serial.println("Couldn't get a wifi connection");
    while(true);
  } 
  // if you are connected, print out info about the connection:
  else {

    // print your subnet mask:
    subnet = WiFi.subnetMask();
    Serial.print("NETMASK: ");
    Serial.println(subnet);

  }
}

void loop () {
}

Find & Print Gateway : WiFi.gatewayIP():

#include <SPI.h>
#include <WiFi.h>

int status = WL_IDLE_STATUS;     // the Wifi radio's status

//SSID of your network 
char ssid[] = "yourNetwork";
//password of your WPA Network 
char pass[] = "secretPassword";

IPAddress gateway;

void setup()
{
  Serial.begin(9600);

 WiFi.begin(ssid, pass);

  if ( status != WL_CONNECTED) { 
    Serial.println("Couldn't get a wifi connection");
    while(true);
  } 
  // if you are connected, print out info about the connection:
  else {

  // print your gateway address:
  gateway = WiFi.gatewayIP();
  Serial.print("GATEWAY: ");
  Serial.println(gateway);

  }
}

void loop () {}

Find & Print DNS : WiFi.dnsIP():

#include <SPI.h>
#include <WiFi.h>

// the IP address for the shield:
IPAddress dns(8, 8, 8, 8);  //Google dns  

char ssid[] = "yourNetwork";    // your network SSID (name) 
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;

void setup()
{  
  // Initialize serial and wait for port to open:
  Serial.begin(9600); 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    while(true);  // don't continue
  } 

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // print your WiFi shield's IP address:
  WiFi.setDNS(dns);
  Serial.print("Dns configured.");
}

void loop () {
}

How do I find out how to convert this info into the type of string that u8g2.drawString understands ??

I would be more than happy if I there was an easy way to use oled u8g2.drawString routine something like this :

u8g2.drawStr(1,10,WiFi.localIP());
u8g2.drawStr(1,20,WiFi.subnetMask());
u8g2.drawStr(1,30,WiFi.gatewayIP());
u8g2.drawStr(1,40,WiFi.dnsIP());

(I know that the Arduino example in my text is for WiFi.set(dns), not for "getDNS", (cant recall where I saw the "getDNS example), but the command WiFi.dnsIP() works fine on my "reference euipement" :
Wemos /ILI9341TFT display/Adafruit_ILI9346.h library,
so the WiFi.dnsIP() routine is ok.....)

This is what u8g2 expects :
drawStr(u8g2_uint_t x, u8g2_uint_t y, const char *s)

How do I find out how to convert this info into the type of string that u8g2.drawString understands ??

We really don't give a rat's ass what the call to the functions look like. What IS important is what the method signatures are.

Look in the WiFi.h file, and see what localIP() and other functions return.

It looks like they return an IPAddress object, which is nothing more than a collection of bytes that you could easily convert to a string, using sprintf().

Well, first off, I've never used an 8g2lib library. And, it would have been helpful if you had provided a web link to it.

That being said, if you're using this one, then opening the file 'U8g2lib.h', you'll see that the class inherits from the print class:

class U8G2 : public Print
{

So, have you tried something as simple as moving to the proper location and:

u8g2.print(whatever you want here);

Like I said, I've never used this library or type of display. And, I have no idea what will happen if you overrun it. But, give it a try.

Yes, thank you PaulS, gfvalvo & pert.

I tried u8g2.print(xx) and that print the strings just fine.

After doing what they all tell you to do (RTFM),(embarrasing I know !!), I also found a u8g2.setCursor(x,y) command, so I can position my u8g2.print command (as I can using the u8g2.drawStr -command).

I will also test the sprintf command, that PaulS mentioned.
As I am very interested in learning more about Arduino/(Wemos) arrays/pointers/data type conversion, any ideas where to find useful & easy to understand information about this ?? (forums/YouTube/udemy ?)