Skip Navigation

Basic PHP Setup LIFX Smart Light Bulb

In this episode, we will unwrap the LIFX A19 bulb and set it up. Next, we will set up an API token and then run some basic PHP commands.


So the [inaudible] comes in this little tube comes out pretty easy as you can see here, uh, just as the light bulb in it and a tiny little instruction manual, which you don't really even need. I think they probably could've put the instructions sorta on the box and just called it a day, or just told you, download the app. Uh, and that's what you're going to do. You're going to download the app and you're going to connect to it like any other, um, IOT device. You just essentially set it up with your home Wi-Fi or your school wifi or wherever you're at. Once you have that setup, you're going to create an account and that's going to be your account with life X, and then you'll also need to,

Once you have all that set up, so I'm going to assume right now that you have all of that setup. So if you don't have that set up, pause this video right here, set that all up and you'll have the light configured. Okay. Now you got it done. Great. Okay. So the next thing we need to do is if we go to the API and you can do that by going to, and then I'm under authentication, and that's our first step here. We need to be able to authenticate into our light. Now, before we jump into this, make sure that you are all set up. And I mean, by you're all set up is that you should be able to control the color of your light and it's already been named. And all of that should be able to control the color of your light view, your phone.

If you can't do that, that means you don't have it all the way set up. As I went through the setup and I kind of plugged it in and it was like, it's on it's good. And then I ran into a little bit of trouble. What I realized is I needed to turn off the light and turn it back on. And that for some reason, registered it and then said, Hey, and then finish the register on the phone. So then I clicked on the phone and I was able to finish it. I don't know if that was just me. Maybe that's on all of them, but if that run, if you run into that issue, that's what's going on there. Just turn off the light, turn it back on, just be a switch. Okay. So now once you have that

Up an API key to do that. If you click on account settings right here, it's going to take you to this page where it's going to ask you to go in and register and sort of creating an account I have already registered and I've already created an account. I use the same sort of account that I set up on the app. Then you're going to need to generate a new token. This token is your API token, and you're going to give it a meaningful name so that you'll remember what this is. I called mine light and I click generate. And that is my

Is very, that's something that you need to keep very safe as you don't want anybody to know about this token, assuming that I kept this token, uh, and you were able to reference this token inside your code, you would technically be able to control my light because you have my token. And that would be a bit of a problem for me, right? So of course you want to reset your tokens if your token is exposed. So in this case, my token has been exposed. I would want to reset it, uh, so that you wouldn't know my token. That's important. I'll need that token. If I refresh this page, I will lose the token and there'll be no way to get back the token. The only way to do that then would be to create a new token. So keep that in mind as you do this, all right, now that we have this token setup, I'm going to leave this right where it is, and I'm going to go into my, uh, PHP stack.

So, uh, we're running PHP, I assume because you're joining us. You already have a PHB environment setup. I use Laravel valet, which is part of, uh, the Laravel system. And you can check this out here on co-team IO. Uh, let's go in here to the homepage and we'll search for a valet. So that is the series. If you want to get the same exact setup that I have, this is only going to work on a Mac. So that's, Laravel valet. If you might be running PHP a via something else that might be map. So getting started with map and that'll show you how to configure your map installation. And that might be where you're running PHP. Of course, you could also run PHB via the terminal, sort of the PHP version that's installed mammal work on both windows and Mac. There's also lamp and zap in case you need a PHB vironment or you're using one of those.

So their assumption is, you know, uh, which environment you're using. So it's not really an issue. Keep in mind, I am using Laravel valet. Laravel valet is set up in my build directory, which, uh, I then put all my central files inside of it, my projects, for example. So I'm going to set up a new project and I'm going to call it, um, my light. And so with Laravel valet, I can just go into the URL and type of my light dot test. And I now have essentially a folder. It says not found because I have not put anything in it. So then I'm going to go into sublime text and that's the editor that I'm using. And I'm going to set up a basic PHP page. So we'll save this page as index dot PHP, and I want to save it into my builder directory, into my light folder.

