/**********************************************************************************
TITLE: ESP RainMaker + IR + Manual Switch control 1 Relay using ESP32 DHT11 LDR (Real time feedback + no WiFi control)
Click on the following links to learn more.
YouTube Video: https://youtu.be/7knQaSuEgsU
Related Blog : https://iotcircuithub.com/esp32-projects/
by Tech StudyCell
Preferences--> Additional boards Manager URLs :
http://arduino.esp8266.com/stable/package_esp8266com_index.json,https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
Download Board ESP32 (2.0.3): https://github.com/espressif/arduino-esp32
Download the libraries
IRremote Library (3.6.1): https://github.com/Arduino-IRremote/Arduino-IRremote
DHT Library (1.4.4): https://github.com/adafruit/DHT-sensor-library
**********************************************************************************/
#include "RMaker.h"
#include "WiFi.h"
#include "WiFiProv.h"
#include <IRremote.h>
#include <DHT.h>
#include <SimpleTimer.h>
const char *service_name = "PROV_1234RS8";
const char *pop = "12345RS8";
// define the Chip Id
uint32_t espChipId = 5;
// define the Node Name
char nodeName[] = "ESP32_Relay_1S";
// define the Device Names
char deviceName_1[] = "Switch1";
//Update the HEX code of IR Remote buttons 0x<HEX CODE>
#define IR_Button_1 0x80BF49B6
#define IR_All_Off 0x80BF3BC4
// define the GPIO connected with Relay and switch
static uint8_t RelayPin1 = 23; //D23
static uint8_t SwitchPin1 = 13; //D13
static uint8_t wifiLed = 2; //D2
static uint8_t gpio_reset = 0;
static uint8_t IR_RECV_PIN = 35; // D35 (IR receiver pin)
static uint8_t DHTPIN = 16; // RX2 pin connected with DHT
static uint8_t LDR_PIN = 34; // D34 pin connected with LDR
/* Variable for reading pin status*/
// Relay State
bool toggleState_1 = LOW; //Define integer to remember the toggle state for relay 1
// Switch State
bool SwitchState_1 = LOW;
float temperature1 = 0;
float humidity1 = 0;
float ldrVal = 0;
DHT dht(DHTPIN, DHT11);
IRrecv irrecv(IR_RECV_PIN);
decode_results results;
SimpleTimer Timer;
//The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
static Switch my_switch1(deviceName_1, &RelayPin1);
static TemperatureSensor temperature("Temperature");
static TemperatureSensor humidity("Humidity");
static TemperatureSensor ldr("LDR");
void sysProvEvent(arduino_event_t *sys_event)
{
switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
#else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
#endif
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.printf("\nConnected to Wi-Fi!\n");
digitalWrite(wifiLed, true);
break;
}
}
void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx)
{
const char *device_name = device->getDeviceName();
const char *param_name = param->getParamName();
if (strcmp(device_name, deviceName_1) == 0) {
Serial.printf("Switch value = %s\n", val.val.b ? "true" : "false");
if (strcmp(param_name, "Power") == 0) {
Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name);
toggleState_1 = val.val.b;
(toggleState_1 == false) ? digitalWrite(RelayPin1, HIGH) : digitalWrite(RelayPin1, LOW);
(toggleState_1 == false) ? Serial.println("Switch-1 off") : Serial.println("Switch-1 on");
}
}
}
void readSensors() {
temperature1 = dht.readTemperature();
humidity1 = dht.readHumidity();
ldrVal = analogRead(LDR_PIN);
temperature.updateAndReport(temperature1);
humidity.updateAndReport(humidity1);
ldr.updateAndReport(ldrVal);
}
void ir_remote() {
if (irrecv.decode(&results)) {
// Print Code
Serial.println(results.value, HEX);
switch (results.value) {
case IR_Button_1:
digitalWrite(RelayPin1, toggleState_1);
toggleState_1 = !toggleState_1;
my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1);
delay(100);
break;
case IR_All_Off:
digitalWrite(RelayPin1, HIGH);
toggleState_1 = false;
my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1);
delay(100);
break;
}
irrecv.resume(); // Receive the next value
}
}
void manual_control() {
if (digitalRead(SwitchPin1) == LOW && SwitchState_1 == LOW) {
digitalWrite(RelayPin1, LOW);
toggleState_1 = true;
SwitchState_1 = HIGH;
my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1);
Serial.println("Switch-1 on");
}
if (digitalRead(SwitchPin1) == HIGH && SwitchState_1 == HIGH) {
digitalWrite(RelayPin1, HIGH);
toggleState_1 = false;
SwitchState_1 = LOW;
my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1);
Serial.println("Switch-1 off");
}
}
void setup() {
Serial.begin(115200);
irrecv.enableIRIn(); // Start the receiver
//Start provisioning only if there is no configuration done earlier
if (ESP.rmaker_device_configured() != 1) {
Serial.println("Starting Configuration");
pinMode(gpio_reset, INPUT);
delay(5000); // wait for 5 seconds to check for reset button
if (digitalRead(gpio_reset)) {
ESP.rmaker_factory_reset();
} else {
// Initiate provisioning
ESP.rmaker_provision(service_name, pop, sysProvEvent, NULL);
while (1) {
delay(1000);
}
}
}
// Enable BLE only if WiFi is not available
if (WiFi.isConnected()) {
ESP.rmaker_disable_ble();
} else {
ESP.rmaker_enable_ble();
}
// Initialize ESP RainMaker
ESP.rmaker_init();
// Setup WiFi
WiFi.begin();
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Set up the Device
Device *switch_device = ESP.rmaker_get_device_by_name(deviceName_1);
if (switch_device) {
my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1);
}
// Attach the write callback
ESP.rmaker_register_for_write(nodeName, write_callback);
// Setup GPIO
pinMode(RelayPin1, OUTPUT);
pinMode(SwitchPin1, INPUT_PULLUP);
// Setup LEDs
pinMode(wifiLed, OUTPUT);
digitalWrite(wifiLed, false);
// Setup DHT
dht.begin();
// Add sensors
ESP.rmaker_add_device(&temperature);
ESP.rmaker_add_device(&humidity);
ESP.rmaker_add_device(&ldr);
// Read sensors every 5 seconds
Timer.setInterval(5000, readSensors);
}
void loop() {
ESP.rmaker_handle();
// Handle IR Remote
ir_remote();
// Manual control via switches
manual_control();
// Execute timer functions
Timer.run();
delay(100);
}