Get config from web server

Hello,
I'm trying to get some ESP8266 config values from a web page.
The ESP is powered on with a REED swith and stay powered by a GPIO higt level on a mosfet .
It send an SMS
After a predeterminated time it swith off hit's power
This process is automatic but I would like to read a value form a webpage and following the value configure or stay alive for update via OTA.
How to proceed to get a feedback form the web page (SetConfig)
What to put ine the "config.html" page

#include <WiFiManager.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>

ESP8266WebServer server(80);

int IntTestMode = 10;  // intervalle de 10S
unsigned long PrevMillis1 = 0;
const char* WifiPassword = "xxxxxxxx";
const char* WebHost = "http://192.168.12.200/config.html";

void SetConfig() {
  Serial.println("***** Stay alive forever *****");
}

void setup() {
  Serial.begin(115200);

  WiFiManager wifiManager;
  WiFi.mode(WIFI_STA);  // explicitly set mode, esp defaults to STA+AP
  wifiManager.setConfigPortalTimeout(180);
  if (!wifiManager.autoConnect(host, WifiPassword)) {
    Serial.println("failed to connect and hit timeout");
    ESP.restart();
  }

  server.on("/SetConfig", SetConfig);
  server.begin();
}

void loop() {
  server.handleClient();

  if (millis() - PrevMillis1 >= (IntTestMode * 1000)) {
    WiFiClient espClient;
    if (WiFi.status() == WL_CONNECTED) {
      HTTPClient http;
      http.begin(espClient, WebHost);
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");
      String httpRequestData = "message";
      Serial.println(httpRequestData);
      int httpResponseCode = http.POST(httpRequestData);
      Serial.println("HTTP Response code: ");
      http.end();
    }
    PrevMillis1 = millis();
  }
}

Web page :


<!DOCTYPE html>
<html>
<body onload="SetConf()">

<script>
function SetConf() {
  var Config = "Test envoi config";
  var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "SetConfig?" + "Config=" + Config, true);
  xhttp.send();
}
</script>
</body>
</html>

Why do you send that header? Your content isn't form data.

No HTML data I would suggest. Your Arduino is not a web browser, so putting any Javascript code into that HTML doesn't make any sense. The Arduino won't be able to execute that. I would simply send text data. You can change the extension of the file to .txt if you like (it isn't needed) but I wouldn't send HTML as it makes the parsing on the Arduino side more complicated and doesn't help in any way.

you are right for the javascript code, It was a wrong way, I will try with a simple text file.
May be checking if a predeterminated file exist will be sufficiant or a php file can reply something to the ESP.

I put only the MAC address into the text file named with the MAC address : D5BF40C0AB46.txt
and got the requested function

void loop() {

  if (millis() - PrevMillis1 >= (IntTestMode * 1000)) {
    WiFiClient espClient;
    if (WiFi.status() == WL_CONNECTED) {
      HTTPClient http;
      http.begin(espClient, WebHost + "/" + MAC + ".txt");
      int httpCode = http.GET();
      if (httpCode > 0) {
        String payload = http.getString();
        Serial.println(payload);
        if (payload.indexOf(MAC) != -1) {
          Serial.println("***** Stay alive forever *****");
        } 
        http.end();
      }
    }
    PrevMillis1 = millis();
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.