Avoid Hard Coding ESP32 || Update Wi-Fi Credentials Using Bluetooth Application.

Introduction: Avoid Hard Coding ESP32 || Update Wi-Fi Credentials Using Bluetooth Application.

Have you ever encountered hurdles in saving the parameter information in Microcontrollers that may be needed to get updated often? These pieces of information are part of the code and may not change once compiled and burned in the chip.

Here I give you an example: Let suppose you made an IoT project using wifi module i.e ESP32 for some remote location and this WiFi module might require wifi credentials SSID and Password to be connected with an Access point(AP) for network connection. So this is how you usually do; open the code and provide SSID and Password of your Acces Point (AP) to the SSID and PASSWORD array. i.e. char SSID[] = "MyHomeAP"; char PASSWORD[] = "123456789"; This information will be compiled and be part of the code as long as it resides in the ESP. But what if the Access Point (AP) SSID or PASSWORD get changed or there might be an AP with more bandwidth than you may want ESP32 to be associated. So you might need to get back the ESP module back to your laptop reprogram it and then place it back to the project location. It leads to a lot of overhead and maybe infeasible for most of the projects. So today we are going to tackle this hurdle with the most optimal solution for ESP32.

Step 1: Compiling and Uploading Sketch to Your ESP32

I assume that you have installed the ESP32 board in your Arduino ide and cmpiled and burned any sketch to ESP32. Otherwise you can search lot of tutorial on getting started with ESP32.

Compile and upload the sketch to ESP32.

#include "BluetoothSerial.h" //Header File for Serial Bluetooth, will be added by default into Arduino
#include #include "WiFi.h" BluetoothSerial ESP_BT; //Object for Bluetooth String buffer_in; unsigned long previousMillis = 0; byte val; int addr = 0; byte indS=0; byte indP=0; String stream; byte len=0; String temp; String temp2; unsigned int interval=30000; void setup() { EEPROM.begin(50); Serial.begin(9600); //Start Serial monitor in 9600 Serial.println("Bluetooth Device is Ready to Pair");

Serial.println("Waiting For Wifi Updates 30 seconds"); ESP_BT.begin("ESP32_BLUETOOTH"); //Name of your Bluetooth Signal

while(!check_wifiUpdate()==true) { }

Serial.println("The Stored Wifi credetial are : "); for(int i=0;i<50;i++) {val=EEPROM.read(i); stream+=(char)val; if((val==10) && (indS==0)) { indS=i; //Serial.println("indS"+(String)i); } else if(val==10 && indP==0) { indP=i; break; //Serial.println("indP"+(String)i); } }

// Serial.println(stream); // Serial.println("Stream Ended"); temp=stream.substring(0,indS); temp=temp.substring(5,indS);

//ssid2=ssid; temp2=stream.substring(indS+1,indP); temp2=temp2.substring(5,indP-indS);

int i=temp.length(); int j=temp2.length(); char ssid[i]; char pass[j]; temp.toCharArray(ssid,i); temp2.toCharArray(pass,j);

Serial.println("Stored SSID"); Serial.println(ssid); Serial.println("Stored PASS"); Serial.println(pass); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass);

if (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("WiFi Failed"); while(1) { delay(1000); } } else { Serial.print("Wifi Connected to "); Serial.println(ssid);

}}

boolean check_wifiUpdate() { unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis;

Serial.println("30 Seconds Over");

return true; }

else if (ESP_BT.available()) //Check if we receive anything from Bluetooth {interval=50000; buffer_in = ESP_BT.readStringUntil('\n'); //Read what we recevive //Serial.println("Received:"); Serial.println(buffer_in); delay(20);

if(buffer_in.charAt(0)=='S') { for(int i=0;i

EEPROM.write(addr, val); //Serial.println(val); addr++; } //Serial.print("New "); //Serial.print(buffer_in); EEPROM.write(addr, 10); addr++; EEPROM.commit(); ESP_BT.println("SSID Stored"); }

else if(buffer_in.charAt(0)=='P') { for(int i=0;i

void loop() { }

Step 2: Open Bluetooth Terminal Application for Updating Credentials

Once you burned the code in ESP on restart it waits for Bluetooth pairing

1. Pair your cell phone with ESP

2. Provide SSID in command like

"SSID:yourssid" SEND

"PASS:yourpass"SEND

The ESP will respond with the confirmation on your Bluetooth terminal.

The new SSID and Password will be updated in your EEPROM.

Be the First to Share

    Recommendations

    • DIY Summer Camp Contest

      DIY Summer Camp Contest
    • Pets Challenge

      Pets Challenge
    • Water Speed Challenge

      Water Speed Challenge

    Comments