0

I am struggling to use ESP8266 client to log into my web server (which is running in a PC host in local network). The ESP must login in order to see weather it must activate the sensor connected to it or not. Users can change the state of sensor activation (on/off) by logging in their portal. Therefore the esp8266 must be always aware of that. The way that comes to my mind is that the ESP8266 must log in like other browser clients. First, it should enter the login page through the following code:

client.print(String("GET /public_html/login.php") + " HTTP/1.1\r\n" +
             "Host: " + host + "\r\n" +
             "Connection: close\r\n" +
             "\r\n"
            );

Then I should post the username and password to the server. Normal human users who log in through a browser log in through the form below:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
            <label>username</label>
            <input type="text" name="username" class="form-control" value="<?php echo $username; >">
        </div>    
        <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
            <label>password</label>
            <input type="password" name="password" class="form-control">
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Login">
        </div>
    </form>

How can I do the same thing implemented above in ESP8266?

Bornak
  • 69
  • 2
  • 7
  • The HTML is more for human to interactive with server through browser, if your web client is an ESP8266, then you should develop a set of REST APIs on your server to "listen" for the various requests from the ESP8266. Your esp8266 send post requests to /login endpoint direct to the server instead of going through the index.html page. – hcheung May 21 '20 at 12:34
  • try to use ESP8266HTTPClient library and use setAuthorization to send the login and password for all requests – Juraj May 21 '20 at 13:00
  • @Jura Thanks. I am pretty new in this area. could you please explain a little bit more in form of an answer? – Bornak May 21 '20 at 14:11
  • I think if I could define an associative array, in which username and password values are defined and sent to server through post method, this problem will be solved. But I am not sure that such array can be defined in Arduino IDE. – Bornak May 21 '20 at 15:09
  • https://en.wikipedia.org/wiki/Basic_access_authentication – Juraj May 21 '20 at 15:50

1 Answers1

0

First you have to define a post route at server which can listen to esp post requests. And you can send a json string from esp with post method which contains encrypted credentials. Depending on credentials received at server end you can decide whether to authenticate or not the device and respond the same result back to device.

Suraj Inamdar
  • 181
  • 1
  • 1