Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- #include <DHT.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266WiFi.h>
- #define LED_PIN 2
- #define NUM_LEDS 60
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
- #define DHT_PIN D3
- #define DHT_TYPE DHT11
- DHT dht(DHT_PIN, DHT_TYPE);
- #define BUZZER_PIN D4
- const char* ssid = "Ваше_название_сети"; // Введите имя своей Wi-Fi сети
- const char* password = "Ваш_пароль"; // Введите пароль для Wi-Fi сети
- ESP8266WebServer server(80);
- const char* webPage = R"(
- <!DOCTYPE html>
- <html>
- <head>
- <title>Умный дом</title>
- <meta charset='utf-8'>
- <style>
- body {
- font-family: Arial, sans-serif;
- text-align: center;
- background: linear-gradient(135deg, #ffe4b5, #ff6347);
- margin: 0;
- padding: 0;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- height: 100vh;
- }
- .logo {
- margin: 20px;
- animation: float 2s ease-in-out infinite;
- }
- .icon {
- font-size: 140px;
- color: #ff6347;
- animation: pulse 1s infinite;
- }
- .title {
- font-size: 28px;
- color: #333;
- margin: 20px;
- text-transform: uppercase;
- letter-spacing: 2px;
- }
- .feed-button {
- display: inline-block;
- padding: 20px 40px;
- background-color: #ff6347;
- color: #fff;
- border: none;
- border-radius: 50px;
- cursor: pointer;
- font-size: 24px;
- margin: 20px;
- transition: background-color 0.3s ease;
- }
- .feed-button:hover {
- background-color: #ff4500;
- }
- .loading {
- font-size: 24px;
- color: #008000;
- margin: 20px;
- display: none;
- }
- .progress-container {
- width: 60%;
- background-color: #ddd;
- border-radius: 10px;
- height: 20px;
- margin: 20px;
- }
- .progress-bar {
- width: 0;
- height: 100%;
- background-color: #00cc44;
- border-radius: 10px;
- transition: width 20s linear;
- }
- .instructions {
- font-size: 20px;
- color: #333;
- margin: 20px;
- max-width: 80%;
- }
- @keyframes pulse {
- 0% {
- transform: scale(1);
- }
- 50% {
- transform: scale(1.05);
- }
- 100% {
- transform: scale(1);
- }
- }
- @keyframes float {
- 0%, 100% {
- transform: translateY(0);
- }
- 50% {
- transform: translateY(-20px);
- }
- }
- </style>
- </head>
- <body>
- <div class='logo'><span class='icon'>🏠</span></div>
- <h1 class='title'>Умный дом</h1>
- <button class='feed-button' onclick='changeColor()'>Изменить цвет</button>
- <p class='loading' id='loading'>Изменение цвета...</p>
- <div class='progress-container'><div class='progress-bar' id='progress-bar'></div></div>
- <p class='instructions'>Нажмите кнопку, чтобы изменить цвет светодиодной ленты. Подождите несколько секунд.</p>
- <script>
- function changeColor() {
- var loading = document.getElementById('loading');
- var progressBar = document.getElementById('progress-bar');
- loading.style.display = 'block';
- progressBar.style.width = '100%';
- var color = getRandomColor();
- fetch('/setcolor?color=' + color)
- .then(response => response.text())
- .then(data => {
- console.log(data);
- loading.innerHTML = 'Цвет изменен!';
- progressBar.style.width = '0';
- setTimeout(function() {
- loading.style display = 'none';
- }, 3000);
- })
- .catch(error => {
- console.error('Ошибка: ' + error);
- loading.innerHTML = 'Ошибка при изменении цвета';
- progressBar.style.width = '0';
- setTimeout(function() {
- loading.style.display = 'none';
- }, 3000);
- });
- }
- function getRandomColor() {
- var letters = '0123456789ABCDEF';
- var color = '#';
- for (var i = 0; i < 6; i++) {
- color += letters[Math.floor(Math.random() * 16)];
- }
- return color;
- }
- </script>
- </body>
- </html>
- )";
- void setup() {
- strip.begin();
- strip.show();
- strip.setBrightness(50);
- dht.begin();
- pinMode(BUZZER_PIN, OUTPUT);
- noTone(BUZZER_PIN);
- // Подключение к Wi-Fi сети
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Подключение к Wi-Fi...");
- }
- Serial.println("Подключено к Wi-Fi");
- Serial.print("IP-адрес: ");
- Serial.println(WiFi.localIP());
- server.on("/", HTTP_GET, handleRoot);
- server.on("/setcolor", HTTP_GET, handleSetColor);
- server.begin();
- Serial.begin(115200);
- }
- void loop() {
- server.handleClient();
- float temperature = dht.readTemperature();
- float humidity = dht.readHumidity();
- // Управление светодиодной лентой в зависимости от температуры
- if (temperature >= 25.0) {
- setLedColor(strip.Color(255, 0, 0)); // Красный цвет
- } else if (temperature >= 20.0) {
- setLedColor(strip.Color(0, 255, 0)); // Зеленый цвет
- } else {
- setLedColor(strip.Color(0, 0, 255)); // Синий цвет
- }
- // Управление пьезоэлементом в зависимости от влажности
- if (humidity >= 60.0) {
- tone(BUZZER_PIN, 1000);
- } else {
- noTone(BUZZER_PIN);
- }
- delay(10000); // Обновление каждые 10 секунд
- }
- void setLedColor(uint32_t color) {
- for (int i = 0; i < NUM_LEDS; i++) {
- strip.setPixelColor(i, color);
- }
- strip.show();
- }
- void handleRoot() {
- server.send(200, "text/html", webPage);
- }
- void handleSetColor() {
- if (server.hasArg("color")) {
- String color = server.arg("color");
- setLedColor(strtoul(color.c_str(), 0, 16)); // Преобразование цвета в число
- server.send(200, "text/plain", "Цвет установлен: " + color);
- } else {
- server.send(400, "text/plain", "Ошибка: параметр 'color' отсутствует");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement