Miguel Montiel Vega
Published

Environmental Monitoring of Electronics Laboratory

"Instantly monitor your lab! Real-time temp, humidity, gas data, plus alerts for total control. "

IntermediateWork in progress10 hours63
Environmental Monitoring of Electronics Laboratory

Things used in this project

Story

Read more

Code

Untitled fileEnvironmental Monitoring of Electronics Laboratory

C/C++
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <bsec.h>                // Librería para BME680 (RAK1906)
#include <MQ2.h>                 // Librería para MQ2 (RAK12004)
#include <LoRaWan-RAK4630.h>     // SDK de RAK para LoRaWAN

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Pines del MQ2
#define MQ2_PIN A0
MQ2 mq2(MQ2_PIN);

// BSEC para BME680
Bsec iaqSensor;

// Credenciales LoRaWAN
uint8_t nodeDevEUI[8] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX };
uint8_t nodeAppEUI[8] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX };
uint8_t nodeAppKey[16] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX,
                           0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX };

// Intervalo de envío (en milisegundos)
uint32_t sendInterval = 5 * 60 * 1000;
TimerEvent_t sendTimer;

void setup() {
  Serial.begin(115200);
  Wire.begin();

  // Inicializar OLED
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("No se encontró la pantalla OLED"));
    while (true);
  }
  display.clearDisplay();

  // Inicializar BME680
  iaqSensor.begin(BME680_I2C_ADDR_PRIMARY, Wire);
  iaqSensor.setConfig(bsec_config_iaq);

  // Inicializar MQ2
  mq2.begin();

  // Inicializar LoRaWAN
  api.lorawan.initOTAA(nodeDevEUI, nodeAppEUI, nodeAppKey, true, CLASS_A, REGION_EU868);
  api.lorawan.join();

  // Configurar timer para enviar datos
  TimerInit(&sendTimer, onSend);
  TimerSetValue(&sendTimer, sendInterval);
  TimerStart(&sendTimer);
}

void loop() {
  api.lorawan.run();
  iaqSensor.run();
  delay(100);
}

// Función de envío de datos
void onSend(void) {
  if (!api.lorawan.isJoined()) {
    Serial.println("Aún no unido a LoRaWAN");
    TimerStart(&sendTimer);
    return;
  }

  // Leer BME680
  float temperature = iaqSensor.temperature;
  float humidity = iaqSensor.humidity;
  float pressure = iaqSensor.pressure;
  float iaq = iaqSensor.iaq;

  // Leer MQ2
  float gas = mq2.readLPG();

  // Mostrar en OLED
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("Temp: "); display.print(temperature); display.println(" C");
  display.print("Hum: "); display.print(humidity); display.println(" %");
  display.print("Pres: "); display.print(pressure); display.println(" hPa");
  display.print("IAQ: "); display.print(iaq);
  display.print(" MQ2: "); display.println(gas);
  display.display();

  // Crear payload simple (ejemplo)
  uint8_t payload[8];
  payload[0] = (uint8_t)temperature;
  payload[1] = (uint8_t)humidity;
  payload[2] = (uint16_t)(pressure / 10) >> 8;
  payload[3] = (uint16_t)(pressure / 10) & 0xFF;
  payload[4] = (uint8_t)iaq;
  payload[5] = (uint16_t)gas >> 8;
  payload[6] = (uint16_t)gas & 0xFF;
  payload[7] = 0x00; // Libre

  api.lorawan.send(1, payload, sizeof(payload), false);

  Serial.println("Datos enviados por LoRaWAN");

  TimerStart(&sendTimer);
}

Credits

Miguel Montiel Vega
11 projects • 8 followers
Teacher at Maude Studio & Erasmus+ project member: "Developing Solutions to Sustainability Using IoT" (2022-1-PT01-KA220-VET-000090202)
Thanks to Jose Migue Fuentes.

Comments