Advertisement
fuccpuff

Untitled

Nov 2nd, 2023 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <DHT.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266WiFi.h>
  5.  
  6. #define LED_PIN 2
  7. #define NUM_LEDS 60
  8. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
  9.  
  10. #define DHT_PIN D3
  11. #define DHT_TYPE DHT11
  12. DHT dht(DHT_PIN, DHT_TYPE);
  13.  
  14. #define BUZZER_PIN D4
  15.  
  16. const char* ssid = "Ваше_название_сети"; // Введите имя своей Wi-Fi сети
  17. const char* password = "Ваш_пароль"; // Введите пароль для Wi-Fi сети
  18.  
  19. ESP8266WebServer server(80);
  20.  
  21. const char* webPage = R"(
  22. <!DOCTYPE html>
  23. <html>
  24. <head>
  25. <title>Умный дом</title>
  26. <meta charset='utf-8'>
  27. <style>
  28. body {
  29. font-family: Arial, sans-serif;
  30. text-align: center;
  31. background: linear-gradient(135deg, #ffe4b5, #ff6347);
  32. margin: 0;
  33. padding: 0;
  34. display: flex;
  35. flex-direction: column;
  36. align-items: center;
  37. justify-content: center;
  38. height: 100vh;
  39. }
  40. .logo {
  41. margin: 20px;
  42. animation: float 2s ease-in-out infinite;
  43. }
  44. .icon {
  45. font-size: 140px;
  46. color: #ff6347;
  47. animation: pulse 1s infinite;
  48. }
  49. .title {
  50. font-size: 28px;
  51. color: #333;
  52. margin: 20px;
  53. text-transform: uppercase;
  54. letter-spacing: 2px;
  55. }
  56. .feed-button {
  57. display: inline-block;
  58. padding: 20px 40px;
  59. background-color: #ff6347;
  60. color: #fff;
  61. border: none;
  62. border-radius: 50px;
  63. cursor: pointer;
  64. font-size: 24px;
  65. margin: 20px;
  66. transition: background-color 0.3s ease;
  67. }
  68. .feed-button:hover {
  69. background-color: #ff4500;
  70. }
  71. .loading {
  72. font-size: 24px;
  73. color: #008000;
  74. margin: 20px;
  75. display: none;
  76. }
  77. .progress-container {
  78. width: 60%;
  79. background-color: #ddd;
  80. border-radius: 10px;
  81. height: 20px;
  82. margin: 20px;
  83. }
  84. .progress-bar {
  85. width: 0;
  86. height: 100%;
  87. background-color: #00cc44;
  88. border-radius: 10px;
  89. transition: width 20s linear;
  90. }
  91. .instructions {
  92. font-size: 20px;
  93. color: #333;
  94. margin: 20px;
  95. max-width: 80%;
  96. }
  97. @keyframes pulse {
  98. 0% {
  99. transform: scale(1);
  100. }
  101. 50% {
  102. transform: scale(1.05);
  103. }
  104. 100% {
  105. transform: scale(1);
  106. }
  107. }
  108. @keyframes float {
  109. 0%, 100% {
  110. transform: translateY(0);
  111. }
  112. 50% {
  113. transform: translateY(-20px);
  114. }
  115. }
  116. </style>
  117. </head>
  118. <body>
  119. <div class='logo'><span class='icon'>🏠</span></div>
  120. <h1 class='title'>Умный дом</h1>
  121. <button class='feed-button' onclick='changeColor()'>Изменить цвет</button>
  122. <p class='loading' id='loading'>Изменение цвета...</p>
  123. <div class='progress-container'><div class='progress-bar' id='progress-bar'></div></div>
  124. <p class='instructions'>Нажмите кнопку, чтобы изменить цвет светодиодной ленты. Подождите несколько секунд.</p>
  125. <script>
  126. function changeColor() {
  127. var loading = document.getElementById('loading');
  128. var progressBar = document.getElementById('progress-bar');
  129. loading.style.display = 'block';
  130. progressBar.style.width = '100%';
  131. var color = getRandomColor();
  132. fetch('/setcolor?color=' + color)
  133. .then(response => response.text())
  134. .then(data => {
  135. console.log(data);
  136. loading.innerHTML = 'Цвет изменен!';
  137. progressBar.style.width = '0';
  138. setTimeout(function() {
  139. loading.style display = 'none';
  140. }, 3000);
  141. })
  142. .catch(error => {
  143. console.error('Ошибка: ' + error);
  144. loading.innerHTML = 'Ошибка при изменении цвета';
  145. progressBar.style.width = '0';
  146. setTimeout(function() {
  147. loading.style.display = 'none';
  148. }, 3000);
  149. });
  150. }
  151.  
  152. function getRandomColor() {
  153. var letters = '0123456789ABCDEF';
  154. var color = '#';
  155. for (var i = 0; i < 6; i++) {
  156. color += letters[Math.floor(Math.random() * 16)];
  157. }
  158. return color;
  159. }
  160. </script>
  161. </body>
  162. </html>
  163. )";
  164.  
  165. void setup() {
  166. strip.begin();
  167. strip.show();
  168. strip.setBrightness(50);
  169.  
  170. dht.begin();
  171.  
  172. pinMode(BUZZER_PIN, OUTPUT);
  173. noTone(BUZZER_PIN);
  174.  
  175. // Подключение к Wi-Fi сети
  176. WiFi.begin(ssid, password);
  177. while (WiFi.status() != WL_CONNECTED) {
  178. delay(1000);
  179. Serial.println("Подключение к Wi-Fi...");
  180. }
  181. Serial.println("Подключено к Wi-Fi");
  182. Serial.print("IP-адрес: ");
  183. Serial.println(WiFi.localIP());
  184.  
  185. server.on("/", HTTP_GET, handleRoot);
  186. server.on("/setcolor", HTTP_GET, handleSetColor);
  187. server.begin();
  188.  
  189. Serial.begin(115200);
  190. }
  191.  
  192. void loop() {
  193. server.handleClient();
  194.  
  195. float temperature = dht.readTemperature();
  196. float humidity = dht.readHumidity();
  197.  
  198. // Управление светодиодной лентой в зависимости от температуры
  199. if (temperature >= 25.0) {
  200. setLedColor(strip.Color(255, 0, 0)); // Красный цвет
  201. } else if (temperature >= 20.0) {
  202. setLedColor(strip.Color(0, 255, 0)); // Зеленый цвет
  203. } else {
  204. setLedColor(strip.Color(0, 0, 255)); // Синий цвет
  205. }
  206.  
  207. // Управление пьезоэлементом в зависимости от влажности
  208. if (humidity >= 60.0) {
  209. tone(BUZZER_PIN, 1000);
  210. } else {
  211. noTone(BUZZER_PIN);
  212. }
  213.  
  214. delay(10000); // Обновление каждые 10 секунд
  215. }
  216.  
  217. void setLedColor(uint32_t color) {
  218. for (int i = 0; i < NUM_LEDS; i++) {
  219. strip.setPixelColor(i, color);
  220. }
  221. strip.show();
  222. }
  223.  
  224. void handleRoot() {
  225. server.send(200, "text/html", webPage);
  226. }
  227.  
  228. void handleSetColor() {
  229. if (server.hasArg("color")) {
  230. String color = server.arg("color");
  231. setLedColor(strtoul(color.c_str(), 0, 16)); // Преобразование цвета в число
  232. server.send(200, "text/plain", "Цвет установлен: " + color);
  233. } else {
  234. server.send(400, "text/plain", "Ошибка: параметр 'color' отсутствует");
  235. }
  236. }
  237.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement