#include "arduino_secrets.h"
#include "thingProperties.h"
#define RelayPin1 2 //D1
#define RelayPin2 4 //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6
#define SwitchPin1 10 //SD3
#define SwitchPin2 0 //D3
#define SwitchPin3 13 //D7
#define SwitchPin4 3 //RX
#define wifiLed 16 //D0
int toggleState_1 = 0;
int toggleState_2 = 0;
int toggleState_3 = 0;
int toggleState_4 = 0;
void relayOnOff(int relay) {
switch (relay) {
case 1:
if (toggleState_1 == 0) {
digitalWrite(RelayPin1, LOW);
toggleState_1 = 1;
Serial.println("Device1 ON");
}
else{
digitalWrite(RelayPin1, HIGH);
toggleState_1 = 0;
Serial.println("Device1 OFF");
}
delay(100);
break;
case 2:
if (toggleState_2 == 0) {
digitalWrite(RelayPin2, LOW);
toggleState_2 = 1;
Serial.println("Device2 ON");
}
else {
digitalWrite(RelayPin2, HIGH);
toggleState_2 = 0;
Serial.println("Device2 OFF");
}
delay(100);
break;
case 3:
if (toggleState_3 == 0) {
digitalWrite(RelayPin3, LOW);
toggleState_3 = 1;
Serial.println("Device3 ON");
}
else {
digitalWrite(RelayPin3, HIGH);
toggleState_3 = 0;
Serial.println("Device3 OFF");
}
delay(100);
break;
case 4:
if (toggleState_4 == 0) {
digitalWrite(RelayPin4, LOW);
toggleState_4 = 1;
Serial.println("Device4 ON");
}
else {
digitalWrite(RelayPin4, HIGH);
toggleState_4 = 0;
Serial.println("Device4 OFF");
}
delay(100);
break;
default : break;
}
}
void manual_control() {
if (digitalRead(SwitchPin1) == LOW) {
delay(200);
relayOnOff(1);
}
else if (digitalRead(SwitchPin2) == LOW) {
delay(200);
relayOnOff(2);
}
else if (digitalRead(SwitchPin3) == LOW) {
delay(200);
relayOnOff(3);
}
else if (digitalRead(SwitchPin4) == LOW) {
delay(200);
relayOnOff(4);
}
}
void setup() {
Serial.begin(9600);
delay(1500);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(wifiLed, OUTPUT);
pinMode(SwitchPin1, INPUT_PULLUP);
pinMode(SwitchPin2, INPUT_PULLUP);
pinMode(SwitchPin3, INPUT_PULLUP);
pinMode(SwitchPin4, INPUT_PULLUP);
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, HIGH);
digitalWrite(RelayPin3, HIGH);
digitalWrite(RelayPin4, HIGH);
digitalWrite(wifiLed, HIGH);
}
void loop() {
ArduinoCloud.update();
manual_control();
if (WiFi.status() != WL_CONNECTED)
{
digitalWrite(wifiLed, HIGH);
}
else{
digitalWrite(wifiLed, LOW);
}
}
void onSwitch1Change() {
if (switch1 == 1)
{
digitalWrite(RelayPin1, LOW);
Serial.println("Device1 ON");
toggleState_1 = 1;
}
else
{
digitalWrite(RelayPin1, HIGH);
Serial.println("Device1 OFF");
toggleState_1 = 0;
}
}
void onSwitch2Change() {
if (switch2 == 1)
{
digitalWrite(RelayPin2, LOW);
Serial.println("Device2 ON");
toggleState_2 = 1;
}
else
{
digitalWrite(RelayPin2, HIGH);
Serial.println("Device2 OFF");
toggleState_2 = 0;
}
}
void onSwitch3Change() {
if (switch3 == 1)
{
digitalWrite(RelayPin3, LOW);
Serial.println("Device2 ON");
toggleState_3 = 1;
}
else
{
digitalWrite(RelayPin3, HIGH);
Serial.println("Device3 OFF");
toggleState_3 = 0;
}
}
void onSwitch4Change() {
if (switch4 == 1)
{
digitalWrite(RelayPin4, LOW);
Serial.println("Device4 ON");
toggleState_4 = 1;
}
else
{
digitalWrite(RelayPin4, HIGH);
Serial.println("Device4 OFF");
toggleState_4 = 0;
}
}
#define SECRET_SSID "Redmi Note 11"
#define SECRET_PASS "note 11 sai"
#define SECRET_DEVICE_KEY "MZHTUKVZBRHQ7Y24LEAC"