#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
#include <Wire.h>
// ===== Налаштування мережі =====
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ===== ПІНИ =====
#define LED_PIN 4
#define BUZZER_PIN 3
#define LDR_PIN 2
#define IR_PIN 6
#define BUTTON_PIN 5
#define SDA_PIN 8
#define SCL_PIN 9
#define NUM_LEDS 8
WebServer server(80);
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
LiquidCrystal_I2C lcd(0x27, 16, 2);
IRrecv irrecv(IR_PIN);
decode_results results;
// ===== СТАН =====
int brightness = 150;
int mode = 0; // 0: Auto, 1: Manual, 2: Rainbow, 3: Police
bool powerState = true;
unsigned long lastUpdate = 0;
// Допоміжна функція для кольорів
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
if(WheelPos < 170) { WheelPos -= 85; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); }
WheelPos -= 170; return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
// ===== ЕФЕКТИ =====
void effectRainbow() {
static byte j = 0;
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
j++;
}
void effectPolice() {
static bool flip = false;
strip.clear();
for(int i=0; i<NUM_LEDS/2; i++) {
strip.setPixelColor(flip ? i : i+NUM_LEDS/2, flip ? strip.Color(255,0,0) : strip.Color(0,0,255));
}
strip.show();
flip = !flip;
delay(100);
}
// ===== ВЕБ-ІНТЕРФЕЙС =====
String htmlPage(){
return "<!DOCTYPE html><html><head><meta charset='utf-8'><title>Smart Light</title></head><body>"
"<h1>Control Panel</h1>"
"<button onclick=\"fetch('/toggle')\">ON/OFF</button><br><br>"
"<button onclick=\"fetch('/mode?m=0')\">AUTO</button>"
"<button onclick=\"fetch('/mode?m=1')\">MANUAL</button>"
"<button onclick=\"fetch('/mode?m=2')\">RAINBOW</button>"
"<button onclick=\"fetch('/mode?m=3')\">POLICE</button><br><br>"
"Brightness: <input type='range' min='0' max='255' value='"+String(brightness)+"' onchange=\"fetch('/bright?b='+this.value)\">"
"</body></html>";
}
void setup(){
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
Wire.begin(SDA_PIN, SCL_PIN);
lcd.init();
lcd.backlight();
lcd.print("WiFi Connect...");
strip.begin();
strip.show();
irrecv.enableIRIn();
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println("\n--- CONNECTED ---");
Serial.print("Internal IP: "); Serial.println(WiFi.localIP());
lcd.clear();
lcd.print("Ready!");
delay(1500);
lcd.clear();
server.on("/", [](){ server.send(200, "text/html", htmlPage()); });
server.on("/toggle", [](){ powerState = !powerState; server.send(200); lcd.clear(); });
server.on("/mode", [](){ if(server.hasArg("m")) mode = server.arg("m").toInt(); server.send(200); lcd.clear(); });
server.on("/bright", [](){ brightness = server.arg("b").toInt(); server.send(200); });
server.begin();
}
void loop(){
server.handleClient();
// 1. Фізична кнопка (Вкл/Викл)
if(!digitalRead(BUTTON_PIN)){
powerState = !powerState;
tone(BUZZER_PIN, 1000, 50);
lcd.clear();
delay(400);
}
// 2. ІЧ ПУЛЬТ (Оновлена логіка)
if (irrecv.decode(&results)) {
unsigned long code = results.value;
Serial.print("Received IR: 0x"); Serial.println(code, HEX);
// Кнопка POWER (Вмикання/Вимикання)
if (code == 0xFFA25D || code == 0xFD00FF) {
powerState = !powerState;
}
// Кнопки 1, 2, 3, 4 (Вибір режимів)
else if (code == 0xFF30CF || code == 0xFD08F7) mode = 0; // Кнопка "1" -> AUTO
else if (code == 0xFF18E7 || code == 0xFD8877) mode = 1; // Кнопка "2" -> MANUAL
else if (code == 0xFF7A85 || code == 0xFD48B7) mode = 2; // Кнопка "3" -> RAINBOW
else if (code == 0xFF10EF || code == 0xFD28D7) mode = 3; // Кнопка "4" -> POLICE
// Кнопки + та - (Регулювання яскравості)
else if (code == 0xFF906F || code == 0xFD50AF) { // Кнопка "+"
brightness = min(255, brightness + 25);
}
else if (code == 0xFFE01F || code == 0xFD10EF) { // Кнопка "-"
brightness = max(0, brightness - 25);
}
tone(BUZZER_PIN, 1200, 30);
lcd.clear();
irrecv.resume();
}
// 3. Робота світла
if (!powerState) {
strip.clear();
strip.show();
} else {
strip.setBrightness(brightness);
if(mode == 0) { // AUTO
if(analogRead(LDR_PIN) < 1500) strip.fill(strip.Color(255,255,255));
else strip.clear();
strip.show();
}
else if(mode == 1) { strip.fill(strip.Color(255,255,255)); strip.show(); }
else if(mode == 2) { effectRainbow(); }
else if(mode == 3) { effectPolice(); }
}
// 4. Дисплей
if(millis() - lastUpdate > 500) {
lcd.setCursor(0, 0);
if (!powerState) {
lcd.print("POWER: OFF ");
} else {
lcd.print("M:");
if(mode==0) lcd.print("AUTO ");
else if(mode==1) lcd.print("MANUAL ");
else if(mode==2) lcd.print("RAINBOW");
else if(mode==3) lcd.print("POLICE ");
lcd.setCursor(13, 0); lcd.print(" ON");
}
lcd.setCursor(0, 1);
lcd.print("Bright: "); lcd.print(brightness); lcd.print(" ");
lastUpdate = millis();
}
}Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1