0

empowering creative people

Videos / Raspberry Pi Workshop - Chapter 5 - Particle Pi and IFTTT

At the end of this video we'll be able to control a Raspberry Pi GPIO by email. Let that sink in. We will use Particle Pi and IFTTT to do it.

Search Videos

Related Content

Transcript

+

By the end of this video we're going to be able to flash an LED by sending an email, so to proceed you'll need to subscribe to the services that we talked about in the previous video and this is how they're all going to connect together. A Gmail account is going to be linked with IFTTT that's going to be linked with the particle cloud and the particle cloud is going to publish events to our raspberry pi, which is also running a piece of particle software called the particle agent and when it receives this event it will change the LED state depending on the event type so what's going to happen is we're going to receive some email and we're going to look at the subject what we're going to do is we're going to change the LED state based on the contents of that subject so we'll look for the content LED - on and LED - off, all in lowercase, so this is going to be our control signal basically. Gmail was linked with IFTTT so it is going to look at the subject of that email and decide what to do about the contents if the content matches either of these then it is going to publish a particle cloud event which will get pushed to a Raspberry Pi. Let's get started.

Over on the Pi I have the beginners workshop open in my browser and I have a terminal session on the right, so what I'm going to do is grab the step set up the particle agent on your Pi I'm just going to grab the command that's shown here and just paste it straight in and execute that. Okay we're in the future now that's finished downloading and installing I need to now enter my credentials for my particle account so this will be the ones that you use to sign up for the particle cloud so I'm going to [email protected], it just so happens that I'm using the same email address both for my account and also doing these actions like turning the LED on and off you don't have to use the same one I just am because it's quite convenient. Okay so the Raspberry pi is logging into particle and now we have the opportunity to give our raspberry pi a name and this is just a helpful reference so I'm going to name it after the model of the PI that I'm using which is a model which is the Raspberry Pi 3 model B so I'm going to call it PI 3 B, that will just help, down the track, to distinguish different devices. Ok so something quite important happened with registered our PI to the particle cloud so I'm just going to minimize that and stretch this out a bit, so we can see that our raspberry pi is currently running a particle app and that's called tinker, that's quite important you can you can play with tinker by downloading a mobile application but today we'll just press on with the project's. A quite important point is if you ever want to roll back the particle agent on your Raspberry Pi to factory defaults you can always enter the command particle agent set up and that will roll it back to when we had to sign in with our particle account and name our pi, so that's like a factory reset almost and that's all we need to do on the Raspberry Pi for this video I've already set up my PI with the schematic that you can find in the resources for this video and it's just another basic resistor LED circuit that we're used to building.

So over on the computer this is where we're going to program our Raspberry Pi we need to go over the particle build.particle.io and this is going to open our online programming development environment while that's opening I'll also open up if this then that, and I'll open my Gmail account as well. So back over on particle this is the particle IDE this is where we going to program our Raspberry Pi so you can see at the moment we have a few example apps and we have a what's called an empty app this is like an Arduino sketch and down the bottom you'll see a little heartbeat device connected indicator and there is our PI 3 B so our particle agent is running on our Raspberry Pi it's connected to your account and that means that once we log in to build.particle.io there it is it's connected and it's ready to go so we're going to be programming our Raspberry Pi with from within your web browser that means we could do this from anywhere in the world I'm going to quickly grab the example code to this section so I'll go over to the workshop and find myself down at the particle app so I'm just going to grab this code and copy it straight in okay that all looks okay, I'm going to give it a title I'm going to call this email to led that seems reasonable and the one thing that we have to change with this app is that we need a unique event name this is a public event so this needs to be something that's unique this is the key that's going to turn on or off our LED, so anyone publishing to this event with the correct data will be able to do that I'm going to make it something long like Michael's email is received now, and then just a bunch of random numbers, I'm just going to double-click on that and copy it I'm going to need it quite soon and I think everything else is okay so just use for a magic moment right we're going to save this, email to LED, that's just with that folder button save and now you can see that under my apps we have the app we've just created below there are the example apps by clicking this flash button and looking at this status down here we can see that we're programming our Raspberry Pi from a web browser this could be anywhere in the world.

