Saturday, September 2, 2017

Create a unique SSID with a ESP32 board.

I recently started to work on a ESP32 project.  And I needed a unique SSID for each access point I was creating.  I didn't want to hard code a different SSID in each device, since that would be time consuming.

I did what most people do and googled what I wanted to do.  And I really didn't find what I was looking for.

The ESP32 doesn't really have a unique ID itself, but it does contain a MAC address that should be different on each device.

I did find a sketch that does give the MAC address:
https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ChipID/GetChipID/GetChipID.ino

But I still need a unique SSID - well here's how to do it....as simple as I can make it.


#include <WiFi.h>
         char ssid[15]; //Create a Unique AP from MAC address
void createSSID() {
  uint64_t chipid=ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
  uint16_t chip = (uint16_t)(chipid>>32);
  snprintf(ssid,15,"LoRaHam-%04X",chip);
  
}
void setup() {
  createSSID();
  Serial.begin(9600);
   WiFi.softAP(ssid, password);
    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);
    Serial.print("SSID: ");
    Serial.println(ssid);
}

That is it, simple.... add the rest of your code and have fun.

2 comments:

  1. I have a problem with your ESP32 project: https://github.com/kd8bxp/Wifi_Display_Badge/tree/master/v2/ESP32_with_OLED_wifidisplay
    I used ESP32 devkit. After adding wifi credentials I flashed this code with success.
    Unfortunately this device not loging into wifi. I do not know why. What I is wrong?

    ReplyDelete