I want to be able to set the Wifi SSID and password using classic bluetooth. I believe that this will involve sending a string containing both the SSID and password from the phone/tablet (assume Android), say using a simple app developed on AppInventor.
Usage scenario:
Pair phone with the ESP32 bluetooth.
Open app on phone
Start up ESP32
In void setup() the ESP32:
– Goes to a function eg void connect() which tries to connect to the Wifi network as follows:
- Looks for a SSID/password saved to EEPROM at 2 specified addresses, one each for the SSID and password.
- Uses those credientials to connect to Wifi
– If unsuccessful within say 30 seconds then invokes a BluetoothsetSSID function
BluetoothSSIDfunction:
- Sends a message to the app requesting the SSID and password, with a divider character in between them, such as a comma.
- in the app user enters in the SSID and password eg “SSID,password”, and sends back to ESP32
- ESP 32 invokes a function to separate the SSID and password (looking for a comma), save the SSID and password in two different memory addresses, noting that each one will be several bytes, so span several memory addresses.
- Wifi connect() function is then started again.
– If connect still fails: probably the wrong SSID/password; so then it tries again.
– Allow say 3 attempts before exiting.
Do you have some code already done to do this? Also there would probably have to be a limit to the number of characters in the SSID and password. I think with BT one message can contain up to 128 characters, correct? That should be plenty
And if 1 character takes up 2 bytes on EEPROM, and a limit of say 32 characters for the SSID and password each (do most routers normally have a limit on SSID and password length?
What is the standard limit?), then 64 EEPROM memory addressess would need to be assigned to each of the SSID and password, correct. So I’ll need some sort of function to spread it across lots of characters. I already have one here that does it across 4 characters, would be nice to have code with a loop that more elegantly went across a larger number of bytes.
//sets up the EEPROM and reads the latest value. See makeuseof.com/tag/use-arduino-eeprom-save-data-power-cycles/, playground.arduino.cc/Code/EEPROMReadWriteLong #include <EEPROM.h>; long address=0; /* the address written to. In this case we will always write to the same address as it doesn't matter if we over-write */ //This function will write a 4 byte (32bit) long to the eeprom at //the specified address to address + 3. void EEPROMWritelong(int address, long value) { //Decomposition from a long to 4 bytes by using bitshift. //One = Most significant -> Four = Least significant byte byte four = (value & 0xFF); byte three = ((value >> 8) & 0xFF); byte two = ((value >> 16) & 0xFF); byte one = ((value >> 24) & 0xFF); //Write the 4 bytes into the eeprom memory. I have changed it from "write" to "update" in case some of the values don't change to extend EEPROM life EEPROM.update(address, four); EEPROM.update(address + 1, three); EEPROM.update(address + 2, two); EEPROM.update(address + 3, one); } //This function will return a 4 byte (32bit) long from the eeprom //at the specified address to address + 3. long EEPROMReadlong(long address) { //Read the 4 bytes from the eeprom memory. long four = EEPROM.read(address); long three = EEPROM.read(address + 1); long two = EEPROM.read(address + 2); long one = EEPROM.read(address + 3); //Return the recomposed long by using bitshift. return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF); }
What is a character that is usually NOT allowed in a SSID and password that could be used as a divider in a string? I’m guessing it could be a comma “,”
Hi.
That is a quite complex project. Is there any reason why you want to set up the network credentials via Bluetooth?
In my opinion, it doesn’t seem a good idea.
(But answering your questions, we don’t have any examples about what you are asking for, and using a coma seems a good idea to separate those strings).
If you want to set up the SSID and password on your ESP32 without having to hard code, you can use the WiFi manager. Basically, it opens a portal where you can set your SSID and password through your smartphone, for example.
You can take a look at the library here: https://github.com/tzapu/WiFiManager/tree/development
Read the How it works section: https://github.com/tzapu/WiFiManager/tree/development#how-it-works
I think it does exactly what you want to do.
Also, the documentation is well explained, so you should be able to follow the steps to make it work.
I hope this helps.
Regards,
Sara