How to use PHP to change the SSID and Password of the arduino esp8266-01

Hello Guys!

I wanna ask if there's already a project you know that changed the wifi name and password on the set values (programmed) in the arduino esp8266-01?

any help will do, related articles and projects.

Thank you so much :slight_smile:

great help!!

have a nice day :slight_smile:

what have you tried ?

if your ESP sends GET/POST request to a server, then server returns response as 'echo' back to your ESP01

that echo carries new ssid/password,

This was my code in php and I’m using library to connect it to my arduino esp

<?php

require 'firebaseLib.php';
$Trash = $_GET["trash"];

$Distance = $_GET["distance"];
$Temperature = $_GET["temp_f"];
$Humidity= $_GET["humidity"];

// --- This is your Firebase URL
$baseURI = 'x';
// --- Use your token from Firebase here
$token = 'x';
// --- Here is your parameter from the http GET



$devicestatus= array('Distance' => $Distance,'Temperature' => $Temperature,'Humidity' => $Humidity);

$firebasePath = '/thesis1-69/';




$full= array($Trash => $devicestatus);

/// --- Making calls
$fb = new Firebase($baseURI, $token);
$fb -> update($firebasePath, $full);

echo 'Hello from php'
?>

arduino code

#include <SoftwareSerial.h>
#include <DHT.h>;
SoftwareSerial Serial1(10, 11);
#define Trash "Trash3"
#define DHTTYPE DHT11
#define DHTPIN 2
#define TRIGGER_PIN 4
#define ECHO_PIN 3
DHT dht(DHTPIN, DHTTYPE, 11);
float humidity, temp_f;
int distance;
long duration;
String buff(64);
String getStr(128);
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  //Serial1.resetESP();
  delay(2000);
  Serial1.setTimeout(5000);
  dht.begin();
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  if (!connectWiFi()) {
    Serial.println("Can not connect to the WiFi.");
    while (true)
      ; // do nothing
  }
  Serial.println("OK, Connected to WiFi.");
  sendCommand("AT+CIPSTA?");
  //sendCommand("AT+CIPDNS_CUR?");
  sendCommand("AT+CIPSTAMAC?");
  
}
void loop() {
  temp_f = dht.readTemperature();
  humidity = dht.readHumidity();
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2;
  // connect to server
  if (sendCommand("AT+CIPSTART=\"TCP\",\"expository-spoke.000webhostapp.com\",80")) {
    Serial.println("connected to Cloud");
    // build HTTP request
    getStr = "GET /upload.php?trash=";
    getStr += Trash;
    getStr += "&distance=";
    getStr += distance;
    getStr += "&temp_f=";
    getStr += temp_f;
    getStr += "&humidity=";
    getStr += humidity;
    getStr += " HTTP/1.1\r\n";
    //WEBSERVER
    getStr += "Host: expository-spoke.000webhostapp.com\r\n\r\n";
    // open send buffer
    buff = "AT+CIPSEND=";
    buff += getStr.length();
    if (sendCommand(buff.c_str()) && Serial1.find(">")) { // AT firmware is ready to accept data
      // send HTTP request
      Serial.println(getStr);
      Serial1.print(getStr);
      // print HTTP response
      if (Serial1.find("+IPD,")) { // response received
        int l = Serial1.parseInt();
        while (l > 0) {
          if (Serial1.available()) {
            Serial.write(Serial1.read());
          }
        }
        Serial.println("--------------");
      } else {
        Serial.println("no response");
      }
    } else {
      Serial.println("send error");
    }
    sendCommand("AT+CIPCLOSE");
  } else {
    Serial.println("Error connecting");
  }
}
bool connectWiFi() {
  if (!sendCommand("ATE0")) // echo off
    return false;
  if (!sendCommand("AT+CIPMUX=0")) // set single connection mode
    return false;
  if (!sendCommand("AT+CWMODE=1")) // set STA mode
    return false;
 
  return sendCommand("AT+CWJAP=\"c902b6\",\"262459105\"");
}
bool sendCommand(const char* cmd) {
  Serial.println(cmd);
  Serial1.println(cmd);
  while (true) {
    buff = Serial1.readStringUntil('\n');
    buff.trim();
    if (buff.length() > 0) {
      Serial.println(buff);
      if (buff == "OK" || buff == "SEND OK" || buff == "ALREADY CONNECTED")
        return true;
      if (buff == "ERROR" || buff == "FAIL" || buff == "SEND FAIL")
        return false;
    }
  }
}

in php you need something like this

$new_ssid = "arduino";  //can be retrieved from some web UI

$new_pass = "mechanical";   // this too


// echo 'Hello from php'; //  do not send this one back anymore


echo $new_ssid."/".$new_pass ;  // this sends  arduino/mechanical  back to your ESP

and in your ESP01

you take this part that reads http response, and do the parsing

if (Serial1.find("+IPD,")) {  // response received

String buffer_ = "";

        int l = Serial1.parseInt();

        if (l > 0) {
          while (Serial1.available()) {
         //delay(1);   // uncomment this one to slow things down a bit
          char c = Serial1.read();
            buffer_x += c;
          }
        }

        Serial.println(buffer_x);   // this prints response from  server

        parse_response(buffer_x);   // this parses response and extract new ssid and and password, you have   to define it

        Serial.println("--------------");

      } else {
        Serial.println("no response");
      }
    } else {
      Serial.println("send error");
    }

codes not tested

awesome! I will try it :slight_smile:
thank you sir

$new_ssid and $new_pass // I need to declare the variables to the arduino code and its values = ' '.

or also set a variable for this code :

return sendCommand("AT+CWJAP="c902b6","262459105"");

2 questions, I hope you can guide me

*Parse_response should be declared, I put parse_response(); in void setup //but it doesn't work
*How to use the $new_ssid and $new_pass in my arduino code. //in the CWJAP

any help please?

I finally received the response, by just uncomment the "parse_response(buffer_x);". How I can utilized the values coming from PHP, in order to change the wifi credentials?