#include #include #include #include #define TFT_GREY 0x5AEB // WiFi credentials const char *ssid = "MLDEV"; const char *password = "Aysyw2ch?"; // Server URL const char* serverURL = "http://192.168.41.235:3000"; // IR Sensor and Backlight (via LED pin) const int inputPin = 5; const int ledPin = 22; // Backlight control pin const unsigned long displayTimeout = 10000; // Timeout in milliseconds unsigned long lastActivationTime = 0; // Tracks the last time the display was turned on // TFT Display TFT_eSPI tft = TFT_eSPI(); bool displayOn = false; // Track if the display is currently on void setup() { Serial.begin(115200); // IR sensor and backlight setup pinMode(inputPin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); // Turn off backlight initially // WiFi setup WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi!"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); // TFT setup tft.init(); tft.setRotation(1); // Horizontal orientation tft.fillScreen(TFT_GREY); } void loop() { int value = digitalRead(inputPin); // Read IR sensor unsigned long currentTime = millis(); if (value == 0) { // Hand detected lastActivationTime = currentTime; // Update the last activation time if (!displayOn) { turnOnDisplay(); } } else if (currentTime - lastActivationTime > displayTimeout) { // No hand detected and timeout elapsed if (displayOn) { turnOffDisplay(); } } // Fetch and display checklist if the display is on if (displayOn) { fetchAndDisplayChecklist(); delay(5000); // Fetch every 4 seconds } } void turnOnDisplay() { displayOn = true; fetchAndDisplayChecklist(); tft.writecommand(0x29); // Turn on the display digitalWrite(ledPin, HIGH); // Turn on the backlight Serial.println("Display turned on"); } void turnOffDisplay() { displayOn = false; tft.writecommand(0x28); // Turn off the display digitalWrite(ledPin, LOW); // Turn off the backlight Serial.println("Display turned off"); } // void turnOffDisplay() { // displayOn = false; // // tft.fillScreen(TFT_BLACK); // Make the screen completely black // // digitalWrite(ledPin, LOW); // Turn off the LED // Serial.println("Display turned off (screen black)"); // } void fetchAndDisplayChecklist() { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; http.begin(String(serverURL) + "/checklist.json"); int httpResponseCode = http.GET(); if (httpResponseCode > 0) { String payload = http.getString(); Serial.println("JSON Content Received:"); Serial.println(payload); String ingredients = extractChecklistFromJSON(payload); if (ingredients == "No data found!") { Serial.println("Error: No valid data found in JSON!"); } else { displayIngredients(ingredients); } } else { Serial.print("Error on HTTP request: "); Serial.println(httpResponseCode); } http.end(); } else { Serial.println("WiFi not connected!"); } } String extractChecklistFromJSON(const String& json) { StaticJsonDocument<512> doc; String ingredients = "No data found!"; DeserializationError error = deserializeJson(doc, json); if (!error) { JsonArray array = doc["checklist"]; ingredients = ""; // Clear the placeholder for (String item : array) { ingredients += item + ","; } if (ingredients.endsWith(",")) { ingredients.remove(ingredients.length() - 1); // Remove trailing comma } } else { Serial.print("Error parsing JSON: "); Serial.println(error.c_str()); } return ingredients; } void displayIngredients(const String& ingredients) { Serial.println("Displaying Ingredients: " + ingredients); tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextSize(2); tft.setCursor(10, 10); tft.println("Reminder of the Day <3 :"); int y = 50; // Start position for items int checkboxSize = 15; // Size of the checkbox int start = 0; int end = ingredients.indexOf(","); while (end != -1) { String item = ingredients.substring(start, end); displayCheckbox(item, 10, y, checkboxSize); y += checkboxSize + 10; // Move to the next line start = end + 1; end = ingredients.indexOf(",", start); } if (start < ingredients.length()) { String item = ingredients.substring(start); displayCheckbox(item, 10, y, checkboxSize); } } void displayCheckbox(const String& item, int x, int y, int size) { tft.drawRect(x, y, size, size, TFT_WHITE); // Draw square tft.setCursor(x + size + 10, y); // Position text next to the checkbox tft.println(item); // Print the item }