#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
#include <IRremote.h>
#include <WiFi.h> // Wi-Fi library for ESP32
#include <HTTPClient.h> // HTTP client for Firebase and ThingSpeak
// Wi-Fi Credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Firebase configuration
const char* firebaseHost = "https://iv-drip-iot-default-rtdb.asia-southeast1.firebasedatabase.app/";
const char* firebaseApiKey = "AIzaSyAu7M_esvv8UdsSYrMRsD9LlBxFsOkxo5U";
// ThingSpeak configuration
const char* thingspeakUrl = "https://api.thingspeak.com/update";
const char* thingspeakApiKey = "W02TUL9BWBNE04TU";
// Load cell setup (using HX711 library)
#define DOUT 15
#define CLK 16
HX711 scale;
// LCD setup (using LiquidCrystal_I2C library)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Setup for the infusion pump (to be controlled by motor driver)
#define DIR_PIN 4
#define STEP_PIN 5
// Buzzer setup
#define BUZZER_PIN 13
// IR Receiver setup
#define IR_PIN 12
IRrecv irrecv(IR_PIN);
decode_results results;
// Volume decrement rate
const float decrementRate = 0.01;
float volume = 2.0;
// Pumpfluid decrement rate
const int pumpDecrementRate = 0;
int pumpfluid = 80;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IV Fluid Monitor");
scale.begin(DOUT, CLK);
scale.set_scale();
scale.tare();
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
irrecv.enableIRIn();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWi-Fi connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Simulate volume decrement
if (volume > 0) {
volume -= decrementRate;
if (volume < 0) volume = 0;
// Send both volume and pumpfluid to Firebase and ThingSpeak
sendDataToCloud(volume, pumpfluid);
}
// Serial output for debugging
Serial.print("Volume: ");
Serial.print(volume);
Serial.println(" L");
Serial.print("Pumpfluid Rate: ");
Serial.print(pumpfluid);
Serial.println(" mL");
// Check IR signal
if (irrecv.decode(&results)) {
long int decCode = results.value;
Serial.println(decCode);
if (decCode == 16753245) {
togglePump();
}
irrecv.resume();
}
// Pump control based on volume
if (volume > 0.7) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Infusion: Normal");
lcd.setCursor(0, 1);
lcd.print("Volume: " + String(volume, 2) + " L");
pumpFluid(80);
noTone(BUZZER_PIN);
}
else if (volume <= 0.7 && volume > 0.1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Critical Level");
lcd.setCursor(0, 1);
lcd.print("Volume: " + String(volume, 2) + " L");
pumpFluid(30);
tone(BUZZER_PIN, 500, 4000);
}
else if (volume <= 0.1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Bag Empty!");
stopPump();
tone(BUZZER_PIN, 1000, 4000);
}
delay(1000);
}
void pumpFluid(int rate) {
Serial.print("Pumping at: ");
Serial.print(rate);
Serial.println(" mL");
digitalWrite(DIR_PIN, HIGH);
for (int i = 0; i < rate; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(500);
}
}
void stopPump() {
Serial.println("Stopping pump.");
digitalWrite(STEP_PIN, LOW);
noTone(BUZZER_PIN);
}
void togglePump() {
static bool pumpState = false;
pumpState = !pumpState;
if (pumpState) {
Serial.println("Pump ON");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pump: ON");
} else {
Serial.println("Pump OFF");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pump: OFF");
stopPump();
}
}
void sendDataToCloud(float currentVolume, int currentPumpFluid) {
// Send data to Firebase
String firebaseUrl = String(firebaseHost) + "/data.json?auth=" + firebaseApiKey;
HTTPClient http;
http.begin(firebaseUrl);
http.addHeader("Content-Type", "application/json");
String firebasePayload = "{\"volume\":" + String(currentVolume, 2) + ",\"pumpfluid\":" + String(currentPumpFluid) + "}";
int firebaseResponseCode = http.PUT(firebasePayload);
http.end();
if (firebaseResponseCode > 0) {
Serial.print("Firebase response code: ");
Serial.println(firebaseResponseCode);
} else {
Serial.print("Error sending data to Firebase: ");
Serial.println(firebaseResponseCode);
}
// Send data to ThingSpeak
HTTPClient tsHttp;
String tsPayload = "api_key=" + String(thingspeakApiKey) + "&field1=" + String(currentVolume, 2) + "&field2=" + String(currentPumpFluid);
tsHttp.begin(thingspeakUrl);
tsHttp.addHeader("Content-Type", "application/x-www-form-urlencoded");
int tsResponseCode = tsHttp.POST(tsPayload);
tsHttp.end();
if (tsResponseCode > 0) {
Serial.print("ThingSpeak response code: ");
Serial.println(tsResponseCode);
} else {
Serial.print("Error posting to ThingSpeak: ");
Serial.println(tsResponseCode);
}
}