#include <DHT.h>
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
// ---- Pin map (matches diagram.json) ----
#define DHTPIN 18
#define DHTTYPE DHT22
#define PIR_PIN 27
#define MQ2_PIN 34 // ADC1_CH6, after the R1/R2 divider
#define LED1_PIN 25
#define LED2_PIN 32
#define BUZZER_PIN 33
// GPIO16/17 (UART2) are reserved for the GPS module on real hardware.
// GPIO5/18/19/23/14/26 are reserved for the SX1276 LoRa module on real hardware.
// Neither GPS nor LoRa has native Wokwi simulation support, so this sketch
// only exercises the sensors that ARE simulated. Swap in TinyGPS++ / RadioHead
// LoRa code once you move this onto real hardware.
DHT dht(DHTPIN, DHTTYPE);
Adafruit_MPU6050 mpu;
void setup() {
Serial.begin(115200);
delay(300);
Serial.println("AEROMESH node - sensor bring-up test");
pinMode(PIR_PIN, INPUT);
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
dht.begin();
if (!mpu.begin()) {
Serial.println("MPU6050 not found - check wiring");
} else {
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
Serial.println("MPU6050 ready");
}
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
int motion = digitalRead(PIR_PIN);
int gasRaw = analogRead(MQ2_PIN);
sensors_event_t a, g, tempEvent;
mpu.getEvent(&a, &g, &tempEvent);
Serial.println("---- reading ----");
Serial.printf("Temp: %.1f C Humidity: %.1f %%\n", temp, hum);
Serial.printf("Motion: %s\n", motion ? "DETECTED" : "clear");
Serial.printf("Gas (raw ADC): %d\n", gasRaw);
Serial.printf("Accel X/Y/Z: %.2f %.2f %.2f m/s^2\n", a.acceleration.x, a.acceleration.y, a.acceleration.z);
bool alert = (motion || gasRaw > 2000 || isnan(temp));
digitalWrite(LED1_PIN, alert ? HIGH : LOW);
digitalWrite(LED2_PIN, alert ? LOW : HIGH);
digitalWrite(BUZZER_PIN, alert ? HIGH : LOW);
delay(2000);
digitalWrite(BUZZER_PIN, LOW);
}