Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- const char* ssid = "ntw";
- const char* password = "pwd";
- ESP8266WebServer server(80);
- int pwmPin = D2; // PWM pin
- int pwmValue = 0; // PWM hodnota
- bool pwmEnabled = false; // Stav PWM
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to WiFi. IP address: ");
- Serial.println(WiFi.localIP());
- pinMode(pwmPin, OUTPUT);
- analogWriteFreq(5000); // Nastavenie frekvencie PWM na 5 kHz
- analogWrite(pwmPin, pwmValue);
- server.on("/", HTTP_GET, []() {
- String html = "<h1>Ovladanie PWM</h1>"
- "<form action=\"/increase\" method=\"POST\"><input type=\"submit\" value=\"Zvysit PWM\" style=\"height:50px; width:100px\"></form>"
- "<form action=\"/decrease\" method=\"POST\"><input type=\"submit\" value=\"Znizit PWM\" style=\"height:50px; width:100px\"></form>"
- "<form action=\"/toggle\" method=\"POST\"><input type=\"submit\" value=\"Zapnut/Vypnut PWM\" style=\"height:50px; width:100px\"></form>";
- server.send(200, "text/html", html);
- });
- server.on("/increase", HTTP_POST, []() {
- pwmValue = min(pwmValue + 100, 1023); // Zvýšenie PWM hodnoty s ochranou proti pretečeniu
- analogWrite(pwmPin, pwmEnabled ? pwmValue : 0);
- Serial.println("Aktuálna hodnota PWM: " + String(pwmValue));
- server.sendHeader("Location", "/");
- server.send(303);
- });
- server.on("/decrease", HTTP_POST, []() {
- pwmValue = max(pwmValue - 100, 0); // Zníženie PWM hodnoty s ochranou proti podtečeniu
- analogWrite(pwmPin, pwmEnabled ? pwmValue : 0);
- Serial.println("Aktuálna hodnota PWM: " + String(pwmValue));
- server.sendHeader("Location", "/");
- server.send(303);
- });
- server.on("/toggle", HTTP_POST, []() {
- pwmEnabled = !pwmEnabled;
- analogWrite(pwmPin, pwmEnabled ? pwmValue : 0);
- Serial.println("PWM " + String(pwmEnabled ? "zapnuté" : "vypnuté"));
- server.sendHeader("Location", "/");
- server.send(303);
- });
- server.begin();
- }
- void loop() {
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement