Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <AsyncTCP.h>
- #include <ESPAsyncWebServer.h>
- #include <LiquidCrystal_I2C.h> // LCD header file
- #include <WiFi.h>
- #include <WiFiClient.h>
- int lcdColumns = 20;
- int lcdRows = 2;
- LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
- AsyncWebServer server(80);
- // Enter your netwrok credentials
- const char* ssid = "*******";
- const char* password = "*******";
- const char* PARAM_INPUT_1 = "data_field1";
- const char* PARAM_INPUT_2 = "data_field2";
- // HTML web page to handle data input fields
- 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">
- <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">
- 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">
- </form><br><br>Vsechen text musi byt <b>bez</b> diakritiky!</body></html>)rawliteral";
- void notFound(AsyncWebServerRequest* request) {
- request->send(404, "text/plain", "Not found");
- }
- void printOnLine(int line, String message) {
- lcd.setCursor(0, line);
- // % : print a variable
- // - : left justify
- // * : use lcdColumns as minimum width (pad with spaces)
- // s : it's a string type
- lcd.printf("%-*s", lcdColumns, message.c_str());
- Serial.printf("line %d : %-*s\n", line, lcdColumns, message.c_str()); // < prints to serial also
- }
- void setup() {
- // put your setup code here, to run once:
- 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());
- //===set LCD
- lcd.begin();
- server.begin();
- server.onNotFound(notFound);
- // Send web page with input fields to client
- server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
- request->send(200, "text/html", index_html);
- });
- // respond to GET
- server.on("/get", HTTP_GET, [](AsyncWebServerRequest* request) {
- String inputMessage;
- String inputParam;
- if (request->hasParam(PARAM_INPUT_1)) {
- printOnLine(0, request->getParam(PARAM_INPUT_1)->value());
- } else if (request->hasParam(PARAM_INPUT_2)) {
- printOnLine(1, request->getParam(PARAM_INPUT_2)->value());
- } else {
- // no message sent
- }
- request->send(200, "text/html", " HTTP GET request sent to ESP32(" + inputParam + "): " + inputMessage + "<br><a href=\"/\"> Back to Home Page </a>");
- });
- }
- void loop() {
- // put your main code here, to run repeatedly:
- }
Advertisement
Add Comment
Please, Sign In to add comment