#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HX711.h>
#include <SoftwareSerial.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// HX711 pins
#define DOUT 4
#define CLK 5
HX711 scale;
// MQ-2 Sensor
#define MQ2_PIN A0
// Bluetooth
#define BT_TX 10
#define BT_RX 11
SoftwareSerial bluetooth(BT_RX, BT_TX);
float weight = 0;
int gasValue = 0;
const int gasThreshold = 300; // Ajustable según pruebas reales
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
// OLED init
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED no detectada"));
while(true);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// HX711 init
scale.begin(DOUT, CLK);
scale.set_scale(); // se debe ajustar tras calibración
scale.tare(); // poner en 0
pinMode(MQ2_PIN, INPUT);
}
void loop() {
// Leer peso
weight = scale.get_units();
// Leer gas
gasValue = analogRead(MQ2_PIN);
// Mostrar en OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("Peso: ");
display.print(weight, 1);
display.println(" kg");
display.print("Gas: ");
display.println(gasValue);
if (gasValue > gasThreshold) {
display.setTextColor(SSD1306_INVERSE);
display.println("FUGA DETECTADA!");
display.setTextColor(SSD1306_WHITE);
}
display.display();
// Enviar por Bluetooth
bluetooth.print("Peso:");
bluetooth.print(weight, 1);
bluetooth.print("kg, Gas:");
bluetooth.print(gasValue);
if (gasValue > gasThreshold) {
bluetooth.print(", FUGA DETECTADA");
}
bluetooth.println();
delay(1000);
}