Page 1 of 1

TTGO cant get mac address to display

Posted: Thu Dec 15, 2022 4:27 pm
by tjodork
I'm trying to display the mac address on the display... Ive had no trouble displaying anything else but it displays
mac: ?
I use the same String variable and output to the serial console with no issue

here is what displays in serial console
IP Address: 192.168.1.162
signal strength (RSSI):-53 dBm
To see this page in action, open a browser to http://192.168.1.162
mac:3C:61:05:0C:F3:44

and again ...what is displayed in the TTGO display for the mac line is
mac: ?
Note that I was able to display the IP where the mac was with no issue

Code: Select all


#include <SPI.h>
#include <TFT_eSPI.h>       // Hardware-specific library
#include <WiFi.h>
//const char* ssid     = "IBMInternet";
//const char* password = "";
const char* ssid     = "xxxx";
const char* password = "xxxxxx";
WiFiServer server(80);
uint32_t IP = 0;
String IPString;
String wifiMacString;
long rssi = 0;
byte mac[6];

TFT_eSPI    tft = TFT_eSPI();
float maxpower;
float fivevolt;
float fivecur;
float twelvevolt;
float twelvecur;


void setup(void) {

  Serial.begin(115200);
  pinMode(5, OUTPUT);      // set the LED pin mode

  tft.begin();
  tft.fillScreen(TFT_BLACK);
  tft.setTextFont(4);
  tft.setRotation(1);

  tft.setTextColor(TFT_WHITE, TFT_BLACK); // Set the font colour and the background colour
  fivevolt = 5.1;
  fivecur = 2.1;
  twelvevolt = 12.5;
  twelvecur = 3.1;

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  WiFi.macAddress(mac);
  wifiMacString = mac2String(mac);
  Serial.println(wifiMacString);
  drawPowerInfo(fivevolt, fivecur, twelvevolt, twelvecur);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  IP = WiFi.localIP();
  IPString = WiFi.localIP().toString();
  rssi = WiFi.RSSI();
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(IP);
  Serial.println(rssi);
  Serial.println(wifiMacString);
  server.begin();

}

void loop() {
  drawPowerInfo( fivevolt + .2 * random(20), fivecur, twelvevolt + + .2 * random(20), twelvecur);
  printWifiStatus();

  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.<br>");
            client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(5, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(5, LOW);                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
  delay(1000);
}





void drawPowerInfo(float _5v, float _5vc, float _12v, float _12vc) {
  char line1[30];
  char line2[30];
  char line3[30];
  char line4[30];
  char line5[40];
  tft.fillScreen(TFT_BLACK);
  tft.setTextFont(4);
  float _5vp =  _5v * _5vc;
  sprintf(line1, "  %4.1fv  %3.1fa  %5.1fw",  _5v, _5vc, _5vp);
  tft.setTextColor(TFT_WHITE);
  tft.drawString(line1, 5, 5);
  float _12vp =  _12v * _12vc;
  sprintf(line2, "%4.1fv  %3.1fa  %5.1fw", _12v, _12vc, _12vp);
  tft.drawString(line2, 5, 35);
  float totalpower = _5vp + _12vp;
  sprintf(line3, "           power %6.1fw", totalpower);
  tft.drawString(line3, 5, 60);
  tft.setTextColor(TFT_RED);
  maxpower = max(maxpower, totalpower);
  sprintf(line4, "max power %6.1fw", maxpower);
  tft.drawString(line4, 5, 85);
  //sprintf(line5, "IP:%s  mac:%s", IPString, wifiMacString);
  sprintf(line5, "mac: %s", wifiMacString);
  tft.setTextFont(2);
  tft.setTextColor(TFT_BLUE);
  tft.drawString(line5, 5, 110);

}



void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.print("mac:");
  Serial.println(wifiMacString);
}

String mac2String(byte ar[]) {
  String s;
  for (byte i = 0; i < 6; ++i)
  {
    char buf[3];
    sprintf(buf, "%02X", ar[i]); // J-M-L: slight modification, added the 0 in the format for padding
    s += buf;
    if (i < 5) s += ':';
  }
  return s;
}

Re: TTGO cant get mac address to display

Posted: Fri Dec 16, 2022 5:02 pm
by tjodork
turns out I cannot pass String into sprintf

so I had to add .c_str() to each String

sprintf(line5, "%s %s", IPString.c_str(), wifiMacString.c_str());

whats puzzling is why the compile wouldn't complain about that