How to get ESP8266 Wifi MAC ?

Posted on
  • can someone please explain how to get the wifi mac

  • I don't think we have a function for that :-)

  • Do you mean when running Espruino on the ESP8266 itself, or when running on an Espruino board connected to the ESP8266?

    If it's on the Espruino board I'm not sure there's a function, but you can add one with a simple bit of code. Something like:

      wifi.getMAC = function(callback) {
        var mac;
        at.cmdReg("AT+CIFSR\r\n", 1000, "+CIFSR", function(d) { 
          if (d.indexOf("STAMAC")>=0) mac=JSON.parse(d.slice(d.indexOf(',')+1)­); 
        }, function(d) { 
          if (d!="OK") callback("CIFSR failed: "+d); 
          else callback(null, mac); 
        });
      }
    
    //
    wifi.getMAC(function(err,mac) {
      console.log(mac);
    });
    
  • running Espruino on ESP8266, like esptool.py does:

    esptool.py --port /dev/cu.usbserial --baud 115200 read_mac

    Connecting...
    MAC: 18:fe:34:05:99:c9

    inside esptool.py

    // OTP ROM addresses
        ESP_OTP_MAC0    = 0x3ff00050
        ESP_OTP_MAC1    = 0x3ff00054
    
    def read_mac(self):
            mac0 = self.read_reg(self.ESP_OTP_MAC0)
            mac1 = self.read_reg(self.ESP_OTP_MAC1)
            if ((mac1 >> 16) & 0xff) == 0:
                oui = (0x18, 0xfe, 0x34)
            elif ((mac1 >> 16) & 0xff) == 1:
                oui = (0xac, 0xd0, 0x74)
            else:
                raise FatalError("Unknown OUI")
            return oui + ((mac1 >> 8) & 0xff, mac1 & 0xff, (mac0 >> 24) & 0xff)
    
  • Well, you could try peek32(0x3ff00050).toString(16) and peek32(0x3ff00054).toString(16)?

  • thats it - thanks !

  • @MaBe From your post, a new issue was created (Issue #722) .. fortunately, it wasn't difficult at all to add and can now be found in the latest builds.

    See:

    https://github.com/espruino/Espruino/iss­ues/722

  • @Kolban Thanks !

    so now we have the last part of the MAC: 05:99:c9

    wifi.getIP();
    ={ "ip": 0, "netmask": 0, "gw": 0,
    "mac": "5ccf7f 599c9"
    }

  • @Kolban there's some code in network.c that will properly format the Mac address into a string. Check out how WIZnet does it

     networkPutAddressAsString(data, "mac", &gWIZNETINFO.mac[0], 6, 16, ':');
    
  • Perfect ... added to Issue #722 as a task to be done before completion of that issue.

  • Should be in an upcoming build thanks to Kolban's fix :-).

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to get ESP8266 Wifi MAC ?

Posted by Avatar for MaBe @MaBe

Actions