-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathESP8266-Remote-Blynk-Switch.ino
85 lines (74 loc) · 2.01 KB
/
ESP8266-Remote-Blynk-Switch.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
#define BLYNK_PRINT Serial
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include "settings.h"
#include <wifi_credentials.h>
int switchState, timer1, switchDelay;
SimpleTimer timer;
WidgetBridge gate(vPIN_BRIDGE_GATE);
BLYNK_CONNECTED() {
gate.setAuthToken("ae5eab51641343209ae3c2b139ef6e0b");
}
BLYNK_WRITE(vPIN_BUTTON_TIMEOUT) { // remote delay switch
if (digitalRead(SWITCH_PIN)) {
Switch_ON();
timer.setTimeout(switchDelay, Switch_OFF);
}
}
BLYNK_WRITE(vPIN_BUTTON_MANUAL) { // manual button
Switch_Toggle(param.asInt());
}
BLYNK_WRITE(vPIN_TIME) { // timer switch
if (param.asInt()) {
Switch_ON();
gate.virtualWrite(vPIN_BRIDGE_GATE, param.asInt());
} else {
Switch_OFF();
}
}
BLYNK_WRITE(vPIN_TIMEOUT) {
switchDelay = param[0].asInt() * 60000;
Blynk.syncVirtual(vPIN_BUTTON_TIMEOUT);
}
void Switch_OFF() {
digitalWrite(SWITCH_PIN, 1);
Blynk.virtualWrite(vPIN_BUTTON_MANUAL, 0);
Blynk.virtualWrite(vPIN_LED, 0);
}
void Switch_ON() {
digitalWrite(SWITCH_PIN, 0);
Blynk.virtualWrite(vPIN_BUTTON_MANUAL, 1);
Blynk.virtualWrite(vPIN_LED, 255);
}
void Switch_Toggle(bool state) {
digitalWrite(SWITCH_PIN, !state);
switchState = digitalRead(SWITCH_PIN);
if (!switchState) switchState = 255;
Blynk.virtualWrite(vPIN_LED, switchState);
}
void setup() {
WiFi.mode(WIFI_STA);
Serial.begin(115200);
#ifdef LOCAL_SERVER
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS, LOCAL_SERVER);
#else
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
#endif
while (Blynk.connect() == false) {}
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.begin();
pinMode(SWITCH_PIN, OUTPUT);
digitalWrite(SWITCH_PIN, HIGH);
switchDelay = 60000;
Blynk.syncVirtual(vPIN_TIMEOUT, vPIN_TIME);
timer.setInterval(2000L, []() {
Blynk.setProperty(vPIN_INFO, "label", String("WIFI: ") + String(map(WiFi.RSSI(), -105, -40, 0, 100)) + String("% (") + WiFi.RSSI() + String("dB)"));
});
}
void loop() {
Blynk.run();
ArduinoOTA.handle();
timer.run();
}