Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 8be2871

Browse files
authored
Add files via upload
1 parent 034eff7 commit 8be2871

File tree

2 files changed

+553
-0
lines changed

2 files changed

+553
-0
lines changed

examples/ISR_Switch/ISR_Switch.ino

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
/******************************************************************************
2+
* examples/ISR_Switch.ino
3+
* For ESP8266 boards
4+
* Written by Khoi Hoang
5+
*
6+
* Built by Khoi Hoang https://github.com/khoih-prog/ESP32TimerInterrupt
7+
* Licensed under MIT license
8+
* Version: v1.0.1
9+
*
10+
* Notes:
11+
* Special design is necessary to share data between interrupt code and the rest of your program.
12+
* Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
13+
* variable can not spontaneously change. Because your function may change variables while your program is using them,
14+
* the compiler needs this hint. But volatile alone is often not enough.
15+
* When accessing shared variables, usually interrupts must be disabled. Even with volatile,
16+
* if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
17+
* If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
18+
* or the entire sequence of your code which accesses the data.
19+
*
20+
* Version Modified By Date Comments
21+
* ------- ----------- ---------- -----------
22+
* 1.0.1 K Hoang 25/11/2019 New release fixing compiler error
23+
********************************************************************************/
24+
/* ISR_Switch demontrates the use of ISR to avoid being blocked by other CPU-monopolizing task
25+
*
26+
* In this complex example: CPU is connecting to WiFi, Internet and finally Blynk service (https://docs.blynk.cc/)
27+
* Many important tasks are fighting for limited CPU resource in this no-controlled single-tasking environment.
28+
* In certain period, mission-critical tasks (you name it) could be deprived of CPU time and have no chance
29+
* to be executed. This can lead to disastrous results at critical time.
30+
* We hereby will use interrupt to detect whenever the SW is active, then switch ON/OFF a sample relay (lamp)
31+
* We'll see this ISR-based operation will have highest priority, preempts all remaining tasks to assure its
32+
* functionality.
33+
*/
34+
35+
//These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
36+
#define TIMER_INTERRUPT_DEBUG 1
37+
38+
#define BLYNK_PRINT Serial
39+
//#define BLYNK_DEBUG true
40+
41+
#include <ESP8266WiFi.h>
42+
43+
//#define USE_BLYNK_WM true
44+
#define USE_BLYNK_WM false
45+
46+
#define USE_SSL false
47+
48+
#if USE_BLYNK_WM
49+
#if USE_SSL
50+
#include <BlynkSimpleEsp8266_SSL_WM.h> //https://github.com/khoih-prog/Blynk_WM
51+
#else
52+
#include <BlynkSimpleEsp8266_WM.h> //https://github.com/khoih-prog/Blynk_WM
53+
#endif
54+
#else
55+
#if USE_SSL
56+
#include <BlynkSimpleEsp8266_SSL.h>
57+
#define BLYNK_HARDWARE_PORT 9443
58+
#else
59+
#include <BlynkSimpleEsp8266.h>
60+
#define BLYNK_HARDWARE_PORT 8080
61+
#endif
62+
#endif
63+
64+
#if !USE_BLYNK_WM
65+
#define USE_LOCAL_SERVER true
66+
67+
// If local server
68+
#if USE_LOCAL_SERVER
69+
char blynk_server[] = "yourname.duckdns.org";
70+
//char blynk_server[] = "192.168.2.110";
71+
#else
72+
char blynk_server[] = "";
73+
#endif
74+
75+
char auth[] = "***";
76+
char ssid[] = "***";
77+
char pass[] = "***";
78+
79+
#endif
80+
81+
#define DEBOUNCE_TIME 25
82+
#define LONG_BUTTON_PRESS_TIME_MS 100
83+
#define DEBUG_ISR 0
84+
85+
#define VPIN V1
86+
#define LAMPSTATE_PIN V2
87+
88+
//Blynk Color in format #RRGGBB
89+
#define BLYNK_GREEN "#23C48E"
90+
#define BLYNK_RED "#D3435C"
91+
92+
WidgetLED LampStatus(LAMPSTATE_PIN);
93+
94+
volatile unsigned long lastDebounceTime = 0;
95+
volatile bool buttonPressed = false;
96+
volatile bool alreadyTriggered = false;
97+
98+
volatile bool LampState = false;
99+
volatile bool SwitchReset = true;
100+
101+
#define RELAY_PIN D1
102+
#define BUTTON_PIN D3
103+
104+
BlynkTimer Timer;
105+
106+
unsigned int myWiFiTimeout = 3200L; // 3.2s WiFi connection timeout (WCT)
107+
unsigned int buttonInterval = 511L; // 0.5s update button state
108+
109+
void ICACHE_RAM_ATTR Falling();
110+
void ICACHE_RAM_ATTR Rising();
111+
112+
void ICACHE_RAM_ATTR lightOn();
113+
void ICACHE_RAM_ATTR lightOff();
114+
void ICACHE_RAM_ATTR ButtonCheck();
115+
void ICACHE_RAM_ATTR ToggleRelay();
116+
117+
BLYNK_CONNECTED()
118+
{
119+
LampStatus.on();
120+
Blynk.setProperty(LAMPSTATE_PIN, "color", LampState? BLYNK_RED : BLYNK_GREEN );
121+
Blynk.syncVirtual(VPIN);
122+
}
123+
124+
// Make this a autoreleased pushbutton
125+
BLYNK_WRITE(VPIN)
126+
{
127+
if (param.asInt())
128+
ToggleRelay();
129+
}
130+
131+
void ICACHE_RAM_ATTR Rising()
132+
{
133+
unsigned long currentTime = millis();
134+
unsigned long TimeDiff;
135+
136+
TimeDiff = currentTime - lastDebounceTime;
137+
if ( digitalRead(BUTTON_PIN) && (TimeDiff > DEBOUNCE_TIME) )
138+
{
139+
buttonPressed = false;
140+
ButtonCheck();
141+
lastDebounceTime = currentTime;
142+
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), Falling, FALLING);
143+
}
144+
}
145+
146+
void ICACHE_RAM_ATTR Falling()
147+
{
148+
unsigned long currentTime = millis();
149+
150+
if ( !digitalRead(BUTTON_PIN) && (currentTime > lastDebounceTime + DEBOUNCE_TIME))
151+
{
152+
lastDebounceTime = currentTime;
153+
ButtonCheck();
154+
buttonPressed = true;
155+
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), Rising, RISING);
156+
}
157+
}
158+
159+
void heartBeatPrint(void)
160+
{
161+
static int num = 1;
162+
163+
Serial.print("B");
164+
if (num == 80)
165+
{
166+
Serial.println();
167+
num = 1;
168+
}
169+
else if (num++ % 10 == 0)
170+
{
171+
Serial.print(" ");
172+
}
173+
}
174+
175+
void checkButton()
176+
{
177+
heartBeatPrint();
178+
179+
if (LampState)
180+
Blynk.setProperty(LAMPSTATE_PIN, "color", BLYNK_RED);
181+
else
182+
Blynk.setProperty(LAMPSTATE_PIN, "color", BLYNK_GREEN);
183+
}
184+
185+
void ICACHE_RAM_ATTR ButtonCheck()
186+
{
187+
boolean SwitchState = (digitalRead(BUTTON_PIN));
188+
189+
if (!SwitchState && SwitchReset)
190+
{
191+
ToggleRelay();
192+
SwitchReset = false;
193+
}
194+
else if (SwitchState)
195+
{
196+
SwitchReset = true;
197+
}
198+
}
199+
200+
void ICACHE_RAM_ATTR ToggleRelay()
201+
{
202+
if (LampState)
203+
lightOff();
204+
else
205+
lightOn();
206+
}
207+
208+
void ICACHE_RAM_ATTR lightOn()
209+
{
210+
digitalWrite(RELAY_PIN, HIGH);
211+
LampState = true;
212+
}
213+
214+
void ICACHE_RAM_ATTR lightOff()
215+
{
216+
digitalWrite(RELAY_PIN, LOW);
217+
LampState = false;
218+
}
219+
220+
void setup()
221+
{
222+
Serial.begin(115200);
223+
224+
pinMode(RELAY_PIN, OUTPUT);
225+
pinMode(BUTTON_PIN, INPUT_PULLUP);
226+
227+
digitalWrite(RELAY_PIN, LOW);
228+
LampState = false;
229+
230+
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), Falling, FALLING);
231+
232+
#if USE_BLYNK_WM
233+
Blynk.begin();
234+
#else
235+
unsigned long startWiFi = millis();
236+
237+
WiFi.begin(ssid, pass);
238+
239+
do
240+
{
241+
delay(200);
242+
if ( (WiFi.status() == WL_CONNECTED) || (millis() > startWiFi + myWiFiTimeout) )
243+
break;
244+
} while (WiFi.status() != WL_CONNECTED);
245+
246+
Blynk.config(auth, blynk_server, BLYNK_HARDWARE_PORT);
247+
Blynk.connect();
248+
249+
if (Blynk.connected())
250+
Serial.println("Blynk connected");
251+
else
252+
Serial.println("Blynk not connected yet");
253+
#endif
254+
255+
Timer.setInterval(buttonInterval, checkButton);
256+
}
257+
258+
void loop()
259+
{
260+
Blynk.run();
261+
Timer.run();
262+
}
263+

0 commit comments

Comments
 (0)