Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailem ESP8266 webserver formulář

Zdravím
Hledám způsob jak udělat jednoduchý formulář něco jako pro vložení SSID a hesla tady jsem něco našel ale nejsem si jistý :-(

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-input-data-html-form/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

#include <Arduino.h>
#ifdef ESP32
  #include <WiFi.h>
  #include <AsyncTCP.h>
#else
  #include <ESP8266WiFi.h>
  #include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>

AsyncWebServer server(80);

// REPLACE WITH YOUR NETWORK CREDENTIALS
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

const char* PARAM_INPUT_1 = "input1";
const char* PARAM_INPUT_2 = "input2";
const char* PARAM_INPUT_3 = "input3";

// HTML web page to handle 3 input fields (input1, input2, input3)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
  <title>ESP Input Form</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  </head><body>
  <form action="/get">
    input1: <input type="text" name="input1">
    <input type="submit" value="Submit">
  </form><br>
  <form action="/get">
    input2: <input type="text" name="input2">
    <input type="submit" value="Submit">
  </form><br>
  <form action="/get">
    input3: <input type="text" name="input3">
    <input type="submit" value="Submit">
  </form>
</body></html>)rawliteral";

void notFound(AsyncWebServerRequest *request) {
  request->send(404, "text/plain", "Not found");
}

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Failed!");
    return;
  }
  Serial.println();
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Send web page with input fields to client
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html);
  });

  // Send a GET request to <ESP_IP>/get?input1=<inputMessage>
  server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String inputMessage;
    String inputParam;
    // GET input1 value on <ESP_IP>/get?input1=<inputMessage>
    if (request->hasParam(PARAM_INPUT_1)) {
      inputMessage = request->getParam(PARAM_INPUT_1)->value();
      inputParam = PARAM_INPUT_1;
    }
    // GET input2 value on <ESP_IP>/get?input2=<inputMessage>
    else if (request->hasParam(PARAM_INPUT_2)) {
      inputMessage = request->getParam(PARAM_INPUT_2)->value();
      inputParam = PARAM_INPUT_2;
    }
    // GET input3 value on <ESP_IP>/get?input3=<inputMessage>
    else if (request->hasParam(PARAM_INPUT_3)) {
      inputMessage = request->getParam(PARAM_INPUT_3)->value();
      inputParam = PARAM_INPUT_3;
    }
    else {
      inputMessage = "No message sent";
      inputParam = "none";
    }
    Serial.println(inputMessage);
    request->send(200, "text/html", "HTTP GET request sent to your ESP on input field (" 
                                     + inputParam + ") with value: " + inputMessage +
                                     "<br><a href=\"/\">Return to Home Page</a>");
  });
  server.onNotFound(notFound);
  server.begin();
}

void loop() {
  
}
Jsou zobrazeny jen nové odpovědi. Zobrazit všechny
Předmět Autor Datum
Si přečti, co jsem Ti tu napsal, když jsem tu dal ten odkaz. Takto Ti bude wifiManager najíždět při…
BigSandy 23.12.2019 10:34
BigSandy
Já jsem to upravil takto označeno ************* je to dobře? zatím nevím jak předávat SSID a PASSWO… poslední
Víťa 26.12.2019 19:55
Víťa

Si přečti, co jsem Ti tu napsal, když jsem tu dal ten odkaz.
Takto Ti bude wifiManager najíždět při každém spouštění.
Ani sis nepřečetl, co tam píše, a plácl jsi tu první přiklad co tam je.
To delay(30000) je také pěkná prasarna a mělo by se to řešit přes millis nebo Timer.h.

Já jsem to upravil takto označeno ************* je to dobře? zatím nevím jak předávat SSID a PASSWORD ale to zatím neřeším
GPIO0 je na ESP-01 je při programování musí být na LOW ,jak zapojit tlačítko mám tam dát jako Pull UP odpor na zem nebo na 3V3 nebo to je zapnuto tímto pinMode(TRIGGER_PIN, INPUT_PULLUP)? díky


  #include <ESP8266WebServer.h>
  #include <WiFiManager.h>
  #include <DNSServer.h>
 //http://www.urel.feec.vutbr.cz/MPOA/2015/esp8266-ds18b20
  #include <ESP8266WiFi.h>
  #include <OneWire.h>
  #include <DallasTemperature.h>
  #define ONE_WIRE_BUS 2        
  OneWire oneWire(ONE_WIRE_BUS);                 // Inicializace DS18B20 na pinu GPIO2
  DallasTemperature DS18B20(&oneWire);
  
 *********** #define TRIGGER_PIN 0                          //GPIO0 na ESP8266 ESP-01 ***************
  const char* ssid     = "     ";               // Nastavení připojení k síti
  const char* password = "       ";
 
  const char* host = "api.thingspeak.com";       // Informace pro připojení k thingspeak.com
  const char* APIkey   = "";
 
  bool scnd = false; 
void setup() {
  // put your setup code here, to run once:
 *****    pinMode(TRIGGER_PIN, INPUT_PULLUP);********
*****    if(digitalRead(TRIGGER_PIN) == LOW) {***********
*****    WiFiManager wifiManager;                 ***********
*****   wifiManager.startConfigPortal("ArduinoAP");*********
    
****    }***********
   
    Serial.begin(115200);                        // Otevření komunikace po sériové lince
    delay(10);
 
    Serial.println();    
    Serial.print("Connecting to ");
    Serial.println(ssid);
 
    WiFi.begin(ssid, password);                  // Inicializace připojení k síti
 
    while (WiFi.status() != WL_CONNECTED) {      // Potvrzení připojení k síti
      delay(500);
      Serial.print(".");
      
    }
    
 
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.println();
    
  

}

void loop() {
  // put your main code here, to run repeatedly:
   delay(30000);                                // Měření a odesílání teploty probíhá v intervalu cca 30s
 
    float temp;
 
    DS18B20.requestTemperatures();               // Získání informace o teplotě teploty
    temp = DS18B20.getTempCByIndex(0);
    Serial.print("Temperature: ");
    Serial.println(temp);
 
    char charVal[12];
 
    dtostrf(temp, 8, 2, charVal);                // Konverze vyčtené teploty do stringu 
                                                 // (hodnota, počet míst, počet desetinných míst,
                                                 // umístění stringu) 
  if(scnd){
 
    Serial.print("connecting to ");
    Serial.println(host);
 
    WiFiClient client;
 
    const int httpPort = 80;
 
    if (!client.connect(host, httpPort)) {
      Serial.println("connection failed");
      return;
    }
 
    String url = "/update?key=";                // Vytvoření URL pro odeslání informace o teplotě
    url += APIkey;
    url += "&field1=";
    url += charVal;       
 
    Serial.print("Requesting URL: ");
    Serial.println(url);
 
                                                 // Odeslání požadavku na server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");
    delay(10);
 
    Serial.println();
    while (client.available()) {
      String line = client.readStringUntil('\r');
      Serial.print(line);                        // tisk informací přijatých ze serveru
    }
 
    Serial.println();
    Serial.println("connection completed");      // potvrzení ukončení odesílání
    Serial.println();
    Serial.println();
   }
   else
   {
    Serial.println("uncorrect first temperatue");
    Serial.println();                            // Ošetření nesprávnosti první změřené hodnoty po připojení
    scnd=true;                                   // k napájení
   }                                             
  }   

Zpět do poradny Odpovědět na původní otázku Nahoru