#include <Wire.h>
#include <SPI.h>
#include <LoRa.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4
#define I2C_SDA 5
#define I2C_SCL 6
#define LORA_SCK 18
#define LORA_MISO 19
#define LORA_MOSI 1
#define LORA_SS 7
#define LORA_RST 10
#define LORA_DIO0 3
Adafruit_MPU6050 mpu;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensor(&oneWire);
void setup() {
Serial.begin(115200);
Wire.begin(I2C_SDA, I2C_SCL);
if (!mpu.begin()) {
Serial.println("MPU6050 not found!");
while (1) delay(10);
}
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
tempSensor.begin();
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_SS);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
if (!LoRa.begin(433E6)) { // match this frequency on your receiver too
Serial.println("LoRa init failed!");
while (1) delay(10);
}
Serial.println("N1 Reference node online: MPU6050 + DS18B20 + LoRa TX");
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
tempSensor.requestTemperatures();
float ambientTemp = tempSensor.getTempCByIndex(0);
float pitch = atan2(a.acceleration.y,
sqrt(a.acceleration.x * a.acceleration.x +
a.acceleration.z * a.acceleration.z)) * 180.0 / PI;
// Build a simple CSV packet: nodeID,pitch,temp
String packet = "N1," + String(pitch, 3) + "," + String(ambientTemp, 2);
LoRa.beginPacket();
LoRa.print(packet);
LoRa.endPacket();
Serial.print("Sent: "); Serial.println(packet);
delay(1000);
}