Multi-Board DHT22 Monitoring — ESP32 simulation

Multi-board DHT22 and LCD I2C monitoring system with ESP32 and Arduino Mega.

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by juliwicksmancay.

Components

Sketch code

/*
   NODE SENSOR KANDANG AYAM - ESP32 + DHT22
   Firmware SAMA untuk 4 node. Ubah NODE_ID (1..4) sebelum upload.

   Tugas node:
     1. Baca DHT22 tiap 2,5 detik
     2. Validasi (rentang, lonjakan tak wajar) dan filter rata-rata bergerak 5 sampel
     3. Hitung status sensor (OK / ERROR jika gagal baca 3x berturut-turut)
     4. Saat Mega mengirim "?" balas dengan paket data hasil olahan lewat UART2

   Format paket balasan (diakhiri newline):
     $<id>,<suhu>,<kelembapan>,<status>*<checksum_hex>
     contoh: $1,29.5,68.2,0*3C
     status: 0 = sensor OK, 1 = sensor error
     checksum: XOR semua karakter antara '$' dan '*'

   Library: "DHT sensor library" + "Adafruit Unified Sensor" (Adafruit)
   Board  : ESP32 Dev Module (ESP32-WROOM)
*/

#include <DHT.h>

#define NODE_ID   4          // <-- GANTI: 1, 2, 3, atau 4

#define DHT_PIN   4
#define DHTTYPE   DHT22
#define RX2_PIN   16         // UART2 RX  <- TX Mega (lewat pembagi tegangan)
#define TX2_PIN   17         // UART2 TX  -> RX Mega
#define LED_PIN   2          // LED bawaan ESP32

const unsigned long INTERVAL_BACA = 2500;
const byte  N_AVG = 5;
const float LONJAKAN_MAKS = 10.0;   // selisih suhu antar sampel yang dianggap tidak wajar

DHT dht(DHT_PIN, DHTTYPE);

float bufT[N_AVG], bufH[N_AVG];
byte  jumlahSampel = 0, indeks = 0;
float suhuOlah = 0, lembabOlah = 0;
byte  gagalBeruntun = 0;
byte  statusSensor = 1;             // 1 = error sampai ada pembacaan valid pertama
unsigned long lastBaca = 0;

void bacaDanOlah() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  bool ok = !isnan(t) && !isnan(h) && t >= -40 && t <= 80 && h >= 0 && h <= 100;
  if (ok && jumlahSampel > 0 && fabs(t - suhuOlah) > LONJAKAN_MAKS) ok = false;

  if (ok) {
    gagalBeruntun = 0;
    bufT[indeks] = t;
    bufH[indeks] = h;
    indeks = (indeks + 1) % N_AVG;
    if (jumlahSampel < N_AVG) jumlahSampel++;

    float sT = 0, sH = 0;
    for (byte i = 0; i < jumlahSampel; i++) {
      sT += bufT[i];
      sH += bufH[i];
    }
    suhuOlah   = sT / jumlahSampel;
    lembabOlah = sH / jumlahSampel;
    statusSensor = 0;
  } else {
    if (gagalBeruntun < 255) gagalBeruntun++;
    if (gagalBeruntun >= 3) {
      statusSensor = 1;
      jumlahSampel = 0;    // buang riwayat; pembacaan valid berikutnya diterima apa adanya
      indeks = 0;
    }
  }

  Serial.printf("[Node %d] raw T=%.1f H=%.1f | olah T=%.1f H=%.1f | status=%d\n",
                NODE_ID, t, h, suhuOlah, lembabOlah, statusSensor);
}

void kirimPaket() {
  char body[40];
  float t = (statusSensor == 0) ? suhuOlah : 0.0;
  float h = (statusSensor == 0) ? lembabOlah : 0.0;
  snprintf(body, sizeof(body), "%d,%.1f,%.1f,%d", NODE_ID, t, h, statusSensor);

  byte cs = 0;
  for (char* p = body; *p; p++) cs ^= *p;

  Serial2.printf("$%s*%02X\n", body, cs);

  digitalWrite(LED_PIN, HIGH);   // LED kedip singkat tanda ada permintaan dari Mega
  delay(15);
  digitalWrite(LED_PIN, LOW);
}

void terimaPerintah() {
  while (Serial2.available()

More projects by juliwicksmancay