Nick Stanley
Published © GPL3+

Bluetooth Light Switch

Physically engaging light switches with cloud connectivity. Featuring the Wicked Device Turner Onner and Bluz DK.

BeginnerFull instructions provided2 hours2,463
Bluetooth Light Switch

Things used in this project

Hardware components

Bluz DK
Bluz DK
×1
Turner Onner
×1

Software apps and online services

Amazon Alexa service
IFTTT Amazon Alexa service

Story

Read more

Schematics

Overview

Code

turneronner.ino

C/C++
/*
 * turneronner.ino
 * Nick Stanley
 *
 * Uses the Turner Onner from wickeddevice to control light switches via Paricle Cloud
 * Designed for Bluz DK - bluetooth module linked to a Particle Gateway
 *
 * Define the room/load name in LOCALE
 * Define the side the Turner Onner is mounted on in MOUNT
 *
 */
#include "application.h"

//Mounting options
#define LEFT 0
#define RIGHT 1

//Servo PWM pin
#define SERVO_PIN D0
//LED on D7
#define LED_PIN D7

//Room or load name, appends "on" and "off" to cloud function
//ex "Bedroom", "BedroomFan", "Kitchen"
#define LOCALE "BedroomLights"  

//What side the turner onner is mounted on
//ex LEFT or RIGHT       
#define MOUNT RIGHT              

//Servo and position objects
Servo LightSwitchServo;
int pos = 90;

//Pointer functions called by cloud.
//Pointed functions differ based on MOUNT definition
void (*TurnOn)(const char*, const char*);
void (*TurnOff)(const char*, const char*);

/* 
 * FlipUp_CCW
 *
 * Turn the servo counter clockwise, 
 * such that it would flip a switch up
 * LEFT = On
 * RIGHT = Off
 */
void FlipUp_CCW(const char *e, const char *arg)
{
    digitalWrite(LED_PIN, HIGH);
    //Counter clockwise 80 degrees
    for(pos = 90; pos <= 170; pos += 1)
    {
        LightSwitchServo.write(pos);
        delay(15);
    }
    //back to center
    for(pos = 170; pos >= 90; pos -= 1)
    {
        LightSwitchServo.write(pos);
        delay(15);
    }
    digitalWrite(LED_PIN, LOW);
}
/* 
 * FlipDown_CW
 *
 * Turn the servo clockwise, 
 * such that it would flip a switch down
 * LEFT = Off
 * RIGHT = On
 */
void FlipDown_CW(const char *e, const char *arg)
{
    digitalWrite(LED_PIN, HIGH);
    //clockwise 90 degrees
    for(pos = 90; pos >= 0; pos -= 1)
    {
        LightSwitchServo.write(pos);
        delay(15);
    }
    //back to center
    for(pos = 0; pos <= 90; pos += 1)
    {
        LightSwitchServo.write(pos);
        delay(15);
    }
    digitalWrite(LED_PIN, LOW);
}

/*
 * setup
 *
 * Point the on/off functions per the defined 
 * mounting position, initialize the servo, and
 * subscribe to cloud events
 */
void setup() 
{
    //Name for cloud function
    String loc = LOCALE;
    //Direction pointing
    if(MOUNT == LEFT)
    {
        TurnOff = &FlipUp_CCW;
        TurnOn = &FlipDown_CW;
    }
    else
    {
        TurnOn = &FlipUp_CCW;
        TurnOff = &FlipDown_CW;
    }
    //Servo init
    LightSwitchServo.attach(SERVO_PIN);
    LightSwitchServo.write(pos);
    delay(15);
    //LED init
    pinMode(LED_PIN, OUTPUT);
    //Cloud subscriptions
    Particle.subscribe(loc + "On", TurnOn, MY_DEVICES);
    Particle.subscribe(loc + "Off", TurnOff, MY_DEVICES);
}
/*
 * loop
 * 
 * Sleep_Mode_CPU - check in every 100ms and
 * process any cloud events, otherwise run in
 * low power mode
 */
void loop() 
{
   System.sleep(SLEEP_MODE_CPU);
}

Credits

Nick Stanley

Nick Stanley

5 projects • 13 followers
Software Engineer - System Integrator - Maker of Things

Comments