Skip to content

Commit

Permalink
First upload
Browse files Browse the repository at this point in the history
  • Loading branch information
aallan committed Sep 9, 2015
1 parent eab4471 commit e65ab0b
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 2 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
# smart-light-switch
Example code showing how to build a smart light switch.
# Smart Light Switch

One of the real problems with the current generation of smart light bulbs, is that the smarts are in the bulb.

While they have been held up as a massive Internet of Things success story possibly, in the longer term, this will be not turn out to be the case. Because light bulbs are something that you turn on, and off, from a switch in the wall.

You can make most smart light bulb systems unresponsive by using the wall switch. Effectively a smart light bulb replaces a thing we use every day, the light switch, but it does it poorly. Really we need to replace the switch, not the bulb.

=== What is a smart switch?

A smart light switch not only lets you turn the light on, or off, using the switch itself but also remotely via Bluetooth LE. The switch should also know it's current status — in other words whether the bulb is on or off — and send out a notification over Bluetooth to subscribed applications when the switch is toggled to allow them to update their local status.

=== What hardware will I need?

We'll need the following hardware to build the light switch:

* http://www.makershed.com/products/arduino-uno-revision-3[Arduino Uno]
* https://www.adafruit.com/products/1697[Adafruit nRF8001 Bluefruit LE]
* A breadboard
* An LED
* A 220Ω and a 10kΩ resistor
* Jumper wires
* A tactile button switch

optionally (see <<real-lightbulbs>>) we'll also make use of a:

* http://www.makershed.com/products/powerswitch-tail-ii[PowerSwitch Tail]
Binary file added ble-light-switch.fzz
Binary file not shown.
Binary file added ble-light-with-powertail.fzz
Binary file not shown.
Binary file added ble-light-with-powertail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ble-smart-switch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions ble-smart-switch/ble-smart-switch.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <SPI.h>
#include <BLEPeripheral.h>

#define LED_PIN 3
#define BUTTON_PIN 4

#define BLE_REQ 10
#define BLE_RDY 2
#define BLE_RST 9

int currentState;
int debounceState;
int switchState = 0;
int ledState = 0;

// create peripheral instance
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);

// create service
BLEService lightswitch = BLEService("FF10");

// create switch characteristic
BLECharCharacteristic switchCharacteristic = BLECharCharacteristic("FF11", BLERead | BLEWrite);
BLEDescriptor switchDescriptor = BLEDescriptor("2901", "Switch");

BLECharCharacteristic stateCharacteristic = BLECharCharacteristic("FF12", BLENotify);
BLEDescriptor stateDescriptor = BLEDescriptor("2901", "State");

void setup() {
Serial.begin(9600);

pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);

// set advertised local name and service UUID
blePeripheral.setLocalName("Light Switch"); // Advertised in scan data as part of GAP
blePeripheral.setDeviceName("Smart Light Switch"); // Advertised in generic access as part of GATT
blePeripheral.setAdvertisedServiceUuid(lightswitch.uuid());

// add service and characteristics
blePeripheral.addAttribute(lightswitch);

blePeripheral.addAttribute(switchCharacteristic);
blePeripheral.addAttribute(switchDescriptor);

blePeripheral.addAttribute(stateCharacteristic);
blePeripheral.addAttribute(stateDescriptor);

// assign event handler for characteristic
switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);

// begin initialization
blePeripheral.begin();

Serial.println(F("Smart Light Switch"));
}

void loop() {
blePeripheral.poll();

currentState = digitalRead(BUTTON_PIN);
delay(10);
debounceState = digitalRead(BUTTON_PIN);


if( currentState == debounceState ) {
if ( currentState != switchState ) {

if ( currentState == LOW ) {
// Button just released

} else {
Serial.print(F("Button event: "));
if ( ledState == 0 ) {
stateCharacteristic.setValue(1);
switchCharacteristic.setValue(1);
digitalWrite(LED_PIN, HIGH);
ledState = 1;
Serial.println(F("light on"));
} else {
stateCharacteristic.setValue(0);
switchCharacteristic.setValue(0);
digitalWrite(LED_PIN, LOW);
ledState = 0;
Serial.println(F("light off"));
}
}
switchState = currentState;
}
}
}

void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
Serial.print(F("Characteristic event: "));
if (switchCharacteristic.value()) {
Serial.println(F("light on"));
digitalWrite(LED_PIN, HIGH);
ledState = 1;
stateCharacteristic.setValue(1);

} else {
Serial.println(F("light off"));
digitalWrite(LED_PIN, LOW);
ledState = 0;
stateCharacteristic.setValue(0);

}
}

0 comments on commit e65ab0b

Please sign in to comment.