Circuit Documentation
Summary
This circuit is designed to read temperature and humidity data from a DHT22 sensor using an ESP32C3 microcontroller. The data is then transmitted via BLE (Bluetooth Low Energy). Additionally, the circuit controls a humidifier through a relay module. The ESP32C3 microcontroller is the central unit that interfaces with the DHT22 sensor and the relay module.
Component List
ESP32C3 Supermini
- Description: A microcontroller with built-in Wi-Fi and Bluetooth capabilities.
- Pins: GPIO05, GPIO06, GPIO07, GPIO08, GPIO09, GPIO10, GPIO20, GPIO21, GPIO00, GPIO01, GPIO02, GPIO03, GPIO04, 3.3V, GND, +5V
1 Channel Relay 5V
- Description: A relay module used to control high voltage devices.
- Pins: VCC, GND, IN, NC, COM, NO
DHT22 (Wokwi Compatible)
- Description: A digital temperature and humidity sensor.
- Pins: VCC, SDA, NC, GND
Resistor (10k Ohms)
- Description: A resistor with a resistance of 10k Ohms.
- Pins: pin1, pin2
Humidifier
- Description: A device that adds moisture to the air.
- Pins: GND, 5V
BC547 Transistor
- Description: A general-purpose NPN transistor.
- Pins: Collector, Base, Emitter
12v Power Supply
- Description: A power supply providing 12V output.
- Pins: +, -
Wiring Details
ESP32C3 Supermini
- GPIO02: Connected to IN of the first 1 Channel Relay 5V.
- GPIO03: Connected to pin1 of the first Resistor (10k Ohms) and SDA of the DHT22 sensor.
- 3.3V: Connected to pin2 of the first Resistor (10k Ohms) and VCC of the DHT22 sensor.
- GND: Connected to GND of the first 1 Channel Relay 5V, GND of the second 1 Channel Relay 5V, GND of the Humidifier, GND of the DHT22 sensor, - of the 12v Power Supply, and Collector of the second BC547 Transistor.
- +5V: Connected to VCC of the first 1 Channel Relay 5V.
1 Channel Relay 5V (First)
- IN: Connected to GPIO02 of the ESP32C3 Supermini.
- GND: Connected to GND of the ESP32C3 Supermini.
- VCC: Connected to +5V of the ESP32C3 Supermini.
- COM: Connected to 5V of the Humidifier.
1 Channel Relay 5V (Second)
- IN: Connected to Emitter of the second BC547 Transistor.
- GND: Connected to GND of the ESP32C3 Supermini.
- VCC: Connected to + of the 12v Power Supply.
DHT22 (Wokwi Compatible)
- SDA: Connected to GPIO03 of the ESP32C3 Supermini and pin1 of the first Resistor (10k Ohms).
- VCC: Connected to pin2 of the first Resistor (10k Ohms) and 3.3V of the ESP32C3 Supermini.
- GND: Connected to GND of the ESP32C3 Supermini.
Resistor (10k Ohms) (First)
- pin1: Connected to GPIO03 of the ESP32C3 Supermini and SDA of the DHT22 sensor.
- pin2: Connected to 3.3V of the ESP32C3 Supermini and VCC of the DHT22 sensor.
Resistor (10k Ohms) (Second)
- pin2: Connected to Base of the second BC547 Transistor.
Humidifier
- GND: Connected to GND of the ESP32C3 Supermini.
- 5V: Connected to COM of the first 1 Channel Relay 5V.
BC547 Transistor (Second)
- Collector: Connected to GND of the ESP32C3 Supermini.
- Base: Connected to pin2 of the second Resistor (10k Ohms).
- Emitter: Connected to IN of the second 1 Channel Relay 5V.
12v Power Supply
- -: Connected to GND of the ESP32C3 Supermini.
- +: Connected to VCC of the second 1 Channel Relay 5V.
Documented Code
/*
* Projeto Cogumelo
* This code reads temperature and humidity data from a DHT22 sensor
* connected to an ESP32C3 microcontroller and prints the values to the
* serial monitor.
*/
#include <DHT.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define DHTPIN 3 // GPIO03 connected to DHT22 data pin
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define RELAY_PIN 2 // Pino do módulo relé
DHT dht(DHTPIN, DHTTYPE);
// UUIDs do serviço e característica
#define SERVICE_UUID "12345678-1234-1234-1234-123456789abc"
#define CHARACTERISTIC_UUID "abcd1234-5678-90ab-cdef-1234567890ab"
// Variáveis BLE
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false; // Estado de conexão do cliente BLE
// Callback para lidar com conexão/desconexão do cliente
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("Cliente BLE conectado!");
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Cliente BLE desconectado!");
BLEDevice::startAdvertising(); // Reinicia o anúncio BLE
}
};
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Relé começa desligado
Serial.println("Iniciando BLE...");
// Inicializa o BLE
BLEDevice::init("ESP32-C3 Server");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Cria o serviço BLE
BLEService* pService = pServer->createService(SERVICE_UUID);
// Cria a característica para enviar dados
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
);
// Adiciona descritor para permitir notificações
pCharacteristic->addDescriptor(new BLE2902());
pCharacteristic->setValue("Iniciando...");
// Inicia o serviço e começa a anunciar
pService->start();
BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->start();
Serial.println("Servidor BLE iniciado e aguardando conexões...");
}
void loop() {
delay(2000); // Wait a few seconds between measurements
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}
This code initializes the DHT22 sensor and the BLE server, reads the temperature and humidity data from the sensor, and prints the values to the serial monitor. The relay is controlled based on the readings, and the data is also made available via BLE.
