E2

← разместить еще код

Расшаренный код:

#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <analogWrite.h>

const char* ssid = "IoT-1";
const char* password = "123456789";

const byte port_led_green = 16;
const byte port_led_yellow = 17;
const byte port_led_red = 18;

byte value_led_green = 0;
byte value_led_yellow = 0;
byte value_led_red = 0;

WebServer server(80);

void pageRoot() {                  
  String html = "<!DOCTYPE html>"
    "<html>"
    "  <head>"
    "    <title>Device Dashboard</title>"
    "    <meta charset='utf-8'>"
    "    <meta name='viewport' content='width=device-width, initial-scale=1'>"
    "  </head>"
    "  <body>"
    "    <form action='/led'>"
    "      <h2>Leds control</h2>"
    "      <p>Green: <input type='text' name='green' value='" + String(value_led_green) + "'></p>"
    "      <p>Yellow: <input type='text' name='yellow' value='" + String(value_led_yellow) + "'></p>"
    "      <p>Red: <input type='text' name='red' value='" + String(value_led_red) + "'></p>"
    "      <input type='submit'>"
    "    </form>"
    "  </body>"
    "</html>";
  server.send(200, "text/html", html);
}

void pageLed() {
  value_led_green = server.arg("green").toInt();
  value_led_yellow = server.arg("yellow").toInt();
  value_led_red = server.arg("red").toInt();

  analogWrite(port_led_green, value_led_green);
  analogWrite(port_led_yellow, value_led_yellow);
  analogWrite(port_led_red, value_led_red);
    
  server.sendHeader("Location", "/", true);
  server.send(302);
}

void setup() {
  pinMode(port_led_green, OUTPUT);
  pinMode(port_led_yellow, OUTPUT);
  pinMode(port_led_red, OUTPUT);

  WiFi.softAP(ssid, password);
  MDNS.begin("device");

  server.on("/", pageRoot);
  server.on("/led", pageLed);

  server.begin();
}

void loop(){
  server.handleClient();
}