• ESP32 Điều khiển LED Sáng Dần-Tối Dần WEBSERVER

QC

ESP32 Điều khiển LED Sáng Dần-Tối Dần WEBSERVER

 ESP32 Điều khiển LED Sáng Dần-Tối Dần WEBSERVER

1. Chuẩn bị phần cứng như sau:

 - ESP32.

 - Led 3v.

 - Điện trở niếu bạn muốn (220 ohm).

KẾT NỐI LED VỚI ESP32 QUA CHÂN SỐ 2.



Nhìn vào hình ta nhận thấy 1023 LED Sáng cao nhất ( ngang bằng xuất ra mức 1).

value =0 => Led tắt ứng với Mức 0....

2. Cài đặt thư viện sau:

Tải xuống và add vào :  Sketch Include Library > Add .zip Library

3. Code chương trình ( nhớ thay đổi WIFI).

// Import required libraries
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

const int output = 2;

String sliderValue = "0";

// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;

const char* PARAM_INPUT = "value";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>ESP Web Server</title>
  <style>
    html {font-family: Arial; display: inline-block; text-align: center;}
    h2 {font-size: 2.3rem;}
    p {font-size: 1.9rem;}
    body {max-width: 400px; margin:0px auto; padding-bottom: 25px;}
    .slider { -webkit-appearance: none; margin: 14px; width: 360px; height: 25px; background: #FFD65C;
      outline: none; -webkit-transition: .2s; transition: opacity .2s;}
    .slider::-webkit-slider-thumb {-webkit-appearance: none; appearance: none; width: 35px; height: 35px; background: #003249; cursor: pointer;}
    .slider::-moz-range-thumb { width: 35px; height: 35px; background: #003249; cursor: pointer; } 
  </style>
</head>
<body>
  <h2>ESP Web Server</h2>
  <p><span id="textSliderValue">%SLIDERVALUE%</span></p>
  <p><input type="range" onchange="updateSliderPWM(this)" id="pwmSlider" min="0" max="255" value="%SLIDERVALUE%" step="1" class="slider"></p>
<script>
function updateSliderPWM(element) {
  var sliderValue = document.getElementById("pwmSlider").value;
  document.getElementById("textSliderValue").innerHTML = sliderValue;
  console.log(sliderValue);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value="+sliderValue, true);
  xhr.send();
}
</script>
</body>
</html>
)rawliteral";

// Replaces placeholder with button section in your web page
String processor(const String& var){
  //Serial.println(var);
  if (var == "SLIDERVALUE"){
    return sliderValue;
  }
  return String();
}

void setup(){
  // Serial port for debugging purposes
  Serial.begin(115200);
  
  // configure LED PWM functionalitites
  ledcSetup(ledChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(output, ledChannel);
  
  ledcWrite(ledChannel, sliderValue.toInt());

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Print ESP Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });

  // Send a GET request to <ESP_IP>/slider?value=<inputMessage>
  server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String inputMessage;
    // GET input1 value on <ESP_IP>/slider?value=<inputMessage>
    if (request->hasParam(PARAM_INPUT)) {
      inputMessage = request->getParam(PARAM_INPUT)->value();
      sliderValue = inputMessage;
      ledcWrite(ledChannel, sliderValue.toInt());
    }
    else {
      inputMessage = "No message sent";
    }
    Serial.println(inputMessage);
    request->send(200, "text/plain", "OK");
  });
  
  // Start server
  server.begin();
}
  
void loop() {
  
}

Phần 2: Giải Thích Code.

 2.1.  THƯ VIỆN.

#include <WiFi.h> #include <AsyncTCP.h> #include <ESPAsyncWebServer.h>


2.2. HÀM CẤP TRUY CẬP WIFI.

const char* ssid = "TÊN WIFI"; const char* password = "MẬT KHẨU WIFI";

2.3.  GIÁ TRỊ BAN ĐẦU CỦA LED.

String sliderValue = "0";

NIẾU 8 BIT THÌ String sliderValue = "255"; SÁNG NHẤT.

2.4. CẤU HÌNH PWM.

// setting PWM properties const int freq = 5000; // TẦN SỐ PWM const int ledChannel = 0; // KÊNH const int resolution = 8; // 8 BIT TỪ 0- 255.

2.5.  CSS trang web.

<style>

  html {font-family: Arial; display: inline-block; text-align: center;}

  h2 {font-size: 2.3rem;}

  p {font-size: 1.9rem;}

  body {max-width: 400px; margin:0px auto; padding-bottom: 25px;}

  .slider { -webkit-appearance: none; margin: 14px; width: 360px; height: 25px; background: #FFD65C;

    outline: none; -webkit-transition: .2s; transition: opacity .2s;}

  .slider::-webkit-slider-thumb {-webkit-appearance: none; appearance: none; width: 35px; height: 35px; background: #003249; cursor: pointer;}

  .slider::-moz-range-thumb { width: 35px; height: 35px; background: #003249; cursor: pointer; }

</style>


2.6 Hàm xử lý khi trượt.

<script> function updateSliderPWM(element) { var sliderValue = document.getElementById("pwmSlider").value; document.getElementById("textSliderValue").innerHTML = sliderValue; console.log(sliderValue); var xhr = new XMLHttpRequest(); xhr.open("GET", "/slider?value="+sliderValue, true); xhr.send(); } </script>


Hóa học 10 Bài 8. Định luật tuần hoàn. Ý nghĩa của bảng tuần hoàn các nguyên tố hóa học trang 43, 44 Hóa 10 Kết nối tri thức

Câu hỏi Trang 43 MĐ Định luật tuần hoàn đóng vai trò như thế nào trong việc dự đoán tính chất của các chất? Phương pháp giải: Bảng tuần hoàn...