And I'll click save all, then write PHP, and then something simple like echo, hello, and then a semi-colon. If I refresh this, then you'll see the word. Hello, and I know everything's set up. So I'm assuming that you have that down. If you don't have that down, check out one of those previous series here on co time IO, and we'll get you all figured out and set up. So in, so index dot PHP, this is going to be our first test. And this episode, we're just going to try to change the color of the light VR PHP page and not integrate anything fancy yet. We'll need to set up our auth key and all of that to get this to work. So if we go back to our, um, our cloud here, this is our API token. Okay. If we go back to her, I have another tab here and I click on authentication.

This is where we just, when, and we clicked on account settings, I'm opened it up in a new tab. That way I wouldn't lose that token. I could always reset. It's not that big a deal. So you have a couple basic ways to set this up. You can do HTTP authorization, headers, or basic authentication or Oh, Olaf access now. And anytime I do Orr thing, I always get like takes me forever for some reason. So I'm going to do the basic ones. So we'll can either do age to be auth authorization, header, or basic authentication. I'm going to just do the basis authentication. Now you have your options of curl PHP and Python. I'm going to use PHP and here's some basic code here, so I can copy this code and paste it. The link that is where we're going to reference the API, the auth token. That is your auth token that you generated right here. So I'll paste it in right there. Then it's a C H is equal to a curl, a knit link curl set, C H curl LLTP user password, AutoCAD

Dot response.

And that should be our setup. Now we can also do the authorization headers, this one similar code here. I could then paste to this one and do the same thing and paste in the token. And that's essentially the same kind of thing. Curlin knit array. And you're off token. If I want to see what this is, I can then say, print are the response. And that should give me the data. That's a B that's connected to my light. So let's go ahead and give this a shot. I'll go to my light dot test. I'll refresh.


I now load up the adjacent object. So I called it label is lamp. It's connected as true. It's currently off it's current color. That was set up last time. It's brightness. It's a group it's in the living room. It's location is home it's product it's company capabilities. Any of this other information, we'll, won't be using that. But up here, this is what we want to set the color, the hue, the saturation, the brightness for our light. So now that we have this set up, we can do something as simple as turning the power on. So let's go ahead and do that. If we go back to our author authentication tab here, and we see some endpoints for the API, so here's toggling power state, uh, breathe, a fact pulse effect cycle, uh, lists, scenes, activate scenes, validate colors. So quite a bit of things that you can do with this light. I want to start with something super basic, and that is set state. If we scroll down to set state, these are some of the body parameters that you can send. So power color, brightness duration,

And infrared fast. So

Here is infrared. My, uh, a 19 does not have infrared capabilities. So this won't work for my light as it doesn't have that hardware. If I scroll down here as the example, they give you a Python and go and curl request. I want to use this PHP one. We have some of this already set up, but we can copy the same code again and paste it. And you'll see the difference here. So the first two lines, this line, all lights all, and then state we've added the word state to it. We have an API token. That's our API token. We have our Curlin knit curl in it the same as before their authorization here, our setup and our response. So it's pretty much the same thing here. If we bring it back to this, we can see headers and then here's our data, and this is the data here or a body parameters that we're going to go and send. Currently, we're saying power is equal to on. So that should take us and turn on the light. Let's go and take a look. So I'm going to refresh this page

And my light is now on pretty cool.

So that's our basics. What I want to do next is change its color. Okay. So let's go back to here. It says color and color sets a string value. The color is set to the, uh, the color set, the light to, if we click on color, it says, these are the options that we can specify for our color value. Well, the easiest from these probably is either just red or a hexadecimal color for deep red. What I can do for that then is say color, and then specify the value here of the color. Now, if a text, a decimal makes sure to put the pound sign. Now, if I refresh

The color is now red, pretty awesome. Right?

So that's the super basics. Let's go onto the next episode. And we'll talk about configuring multiple things at the same time. And then we'll start integrating JavaScript into this.