The circuit in question is designed to control four AC bulbs via a 4-channel relay module, which is interfaced with an ESP32 DEVKIT V1 microcontroller. The ESP32 is also connected to an IR receiver for remote control functionality and several flush switches for manual control. The circuit is powered by an AC supply, with a HLK-PM12 module converting AC to a stable 5V DC supply for the microcontroller and relay module. The ESP32 runs embedded code that allows for IoT connectivity, sensor reading, and control of the relays based on IR signals or manual switch input.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <DHT.h>
#include <IRremote.h>
// Credentials and settings (fill with your data)
const char DEVICE_LOGIN_NAME[] = ""; // Enter DEVICE ID
const char SSID[] = ""; // Enter WiFi SSID (name)
const char PASS[] = ""; // Enter WiFi password
const char DEVICE_KEY[] = ""; // Enter Secret device password (Secret Key)
// Pin definitions
#define DHTPIN 4 // D4 pin connected with DHT
#define IR_RECV_PIN 35 // D35 pin connected with IR Receiver IC
// GPIO connected with Relays and switches
#define RelayPin1 23 // D23
#define RelayPin2 19 // D19
#define RelayPin3 18 // D18
#define RelayPin4 5 // D5
#define SwitchPin1 13 // D13
#define SwitchPin2 12 // D12
#define SwitchPin3 14 // D14
#define SwitchPin4 27 // D27
#define wifiLed 2 // D2
// DHT sensor type
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
IRrecv irrecv(IR_RECV_PIN);
decode_results results;
// Toggle state for relays
int toggleState_1 = 0;
int toggleState_2 = 0;
int toggleState_3 = 0;
int toggleState_4 = 0;
// Switch State
bool SwitchState_1 = LOW;
bool SwitchState_2 = LOW;
bool SwitchState_3 = LOW;
bool SwitchState_4 = LOW;
// Sensor data
float temperature1 = 0;
float humidity1 = 0;
int reconnectFlag = 0;
// Function declarations
void onLight1Change();
void onLight2Change();
void onLight3Change();
void onLight4Change();
// Cloud properties
CloudSwitch light1;
CloudSwitch light2;
CloudSwitch light3;
CloudSwitch light4;
CloudTemperatureSensor temperature;
// Initialize cloud properties
void initProperties(){
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(light1, READWRITE, ON_CHANGE, onLight1Change);
ArduinoCloud.addProperty(light2, READWRITE, ON_CHANGE, onLight2Change);
ArduinoCloud.addProperty(light3, READWRITE, ON_CHANGE, onLight3Change);
ArduinoCloud.addProperty(light4, READWRITE, ON_CHANGE, onLight4Change);
ArduinoCloud.addProperty(temperature, READ, 8 * SECONDS, NULL); // Update temperature value after every 8 seconds
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
// Read sensor data
void readSensor(){
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
else {
humidity1 = h;
temperature = t;
}
}
// Send sensor data
void sendSensor()
{
readSensor();
}
// IR remote control
void ir_remote_control(){
if (irrecv.decode(&results)) {
switch(results.value){
case 0x80BF49B6: relayOnOff(1); light1 = toggleState_1; break; // Update the HEX-code
case 0x80BFC936: relayOnOff(2); light2 = toggleState_2; break; // Update the HEX-code
case 0x80BF33CC: relayOnOff(3); light3 = toggleState_3; break; // Update the HEX-code
case 0x80BF718E: relayOnOff(4); light4 = toggleState_4; break; // Update the HEX-code
default : break;
}
irrecv.resume();
}
}
// Relay control function
void relayOnOff(int relay) {
switch (relay) {
case 1:
if (toggleState_1 == 0) {
digitalWrite(RelayPin1, LOW); // turn on relay 1
toggleState_1 = 1;
Serial.println("Device1 ON");
}
else {
digitalWrite(RelayPin1, HIGH); // turn off relay 1
toggleState_1 = 0;
Serial.println("Device1 OFF");
}
delay(100);
break;
case 2:
if (toggleState_2 == 0) {
digitalWrite(RelayPin2, LOW); // turn on relay 2
toggleState_2 = 1;
Serial.println("Device2 ON");
}
else {
digitalWrite(RelayPin2, HIGH); // turn off relay 2
toggleState_2 = 0;
Serial.println("Device2 OFF");
}
delay(100);
break;
case 3:
if (toggleState_3 == 0) {
digitalWrite(RelayPin3, LOW); // turn on relay 3
toggleState_3 = 1;
Serial.println("Device3 ON");
} else {