Alan Mond
Published © MIT

WiFi My Lights

Turn your plain light switch into an internet connected luxury item that will wow your friends and family.

IntermediateFull instructions provided5 hours3,671
WiFi My Lights

Things used in this project

Hardware components

Photon
Particle Photon
×1
Particle RelayShield
×1
12V Power Supply
×1
Rugged Metal Pushbutton With Blue LED ring
×1

Software apps and online services

Blynk
Blynk

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Wifi Lights

This is an STL file to be printed on a 3D printer.

Schematics

Wiring Schematic

Code

wifi-lights.ino

Arduino
This is C++ based code to run on the Particle Photon
#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[] = "your authorization token";


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 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;    
}

Credits

Alan Mond

Alan Mond

1 project • 7 followers

Comments

Add projectSign up / Login