Norway


(Already familiar with and LIFX? Skip straight to the tutorial; TL;DR is that you can, with a few lines of code, directly control lights in your house or office right from your )

A brief introduction: “Why IOT?”

For a long time, IOT (Internet of Things) has been something that doesn’t interest me at all.

For starters, I always considered ot “ your house with Alexa”, and my family has a “no always-on microphones” rule in the house, so no Echo, no Google Home, no nothing. (Yes, we have smartphones, but we even work—and are trying to work even more—to limit their ubiquity in the house). It was only recently that I realized there are many other ways to control smart .

But more importantly, I just didn’t get the appeal. Why is this actually valuable?

It took visiting my dad for Thanksgiving and seeing that he received practical value from his IOT devices—for example, turning on the exterior lights of his house when he drives up actually makes it easier to navigate the driveway, or pressing a single button at bed time to trigger a series of events saves him from manually walking around the house every night flipping switches.

As a programmer, of course, I instantly saw how I could use IOT devices to allow my applications to interact with the real world. I’m totally fascinated by this idea; I’ve long wanted a button I could press that would make things happen on the Internet. (Yes, I own several Amazon Dash buttons, but they are pretty steep learning curve and not particularly easy to get).

So, at a friend’s recommendation, I got my first IOT devices: LIFX lights.

Why LIFX?

There are many different smart lighting companies. Sylvania, Phillips Hue, LIFX; the list goes on and on.

Hue requires you to have a Hue hub, so it’s hard to just get started with. Sylvania has a hub, but also connects to SmartThings (a more centralized, shared system for IOT); great bulbs, but not good for one-offs like this.

With LIFX, each bulb functions independently; that means you can just buy one and only one, and also each individual bulb can work with multiple systems like SmartThings and ’s HomeKit. The bulbs are pricier as a result, so if you’re going to do your whole house with them, I’d consider a SmartThings hub and some Sylvania bulbs. But if you just want to test the waters, LIFX is a solid place to start.

Another win for LIFX: There’s a direct IFTTT integration to your LIFX account. This is the easiest possible connection between an and an IOT device you can possibly make.

If you’re just getting started, your cheapest multi-color option is the currently-$45 “LIFX Mini Color” (https://www.lifx.com/products/lifx-mini). I bought mine all during their incredibly discounted holiday season, so if all else fails you can wait until Black Friday to snatch some up.

Let’s start simple and look at how to control a LIFX light from your Laravel application.

The actual tutorial

Let’s do this. We’re going to enable your Laravel app, with just a few lines of code, to control your LIFX lights, right in front of you, right now.

Connecting your LIFX bulbs to a webhook in IFTTT

First, visit https://ifttt.com/lifx and authorize your IFTTT account with your LIFX account.

Next, create a new IFTTT Applet and set up the type “Webhooks”. IFTTT calls this the “Maker webhooks” type, and you’ll have to give it permission to hook into your IFTTT account.

Starting your IFTTT with the webhooks 'this'  - lifx ifttt choose webhook - Controlling LIFX lights with your Laravel (or other PHP) applications

You’ll want to go into that Maker webhooks type and visit the documentation page, where you’ll get a sample webhook URL you can ping from your app. You only get one URL, but you can its {event} segment per intended use.

Getting the IFTTT webhooks documentation  - lifx ifttt maker documentation - Controlling LIFX lights with your Laravel (or other PHP) applications

The sample URL you get should look something like this:

https://maker.ifttt.com/trigger/{eventName}/with//{yourKey}

For this example, we’re going to be triggering the “new_episode” event, which I’ll trigger from my Laravel app every time there’s a new episode of my podcast.

Finish creating the Applet

Let’s go back to that new Applet you were creating. Now that you have Maker webhooks enabled, you can set it so your “this” is “Receive a web request with Event Name of ‘new_episode'”. Done. It’s now just listening for this URL:

https://maker.ifttt.com/trigger/new_episode/with/key/{yourKey}

Now, let’s set the “that”. Pick LIFX, and authorize your LIFX account with your IFTTT account.

Choosing the LIFX 'that'  - lifx ifttt choose lifx - Controlling LIFX lights with your Laravel (or other PHP) applications

There are a few actions you can to take with your LIFX lights, and you can dig into all of them. I picked “Blink lights”, and I set the lights on the front of my house to blink blue 3 times at a bright setting. Why not.

Setting your LIFX settings  - lifx ifttt lifx settings - Controlling LIFX lights with your Laravel (or other PHP) applications

Testing your webhook

Want to test it to make sure it works? Just ping it with Curl on your command line. Remember, {yourKey} can be gotten by visiting the “Maker webhooks” service section of IFTTT and visiting “Documentation”.

Run this command:

curl -X POST https://maker.ifttt.com/trigger/new_episode/with/key/{yourKey}

and you should, after a second or three, see your lights blink. Almost done!

Pinging a webhook from your Laravel/PHP apps

Finally, you just have to ping a webhook from your Laravel (or other PHP) app.

There are three main ways to send a POST HTTP request in PHP: Curl, file_get_contents, or Guzzle. There are a million examples if you just google “PHP send POST”, so I’ll just give you quick examples for Guzzle (if you already have it on your project, which you will if it’s Laravel) or file_get_contents if you don’t.

Pinging a webhook with Guzzle

Let’s assume you’re using Laravel. You’ll want to move your key out of your code in case you ever share it with anyone else, which you can do by adding a section to the config/services.php file:

    // config/services.php
    // ...
    'ifttt' => [
        'webhook_key' => env('IFTTT_WEBHOOK_KEY'),
    ],

And then add that environment variable to .env (with the real key) and .env.example (with an empty key, as an example):

# In .env
IFTTT_WEBHOOK_KEY=myKeyFromTheIftttDocumentationPage

# In .env.example
IFTTT_WEBHOOK_KEY=

Finally, you can make the call:

$eventName = 'new_episode';

(new GuzzleHttpClient)->request(
    'POST',
    'https://maker.ifttt.com/trigger/' . $eventName . '/with/key/' . config('services.ifttt.webhook_key')
);

Boom. Up and running. Throw that bad boy into a cron job, an event listener, a controller method, or whatever else, and any action or trigger in your app can now make changes to your LIFX lights.

Pinging a webhook with file_get_contents

Don’t have Guzzle? You can use Curl or file_get_contents; just because Curl is a dependency that MAYYY not be around on some servers, I’ll show you file_get_contents.

file_get_contents(
    'https://maker.ifttt.com/trigger/event_name/with/key/your_key_here',
    false,
    stream_context_create([
        'http' => [
            'header' => "Content-type: application/x-www-form-urlencodedrn",
            'method' => 'POST',
        ]
    ])
);

Outro

That’s just one little thing. One little bit of LIFX, a bit of IFTTT (I’m sure you’ve realized that you can use this to trigger all sorts of different events with your apps and IFTTT), and only a single-direction call (app changes real world).

Next time we’ll talk about receiving input from an IOT device on your apps. Stay tuned, dear listeners (readers)!



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here