// By mircemk June, 2026 #include #include "Adafruit_TCS34725.h" #include #include #include "esp_wifi.h" // ========== RGB LED PINS ========== #define LED_RED 10 #define LED_GREEN 5 // Change according to your board #define LED_BLUE 6 // ========== Wi-Fi ACCESS POINT ========== const char* password = "12345678"; WebServer server(80); // ========== SENSOR ========== Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_16X); // ========== CALIBRATION MAXIMUMS ========== const int MAX_R = 3400; const int MAX_G = 4300; const int MAX_B = 3600; // ========== GAMMA TABLE ========== byte gammaTable[256]; // ========== GLOBAL CURRENT COLORS ========== byte currentR = 0, currentG = 0, currentB = 0; // ========== HTML ========== const char index_html[] PROGMEM = R"rawliteral( Color Detector

COLOR DETECTOR

R: --
G: --
B: --
400nm450nm500nm550nm600nm650nm700nm
)rawliteral"; void handleRoot() { server.send(200, "text/html", index_html); } void handleData() { String json = "{\"r\":" + String(currentR) + ",\"g\":" + String(currentG) + ",\"b\":" + String(currentB) + "}"; server.send(200, "application/json", json); } void setup() { Serial.begin(115200); Wire.begin(8, 9); // SDA=9, SCL=8 (check if it matches your board) if (!tcs.begin()) { Serial.println("No sensor"); while(1); } tcs.setInterrupt(true); pinMode(LED_RED, OUTPUT); pinMode(LED_GREEN, OUTPUT); pinMode(LED_BLUE, OUTPUT); for (int i=0; i<256; i++) { float x = (float)i/255.0; x = pow(x, 2.2); gammaTable[i] = (byte)(x*255.0); } // ========== Wi-Fi Access Point (FIXED) ========== // Ресетирај го Wi-Fi стекот WiFi.disconnect(true, true); delay(100); WiFi.mode(WIFI_OFF); delay(100); // Create a unique SSID (with a random number) randomSeed(analogRead(0)); String ssid = "ESP32_Color_" + String(random(100, 999)); Serial.print("SSID: "); Serial.println(ssid); WiFi.mode(WIFI_AP); WiFi.softAP(ssid.c_str(), password); WiFi.setSleep(false); esp_wifi_set_ps(WIFI_PS_NONE); WiFi.softAPConfig(IPAddress(192,168,4,1), IPAddress(192,168,4,1), IPAddress(255,255,255,0)); // Set channel 6 and maximum transmit power esp_wifi_set_channel(6, WIFI_SECOND_CHAN_NONE); esp_wifi_set_max_tx_power(84); WiFi.softAPsetHostname("ESP32-Color"); Serial.print("IP: "); Serial.println(WiFi.softAPIP()); server.on("/", handleRoot); server.on("/data", handleData); server.begin(); Serial.println("Ready."); } void loop() { // Memory check static unsigned long lastMemCheck = 0; if (millis() - lastMemCheck > 5000) { lastMemCheck = millis(); int freeHeap = ESP.getFreeHeap(); Serial.print("Free heap: "); Serial.println(freeHeap); if (freeHeap < 15000) { Serial.println("CRITICAL: restarting..."); ESP.restart(); } } uint16_t r_raw, g_raw, b_raw, c_raw; tcs.setInterrupt(false); delay(60); tcs.getRawData(&r_raw, &g_raw, &b_raw, &c_raw); tcs.setInterrupt(true); byte r_norm = constrain(map(r_raw, 0, MAX_R, 0, 255), 0, 255); byte g_norm = constrain(map(g_raw, 0, MAX_G, 0, 255), 0, 255); byte b_norm = constrain(map(b_raw, 0, MAX_B, 0, 255), 0, 255); currentR = gammaTable[r_norm]; currentG = gammaTable[g_norm]; currentB = gammaTable[b_norm]; analogWrite(LED_RED, currentR); analogWrite(LED_GREEN, currentG); analogWrite(LED_BLUE, currentB); server.handleClient(); delay(100); }