From 7c3ca26787320b477ea8d20e247717a1210be03f Mon Sep 17 00:00:00 2001 From: cogn99 <100981252+cogn99@users.noreply.github.com> Date: Mon, 7 Mar 2022 12:27:26 +0700 Subject: [PATCH] Create main_htn.ino --- main/main_htn.ino | 299 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 299 insertions(+) create mode 100644 main/main_htn.ino diff --git a/main/main_htn.ino b/main/main_htn.ino new file mode 100644 index 0000000..eb8a69f --- /dev/null +++ b/main/main_htn.ino @@ -0,0 +1,299 @@ +#include +#include +#include +#include + + +const char* ssid = "Asura"; +const char* password = "30121995@sura"; + +//Số hiệu của chân điều khiển của ESP32, 1 là số thứ tự led, R, G, B là red green blue +const int ledPin1R = 23; +const int ledPin1G = 22; +const int ledPin1B = 21; + +const int ledPin2R = 19; +const int ledPin2G = 18; +const int ledPin2B = 5; + +//giá trị RGB đươc chọn trên bảng màu trên giao diện sẽ được lưu vào đây +int redV1 = 255; +int blueV1 = 255; +int greenV1 = 255; + +//giá trị thực tế sẽ sáng ra led, được tính toán = công thức ở setBrightness +int realRedV1 = 0; +int realBlueV1 = 0; +int realGreenV1 = 0; + + +int redV2 = 255; +int blueV2 = 255; +int greenV2 = 255; + +int realRedV2 = 0; +int realBlueV2 = 0; +int realGreenV2 = 0; + +//biến lưu lại độ sáng hiện thời của 2 led +int brightnessPerc1 = 50; +int brightnessPerc2 = 50; + + +//biến lưu lại giá trị rgb dạng hex của 2 led +String rgbHex1 = "FFFFFF"; +String rgbHex2 = "FFFFFF"; + +//pwm setting, frequencyHz = số lần nhấp nháy, resolution = bit màu (2^8 = 256) +const int frequencyHz = 1; +const int resolution = 8; + +//các channel PWM của ESP32 (từ 0 => 15) +const int redC1 = 0; +const int greenC1 = 1; +const int blueC1 = 2; + +const int redC2 = 3; +const int blueC2 = 4; +const int greenC2 = 5; + +//lưu trạng thái tắt mở, nhấp nháy của led +bool isOn1 = false; +bool isOn2 = false; +bool isBlinking1 = false; +bool isBlinking2 = false; + +//lưu tên của các parameter trong request từ client +const char* INPUT_PARAMETER = "value"; +const char* HEX_PARAMETER = "hex"; + +//khai báo webServer ở port 80 +AsyncWebServer webServer(80); + +//mã html css js của webserver +const char htmlCode[] PROGMEM = +R"rawliteral( + Demo giữa kỳ

Demo báo cáo giữa kỳ