Okay and the flash is successful devices being updated, okay so the device is ready we've programmed our Raspberry Pi it's now sitting and waiting patiently for these events so let's give it some. Over on IFTTT I'm already logged in so I'm going to click on my username and I'll just click on New Applet, you can see this is the environment where we create IFTTT applets it's really straightforward this then that and you can see that that this is highlighted so I'm going to click that and these are all the services that it can link with Gmail down here but if you just want to search for a service it's as easy as that we can select our Gmail so we're choosing the trigger for our if this-this is the trigger now I've got a few options here what I'm going to do is choose new email in inbox from search because this gives us quite a lot of options to work with it's quite a powerful trigger so what are we going to do the certified every time a new email arrives on the inbox that matches the search query you specify so I'm going to just click through the search operators to find out a little bit more remember we're searching for something in the subject field so once we've clicked through we can have a look at these examples I want to look for something in the subject field, so I can say, subject and then the subject so I'm going to grab that I know I've already copied my key I'll have to type it out so I'll search for subject led on now remember we want to trigger an event based off LED on but also a subject led off so we need to figure out how to do that we can go back to the syntax page and we can say messages that match multiple terms so this is like an or statement and we can use the word or-or we can wrap our trigger in these curly braces which is what I'm going to do so I'm going to subject LED on and the subject LED off and then I'm going to wrap that whole thing in some curly braces and that's our trigger so we can see now the-the trigger is populated with the Gmail logo so now we can choose our action and our action is a Particle action so I'm just going to search for particle and there it is and this is where we're going to publish our event so I'm going to choose that as the action and this this looks a little more complicated but let's just go through it slowly then publish event name so the event name that we're working with for the is Michael's email is received now three one four one five nine so I''ve already copied that into my into my clipboard so I'm just going to paste that that way I won't make any typos. This event includes data so the data is the thing that's going to be passed to our Raspberry Pi and we do want to pass the subject the subject is LED on and LED off so that's what we want and I'll just leave this as public to make things a little straightforward so we can create that action and yes we can finish that so that is everything we need to do to get our email to get out email to LED functionality working, so let's give it a road test.

Over on Gmail I'm already logged in so I'm just going to go to compose and I'm going to email myself with the subject LED - on and now I'm going to press send. Now IFTTT services can take several minutes to actually come through so while you're debugging you can go back to it where you've created your app and you can just click check now and that does a manual update just to check and you can see that after clicking that my LED has come on so how did this actually work what was this led on led off business well back in the particle script we can look at how events are handled this particle.subscribe is something that runs on the particle agent and it subscribes the particle to an event okay that's fair enough the event has a name and whenever that name events is triggered it triggers this 'my handler' well my handler is down here we can see this void(myhandler) so my handler is just a function that we write that takes in the event name and some data so you remember that IFTTT was able to publish the subject as the data so the data that's coming to this function is the data from the subject field and we're comparing against led off and LED on and that's just toggling the LED so just to close the loop on everything I can go back to compose I can email myself again with the subject LED - off, send that, and it hasn't happened yet so I'll go over the IFTTT I'll click check now and we should momentarily see the led turn off and indeed it does.

