-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblynk_switch_feedback.ino
198 lines (146 loc) · 4.91 KB
/
blynk_switch_feedback.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//Blynk Home Automation with Physical Button and ON OFF State Feedback
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h> // For OTA with ESP8266
#include <WiFiUdp.h> // For OTA
#include <ArduinoOTA.h> // For OTA
BlynkTimer timer;
void checkPhysicalButton();
int relay1State = LOW;
int pushButton1State = HIGH;
int relay2State = LOW;
int pushButton2State = HIGH;
int relay3State = LOW;
int pushButton3State = HIGH;
int relay4State = LOW;
int pushButton4State = HIGH;
#define AUTH "---------------------------" // You should get Auth Token in the Blynk App.
#define WIFI_SSID "------" //Enter Wifi Name
#define WIFI_PASS "------" //Enter wifi Password
#define SERVER "blynk-cloud.com " // Comment-out if use Blynk hosted cloud service
#define PORT 8442
#define RELAY_PIN_1 16 //D0
#define RELAY_PIN_2 5 //D1
#define RELAY_PIN_3 4 //D2
#define RELAY_PIN_4 0 //D3
#define PUSH_BUTTON_1 14 //D5
#define PUSH_BUTTON_2 12 //D6
#define PUSH_BUTTON_3 13 //D7
#define PUSH_BUTTON_4 1 //TX
#define VPIN_BUTTON_2 V12
#define VPIN_BUTTON_1 V13
#define VPIN_BUTTON_3 V14
#define VPIN_BUTTON_4 V15
#define OTA_HOSTNAME "Home_Automation"
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Blynk.syncVirtual(VPIN_BUTTON_3);
Blynk.syncVirtual(VPIN_BUTTON_4);
// Alternatively, you could override server state using:
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
}
// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BUTTON_1) {
relay1State = param.asInt();
digitalWrite(RELAY_PIN_1, relay1State);
}
BLYNK_WRITE(VPIN_BUTTON_2) {
relay2State = param.asInt();
digitalWrite(RELAY_PIN_2, relay2State);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
relay3State = param.asInt();
digitalWrite(RELAY_PIN_3, relay3State);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
relay4State = param.asInt();
digitalWrite(RELAY_PIN_4, relay4State);
}
void checkPhysicalButton()
{
if (digitalRead(PUSH_BUTTON_1) == LOW) {
// pushButton1State is used to avoid sequential toggles
if (pushButton1State != LOW) {
// Toggle Relay state
relay1State = !relay1State;
digitalWrite(RELAY_PIN_1, relay1State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
}
pushButton1State = LOW;
} else {
pushButton1State = HIGH;
}
if (digitalRead(PUSH_BUTTON_2) == LOW) {
// pushButton2State is used to avoid sequential toggles
if (pushButton2State != LOW) {
// Toggle Relay state
relay2State = !relay2State;
digitalWrite(RELAY_PIN_2, relay2State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
}
pushButton2State = LOW;
} else {
pushButton2State = HIGH;
}
if (digitalRead(PUSH_BUTTON_3) == LOW) {
// pushButton3State is used to avoid sequential toggles
if (pushButton3State != LOW) {
// Toggle Relay state
relay3State = !relay3State;
digitalWrite(RELAY_PIN_3, relay3State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
}
pushButton3State = LOW;
} else {
pushButton3State = HIGH;
}
if (digitalRead(PUSH_BUTTON_4) == LOW) {
// pushButton4State is used to avoid sequential toggles
if (pushButton4State != LOW) {
// Toggle Relay state
relay4State = !relay4State;
digitalWrite(RELAY_PIN_4, relay4State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);
}
pushButton4State = LOW;
} else {
pushButton4State = HIGH;
}
}
void setup()
{
Serial.begin(115200);
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS,"blynk-cloud.com", 8442);
ArduinoOTA.setHostname(OTA_HOSTNAME); // For OTA - Use your own device identifying name
ArduinoOTA.begin(); // For OTA
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
digitalWrite(RELAY_PIN_1, relay1State);
pinMode(RELAY_PIN_2, OUTPUT);
pinMode(PUSH_BUTTON_2, INPUT_PULLUP);
digitalWrite(RELAY_PIN_2, relay2State);
pinMode(RELAY_PIN_3, OUTPUT);
pinMode(PUSH_BUTTON_3, INPUT_PULLUP);
digitalWrite(RELAY_PIN_3, relay3State);
pinMode(RELAY_PIN_4, OUTPUT);
pinMode(PUSH_BUTTON_4, INPUT_PULLUP);
digitalWrite(RELAY_PIN_4, relay4State);
// Setup a function to be called every 100 ms
timer.setInterval(500L, checkPhysicalButton);
}
void loop()
{
Blynk.run();
ArduinoOTA.handle(); // For OTA
timer.run();
}