diff --git a/README.md b/README.md index 1c65834..55820b7 100644 --- a/README.md +++ b/README.md @@ -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 <>) we'll also make use of a: + + * http://www.makershed.com/products/powerswitch-tail-ii[PowerSwitch Tail] diff --git a/ble-light-switch.fzz b/ble-light-switch.fzz new file mode 100644 index 0000000..d94cda3 Binary files /dev/null and b/ble-light-switch.fzz differ diff --git a/ble-light-with-powertail.fzz b/ble-light-with-powertail.fzz new file mode 100644 index 0000000..aa777de Binary files /dev/null and b/ble-light-with-powertail.fzz differ diff --git a/ble-light-with-powertail.png b/ble-light-with-powertail.png new file mode 100644 index 0000000..7ed8fc1 Binary files /dev/null and b/ble-light-with-powertail.png differ diff --git a/ble-smart-switch.png b/ble-smart-switch.png new file mode 100644 index 0000000..0b221e6 Binary files /dev/null and b/ble-smart-switch.png differ diff --git a/ble-smart-switch/ble-smart-switch.ino b/ble-smart-switch/ble-smart-switch.ino new file mode 100644 index 0000000..c4570ce --- /dev/null +++ b/ble-smart-switch/ble-smart-switch.ino @@ -0,0 +1,108 @@ +#include +#include + +#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); + + } +}