/* Session 2 — Core agricultural sensors on real hardware (ESP32 DevKit) — greenhouse microclimate DHT22 : DATA -> GPIO 4 (10kΩ pull-up to 3.3V if your module has none), VCC 3.3V Soil : Capacitive Soil Moisture v1.2 AOUT -> GPIO 34 (ADC1), VCC 3.3V LDR : 3.3V — LDR — GPIO 35 — 10kΩ — GND Library: "DHT sensor library" by Adafruit (+ "Adafruit Unified Sensor") NOTE: use ADC1 pins (32-39) — ADC2 pins stop working when Wi-Fi is on. */ #include #define DHT_PIN 4 #define SOIL_PIN 34 #define LDR_PIN 35 DHT dht(DHT_PIN, DHT22); // Calibration values measured in the lab (dry air / glass of water). Capacitive sensors: wet = LOWER value int SOIL_DRY = 3200; int SOIL_WET = 1300; void setup() { Serial.begin(115200); analogReadResolution(12); // 0..4095 analogSetAttenuation(ADC_11db); // full 0..3.3 V range dht.begin(); Serial.println("time_s,air_temp_c,air_hum_pct,soil_raw,soil_pct,light_raw"); } void loop() { float t = dht.readTemperature(); float h = dht.readHumidity(); if (isnan(t) || isnan(h)) { Serial.println("# DHT22 read failed — check wiring / pull-up"); } int soilRaw = analogRead(SOIL_PIN); float soilPct = constrain((float)(SOIL_DRY - soilRaw) / (SOIL_DRY - SOIL_WET) * 100.0, 0, 100); int light = analogRead(LDR_PIN); Serial.printf("%lu,%.1f,%.1f,%d,%.0f,%d\n", millis() / 1000, t, h, soilRaw, soilPct, light); delay(2500); // DHT22 needs >= 2 s between reads }