The use of the ESP8266 in the world of IoT

User avatar
By macross
#71371 Hello masters

I need help
I am newbie in programming
therefore I had a problem in esp8266

currently I try to setting IP, SSID, Password
when ESP8266 is on the run via web

I am doing this because for every time
we replace new access point, we need to change everything

therefore to make the user easy I want to setting IP, SSID, Password
in hope that we doesnt need to reupload the code everytime we change the AP
just login to esp8266 via web and setting trough there



my code is like this
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>

// Create a webserver object that listens for HTTP request on port 80
ESP8266WebServer server(80);   

void handleRoot();              // function prototypes for HTTP handlers
void handleLogin();
void handleNotFound();

// Replace with your network details
const char* ssid = "iot";
const char* password = "password";

// Update these with values suitable for your network.
IPAddress iotip(192,168,1,2);  //Node static IP
IPAddress iotgateway(192,168,1,1);
IPAddress iotsubnet(255,255,255,0);

void setup(void)
{
  Serial.begin(9600);         // Start the Serial communication to send messages to the computer
  delay(100);
  Serial.println('\n');

  server.on("/", HTTP_GET, handleRoot);        // Call the 'handleRoot' function when a client requests URI "/"
  server.on("/login", HTTP_POST, handleLogin); // Call the 'handleLogin' function when a POST request is made to URI "/login"
  server.onNotFound(handleNotFound);           // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"

  server.begin();                            // Actually start the server
  Serial.println("HTTP server started");

}

void loop(void)
{
  if(WiFi.status() != WL_CONNECTED)
  {
    factoryap();
  }

 
 
 
  server.handleClient();                     // Listen for HTTP requests from clients
}

void factoryap()
{                         
 
  WiFi.begin(ssid, password);
  WiFi.config(iotip,iotgateway,iotsubnet);
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println('\n');
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());               // Tell us what network we're connected to
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());            // Send the IP address of the ESP8266 to the computer
}



void handleRoot()
{                         

  // When URI / is requested, send a web page
  server.send(200, "text/html", "<h1>IoTThings - Welcome to Administrator Page</h1><h3>IoTThings Firmware 1.0</h3></br></br><h5>SSID : </h5><form action=\"/ssid\" method=\"POST\"><input type=\"text\" name=\"ssid\" placeholder=\"ssid\"><h5>Password : </h5><input type=\"Password\" name=\"password\" placeholder=\"Password\"></br><input type=\"submit\" value=\"Login\"></form><p>For New User Try Administrator for 'admin' and Password for 'password' ...</p>");
}

void handleLogin()
{                         
  // If a POST request is made to URI /login
 
  // If the POST request doesn't have username and password data server.send(400, "text/plain", "400: Invalid Request");         
  // The request is invalid, so send HTTP status 400
  if( ! server.hasArg("ssid") || ! server.hasArg("password") || server.arg("ssid") == NULL || server.arg("password") == NULL)
      {
        return;
      }
  // If both the username and the password are correct
  if(server.arg("ssid") != NULL && server.arg("password") != NULL)
  {
    ssid = server.hasArg("ssid");
    password = server.hasArg("password");
  }

 
  else
  {                                                                              // Username and password don't match
    server.send(401, "text/plain", "401: Unauthorized");
  }
}

void handleNotFound()
{
  server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}



if I compile the code
it will show the error


Code: Select allArduino: 1.8.5 (Windows 7), Board: "WeMos D1 R2 & mini, 80 MHz, 921600, 4M (3M SPIFFS)"

C:\Users\Helpdesk\AppData\Local\Temp\arduino_modified_sketch_267227\sketch_oct30a.ino: In function 'void handleLogin()':

sketch_oct30a:86: error: cannot convert 'bool' to 'const char*' in assignment

     ssid = server.hasArg("ssid");

          ^

sketch_oct30a:87: error: cannot convert 'bool' to 'const char*' in assignment

     password = server.hasArg("password");

              ^

exit status 1
cannot convert 'bool' to 'const char*' in assignment

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.




I think this is because server argument wa boolean
and the IP, ssid, and password are char

I've been stuck here always
I need help here to how to do that
please i am newbie, so sometimes I dont understand enough about the programming
so please stick with me if I dont know how even a simple things