#include #include #include #include #include #include #include #include #include #include #include "logo_dmh.h" #include "hospital_bg.h" // Set to true to display the hospital building photo in the background, or false for clean white #define SHOW_BACKGROUND_IMAGE true const char* ssid = "Prajwal"; const char* password = ""; const char* api_base_url = "http://10.25.4.115:8000"; const char* device_id = "ICU_01"; const char* api_key = "esp32_secret_key_2024"; // --- Pin Definitions --- #define PIN_CS 5 #define PIN_DC 17 #define PIN_RST 16 #define PIN_BUSY 4 #define PIN_PWR 2 // ========================================== // DISPLAY DRIVER SETUP // ========================================== // Driver: GxEPD2_750c_Z08 matching the '075RW-Z08-C2' sticker on the panel. // BUSY pin set to -1 to BYPASS the hardware BUSY signal. // GPIO 4 floats HIGH, making the library think the screen is always ready // (completing the refresh in 2ms). With -1, the library uses correct fixed // delays giving the panel the full ~20-second refresh cycle it physically needs. GxEPD2_3C display(GxEPD2_750c_Z08(PIN_CS, PIN_DC, PIN_RST, PIN_BUSY)); bool displayDrawn = false; unsigned long lastUpdate = 0; const unsigned long updateInterval = 10000; // Poll heartbeat every 10 seconds // ========================================== // HELPER FUNCTIONS // ========================================== void connectWiFi() { Serial.print("Connecting to WiFi"); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected! IP: " + WiFi.localIP().toString()); } // Returns "--" for null JSON values, otherwise the string value. String safeStr(JsonVariant v) { if (v.isNull()) return "--"; return v.as(); } // ArduinoJson v7: use JsonDocument (DynamicJsonDocument removed in v7) void drawDisplay(JsonDocument& data) { display.setFullWindow(); display.firstPage(); do { display.fillScreen(GxEPD_WHITE); #if SHOW_BACKGROUND_IMAGE // ========================================== // 0. HOSPITAL BUILDING BACKGROUND // ========================================== display.drawBitmap(0, 0, hospital_bg, BG_WIDTH, BG_HEIGHT, GxEPD_BLACK); #endif // ========================================== // 1. TOP HEADER BAR (y: 0 to 65) // ========================================== display.fillRect(0, 0, 800, 65, GxEPD_WHITE); display.drawRect(0, 0, 800, 65, GxEPD_BLACK); display.drawRect(0, 0, 800, 64, GxEPD_BLACK); // Draw DMH Logo in RED display.drawBitmap(12, 0, logo_dmh, LOGO_DMH_WIDTH, LOGO_DMH_HEIGHT, GxEPD_RED); display.setTextColor(GxEPD_BLACK); display.setFont(&FreeSansBold12pt7b); display.setCursor(85, 28); display.print("DEENANATH MANGESHKAR HOSPITAL"); display.setFont(&FreeSansBold9pt7b); display.setCursor(85, 52); display.print("ICU PATIENT WARD"); String roomStr = "ROOM: " + safeStr(data[0]["room"]); display.setFont(&FreeSansBold12pt7b); display.setCursor(620, 38); display.print(roomStr); // ========================================== // 2. PATIENT DEMOGRAPHICS CARD (y: 75 to 165) // ========================================== display.fillRect(15, 75, 770, 90, GxEPD_WHITE); display.drawRect(15, 75, 770, 90, GxEPD_BLACK); // Patient Name display.setTextColor(GxEPD_BLACK); display.setFont(&FreeSansBold18pt7b); display.setCursor(30, 112); display.print(safeStr(data[0]["patient_name"])); // Patient Status (KEPT AS IT IS!) String statusStr = data[0]["status"].as(); if (statusStr.length() == 0 || statusStr == "null") statusStr = "Stable"; uint16_t statusColor = (statusStr.equalsIgnoreCase("Critical")) ? GxEPD_RED : GxEPD_BLACK; display.setFont(&FreeSansBold12pt7b); display.setTextColor(statusColor); display.setCursor(540, 112); display.print("STATUS: " + statusStr); // Demographics Sub-Row with Age / Gender, Doctor, Blood (KEPT AS IT IS!) display.setFont(&FreeSansBold9pt7b); display.setTextColor(GxEPD_BLACK); display.setCursor(30, 145); display.print("Age / Gender: " + safeStr(data[0]["age"]) + " / " + safeStr(data[0]["gender"])); display.setCursor(320, 145); display.print("Doctor: " + safeStr(data[0]["doctor"])); display.setCursor(620, 145); display.print("Blood: " + safeStr(data[0]["blood_group"])); // ========================================== // 3. DIETARY / FOOD POLICY (y: 175 to 230) // ========================================== display.fillRect(15, 175, 770, 55, GxEPD_WHITE); display.drawRect(15, 175, 770, 55, GxEPD_BLACK); display.setFont(&FreeSansBold9pt7b); display.setTextColor(GxEPD_BLACK); display.setCursor(30, 195); display.print("DIETARY / FOOD POLICY:"); String foodInstr = safeStr(data[0]["food_instructions"]); if (foodInstr == "--" || foodInstr.length() == 0) foodInstr = "No outside food allowed"; display.setFont(&FreeSansBold12pt7b); display.setTextColor(GxEPD_RED); display.setCursor(240, 215); display.print(foodInstr); // ========================================== // 4. MIDDLE PANELS: LEFT (Diagnosis & Restrictions) & RIGHT (Visiting Hours) // ========================================== // LEFT COLUMN (x: 15 to 390, width: 375, y: 240 to 395) display.fillRect(15, 240, 375, 155, GxEPD_WHITE); display.drawRect(15, 240, 375, 155, GxEPD_BLACK); // Primary Diagnosis display.setFont(&FreeSansBold9pt7b); display.setTextColor(GxEPD_BLACK); display.setCursor(28, 260); display.print("PRIMARY DIAGNOSIS"); String diagVal = safeStr(data[0]["diagnosis"]); if (diagVal == "--" || diagVal.length() == 0) diagVal = "Fever"; display.drawRect(28, 270, 345, 38, GxEPD_BLACK); display.setFont(&FreeSansBold12pt7b); display.setCursor(45, 296); display.print(diagVal); // Restrictions (Not Allowed) display.setFont(&FreeSansBold9pt7b); display.setCursor(28, 330); display.print("RESTRICTIONS (NOT ALLOWED)"); String restrictVal = safeStr(data[0]["restrictions"]); if (restrictVal == "--" || restrictVal.length() == 0) restrictVal = "Dogs are not allowed"; display.drawRect(28, 340, 345, 38, GxEPD_RED); display.setFont(&FreeSansBold12pt7b); display.setTextColor(GxEPD_RED); display.setCursor(45, 366); display.print(restrictVal); // RIGHT COLUMN (x: 405 to 785, width: 380, y: 240 to 395) display.fillRect(405, 240, 380, 155, GxEPD_WHITE); display.drawRect(405, 240, 380, 155, GxEPD_BLACK); display.setFont(&FreeSansBold9pt7b); display.setTextColor(GxEPD_BLACK); display.setCursor(420, 260); display.print("VISITING POLICY & HOURS"); String visitRules = safeStr(data[0]["visiting_rules"]); if (visitRules == "--" || visitRules.length() == 0) visitRules = "2 times visits"; display.drawRect(420, 270, 350, 35, GxEPD_RED); display.setFont(&FreeSansBold12pt7b); display.setTextColor(GxEPD_RED); display.setCursor(440, 295); display.print(visitRules); // ========================================== // Dynamic Visiting Hours Parsing // ========================================== String visitHours = safeStr(data[0]["visiting_hours"]); String morningHours = "9:00 - 11:00 AM"; String eveningHours = "4:00 - 6:00 PM"; if (visitHours != "--" && visitHours.length() > 0) { int commaIdx = visitHours.indexOf(','); if (commaIdx < 0) commaIdx = visitHours.indexOf(';'); if (commaIdx > 0) { morningHours = visitHours.substring(0, commaIdx); morningHours.trim(); eveningHours = visitHours.substring(commaIdx + 1); eveningHours.trim(); } else { morningHours = visitHours; eveningHours = ""; } } display.setTextColor(GxEPD_BLACK); if (morningHours.length() > 22 || eveningHours.length() > 22) { display.setFont(&FreeSansBold9pt7b); } else { display.setFont(&FreeSansBold12pt7b); } display.setCursor(425, 335); if (eveningHours.length() > 0) { display.print("Morning : " + morningHours); display.setCursor(425, 370); display.print("Evening : " + eveningHours); } else { display.print("Hours : " + morningHours); } // ========================================== // 5. FOOTER (y: 405 to 450) // ========================================== display.fillRect(15, 405, 770, 45, GxEPD_WHITE); display.drawRect(15, 405, 770, 45, GxEPD_BLACK); display.setFont(&FreeSansBold9pt7b); display.setTextColor(GxEPD_BLACK); display.setCursor(30, 432); display.print("Device: " + String(device_id)); display.setCursor(420, 432); display.print("Last Updated: " + safeStr(data[0]["last_updated"])); } while (display.nextPage()); Serial.println("Display refresh finished."); } void fetchDisplayData() { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; String url = String(api_base_url) + "/api/display/" + String(device_id); Serial.println("Fetching data from: " + url); http.setTimeout(10000); // 10s timeout to prevent read errors http.begin(url); int httpResponseCode = http.GET(); if (httpResponseCode == 200) { String payload = http.getString(); Serial.println("Received Payload: " + payload); JsonDocument doc; // ArduinoJson v7 - no size needed DeserializationError error = deserializeJson(doc, payload); if (!error) { Serial.println("Data parsed. Pausing to let voltage stabilize..."); delay(1500); // Let USB voltage recover from WiFi spike WiFi.setSleep(true); drawDisplay(doc); displayDrawn = true; WiFi.setSleep(false); } else { Serial.println("Failed to parse JSON!"); } } else { Serial.print("Error fetching data, HTTP code: "); Serial.println(httpResponseCode); } http.end(); } else { Serial.println("WiFi not connected!"); } } bool checkHeartbeat() { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; String url = String(api_base_url) + "/api/device/heartbeat"; http.setTimeout(10000); http.begin(url); http.addHeader("Content-Type", "application/json"); JsonDocument doc; // ArduinoJson v7 doc["device_id"] = device_id; doc["api_key"] = api_key; doc["firmware_version"] = "1.0.0"; doc["ip_address"] = WiFi.localIP().toString(); String requestBody; serializeJson(doc, requestBody); int httpResponseCode = http.POST(requestBody); bool pending_update = false; if (httpResponseCode == 200) { String payload = http.getString(); JsonDocument respDoc; // ArduinoJson v7 deserializeJson(respDoc, payload); pending_update = respDoc["pending_update"] | false; } else { Serial.print("Heartbeat failed, HTTP code: "); Serial.println(httpResponseCode); } http.end(); return pending_update; } return false; } // ========================================== // MAIN SETUP & LOOP // ========================================== void setup() { Serial.begin(115200); delay(2000); // Turn ON power to the e-Paper display HAT via GPIO 2 pinMode(PIN_PWR, OUTPUT); digitalWrite(PIN_PWR, HIGH); delay(200); // Allow display power to stabilize // BUSY pin is an active output from the Waveshare HAT pinMode(PIN_BUSY, INPUT); Serial.println("Initializing Display..."); // Initialize hardware SPI with explicit pin mapping SPI.begin(18, 19, 23, PIN_CS); // SCK=18, MISO=19, MOSI=23, SS=CS // 2MHz SPI - prevents data corruption over jumper wires display.epd2.selectSPI(SPI, SPISettings(2000000, MSBFIRST, SPI_MODE0)); // 2ms reset pulse - required for Waveshare boards with the reset transistor circuit display.init(115200, true, 2, false); display.epd2.selectFastFullUpdate(false); display.setRotation(0); // 0 = Horizontal Landscape (800x480) connectWiFi(); // Fetch and display patient data immediately on boot fetchDisplayData(); } void loop() { if (millis() - lastUpdate > updateInterval) { lastUpdate = millis(); Serial.println("Sending heartbeat..."); if (checkHeartbeat() || !displayDrawn) { Serial.println("Update pending or initial draw needed. Fetching new data..."); fetchDisplayData(); } else { Serial.println("No update pending."); } } }