#include <WiFi.h>
#include <WebServer.h>
// --- Піни підключення ---
const int SENSOR_PIN = 35; // фоторезистор
const int LED_R = 15;
const int LED_G = 2;
const int LED_B = 4;
const int BUZZER = 18;
// --- Налаштування Wi-Fi ---
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// --- Класи сміття ---
const char* CLASSES[] = {"Plastic", "Paper", "Glass", "Metal", "Organic"};
String instruction = "-";
String category = "-";
// --- Створення веб-сервера ---
WebServer server(80);
// --- Функція класифікації сміття ---
void classifyWaste(int value) {
// Вимикаємо всі світлодіоди
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, LOW);
digitalWrite(LED_B, LOW);
if (value < 200) {
category = "Plastic";
instruction = "Викинь у жовтий бак";
digitalWrite(LED_R, HIGH); digitalWrite(LED_G, HIGH); digitalWrite(LED_B, LOW);
tone(BUZZER, 1000, 200);
} else if (value < 400) {
category = "Paper";
instruction = "Викинь у синій бак";
digitalWrite(LED_R, LOW); digitalWrite(LED_G, LOW); digitalWrite(LED_B, HIGH);
tone(BUZZER, 800, 200);
} else if (value < 600) {
category = "Glass";
instruction = "Викинь у зелений бак";
digitalWrite(LED_R, LOW); digitalWrite(LED_G, HIGH); digitalWrite(LED_B, LOW);
tone(BUZZER, 600, 200);
} else if (value < 800) {
category = "Metal";
instruction = "Віднеси у металевий контейнер";
digitalWrite(LED_R, HIGH); digitalWrite(LED_G, HIGH); digitalWrite(LED_B, HIGH);
tone(BUZZER, 400, 200);
} else {
category = "Organic";
instruction = "Органічні відходи — на компост";
digitalWrite(LED_R, HIGH); digitalWrite(LED_G, LOW); digitalWrite(LED_B, LOW);
tone(BUZZER, 200, 200);
}
}
// --- HTML-сторінка (оновлений дизайн) ---
const char MAIN_page[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Smart Recycling</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #a8edea, #fed6e3);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
h1 {
color: #333;
margin-bottom: 20px;
text-shadow: 1px 1px 2px #fff;
}
.card {
background: rgba(255,255,255,0.8);
padding: 20px 40px;
border-radius: 15px;
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
text-align: center;
}
p {
font-size: 1.2em;
margin: 10px 0;
}
span {
font-weight: bold;
color: #0077cc;
}
</style>
</head>
<body>
<h1>♻️ Smart Recycling</h1>
<div class="card">
<p>Тип сміття: <span id="cat">-</span></p>
<p>Інструкція: <span id="ins">-</span></p>
</div>
<script>
async function refresh() {
const response = await fetch('/data');
const data = await response.json();
document.getElementById('cat').textContent = data.category;
document.getElementById('ins').textContent = data.instruction;
}
setInterval(refresh, 1000);
</script>
</body>
</html>
)rawliteral";
// --- Обробники маршрутів ---
void handleRoot() { server.send(200, "text/html", MAIN_page); }
void handleData() {
String json = "{ \"category\": \"" + category + "\", \"instruction\": \"" + instruction + "\" }";
server.send(200, "application/json", json);
}
// --- Налаштування ---
void setup() {
Serial.begin(115200);
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
pinMode(BUZZER, OUTPUT);
// Підключення до Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Підключення до Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Підключено! IP-адреса: "); Serial.println(WiFi.localIP());
// Ініціалізація сервера
server.on("/", handleRoot);
server.on("/data", handleData);
server.begin();
}
// --- Основний цикл ---
void loop() {
server.handleClient();
int sensorValue = analogRead(SENSOR_PIN); // Зчитування даних сенсора
classifyWaste(sensorValue);
Serial.print("Sensor: "); Serial.print(sensorValue);
Serial.print(" → "); Serial.println(category);
delay(1000);
}