This repository has been archived by the owner on Apr 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Sonoff+Button.ino
93 lines (73 loc) · 2.05 KB
/
Sonoff+Button.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
/***************************************************
Created by: regi18
Version: 3.1.2
Github: https://github.com/regi18/Sonoff-Blynk
**************************************************/
/* Libraries */
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
/*** Here are the things that you have to change ***/
/***************************************************/
/* Blynk AUTH key */
char const AUTH[] = "BLYNK AUTH TOKEN";
/* Wi-Fi Details */
char const SSID[] = "SSID";
char const PASS[] = "WIFI PASSWORD";
char const HOSTNAME[] = "HOSTNAME";
char const OTAPSW[] = "OTA PASSWORD";
/* Blynk Virtual Pins */
#define RELAYVPIN V5
/* Device Pins */
#define RELAY 12
#define LED 13
#define BUTTON 0
/**************************************************/
/* General variables and declarations */
BlynkTimer timer;
volatile bool state;
void setup()
{
pinMode(LED,OUTPUT);
pinMode(RELAY,OUTPUT);
pinMode(BUTTON,INPUT);
WiFi.hostname(HOSTNAME);
Blynk.begin(AUTH, SSID, PASS);
ArduinoOTA.setPassword(OTAPSW);
ArduinoOTA.setHostname(HOSTNAME);
ArduinoOTA.begin();
attachInterrupt(digitalPinToInterrupt(BUTTON), handleButton, FALLING);
digitalWrite(LED, LOW);
delay(700);
digitalWrite(LED, HIGH);
delay(700);
digitalWrite(LED, LOW);
delay(700);
digitalWrite(LED, HIGH);
}
BLYNK_CONNECTED() {
Blynk.syncAll();
}
void loop() {
if (Blynk.connected()) { Blynk.run(); }
else { Blynk.connect(); }
timer.run();
ArduinoOTA.handle();
}
/***************************************************
* Read virtual pin 0 and turn on/off relay
**************************************************/
BLYNK_WRITE(RELAYVPIN){
state = param.asInt();
digitalWrite(RELAY, state);
}
/***************************************************
* Handle the interrupt
**************************************************/
void handleButton()
{
state = !state;
Blynk.virtualWrite(RELAYVPIN, state);
digitalWrite(RELAY, state);
}