Ч3

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

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

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

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

const byte port_led = 16;

bool state_led = LOW;

WebServer server(80);

void setup() {
  pinMode(port_led, OUTPUT);
  
  WiFi.softAP(ssid, password);
  MDNS.begin("device");

  server.on("/", []() {
    if (state_led) {
      server.send(200, "text/html", "Led: ON (<a href=\"/off\">OFF</a>)");
    } else {
      server.send(200, "text/html", "Led: OFF (<a href=\"/on\">ON</a>)");
    }
  });
  server.on("/on", []() {
    state_led = HIGH;
    digitalWrite(port_led, state_led);
    server.send(200, "text/html", "Led: ON (<a href=\"/off\">OFF</a>)");
  });
  server.on("/off", []() {
    state_led = LOW;
    digitalWrite(port_led, state_led);
    server.send(200, "text/html", "Led: OFF (<a href=\"/on\">ON</a>)");
  });
  
  server.begin();
}

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