Incorporar Antena LoRa en Código
1:09
Cpp
#include <Wire.h> #include <SPI.h> #include <LoRa.h> #include <Adafruit_Sensor.h> #include <Adafruit_BMP280.h> #include <MPU6050.h> #include <OneWire.h> #include <DallasTemperature.h> #include <TinyGPSPlus.h> #include <HardwareSerial.h> // Pines personalizados para sensores y comunicación #define SDA_PIN 19 #define SCL_PIN 18 #define ONE_WIRE_BUS 15 #define GPS_RX 16 // ESP32 RX <-- GPS TX #define GPS_TX 17 // ESP32 TX --> GPS RX // Pines para LoRa (ajusta según tu hardware) #define LORA_SS 5 #define LORA_RST 14 #define LORA_DIO0 2 // Frecuencia LoRa (ajusta a la utilizada en tu región, por ejemplo, 868E6 o 915E6) #define LORA_FREQUENCY 868E6 // Objetos de sensores Adafruit_BMP280 bmp; MPU6050 mpu; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature ds18b20(&oneWire); TinyGPSPlus gps; HardwareSerial ss(1); // GPS en Serial1 // Variables del MPU6050 float angleX = 0, angleY = 0, angleZ = 0; unsigned long lastTime = 0; void setup() { Serial.begin(115200); ss.begin(9600, SERIAL_8N1, GPS_RX, GPS_TX); // Inicializar GPS Wire.begin(SDA_PIN, SCL_PIN); // Inicializar BMP280 if (!bmp.begin(0x76)) { Serial.println("No se encontró el BMP280."); while (1); } // Inicializar MPU6050 mpu.initialize(); if (!mpu.testConnection()) { Serial.println("Error al conectar el MPU6050."); while (1); } // Inicializar DS18B20 ds18b20.begin(); analogReadResolution(12); // Inicializar LoRa LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0); if (!LoRa.begin(LORA_FREQUENCY)) { Serial.println("Fallo al inicializar LoRa."); while (1); } else { Serial.println("LoRa iniciado correctamente."); } } void loop() { // ==== LECTURAS DEL MPU6050 ==== int16_t ax, ay, az, gx, gy, gz; mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); unsigned long currentTime = millis(); float deltaTime = (currentTime - lastTime) / 1000.0; lastTime = currentTime; angleX += gx * deltaTime / 131.0; angleY += gy * deltaTime / 131.0; angleZ += gz * deltaTime / 131.0; angleX = fmod(angleX + 360, 360); angleY = fmod(angleY + 360, 360); angleZ = fmod(angleZ + 360, 360); float ax_mps2 = ax * (9.81 / 16384.0); float ay_mps2 = ay * (9.81 / 16384.0); float az_mps2 = az * (9.81 / 16384.0); // ==== BMP280 ==== float bmp_temp = bmp.readTemperature(); float pressure = bmp.readPressure() / 100.0; // hPa float altitude = bmp.readAltitude(1013.25); // m // ==== DS18B20 ==== ds18b20.requestTemperatures(); float ds_temp = ds18b20.getTempCByIndex(0); // ==== GPS ==== while (ss.available()) { gps.encode(ss.read()); } // ==== Conformar mensaje CSV con las medidas ==== String mensaje = ""; mensaje += String(pressure, 2) + ","; // Presión hPa mensaje += String(bmp_temp, 2) + ","; // Temp BMP280 mensaje += String(altitude, 2) + ","; // Altitud estimada mensaje += String(ds_temp, 2) + ","; // Temp DS18B20 mensaje += String(ax_mps2, 2) + ","; // aceleración X mensaje += String(ay_mps2, 2) + ","; // aceleración Y mensaje += String(az_mps2, 2) + ","; // aceleración Z mensaje += String(angleX, 2) + ","; // ángulo X mensaje += String(angleY, 2) + ","; // ángulo Y mensaje += String(angleZ, 2) + ","; // ángulo Z if (gps.location.isValid()) { mensaje += String(gps.location.lat(), 6) + ","; // latitud mensaje += String(gps.location.lng(), 6) + ","; // longitud mensaje += String(gps.altitude.meters(), 2) + ","; // altitud GPS mensaje += String(gps.speed.kmph(), 2); // velocidad km/h } else { mensaje += "0,0,0,0"; } // ==== Salida Serial para depuración ==== Serial.println(mensaje); // ==== Envío vía LoRa ==== LoRa.beginPacket(); LoRa.print(mensaje); LoRa.endPacket(); delay(500); // Tiempo entre lecturas }
0:16
Welcome 👋
Flux helps you build PCBs faster with an AI teammate!
Create your account to collaborate, stay updated, fork your own version, and get instant answers from our AI agent.