By the end of this video we're going to be able to flash an LED by sending an email, so to proceed you'll need to subscribe to the services that we talked about in the previous video and this is how they're all going to connect together. A gmail account is going to be linked with IFTTT that's going to be linked with the particle cloud and the particle cloud is going to publish events to our raspberry pi, which is also running a piece of particle software called the particle agent and when it receives this event it will change the LED state depending on the event type so what's going to happen is we're going to receive some email and we're going to look at the subject what we're going to do is we're going to change the LED state based off the contents of that subject so we'll look for the content LED - on and LED - off, all in lowercase, so this is going to be our control signal basically. Gmail was linked with IFTTT so if is going to look at the subject of that email and decide what to do about the contents if the content matches either of these then it is going to publish a particle cloud event which will get pushed to a raspberry pi. Let's get started.
Over on the Pi I have the beginners workshop open in my browser and I have a terminal session on the right, so what I'm going to do is grab the step set up the particle agent on your Pi I'm just going to grab the command that's shown there and just paste it straight in and execute that. Okay we're in the future now that's finished downloading and installing I need to now enter my credentials for my particle account so this will be the ones that you use to sign up for the particle cloud so I'm going to [email protected], it just so happens that I'm using the same email address both for my account and also doing these actions like turning the LED on and off you don't have to use the same one I just am because it's quite convenient. Okay so the Raspberry pi is logging into particle and now we have the opportunity to give our raspberry pi a name and this is just a helpful reference so I'm going to name it after the model of the PI that I'm using which is a model which is the Raspberry Pi 3 model B so I'm going to call it PI 3 B, that will just help, down the track, to distinguish different devices. Ok so something quite important happened with registered our PI to the particle cloud so I'm just going to minimize that and stretch this out a bit, so we can see that our raspberry pi is currently running a particle app and that's called tinker, that's quite important you can you can play with tinker by downloading a mobile application but today we'll just press on with the project's. A quite important point is if you ever want to roll back the particle agent on your Raspberry Pi to factory defaults you can always enter the command particle agent set up and that will roll it back to when we had to sign in with our particle account and name our pi, so that's like a factory reset almost and that's all we need to do on the Raspberry Pi for this video I've already set up my PI with the schematic that you can find in the resources for this video and it's just another basic resistor LED circuit that we're used to building.
So over on the computer this is where we're going to program our Raspberry Pi we need to go over the particle build.particle.io and this is going to open our online programming development environment while that's opening I'll also open up if this then that, and I'll open my gmail account as well. So back over on particle this is the particle IDE this is where we going to program our Raspberry Pi so you can see at the moment we have a few example apps and we have a what's called an empty app this is like an Arduino sketch and down the bottom you'll see a little heartbeat device connected indicator and there is our PI 3 B so our particle agent is running on our Raspberry Pi it's connected to our account and that means that once we log in to build.particle.io there it is it's connected and it's ready to go so we're going to be programming our Raspberry Pi with from within our web browser that means we could do this from anywhere in the world I'm going to quickly grab the example code to this section so I'll go over to the workshop and find myself down at the particle app so I'm just going to grab this code and copy it straight in okay that all looks okay, I'm going to give it a title I'm going to call this email to led that seems reasonable and the one thing that we have to change with this app is that we need a unique event name this is a public event so this needs to be something that's unique this is like the key that's going to turn on or off our LED, so anyone publishing to this event with the correct data will be able to do that I'm going to make it something long like Michael's email is received now, and then just a bunch of random numbers, I'm just going to double click on that and copy it I'm going to need it quite soon and I think everything else is okay so just use for a magic moment right we're going to save this, email to LED, that's just with that folder button save and now you can see that under my apps we have the app we've just created below there are the example apps by clicking this flash button and looking at this status down here we can see that we're programming our Raspberry Pi from a web browser this could be anywhere in the world.
Okay and the flash is successful devices being updated, okay so the device is ready we've programmed our Raspberry Pi it's now sitting and waiting patiently for these events so let's give it some. Over on IFTTT I'm already logged in so I'm going to click on my username and I'll just click on New Applet, you can see this is the environment where we create IFTTT applets it's really straight forward this then that and you can see that that this is highlighted so I'm going to click that and these are all the services that if can link with Gmail down here but if you just want to search for a service it's as easy as that we can select our Gmail so we're choosing the trigger for our if this this is the trigger now I've got a few options here what I'm going to do is choose new email in inbox from search because this gives us quite a lot of options to work with it's quite a powerful trigger so what are we going to do the certified every time a new email arrives on the inbox that matches the search query you specify so I'm going to just click through the search operators to find out a little bit more remember we're searching for something in the subject field so once we've clicked through we can have a look at these examples I want to look for something in the subject field, so I can say, subject and then the subject so I'm going to grab that I know I've already copied my key I'll have to type it out so I'll search for subject led on now remember we want to trigger an event based off LED on but also a subject led off so we need to figure out how to do that we can go back to the syntax page and we can say messages that match multiple terms so this is like an or statement and we can use the word or or we can wrap our trigger in these curly braces which is what I'm going to do so I'm going to subject LED on and the subject LED off and then I'm going to wrap that whole thing in some curly braces and that's our trigger so we can see now the the trigger is populated with the Gmail logo so now we can choose our action and our action is a Particle action so I'm just going to search for particle and there it is and this is where we're going to publish our event so I'm going to choose that as the action and this this looks a little more complicated but let's just go through it slowly then publish event name so the event name that we're working with for the is Michael's email is received now three one four one five nine so I've already copied that into my into my clipboard so I'm just going to paste that that way I won't make any typos. This event includes data so the data is the thing that's going to be passed to our Raspberry Pi and we do want to pass the subject the subject is LED on and LED off so that's what we want and I'll just leave this as public to make things a little straightforward so we can create that action and yes we can finish that so that is everything we need to do to get our email to get out email to LED functionality working, so let's give it a road test.
Over on Gmail I'm already logged in so I'm just going to go to compose and I'm going to email myself with the subject LED - on and now I'm going to press send. Now IFTTT services can take several minutes to actually come through so while you're debugging you can go back to it where you've created your app and you can just click check now and that does a manual update just to check and you can see that after clicking that my LED has come on so how did this actually work what was this led on led off business well back in the particle script we can look at how events are handled this particle.subscribe is something that runs on the particle agent and it subscribes the particle to an event okay that's fair enough the event has a name and whenever that name events is triggered it triggers this 'my handler' well my handler is down here we can see this void(myhandler) so my handler is just a function that we write that takes in the event name and some data so you remember that IFTTT was able to publish the subject as the data so the data that's coming to this function is the data from the subject field and we're comparing against led off and LED on and that's just toggling the LED so just to close the loop on everything I can go back to compose I can email myself again with the subject LED - off, send that, and it hasn't happened yet so I'll go over the IFTTT I'll click check now and we should momentarily see the led turn off and indeed it does.

comments