Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "blynk/blynk.h"
#include "RelayShield/RelayShield.h"
#include "clickButton/clickButton.h"
SYSTEM_THREAD(ENABLED);
RelayShield myRelays;
int relayOn(String command);
int relayOff(String command);
unsigned long mytime;
const int buttonPin1 = D2;
ClickButton button1(buttonPin1, LOW, CLICKBTN_PULLUP);
const int buttonPin2 = D1;
ClickButton button2(buttonPin2, LOW, CLICKBTN_PULLUP);
const int buttonPin3 = D0;
ClickButton button3(buttonPin3, LOW, CLICKBTN_PULLUP);
WidgetLED BlynkLED1(V4); // define LED to turn on and off on Blynk App.
WidgetLED BlynkLED2(V5); // define LED to turn on and off on Blynk App.
WidgetLED BlynkLED3(V6); // define LED to turn on and off on Blynk App.
WidgetLED BlynkLED4(V8); // define LED to turn on and off on Blynk App.
enum {
LIGHT_OFF,
LIGHT_ON,
} state;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "60e0d1003d5d43a0958ec2734129731a";
void setup()
{
// Serial.begin(9600);
pinMode(buttonPin1, INPUT_PULLDOWN);
pinMode(buttonPin2, INPUT_PULLDOWN);
pinMode(buttonPin3, INPUT_PULLDOWN);
button1.debounceTime = 20; // Debounce timer in ms
button1.multiclickTime = 250; // Time limit for multi clicks
button1.longClickTime = 800; // time until "held-down clicks" register
button2.debounceTime = 20; // Debounce timer in ms
button2.multiclickTime = 250; // Time limit for multi clicks
button2.longClickTime = 800; // time until "held-down clicks" register
button3.debounceTime = 20; // Debounce timer in ms
button3.multiclickTime = 250; // Time limit for multi clicks
button3.longClickTime = 800; // time until "held-down clicks" register
myRelays.begin();
Blynk.begin(auth);
BlynkledsOff(); // Start with all Bynk app LEDs turned off
Particle.function("relayOn", relayOn);
Particle.function("relayOff", relayOff);
}
BLYNK_WRITE(V1) {
static int oldParam = 0;
if (param.asInt() && !oldParam) { // On button press
toggleLight1();
}
oldParam = param.asInt();
}
BLYNK_WRITE(V2) {
static int oldParam = 0;
if (param.asInt() && !oldParam) { // On button press
toggleLight2();
}
oldParam = param.asInt();
}
BLYNK_WRITE(V3) {
static int oldParam = 0;
if (param.asInt() && !oldParam) { // On button press
toggleFan();
}
oldParam = param.asInt();
}
BLYNK_WRITE(V7) {
static int oldParam = 0;
if (param.asInt() && !oldParam) { // On button press
toggleLight1();
toggleLight2();
toggleFan();
}
oldParam = param.asInt();
}
void loop()
{
Blynk.run();
// Update button state
button2.Update();
if(button2.clicks == 1 || button2.clicks == -1) {
toggleLight2();
}
button1.Update();
if(button1.clicks == 1 ) {
toggleLight1();
}
if(button1.clicks == -1 ) {
toggleAll();
}
button3.Update();
if(button3.clicks == 1 || button3.clicks == -1 ) {
toggleFan();
}
}
void toggleLight1() {
if (myRelays.isOn(1) == TRUE) {
state = LIGHT_OFF;
myRelays.off(1);
BlynkLED1.off();
} else {
state = LIGHT_ON;
myRelays.on(1);
BlynkLED1.on();
}
}
void toggleLight2() {
if (myRelays.isOn(2) == TRUE) {
state = LIGHT_OFF;
myRelays.off(2);
BlynkLED2.off();
} else {
state = LIGHT_ON;
myRelays.on(2);
BlynkLED2.on();
}
}
void toggleFan() {
if (myRelays.isOn(3) == TRUE) {
state = LIGHT_OFF;
myRelays.off(3);
BlynkLED3.off();
} else {
state = LIGHT_ON;
myRelays.on(3);
BlynkLED3.on();
}
}
void toggleAll() {
if (state == LIGHT_ON) {
state = LIGHT_OFF;
myRelays.off(1);
myRelays.off(2);
myRelays.off(3);
} else {
state = LIGHT_ON;
myRelays.on(1);
myRelays.on(2);
myRelays.on(3);
}
}
void BlynkLED() {
if (myRelays.isOn(1) == TRUE) {
BlynkLED1.on();
}else {
BlynkLED1.off();
}
if (myRelays.isOn(2) == TRUE) {
BlynkLED2.on();
}else {
BlynkLED2.off();
}
if (myRelays.isOn(3) == TRUE) {
BlynkLED3.on();
}else {
BlynkLED3.off();
}
}
void BlynkledsOff() {
BlynkLED1.off();
BlynkLED2.off();
BlynkLED3.off();
}
int relayOn(String command){
// Ritual incantation to convert String into Int
char inputStr[64];
command.toCharArray(inputStr,64);
int i = atoi(inputStr);
// Turn the desired relay on
myRelays.on(i);
// Respond
return 1;
}
int relayOff(String command){
// Ritual incantation to convert String into Int
char inputStr[64];
command.toCharArray(inputStr,64);
int i = atoi(inputStr);
// Turn the desired relay off
myRelays.off(i);
// Respond
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.