a
Led 1
%phBrightness1% %
Led 2
%phBrightness2% %
+)rawliteral"; + +//thay thế các place holder (nằm trong cặp ký tự %) thành các giá trị, để hiển thị đúng thông tin khi load lại trang +String showCurrentValues(const String & + var) { + if (var == "phBrightness1") { + return String(brightnessPerc1); + } + if (var == "phBrightness2") { + return String(brightnessPerc2); + } + if (var == "phRGB1") { + return rgbHex1; + } + if (var == "phRGB2") { + return rgbHex2; + } + + if (var == "phPowerChecked1") { + if (isOn1) { + return "checked"; + } else { + return String(); + } + } + + if (var == "phPowerChecked2") { + if (isOn2) { + return "checked"; + } else { + return String(); + } + } + return String(); +} + +void setBrightness(int brightness, bool led1) { + double percent = (double) brightness / (double) 100; + if (led1) { + realRedV1 = round((double) redV1 * percent); + realGreenV1 = round((double) greenV1 * percent); + realBlueV1 = round((double) blueV1 * percent); + + ledcWrite(redC1, realRedV1); + ledcWrite(greenC1, realGreenV1); + ledcWrite(blueC1, realBlueV1); + } else { + realRedV2 = round((double) redV2 * percent); + realGreenV2 = round((double) greenV2 * percent); + realBlueV2 = round((double) blueV2 * percent); + + ledcWrite(redC2, realRedV2); + ledcWrite(greenC2, realGreenV2); + ledcWrite(blueC2, realBlueV2); + } +} + +void ledPowerHandler(AsyncWebServerRequest * request, bool & isOn, int & brightnessPerc, bool led1) { + String inputMessage; + Serial.println("entering power method"); + if (request -> hasParam(INPUT_PARAMETER)) { + inputMessage = request -> getParam(INPUT_PARAMETER) -> value(); + if (inputMessage == "false") { + isOn = false; + setBrightness(0, led1); + } else { + isOn = true; + setBrightness(brightnessPerc, led1); + } + } else { + inputMessage = "No message sent"; + } + request -> send(200, "text/plain", "OK"); +} + +void ledBlinkingHandler(AsyncWebServerRequest * request, bool & isBlinking, bool & isOn, bool led1) { + String inputMessage; + Serial.println("entering blink method"); + if (request -> hasParam(INPUT_PARAMETER)) { + inputMessage = request -> getParam(INPUT_PARAMETER) -> value(); + if (inputMessage == "false") { + isBlinking = false; + } else { + isBlinking = true; + Serial.println(isBlinking); + Serial.println(isOn); + esp_task_wdt_init(30, false); + while (isBlinking && isOn) { + setBrightness(100, led1); + Serial.println("led blinking on"); + delay(1000); + setBrightness(0, led1); + delay(1000); + Serial.println("led blinking off"); + } + } + } else { + inputMessage = "No message sent"; + } + request -> send(200, "text/plain", "OK"); +} + +void ledBrightnessHandler(AsyncWebServerRequest * request, int & brightnessPerc, bool & isOn, bool led1) { + String inputMessage; + Serial.println("entering brightness method"); + if (request -> hasParam(INPUT_PARAMETER)) { + inputMessage = request -> getParam(INPUT_PARAMETER) -> value(); + //update lại giá trị, dù có mở hay ko, để khi mở thì sẽ lấy ngay giá trị mới + brightnessPerc = inputMessage.toInt(); + if (isOn) { + setBrightness(brightnessPerc, led1); + } + } else { + inputMessage = "No message sent"; + } + //reply cho client biết đã nhận và hoàn thành request + request -> send(200, "text/plain", "OK"); +} + +void ledRGBHandler(AsyncWebServerRequest * request, String & rgbHex, int & redV, int & greenV, int &blueV, int &brightnessPerc, bool isOn, bool led1) { + String inputMessage; + Serial.println("entering rgb method"); + if (request -> hasParam(INPUT_PARAMETER) && request -> hasParam(HEX_PARAMETER)) { + inputMessage = request -> getParam(INPUT_PARAMETER) -> value(); + + rgbHex = request -> getParam(HEX_PARAMETER) -> value(); + + int pos1 = inputMessage.indexOf('r'); + int pos2 = inputMessage.indexOf('g'); + int pos3 = inputMessage.indexOf('b'); + int pos4 = inputMessage.indexOf('&'); + + redV = inputMessage.substring(pos1 + 1, pos2).toInt(); + greenV = inputMessage.substring(pos2 + 1, pos3).toInt(); + blueV = inputMessage.substring(pos3 + 1, pos4).toInt(); + if(isOn){ + setBrightness(brightnessPerc, led1); + } + + } else { + inputMessage = "No message sent"; + } + request -> send(200, "text/plain", "OK"); +} + +void setup() { + // Begine Serial Communications over USB + Serial.begin(115200); + //gán các thông số frequencyHz và resolution vào các pwm channel + ledcSetup(redC1, frequencyHz, resolution); + ledcSetup(greenC1, frequencyHz, resolution); + ledcSetup(blueC1, frequencyHz, resolution); + //gán các pwm channel vào các chân của esp32 + ledcAttachPin(ledPin1R, redC1); + ledcAttachPin(ledPin1G, greenC1); + ledcAttachPin(ledPin1B, blueC1); + + ledcSetup(redC2, frequencyHz, resolution); + ledcSetup(greenC2, frequencyHz, resolution); + ledcSetup(blueC2, frequencyHz, resolution); + + ledcAttachPin(ledPin2R, redC2); + ledcAttachPin(ledPin2G, greenC2); + ledcAttachPin(ledPin2B, blueC2); + + // setBrightness(100,true); + // setBrightness(100,false); + + WiFi.begin(ssid, password); + Serial.print("Connecting to your WiFi"); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println(""); + Serial.println("Connected successfully, your device local IP: "); + Serial.println(WiFi.localIP()); + + //định nghĩa hàm sẽ xử lý request tương ứng với các url có tên request định nghĩa từ trước, request cụ thể của url đó sẽ được lưu trong *request + webServer.on("/", HTTP_GET, [](AsyncWebServerRequest * request) { + request -> send_P(200, "text/html", htmlCode, showCurrentValues); + }); + + + webServer.on("/brightness1", HTTP_GET, [](AsyncWebServerRequest * request) { + ledBrightnessHandler(request, brightnessPerc1, isOn1, true); + }); + + + webServer.on("/brightness2", HTTP_GET, [](AsyncWebServerRequest * request) { + ledBrightnessHandler(request, brightnessPerc2, isOn2, false); + }); + + webServer.on("/power1", HTTP_GET, [](AsyncWebServerRequest * request) { + ledPowerHandler(request, isOn1, brightnessPerc1, true); + }); + + webServer.on("/power2", HTTP_GET, [](AsyncWebServerRequest * request) { + ledPowerHandler(request, isOn2, brightnessPerc2, false); + }); + + webServer.on("/blink1", HTTP_GET, [](AsyncWebServerRequest * request) { + ledBlinkingHandler(request, isBlinking1, isOn1, true); + }); + + webServer.on("/blink2", HTTP_GET, [](AsyncWebServerRequest * request) { + ledBlinkingHandler(request, isBlinking2, isOn2, false); + }); + + webServer.on("/rgb1", HTTP_GET, [](AsyncWebServerRequest * request) { + ledRGBHandler(request, rgbHex1, redV1,greenV1,blueV1, brightnessPerc1, isOn1, true); + }); + + webServer.on("/rgb2", HTTP_GET, [](AsyncWebServerRequest * request) { + ledRGBHandler(request, rgbHex2, redV2,greenV2,blueV2, brightnessPerc2, isOn2, false); + }); + + webServer.begin(); +} + +void loop() { + +}