#include <IRremote.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h> // Library for stepper motor
// WiFi configuration
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Firebase configuration
const char* firebaseHost = "https://iot-iv-drip-default-rtdb.asia-southeast1.firebasedatabase.app";
const char* firebaseApiKey = "AIzaSyDXdwxb8Zq3zWl9lUoNwc6Gk76IzYLAZF8";
// ThingSpeak configuration
const char* thingspeakUrl = "https://api.thingspeak.com/update";
const char* thingspeakApiKey = "FEDHLIRFZADZT5CD";
// IR Receiver configuration
const int irReceiverPin = 14;
IRrecv irrecv(irReceiverPin);
decode_results results;
// Buzzer configuration
const int buzzerPin = 21;
// LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Infrared sensor configuration
const int irSensorPin = 18;
// Stepper motor configuration
const int stepPin = 23;
const int dirPin = 22;
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
// Volume tracking variable
float volumeInLiters = 1.0; // Simulated volume value
bool lowFluidAlert = false; // Tracks if fluid is low
void setup() {
Serial.begin(115200);
// Connect to WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected to WiFi!");
// Initialize IR receiver
irrecv.enableIRIn();
// Initialize buzzer
pinMode(buzzerPin, OUTPUT);
// Initialize LCD
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("IV Drip Monitor");
// Initialize stepper motor
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
stepper.setMaxSpeed(1000); // Set maximum speed
stepper.setAcceleration(500); // Set acceleration
// Initialize infrared sensor
pinMode(irSensorPin, INPUT);
Serial.println("All components initialized");
}
void loop() {
// Handle IR remote input
if (irrecv.decode(&results)) {
Serial.print("IR Code: ");
Serial.println(results.value, HEX);
irrecv.resume();
// Example action for IR code
if (results.value == 0xFFA25D) { // Replace with your specific IR code
Serial.println("Start Monitoring");
volumeInLiters = 1.0; // Reset volume
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Monitoring Started");
delay(1000);
}
}
// Infrared sensor to detect fluid level
if (digitalRead(irSensorPin) == LOW) { // Fluid level low
if (!lowFluidAlert) { // Avoid repeated alerts
lowFluidAlert = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Low Fluid Alert");
Serial.println("Low fluid detected!");
// Buzzer beeps intermittently
for (int i = 0; i < 5; i++) {
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
delay(500);
}
// Move stepper motor to notify staff or bring stand to refill station
stepper.moveTo(500); // Example: Move motor 500 steps forward
while (stepper.distanceToGo() != 0) {
stepper.run();
}
}
} else {
// Reset alert when fluid is not low
if (lowFluidAlert) {
lowFluidAlert = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fluid Level OK");
Serial.println("Fluid level normal");
}
}
// Simulate fluid level decrease for testing
volumeInLiters -= 0.01;
if (volumeInLiters < 0) {
volumeInLiters = 0;
}
// Send data to Firebase
String firebaseUrl = String(firebaseHost) + "/volumeInLiters.json?auth=" + firebaseApiKey;
HTTPClient http;
http.begin(firebaseUrl);
http.addHeader("Content-Type", "application/json");
int firebaseResponseCode = http.PUT(String(volumeInLiters, 2));
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 payload = "api_key=" + String(thingspeakApiKey) + "&field1=" + String(volumeInLiters, 2);
tsHttp.begin(thingspeakUrl);
tsHttp.addHeader("Content-Type", "application/x-www-form-urlencoded");
int tsResponseCode = tsHttp.POST(payload);
tsHttp.end();
if (tsResponseCode > 0) {
Serial.print("ThingSpeak response code: ");
Serial.println(tsResponseCode);
} else {
Serial.print("Error posting to ThingSpeak: ");
Serial.println(tsResponseCode);
}
// Debugging info
Serial.print("Volume: ");
Serial.print(volumeInLiters, 2);
Serial.println(" L");
delay(15000); // Ensure compliance with ThingSpeak's minimum update interval
}