commit ad10c19214817b4cd6f6dfa29d2bfe2b344d10dc Author: Mufidah Date: Mon Jul 22 08:29:59 2024 +0700 first commit diff --git a/esp1/esp1.ino b/esp1/esp1.ino new file mode 100644 index 0000000..46d4040 --- /dev/null +++ b/esp1/esp1.ino @@ -0,0 +1,49 @@ +#include +#include +#include "MAX30100_PulseOximeter.h" + +#define REPORTING_PERIOD_MS 1000 + +SoftwareSerial mySerial(2, 0); // D3 -> TX, D4 -> RX + +PulseOximeter pox; + +uint32_t tsLastReport = 0; + +void onBeatDetected() { + Serial.println("Beat!"); +} + +void setup() { + Serial.begin(115200); + mySerial.begin(9600); + + if (!pox.begin()) { + Serial.println("FAILED"); + for(;;); + } else { + Serial.println("SUCCESS"); + } + + pox.setOnBeatDetectedCallback(onBeatDetected); +} + +void loop() { + pox.update(); + + if (millis() - tsLastReport > REPORTING_PERIOD_MS) { + float heartRate = pox.getHeartRate(); + float spO2 = pox.getSpO2(); + + Serial.print("Heart rate:"); + Serial.print(heartRate); + Serial.print(" bpm / SpO2:"); + Serial.print(spO2); + Serial.println(" %"); + + String dataToSend = String(heartRate) + "," + String(spO2); + mySerial.println(dataToSend); + + tsLastReport = millis(); + } +} diff --git a/esp2/esp2.ino b/esp2/esp2.ino new file mode 100644 index 0000000..19497c5 --- /dev/null +++ b/esp2/esp2.ino @@ -0,0 +1,248 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SS_PIN 2 //--> SDA / SS terhubung ke pin D4 +#define RST_PIN 0 //--> RST terhubung ke pin D1 +#define BUZZER_PIN 15 // Pin untuk buzzer +#define LM35_PIN A0 // Pin analog untuk sensor LM35 + +#define SDA_PIN 3 // RX untuk SDA +#define SCL_PIN 1 // TX untuk SCL + +MFRC522 mfrc522(SS_PIN, RST_PIN); + +const char* ssid = "Nanad"; // Nama SSID WiFi +const char* password = "12345678"; // Password WiFi +const char* host = "172.20.10.4"; // Alamat IP server lokal + +WiFiClient client; +ESP8266WebServer server(80); + +#define REPORTING_PERIOD_MS 500 + +uint32_t tsLastReport = 0; // Untuk mengembalikan ke 0 + +int readsuccess; +byte readcard[4]; +char str[32] = ""; +String StrUID; + +float suhu = 0; // Variabel untuk menyimpan suhu +float heartRate = 0; // Variabel untuk menyimpan detak jantung +float spO2 = 0; // Variabel untuk menyimpan SpO2 +String status; // Variabel untuk menyimpan status + +SoftwareSerial espSerial(5, 4); // Gunakan D1 -> RX, D2 -> TX untuk komunikasi dengan ESP8266 pertama + +LiquidCrystal_I2C lcd(0x27, 16, 2); // Inisialisasi LCD dengan alamat I2C 0x27 + +void setup() { + Serial.begin(115200); + espSerial.begin(9600); // Gunakan SoftwareSerial untuk berkomunikasi dengan ESP8266 pertama + + pinMode(BUZZER_PIN, OUTPUT); // Atur pin BUZZER_PIN sebagai output + digitalWrite(BUZZER_PIN, LOW); // Pastikan buzzer mati pada awalnya + + SPI.begin(); + mfrc522.PCD_Init(); + Serial.println("Menginisialisasi..."); + + // Inisialisasi Wire dengan pin custom untuk SDA dan SCL + Wire.begin(SDA_PIN, SCL_PIN); + + // Inisialisasi LCD + lcd.begin(16, 2); + lcd.backlight(); + lcd.print("Inisialisasi..."); + + connectToWifi(); + lcd.clear(); + lcd.print("WiFi Terhubung"); + delay(2000); +} + +void loop() { + readsuccess = getid(); + + if (millis() - tsLastReport > REPORTING_PERIOD_MS) { + float voltage = analogRead(LM35_PIN) * (3.3 / 1023.0); + suhu = voltage * 100.0; + + lcd.clear(); + lcd.setCursor(0, 0); + + // Cek apakah data detak jantung dari ESP8266 pertama tersedia + if (espSerial.available()) { + String data = espSerial.readStringUntil('\n'); + Serial.print("Data diterima: "); + Serial.println(data); + + // Pisahkan data detak jantung dan SpO2 + int commaIndex = data.indexOf(','); + if (commaIndex != -1) { + String heartRateStr = data.substring(0, commaIndex); + String spO2Str = data.substring(commaIndex + 1); + + heartRate = heartRateStr.toFloat(); + spO2 = spO2Str.toFloat(); + + // Tentukan status berdasarkan detak jantung + if (heartRate == 0) { + status = "Sensor belum membaca"; + } else if (heartRate < 130) { + status = "Normal"; + } else if (heartRate >= 130 && heartRate < 140) { + status = "Elevasi (Pra-hipertensi)"; + } else if (heartRate >= 140 && heartRate < 160) { + status = "Stadium 1 (Hipertensi Ringan)"; + } else if (heartRate >= 160 && heartRate < 180) { + status = "Stadium 2 (Hipertensi Sedang)"; + } else if (heartRate >= 180 && heartRate < 210) { + status = "Stadium 3 (Hipertensi Berat)"; + } else if (heartRate >= 210) { + status = "Stadium 4 (Hipertensi Maligna)"; + } else { + status = "Tidak Normal"; + } + + Serial.print("Detak jantung: "); + Serial.println(heartRate); + Serial.print(" bpm / SpO2: "); + Serial.print(spO2); + Serial.println(" %"); + Serial.print("Status: "); + Serial.println(status); + + lcd.setCursor(0, 0); + lcd.print("HR: "); + lcd.print(heartRate); + lcd.print(" bpm"); + lcd.setCursor(0, 1); + lcd.print("SpO2: "); + lcd.print(spO2); + lcd.print(" %"); + } + } + + kirimDataKeServer(suhu, heartRate, spO2, status); + tsLastReport = millis(); + } + + if (readsuccess) { + digitalWrite(BUZZER_PIN, HIGH); // Nyalakan buzzer + delay(500); // Buzzer menyala selama 500 ms + digitalWrite(BUZZER_PIN, LOW); // Matikan buzzer + + HTTPClient http; // Buat objek dari kelas HTTPClient + + String UIDresultSend, postData; + UIDresultSend = StrUID; + + // Data yang akan dipost + postData = "UIDresult=" + UIDresultSend; + + // Tentukan tujuan permintaan HTTP + http.begin(client, "http://" + String(host) + "/folder_nodemcu2/getUID.php"); + + // Tambahkan header content-type + http.addHeader("Content-Type", "application/x-www-form-urlencoded"); + + // Kirim permintaan + int httpCode = http.POST(postData); + String payload = http.getString(); + + // Tampilkan hasil di monitor serial + Serial.println(UIDresultSend); + Serial.println(httpCode); + Serial.println(payload); + + http.end(); // Tutup koneksi + delay(1000); + } +} + +void kirimDataKeServer(float suhu, float detakjantung, float oksigen, String status) { + if (WiFi.status() != WL_CONNECTED) { + Serial.println("WiFi tidak terhubung"); + return; + } + + HTTPClient http; + + String url = "http://" + String(host) + "/folder_nodemcu2/kirimdata.php?&detakjantung=" + String(detakjantung) + "&oksigen=" + String(oksigen) + "&status=" + status; + http.begin(client, url); + + int httpResponseCode = http.GET(); + + if (httpResponseCode > 0) { + String respon = http.getString(); + Serial.println("Respon dari server: " + respon); + } else { + Serial.print("Error code: "); + Serial.println(httpResponseCode); + } + + http.end(); + delay(1000); +} + +int getid() { + if (!mfrc522.PICC_IsNewCardPresent()) { + return 0; + } + if (!mfrc522.PICC_ReadCardSerial()) { + return 0; + } + + Serial.print("UID KARTU YANG DIPINDAI : "); + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("UID: "); + lcd.print(StrUID); + + for (int i = 0; i < 4; i++) { + readcard[i] = mfrc522.uid.uidByte[i]; // Menyimpan UID tag dalam readcard + array_to_string(readcard, 4, str); + StrUID = str; + } + mfrc522.PICC_HaltA(); + + return 1; +} + +// Fungsi untuk mengubah hasil pembacaan UID dalam bentuk array menjadi string +void array_to_string(byte array[], unsigned int len, char buffer[]) { + for (unsigned int i = 0; i < len; i++) { + byte nib1 = (array[i] >> 4) & 0x0F; + byte nib2 = (array[i] >> 0) & 0x0F; + buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA; + buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA; + } + buffer[len * 2] = '\0'; +} + +void connectToWifi() { + Serial.println(); + Serial.println(); + Serial.print("Menghubungkan ke "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi terhubung"); + Serial.println("Alamat IP: "); + Serial.println(WiFi.localIP()); +}