here’s a code that i wrote for using a 4channel relay board. i have not modified it but you could just modify this code. this works using an easy to use sinric skill. since i am terrible at coding. Pardon ME.
it also has some other stuff for controlling my ac and the temerature
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRtimer.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Sinric.h>
#include <SinricSwitch.h>
#include <SinricThermostat.h>
#include <SimpleTimer.h>
#include <ESP8266HTTPClient.h>
#include “CallbackFunction.h”
#include <TimeLib.h>
#include <WidgetRTC.h>
//Blynk Credentials
char auth = “BLYNK AUTH”; //Get token from Blynk
const char* api_key = “SINRIC API KEY”;
const char* switchId = "DEVICE ID;
const char* switchId1 = “DEVICE ID”;
const char* switchId2 = “DEVICE ID”;
const char* switchId3 = “DEVICE ID”;
const char* thermoId = “DEVICE ID”;
HTTPClient http;
WidgetRTC rtc;
WidgetBridge bridge1(V7); //Initiating Bridge Widget on V1 of Device A
WidgetTerminal terminal(V25);
SimpleTimer timer;
const int Relays[1][4] = {
{16,13,14,3} //
};
const int Switch[1][4] = {
{5,2,4,0} //
};
unsigned int AcData[4][14]{
//17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
{0xB2BF00,0xB2BF10,0xB2BF30,0xB2BF20,0xB2BF60,0xB2BF70,0xB2BF50,0xB2BF40,0xB2BFC0,0xB2BFD0,0xB2BF90,0xB2BF80,0xB2BFA0,0xB2BFB0},//Auto
{0xB29F00,0xB29F10,0xB29F30,0xB29F20,0xB29F60,0xB29F70,0xB29F50,0xB29F40,0xB29FC0,0xB29FD0,0xB29F90,0xB29F80,0xB29FA0,0xB29FB0},//Low
{0xB25F00,0xB25F10,0xB25F30,0xB25F20,0xB25F60,0xB25F70,0xB25F50,0xB25F40,0xB25FC0,0xB25FD0,0xB25F90,0xB25F80,0xB25FA0,0xB25FB0},//Mediun
{0xB23F00,0xB23F10,0xB23F30,0xB23F20,0xB23F60,0xB23F70,0xB23F50,0xB23F40,0xB23FC0,0xB23FD0,0xB23F90,0xB23F80,0xB23FA0,0xB23FB0}//High
};
int AcR;
int AcC;
const int IR_PIN = 12;
boolean SwitchReset[5] = {true,true,true,true,true};
int DeviceState[5]={0,0,0,0,0};
int Counter[5] = {0,0,0,0,0};
int AcFanSpd = 3;
int AcOnFanOff = 0;
int AcTemp = 23;
int TEMP;
int temperature;
//Real Time Clock
int RTC_HOUR;
int RTC_MINUTES;
int c = 0;
//TIMER_INPUT
int On_timer[2][3];
int Off_timer[2][3];
IRsend irsend(IR_PIN);
void setup()
{
Serial.begin(115200);
pinMode(Relays[0][0], OUTPUT);
pinMode(Relays[0][1], OUTPUT);
pinMode(Relays[0][2], OUTPUT);
pinMode(Relays[0][3], OUTPUT);
digitalWrite(Relays[0][0],HIGH);
digitalWrite(Relays[0][1],HIGH);
digitalWrite(Relays[0][2],HIGH);
digitalWrite(Relays[0][3],HIGH);
pinMode(Switch[0][0], INPUT_PULLUP);
pinMode(Switch[0][1], INPUT_PULLUP);
pinMode(Switch[0][2], INPUT_PULLUP);
pinMode(Switch[0][3], INPUT_PULLUP);
WiFiManager wifi; //WiFiManager intialization.
wifi.autoConnect("IOTA-Automation Systems"); //Create AP, if necessary
wifi.setConfigPortalTimeout(180);
WiFi.mode(WIFI_STA);
//PINMODE
Blynk.config(auth);
ArduinoOTA.begin();
timer.setInterval(60, ButtonCheck);
setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
// Display digital clock every 10 seconds
timer.setInterval(1000, clockDisplay);
setupSinric();
}
void ButtonCheck()
{
SwitchState();
// SwitchState0();
// SwitchState1();
// SwitchState2();
// SwitchState3();
}
// callback for switch...
void onPowerState(const String& deviceId, bool state) {
if (deviceId == switchId){ //LIGHTS
if (state == HIGH){
lightOn(2);
}else if (state == LOW){
lightOff(2);
}
}
if (deviceId == switchId1){ //FAN
if (state == HIGH){
lightOn(0);
}else if (state == LOW){
lightOff(0);
}
}
if (deviceId == switchId2){ //MEDIA CENTER
if (state == HIGH){
lightOn(1);
}else if (state == LOW){
lightOff(1);
}
}
if (deviceId == switchId3){ //SOCKET
if (state == HIGH){
lightOn(3);
}else if (state == LOW){
lightOff(3);
}
}
if (deviceId == thermoId){ //AC
if (state == HIGH){
ACON();
}else if (state == LOW){
ACOFF();
}
}
}
void onSetTargetTemperature(const String& deviceId, double temp, const String& scale) {
if (deviceId == thermoId){
if(DeviceState[4] = 1){
AcTemp = round(temp);
ACTEMP();
} else if (DeviceState[4] = 0){
DeviceState[4] = 1;
AcTemp = round(temp);
timer.setTimeout(1000, ACTEMP);
} Blynk.virtualWrite(V4,HIGH);
ACONFANOFF();
}
}
void onAdjustTargetTemperature(const String& deviceId, double temp, const String& scale) { Serial.printf("onAdjustTargetTemperature(%s,%f,%s)\r\n", deviceId.c_str(), temp, scale.c_str()); }
void onSetThermostatMode(const String& deviceId, const String& mode) { Serial.printf("onSetThermostatMode(%s,%s)\r\n", deviceId.c_str(), mode.c_str()); }
//SETUP SINRIC
void setupSinric() {
Serial.print("Connecting Sinric");
Sinric.begin(api_key);
while (!Sinric.isConnected()) {
Serial.print(".");
delay(500);
}
Serial.println("connected!");
// add new Switch
SinricSwitch& mySwitch = Sinric.add<SinricSwitch>(switchId);
SinricSwitch& mySwitch1 = Sinric.add<SinricSwitch>(switchId1);
SinricSwitch& mySwitch2 = Sinric.add<SinricSwitch>(switchId2);
SinricSwitch& mySwitch3 = Sinric.add<SinricSwitch>(switchId3);
// set the callback
mySwitch.onPowerState(onPowerState);
mySwitch1.onPowerState(onPowerState);
mySwitch2.onPowerState(onPowerState);
mySwitch3.onPowerState(onPowerState);
// add new Thermostat
SinricThermostat& myThermo = Sinric.add<SinricThermostat>(thermoId);
// set the callbacks for thermostat
myThermo.onPowerState(onPowerState);
myThermo.onSetTargetTemperature(onSetTargetTemperature);
myThermo.onAdjustTargetTemperature(onAdjustTargetTemperature);
myThermo.onSetThermostatMode(onSetThermostatMode);
}
void loop(){
Blynk.run();
ArduinoOTA.handle();
timer.run();
irsend.begin();
Sinric.handle();
}
void lightOn(int pin) {
digitalWrite(Relays[0][pin],LOW);
DeviceState[pin] = 1;
switch(pin){
case 0:Blynk.virtualWrite(V0, HIGH); break;
case 1:Blynk.virtualWrite(V1, HIGH); break;
case 2:Blynk.virtualWrite(V2, HIGH); break;
case 3:Blynk.virtualWrite(V3, HIGH); break;
} // Sync the Blynk button widget state
}
void lightOff(int pin) {
digitalWrite(Relays[0][pin],HIGH);
DeviceState[pin] = 0;
switch(pin){
case 0:Blynk.virtualWrite(V0, LOW); break;
case 1:Blynk.virtualWrite(V1, LOW); break;
case 2:Blynk.virtualWrite(V2, LOW); break;
case 3:Blynk.virtualWrite(V3, LOW); break;
} // Sync the Blynk button widget state
}
void ACON(){
DeviceState[4] = 1;
ACTEMP();
ACONFANOFF();
Blynk.virtualWrite(V4, HIGH);
}
void ACOFF(){
irsend.sendCOOLIX(0xB27BE0, 24);
DeviceState[4] = 0;
ACONFANOFF();
Blynk.virtualWrite(V4, LOW);
}
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0, V1, V2, V3, V30 ,V4);
bridge1.setAuthToken("AUTH DEVICE 2"); // Token of the hardware B
rtc.begin();
terminal.clear();
terminal.println("DEVICE CONNECTED");
terminal.print("connected to: ") ;terminal.println(WiFi.SSID());
terminal.print("local ip: ") ;terminal.println(WiFi.localIP());
terminal.flush();
}
BLYNK_WRITE(V0){
int SwitchStatus = param.asInt();
if (SwitchStatus == 1){lightOn(0); // Relay ON
Serial.println("Blynk Virtual Pin V0 state Changed To HIGH ");
}else if (SwitchStatus == 0) {lightOff(0); // Relay OFF
Serial.println("Blynk Virtual Pin V0 state Changed To LOW ");
}
}BLYNK_WRITE(V1){
int SwitchStatus = param.asInt();
if (SwitchStatus == 1){lightOn(1); // Relay ON
Serial.println("Blynk Virtual Pin V1 state Changed To HIGH ");
} else if (SwitchStatus == 0) { lightOff(1); // Relay OFF
Serial.println("Blynk Virtual Pin V1 state Changed To LOW ");
}
}BLYNK_WRITE(V2){
int SwitchStatus = param.asInt();
if (SwitchStatus == 1) { lightOn(2); // Relay ON
Serial.println("Blynk Virtual Pin V2 state Changed To HIGH ");
}else if (SwitchStatus == 0) { lightOff(2); // Relay OFF
Serial.println("Blynk Virtual Pin V2 state Changed To LOW ");
}
}BLYNK_WRITE(V3){
int SwitchStatus = param.asInt();
if (SwitchStatus == 1){ lightOn(3); // Relay ON
Serial.println("Blynk Virtual Pin V3 state Changed To HIGH ");
}else if (SwitchStatus == 0) { lightOff(3); // Relay OFF
Serial.println("Blynk Virtual Pin V3 state Changed To LOW ");
}
}BLYNK_WRITE(V4){
int SwitchStatus = param.asInt();
if (SwitchStatus == 1) {ACON();
Serial.println("Blynk Virtual Pin V4 state Changed To HIGH");
} else if (SwitchStatus == 0) {ACOFF();}}
void ACTEMP(){
AcR = AcFanSpd;
AcC = (AcTemp - 17);
if ((DeviceState[4]) == 1){
irsend.sendCOOLIX(AcData[AcR][AcC], 24);
Blynk.virtualWrite(V21,AcTemp);
switch(AcFanSpd){
case 0:Blynk.virtualWrite(V22,"AUTO"); break;
case 1:Blynk.virtualWrite(V22,"LOW"); break;
case 2:Blynk.virtualWrite(V22,"MEDIUM");break;
case 3:Blynk.virtualWrite(V22,"HIGH"); break;
}
}
}
BLYNK_WRITE(V5){
AcTemp = param.asInt();
ACTEMP();
ACONFANOFF();
}
BLYNK_WRITE(V6){
AcFanSpd = param.asInt();
ACTEMP();
ACONFANOFF();
}
BLYNK_WRITE(V9){
if (param.asInt() == 1){
WiFiManager wifi;
wifi.startConfigPortal("BLYNK-REQUESTED-CONFIG"); }
else{
//DO NOTHING
}
}
BLYNK_WRITE(V30){AcOnFanOff = param.asInt();
ACONFANOFF();}
BLYNK_WRITE(V11) { //ON TIMER 1
int startTimeInSecs = param[0].asLong();
int total_minutes = startTimeInSecs/60;
On_timer[0][0] = total_minutes/60;
On_timer[0][1] = total_minutes %60;
On_timer[0][2] = startTimeInSecs &60;
terminal.print((On_timer[0][0]));
terminal.print(":");
terminal.print(On_timer[0][1]);
terminal.print(":");
terminal.println(On_timer[0][2]);
terminal.flush();
}
BLYNK_WRITE(V12) { //OFF TIMER 1
int startTimeInSecs = param[0].asLong();
int total_minutes = startTimeInSecs/60;
Off_timer[0][0] = total_minutes/60;
Off_timer[0][1] = total_minutes %60;
Off_timer[0][2] = startTimeInSecs % 60;
}
BLYNK_WRITE(V13) { //ON TIMER 2
int startTimeInSecs = param[0].asLong();
int total_minutes = startTimeInSecs/60;
On_timer[1][0] = total_minutes/60;
On_timer[1][1] = total_minutes %60;
On_timer[1][2] = startTimeInSecs &60;
}
BLYNK_WRITE(V14) { //OFF TIMER 2
int startTimeInSecs = param[0].asLong();
int total_minutes = startTimeInSecs/60;
Off_timer[1][0] = total_minutes/60;
Off_timer[1][1] = total_minutes %60;
Off_timer[1][2] = startTimeInSecs % 60;
}
void ACONFANOFF()
{if (AcOnFanOff == 1 && DeviceState[4] == 1){
lightOff(0);
}else if(AcOnFanOff == 1 && DeviceState[4] == 0){
lightOn(0);
}
}
void SwitchState(){
for(int i=0;i<4;i++){
boolean state = (digitalRead(Switch[0][i]));
if ((state) == LOW ){
Counter[i]++;
SwitchReset[i] = false;
}else if ((state) == HIGH && (SwitchReset[i]) == false){
if(Counter[i] < 25){//SHORT PRESS
if ((DeviceState[i]) == 0){
if(i == 0){lightOn(0); //FAN
}else if(i == 1){lightOn(1); //MEDIA CENTER
}else if(i == 2){lightOn(2);bridge1.virtualWrite(V2, 100);bridge1.virtualWrite(V0, 1023); //LIGHT
}else if(i == 3){ACON();
}
}else{
if(i == 0){lightOff(0);//FAN
}else if(i == 1){lightOff(1);// MEDIA CENTER
}else if(i == 2){lightOff(2);bridge1.virtualWrite(V2, 0);bridge1.virtualWrite(V0, 0); //LIGHT
}else if(i == 3){ACOFF();
}
}}else{
ACOFF();// lONG PRESS
}
Counter[i] = 0;
// reset flag the physical button release
SwitchReset[i] = true;
}
}
}
void clockDisplay(){
RTC_HOUR = hour();
if ( RTC_HOUR > 12 ) {
RTC_HOUR = RTC_HOUR - 12;
String currentTime = String(RTC_HOUR) + ":" + minute() + ":" + second() + " PM";
Blynk.virtualWrite(V10, currentTime); // Send PM time to the App
} else {
String currentTime = String(hour()) + ":" + minute() + ":" + second() + " AM";
Blynk.virtualWrite(V10, currentTime); // Send AM time to the App
}
String currentDate = String(day()) + "/" + month() + "/" + year();
Blynk.virtualWrite(V31, currentDate); // Send date to the App
if(On_timer[0][0] == hour() && On_timer[0][1] == minute()&& On_timer[0][2] == second()){
ACON();
}else if (On_timer[1][0] == hour() && On_timer[1][1] == minute() && On_timer[1][2] == second()){
ACON();
}else if (Off_timer[0][0] == hour() && Off_timer[0][1] == minute() && Off_timer[0][2] == second()){
ACOFF();
}else if (Off_timer[1][0] == hour() && Off_timer[1][1] == minute() && Off_timer[1][2] == second()){
ACOFF();
}
}
BLYNK_WRITE(V25){
String Read = param.asStr();
if(Read == String("Clear") || Read == String("clear") || Read == String("cls")){
terminal.clear();
}else if(Read == String("status") || Read == String("Status")){
terminal.print("connected to: ") ;terminal.println(WiFi.SSID());
terminal.print("local ip: ") ;terminal.println(WiFi.localIP());
}
terminal.flush();
}