Guest User

Untitled

a guest
Jun 7th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <AsyncTCP.h>
  3. #include <ESPAsyncWebServer.h>
  4. #include <LiquidCrystal_I2C.h>  // LCD header file
  5. #include <WiFi.h>
  6. #include <WiFiClient.h>
  7.  
  8. int lcdColumns = 20;
  9. int lcdRows = 2;
  10.  
  11. LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
  12. AsyncWebServer server(80);
  13.  
  14. // Enter your netwrok credentials
  15. const char* ssid = "*******";
  16. const char* password = "*******";
  17. const char* PARAM_INPUT_1 = "data_field1";
  18. const char* PARAM_INPUT_2 = "data_field2";
  19.  
  20. // HTML web page to handle data input fields
  21.  
  22. const char index_html[] PROGMEM = R"rawliteral(<!DOCTYPE HTML> <html> <head><title> Cenovka </title><meta name = "viewport" content="width=device-width, initial-scale=1" "utf-8">
  23. <style>html{font-family: Calibri; display: inline-block; text-align: justify; Content-Type: text/html;charset=UTF-8}</style></head> <body><h2>Cenovka</h2><form action="/get">
  24. 1. radek <input type="text" name="data_field1" ><input type="submit" value="Post"></form> <br><form action="/get">2. radek <input type="text" name="data_field2"><input type="submit" value="Post">
  25. </form><br><br>Vsechen text musi byt <b>bez</b> diakritiky!</body></html>)rawliteral";
  26.  
  27. void notFound(AsyncWebServerRequest* request) {
  28.   request->send(404, "text/plain", "Not found");
  29. }
  30.  
  31. void printOnLine(int line, String message) {
  32.   lcd.setCursor(0, line);
  33.     // % : print a variable
  34.     // - : left justify
  35.     // * : use lcdColumns as minimum width (pad with spaces)
  36.     // s : it's a string type
  37.   lcd.printf("%-*s", lcdColumns, message.c_str());
  38.   Serial.printf("line %d : %-*s\n", line, lcdColumns, message.c_str());    // < prints to serial also
  39. }
  40.  
  41. void setup() {
  42.   // put your setup code here, to run once:
  43.   Serial.begin(115200);
  44.   WiFi.mode(WIFI_STA);
  45.   WiFi.begin(ssid, password);
  46.   if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  47.     Serial.println("WiFi Failed!");
  48.     return;
  49.   }
  50.   Serial.println();
  51.   Serial.print("IP Address: ");
  52.   Serial.println(WiFi.localIP());
  53.  
  54.     //===set LCD
  55.   lcd.begin();
  56.   server.begin();
  57.    
  58.   server.onNotFound(notFound);
  59.   // Send web page with input fields to client
  60.   server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
  61.     request->send(200, "text/html", index_html);
  62.   });
  63.     // respond to GET
  64.   server.on("/get", HTTP_GET, [](AsyncWebServerRequest* request) {
  65.     String inputMessage;
  66.     String inputParam;
  67.     if (request->hasParam(PARAM_INPUT_1)) {
  68.       printOnLine(0, request->getParam(PARAM_INPUT_1)->value());
  69.     } else if (request->hasParam(PARAM_INPUT_2)) {
  70.       printOnLine(1, request->getParam(PARAM_INPUT_2)->value());
  71.     } else {
  72.       // no message sent
  73.     }
  74.     request->send(200, "text/html", " HTTP GET request sent to ESP32(" + inputParam + "): " + inputMessage + "<br><a href=\"/\"> Back to Home Page </a>");
  75.   });
  76. }
  77.  
  78. void loop() {
  79.   // put your main code here, to run repeatedly:
  80. }
Advertisement
Add Comment
Please, Sign In to add comment