Monday, August 31, 2015

Automating LIFX Lights With The LightBlue Bean and IFTTT

The LightBlue Bean is a small low energy Bluetooth Arduino microcontroller with a built in 3-axis accelerometer and temperature sensor. It's quite an interesting way to add connectivity to objects via Bluetooth connectivity instead of Wi-Fi. It has digital and analog pins like a typical Arduino, so you can expand what it can do by adding new elements to the set up.

In order to add new elements, a bit of soldering is required. For this article, I wanted a solder-free demo that anyone with a LightBlue Bean can follow along with, so we'll be focusing on the built in accelerometer. A few months ago, I put together an IFTTT demo here on SitePoint which automatically turns off my LIFX light when I set my Jawbone UP24 to sleep mode. It is a nice touch but I still need to get my smartphone out to turn on my lights each night. I'd prefer something more automatic and decided to try out a few technology experiments to turn the lights on in convenient ways too. In this article, I'll cover the first of my prototypes - I'll be using a LightBlue Bean to detect my door movements and turn my lights on or off depending on if my door is opened or closed.

For those who are a fan of the LightBlue Bean, there is a newer version of the LightBlue Bean on Kickstarter right now called the LightBlue Bean+ which looks pretty neat! It has a rechargable battery, solderless connectors and plenty more useful features. There are only a few days left to put in your pledge!

How This Will Work

Our demo will work like so:

  1. Stick the LightBlue Bean on the door we want to track.
  2. Upload an Arduino sketch which watches for accelerometer changes (the door swinging one way or the other) and sends a message via the Bean Loader Virtual Serial.
  3. Set up a Node server watching for the serial messages coming through.
  4. Set the responses to those messages in the Node server to send a HTTP request to IFTTT's Maker channel to tell it to turn on the light.
  5. Set the LightBlue Bean to Virtual Serial mode.
  6. Run the Node server and close your door to see your light turn on!

Additional rules could be included in our Node server later on to ensure that the code only runs during certain times of day too (to avoid the light going on when it really doesn't need to be). Or even better - add on a photo resistor to detect automatically if the light is needed!

Setting Up Your LightBlue Bean

If you are completely new to using the LightBlue Bean, you'll need to have both the latest Arduino software and the Bean Loader software installed on your Windows 8.1 or Mac computer (apparently the Bean Loader software does not work on older Windows PCs). The guides to getting your LightBlue Bean connected up to your Mac or PC are pretty thorough on the LightBlue Bean website:

This demo has all been set up on Mac OSX but should theoretically work on Windows too.

The steps in this article will assume you are comfortable with the basics of putting a sketch onto your LightBlue Bean, if not, run through the "Getting Started" link for your OS above first.

Our Arduino Sketch Code

All the code is available on GitHub for you to use and adapt for any purpose you choose!

Our Arduino sketch contains most of the brains behind the door watching and looks like so:

[code language="c"]
int minXVal = 0;
int maxXVal = 0;
int minYVal = 0;
int maxYVal = 0;
int minZVal = 0;
int maxZVal = 0;
int movement = 15;
bool ready = false;

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

void loop() {
AccelerationReading acceleration = Bean.getAcceleration();

if (!ready) {
for (int i = 0; i < 100; i++) {
if (acceleration.xAxis > maxXVal) {
maxXVal = acceleration.xAxis;
} else if (acceleration.xAxis < minXVal) {
minXVal = acceleration.xAxis;
}

if (acceleration.yAxis > maxYVal) {
maxYVal = acceleration.yAxis;
} else if (acceleration.yAxis < minYVal) {
minYVal = acceleration.yAxis;
}

if (acceleration.zAxis > maxZVal) {
maxZVal = acceleration.zAxis;
} else if (acceleration.zAxis < minZVal) {
minZVal = acceleration.zAxis;
}

delay(10);
}

ready = true;
} else {
if (acceleration.zAxis < minZVal - movement) {
String stringToPrint = "CLOSED";
Serial.println(stringToPrint);
} else if (acceleration.zAxis > maxZVal + movement) {
String stringToPrint = "OPEN";
Serial.println(stringToPrint);
}
}

Bean.sleep(1000);
}
[/code]

Our Sketch Code Explained

To start with, you'll see a range of variables called minXVal, maxXVal, minYVal, maxYVal, minZVal and maxZVal. These track our LightBlue Bean's initial accelerometer readings. Within my demo code, we only really use the z values, however I've left the others in here as you may need to use their values for your own implementations (e.g. if you place the Bean in a different orientation on the door or have a different sort of door, like a sliding door). The accelerometer tracks the force of gravity on each side of the Bean. We only want to know about the changes in these forces, so we need to know the range of forces acting on the Bean originally when it is stationary on our open door. This is where we store those values:

[code language="c"]
int minXVal = 0;
int maxXVal = 0;
int minYVal = 0;
int maxYVal = 0;
int minZVal = 0;
int maxZVal = 0;
[/code]

Next we set a variable called movement which is the level of movement we'll accept before triggering the action. You'll need to adjust this to suit your own situation (your door may move a lot more subtly whilst open than mine). In the sample code, we're allowing for movement of about -15 to +15 from our min and max values before we trigger anything:

[code language="c"]
int movement = 15;
[/code]

We will need time to run the initial test of the min and max values, so we use the ready variable to tell our loop whether we are ready to start watching for door movements within our min and max ranges:

[code language="c"]
bool ready = false;
[/code]

In our setup() Arduino function, we set the serial connection to start running when the Arduino is ready:

[code language="c"]
void setup() {
Serial.begin();
}
[/code]

Within our loop, we perform all our watching of our accelerometer values from the Bean. This will all be readable via the acceleration variable we set up at the start of the loop() function:

[code language="c"]
void loop() {
AccelerationReading acceleration = Bean.getAcceleration();

// Our accelerometer tests
}
[/code]

Continue reading %Automating LIFX Lights With The LightBlue Bean and IFTTT%


by Patrick Catanzariti via SitePoint

No comments:

Post a Comment