Ч5

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

#include #include #include 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; bool state_led_green = LOW; bool state_led_yellow = LOW; bool state_led_red = LOW; WebServer server(80); void pageRoot() { String info_led_green = state_led_green ? "ON (OFF)" : "OFF (ON)"; String info_led_yellow = state_led_yellow ? "ON (OFF)" : "OFF (ON)"; String info_led_red = state_led_red ? "ON (OFF)" : "OFF (ON)"; String html = "" "" " " " Device Dashboard" " " " " " " " " "

Led green: " + info_led_green + "

" "

Led yellow: " + info_led_yellow + "

" "

Led red: " + info_led_red + "

" " " ""; server.send(200, "text/html", html); } void pageLed() { String led = server.pathArg(0); bool state = server.pathArg(1) == "on" ? true : false; if (led == "green") { state_led_green = state; } else if (led == "yellow") { state_led_yellow = state; } else if (led == "red") { state_led_red = state; } digitalWrite(port_led_green, state_led_green); digitalWrite(port_led_yellow, state_led_yellow); digitalWrite(port_led_red, state_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(); }

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

#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.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;

bool state_led_green = LOW;
bool state_led_yellow = LOW;
bool state_led_red = LOW;

WebServer server(80);

void pageRoot() {
  String info_led_green = state_led_green
    ? "ON (<a href=\"/led/green/off\">OFF</a>)"
    : "OFF (<a href=\"/led/green/on\">ON</a>)";
  String info_led_yellow = state_led_yellow
    ? "ON (<a href=\"/led/yellow/off\">OFF</a>)"
    : "OFF (<a href=\"/led/yellow/on\">ON</a>)";
  String info_led_red = state_led_red
    ? "ON (<a href=\"/led/red/off\">OFF</a>)"
    : "OFF (<a href=\"/led/red/on\">ON</a>)";
                              
  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>"
    "    <p><b>Led green</b>: " + info_led_green + "</p>"
    "    <p><b>Led yellow</b>: " + info_led_yellow + "</p>"
    "    <p><b>Led red</b>: " + info_led_red + "</p>"
    "  </body>"
    "</html>";
  server.send(200, "text/html", html);
}

void pageLed() {
  String led = server.pathArg(0);
  bool state = server.pathArg(1) == "on" ? true : false;
  
  if (led == "green") {
    state_led_green = state;
  } else if (led == "yellow") {
    state_led_yellow = state;
  } else if (led == "red") {
    state_led_red = state;
  }
  
  digitalWrite(port_led_green, state_led_green);
  digitalWrite(port_led_yellow, state_led_yellow);
  digitalWrite(port_led_red, state_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();
}