Miguel Montiel Vega
Published

Scalable IoT System for Environmental Monitoring

This project consists of creating a technology-based solution using IoT (Internet of Things) to monitor key environmental variables

BeginnerWork in progress6 hours82
Scalable IoT System for Environmental Monitoring

Things used in this project

Hardware components

RAK12035
×1
RAK11300
×1
RAK19007-O
×1

Software apps and online services

TTN PLATFORM
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Precision screwdriver

Story

Read more

Code

Untitled file

Arduino
#include <Arduino.h>                     // Base Arduino library
#include <Wire.h>                        // Library for I2C communication
#include <Adafruit_BME680.h>             // Specific library for the BME680 sensor
#include <LoRaWan-RAK11300.h>            // RAKWireless API for LoRaWAN with RAK11300
Sensor and Variable Initialization
Adafruit_BME680 bme;                     // Create an object for the environmental sensor
________________________________________
setup()  Initial Configuration
void setup() {
  Serial.begin(115200);                  // Initialize serial port for debugging

  if (!bme.begin()) {                    // Check if the sensor responds correctly
    Serial.println("Sensor BME680 not found");
    while (1);                           // If it fails, stop the program
  }

  // Configure sensor precision and consumption
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setGasHeater(320, 150);           // Activate gas sensor at 320°C for 150 ms

  // Initialize LoRaWAN in OTAA mode
  api.lorawan.init();
  api.lorawan.setOTAAParameters("DevEUI", "AppEUI", "AppKey"); // Replace with your real keys
  api.lorawan.join();                   // Request to join the TTN network
loop()  Data Reading and LoRa Transmission Every 5 Minutes
void loop() {
  if (!bme.performReading()) {
    Serial.println("Error reading sensor data");
    return;                              // Avoid processing incorrect data
// Retrieve environmental variables
  float temp = bme.temperature;
  float hum = bme.humidity;
  float pres = bme.pressure / 100.0;     // Convert to hPa
  float iaq = bme.gas_resistance / 1000.0; // Convert to kilo-ohms

  Serial.printf("T: %.2f°C, H: %.2f%%, P: %.2f hPa, IAQ: %.2f kΩ\n", temp, hum, pres, iaq);

  // Package data to send (compact: 2 bytes per variable)
  uint8_t payload[8];
  memcpy(payload, &temp, 2);
  memcpy(payload + 2, &hum, 2);
  memcpy(payload + 4, &pres, 2);
  memcpy(payload + 6, &iaq, 2);

  // Send data over LoRaWAN (port 2, unconfirmed)
  api.lorawan.send(payload, sizeof(payload), 2, false);

  delay(300000); // Wait 5 minutes between each reading (300,000 ms)

Credits

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

Comments