Advertisement
Nemo1979

stmievac lampa led

Dec 5th, 2023 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | Source Code | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3.  
  4. const char* ssid = "ntw";
  5. const char* password = "pwd";
  6.  
  7. ESP8266WebServer server(80);
  8.  
  9. int pwmPin = D2; // PWM pin
  10. int pwmValue = 0; // PWM hodnota
  11. bool pwmEnabled = false; // Stav PWM
  12.  
  13. void setup() {
  14.   Serial.begin(115200);
  15.   WiFi.begin(ssid, password);
  16.   while (WiFi.status() != WL_CONNECTED) {
  17.     delay(500);
  18.     Serial.print(".");
  19.   }
  20.   Serial.println("");
  21.   Serial.print("Connected to WiFi. IP address: ");
  22.   Serial.println(WiFi.localIP());
  23.  
  24.   pinMode(pwmPin, OUTPUT);
  25.   analogWriteFreq(5000); // Nastavenie frekvencie PWM na 5 kHz
  26.   analogWrite(pwmPin, pwmValue);
  27.  
  28.   server.on("/", HTTP_GET, []() {
  29.     String html = "<h1>Ovladanie PWM</h1>"
  30.                   "<form action=\"/increase\" method=\"POST\"><input type=\"submit\" value=\"Zvysit PWM\" style=\"height:50px; width:100px\"></form>"
  31.                   "<form action=\"/decrease\" method=\"POST\"><input type=\"submit\" value=\"Znizit PWM\" style=\"height:50px; width:100px\"></form>"
  32.                   "<form action=\"/toggle\" method=\"POST\"><input type=\"submit\" value=\"Zapnut/Vypnut PWM\" style=\"height:50px; width:100px\"></form>";
  33.     server.send(200, "text/html", html);
  34.   });
  35.  
  36.   server.on("/increase", HTTP_POST, []() {
  37.     pwmValue = min(pwmValue + 100, 1023); // Zvýšenie PWM hodnoty s ochranou proti pretečeniu
  38.     analogWrite(pwmPin, pwmEnabled ? pwmValue : 0);
  39.     Serial.println("Aktuálna hodnota PWM: " + String(pwmValue));
  40.     server.sendHeader("Location", "/");
  41.     server.send(303);
  42.   });
  43.  
  44.   server.on("/decrease", HTTP_POST, []() {
  45.     pwmValue = max(pwmValue - 100, 0); // Zníženie PWM hodnoty s ochranou proti podtečeniu
  46.     analogWrite(pwmPin, pwmEnabled ? pwmValue : 0);
  47.     Serial.println("Aktuálna hodnota PWM: " + String(pwmValue));
  48.     server.sendHeader("Location", "/");
  49.     server.send(303);
  50.   });
  51.  
  52.   server.on("/toggle", HTTP_POST, []() {
  53.     pwmEnabled = !pwmEnabled;
  54.     analogWrite(pwmPin, pwmEnabled ? pwmValue : 0);
  55.     Serial.println("PWM " + String(pwmEnabled ? "zapnuté" : "vypnuté"));
  56.     server.sendHeader("Location", "/");
  57.     server.send(303);
  58.   });
  59.  
  60.   server.begin();
  61. }
  62.  
  63. void loop() {
  64.   server.handleClient();
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement