Connecting Raspberry Pi to AWS Iot Core : Setup and code Using Python and AWS IOT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in today's video we'll connect an iot device a raspberry pi in our case to aws's ecosystem iot core to be specific so in this video first we'll look at an introduction to all the technologies we'll use in today's video including aws iot core internet of things and mqtt then we'll look at the architecture of today's demo and also we'll take a peek at all the steps we need to take in order to connect an iot device otherwise known as a thing finally we'll get on to the interactive demo in which we'll connect raspberry pi to aws iot core and send messages back and forth so let's look at the introduction first we'll talk about iot so iot or internet of things is a system of a bunch of devices working in conjunction with each other all of the device and the sensors can kind of talk to the cloud through some kind of connectivity in today's video we'll use our iot device which is the raspberry pi and make it talk to aws iot core service so aws iot core is a cloud service provided by amazon's cloud ecosystem and it lets you easily interact with connected devices it's scalable as with most cloud services because it can support billions and even trillions of messages the aws iot message broker implementation is based on mqtt so let's see what that is mqtt stands for message queuing telemetry transport and it's a lightweight messaging protocol used in aws iot core to interact with other devices which in this case is the raspberry pi so let's see how it actually works so mqtt would be the broker in this case and input devices or devices that can give an input would publish data to the broker or mqtt then output devices or input and output devices will subscribe to a certain topic in the broker and in return the broker or mqtt would publish data to the output device or the input output device here's an example temperature sensor could send out temperature data to the broker or mqtt and the nest thermostat that has subscribed to the service uses this information to turn on or off based on the temperature another implementation could be a gps location tracker that can tell nest if the owner is inside the house or outside and based on that nest can also take a decision of turning on or off the ac unit a motion detection sensor right here publishes information to the mqtt broker that motion was detected and a cctv camera can start recording the video footage and finally a soil moisture detection sensor can publish moisture data to mqtt broker and the sprinkler will subscribe to that topic and that'll take action as needed all right let's see the architecture of today's demo so we'll configure aws iot core to create new certificates and attach policies to them so that anonymous users will be able to access iot core with the right certificates then we'll copy the certificates onto our raspberry pi and write a small python program that publishes messages as well as reads the messages finally it can also subscribe to the messages and read them too so ideally we should be sending actual data such as temperature sensor data but in this video we'll send a simple text message back and forth and the last thing we'll do before the demo is check out what steps we have to take in order to successfully connect our aws iot core to raspberry pi every input output iot device is called a thing and we have to register all the things then we can download our iot core certificates onto the raspberry pi we then need to configure a cognito to allow anonymous users with certificates then we'll create and associate some policies after that we'll make a python program to connect these two and finally we'll subscribe to all the topics and publish messages with our python program so let's implement these steps so the first thing we're going to do is register an iot core thing which is the raspberry pi in this case so i'll go to my aws iot core and let me sign into my console and okay so here it is iot core and it'll redirect me to aws iot so here i can register an iot thing so i'll go to manage and go to things and i'm going to press register a thing because we want to register a new one if you're making a product and you want to apply the same certificates to hundreds of different devices you can bulk register many aws iot things but i'm only going to register one raspberry pi so i'll say create a single thing and now you can add your device to the thing registry so i'll call mine raspberry pi because i'm using the raspberry pi and pretty much everything you can just keep default and i'll press next i'm just going to use the one click certificate creation so i'm just going to press create certificate and we need to download all of these the public key the private key and just the regular certificate for this thing so i'll press download and then download download and the last thing you'll need to download is a root ca for aws iot since it uses ssl we'll need to securely connect raspberry pi to aws iot so i'll just press download here and this has changed in the last year so it takes us to our amazon documentation and since i'm using amazon trust services i'm going to download this one so i'll click it so here we have a root ca certificate and i can just say file and then save page as and it'll save it as a certificate file for us so i'll go to my correct folder raspberry files here we go and i'll call it root dash ca dot pem not pem okay now let's save it and then it just saves it for us and since i need to copy these three files into my right folder i'll just say show in finder and that'll open up the folder for me so here we have our keys so i'll just select this and then copy them and then i'll just paste them into my folder and here it copied both of them but it's missing one it has the keys it just needs the certificate so i'll open this in finder also and i believe it shows it here yes we'll copy this and we'll paste it into here okay so now we can rename all these files so i'll just take out the device id of these so that it looks a bit nicer here we'll just take that out and it'll just be public and private.pem.key okay and this is optional i'm just taking it out so that it's easier for us later on now we'll want to take these local files and push them to our raspberry pi and to do that i'm going to use filezilla here we go filezilla is an ftp client and it helps you upload and download files what from one server to another in this case it's our macbook to the raspberry pi so i'm going to connect to my raspberry pi i've already configured all the details so i'm just going to press connect and it's connecting to my raspberry pi all right now it connected to our raspberry pi so i'm just going to drag over the files but first i want to create a new directory in my raspberry pi so i'll right click and say create directory and the directory name is going to be aws iot and then i'll press ok so it creates our directory now i just have to go into that folder uh yeah there it is okay so i'll double click to go inside and here we're in the folder now so now i can take my files all these four files so i'll just select them and then drag them over into here and there it transferred into our raspberry pi okay that worked alright so we've completed steps one through three now we have to create a cognito and associate policies so let's do it alright so i'll go back to aws and i'm actually going to create a policy first because we have to associate some stuff so i'll go to secure and policies and right now i don't have any so i'll create a policy for the name it can be anything i'll just call it raspberry pi policy and actions and actions will just tell what kind of actions the policy can do for example publish or subscribe to a topic if i wanted to i could just individually say publish and subscribe but i'll say iot colon star that'll just say it can do anything and everything and it creates an arn for us so instead of just doing one specific errand i'm gonna give it access to anything by saying star and then for effect i'll say allow and now we can create our policy and here it shows us we created our policy so we've created a policy but now i need to associate it with my certificate and associate this certificate to my thing which is the raspberry pi so i'll go to certificates and here we have our certificates so i'll right click and say attach policy here we can attach our created policy which is raspberry pi policy and i'll press attach and we also want to attach a thing to it which is our raspberry pi device so i'll press attach and yes now it's connected to our policy and our iot device and now before creating an incognito i'm going to activate my certificate so now it's activated and now we can create a cognito so how i'm going to do that is by going to services and cognito so i'll just search it right here so i'll open it in a new tab and it'll take us to our amazon cognito here we're going to manage our identity pools and create a new federated identity so i'll press create new identity pool and for the name i'll call it aws identity pool okay and i'm just going to enable access to unauthenticated identities and everything else should be default so i'll just press create pool and it's creating it for us okay so it we can view the details and what i'm gonna do is for the role summary i'm gonna the bottom one i'm going to view the policy document and here it shows us the policy now i want to add the resource arn of my thing so i'll press edit and i'll just press ok so now we can edit it and in here in the resource we'll add the arn so i'm going to go back to aws iot and i'll go to my things and we can see the ar in here we just click it and then here's the arn we'll just copy all the way to raspberry pi there we go and i'll go back here and inside resource we'll paste it now we can press allow and it'll create it for us it says successful and now we can just go back to aws iot and continue all right so we've associated our policy and created a cognito now we're going to create a python program to subscribe to a topic and publish data so this is the page that i kind of used as the skeleton for my python program it's just amazon's documentation on a little python program with mqtt and in aws iot so here's kind of the most of the code that i'm going to use and yeah let's remote into our raspberry pi now so that we can code it so here's my vnc viewer and i'm going to remote into my raspberry pi okay so it's asking me for passwords so i'll just enter it in and here now we have a raspberry pi and i've already created a file here aws iot core dot pi and i'll show you the folder structure so in home slash pi i have a folder called aws iot and inside of that i have all of the certificates and my python program all right so first i'll paste in the imports we're just importing time and the aws iot mqtt client all right now i'm going to paste in the rest of this code so here this might look a little intimidating but it's easy once you think about it so here we're just connecting to a client this is just a random key i just called it reshub client id but you can really call it whatever you want so here we're configuring the endpoint it this is the default port number for ats and we actually have to configure the endpoint so what i'll do is i'll go to aws iot to get the endpoint so i'll go here and it's in settings so i'll go to settings and here we have our endpoint so i'll just copy this and i'll put it in my code here now we have the endpoint and now we're configuring the credentials here we have our root ca permission file and the private key and the certificate file so let me go to the folder to show you here we have the private key the root ca permission file and the certificate file we're all we're using all these in our python code so let's go back and all of this is just trying to connect to the aws iot core and then it's just printing initiating iot core topic and it's connecting if all this gets done correctly then it'll connect to my mqtt so now i want to subscribe to a topic how i'm going to do that is i said my mqtt client dot which is a variable we defined up here so i'm going to say that again my mqtt client dot and then subscribe because we want to subscribe to a certain topic subscribe and then in that we can define the topic i'll say home slash hello world i'll just call the topic hello world we didn't define a topic yet but we will in a bit and then i'm gonna pass in some more parameters i'll add one and hello world which is a function we'll define in a second hello world okay so now let's define that hello world function so i'll put it at the top because we're defining it down here because we're mentioning it down there sorry so here i'll paste it in and here it is so we're defining hello world with these parameters and then we're printing received message from aws iot core this is optional and then we're printing the topic and the payload which is the actual messages that was received from aws iot core all right now this code is ready to run so i'll go to terminal to run our code and first i need to get into the desired directory all right now we can run our code you might need to install a few packages and modules but i've already done all that so i'll just say python3 aws iot core dot pi let me just check if that's the correct file name yep okay here we go let's run it okay it's saying initiating iot core topic and then it just stopped the program alright it stopped because we didn't add a loop to wait it just stops the program after printing that so let's create a loop i'll go back to my python code and let's edit this all right so after all of this we can create a while loop i'll just paste it in it's saying while true time dot sleep for five seconds wait i indented this okay there we go now we can save this and let's run it again now it'll just keep waiting and it'll have a five second break in between so let's just run that code again and now it's saying initiating iot core topic and then it's just waiting that's because we don't have a we didn't publish anything to iot core yet so let's do that so i've just resized my screen so that we can publish some data and see it on both screens so in aws iot core i'll just go to test and here we want to publish to a topic so the topic we specified in my python code was home slash hello world so we're just going to keep that topic and the message should be aws hello from aws iot console you can make the message anything you want but i'm just going to keep it default and now i'll press publish to topic it says published and here we go almost instantaneously it showed the topic and it shows the payload it's saying a message is hello from aw aws iot console okay that worked perfectly so in this example we just printed a hello world message but in real life project we would actually be doing some actions such as turning on an ac or a motor or something like that i'm not going to get too deep into it and we also published a message from iot core and it printed it here in a raspberry pi now we're going to do the opposite we're going to publish it from raspberry pi and it should print it in iot core alright let's do that all right so first i'll stop this program and then i'm gonna go to my code here and now i want to publish the data to aws io decor so i'm just gonna comment out this and i'm also gonna comment out this line because i don't want uh the raspberry pi to subscribe to aws iot core i want the opposite to happen now we can write our code for publishing so just so that i know it's publishing some data i'm going to say print and publishing message from rpi which is raspberry pi that's just so that i know it's publishing and now i can see just like i said my mqtt client dot subscribe i can say the same for publish my mqtt client dot publish and then the parameters so inside here i'll just paste this in here here this indentation is wrong give me a second okay so the first one is the topic again it's home slash hello world and then this is the quality of service if you want to change it you can check out some documentation i'm going to keep it as one and the payload is again this is just the message this is in simple json format for the message i'll say message by rpi and if you want you can you could you could add more parameters like some sensor data or something like that but i'm just going to keep it simple so now we can save it and that should be it okay so i've cleared my screen and i've again positioned these wind tabs and again i've positioned these windows so that i can see both of them and before running our program i'm going to have to just like i subscribe to aws i o decor from the raspberry pi i'm going to have to subscribe to the raspberry pi from aws iot so i'm going to say subscribe to a topic up here and home slash hello world i'll press subscribe and hear it subscribe to our topic and now we can run our program so python3 aws iot chord upi oh it's giving us an error let me check the code and oh yeah here there's an extra bracket whoops okay let's save that and now we can run it again now it should work okay it's saying initiating and here it shows the message message is message by rpa which is what we specified in the code and it also said publishing message from rpa okay that worked great we successfully published messages back and forth from aws iot core service and our raspberry pi using subscription topics in this video we sent a simple message back and forth to iot core from our raspberry pi and real projects could have multiple sensor data published through different topics and event-based actions could be taken by various subscriber devices such as turning ac on if the temperature is too high i'll also post this code on one of my github repositories check it out if you want to download this code thanks very much for watching if you all had any doubts please comment down below i would love to help you out if you're stuck with any iot core or raspberry pi questions or issues please like subscribe all that jazz until then you can learn anything
Info
Channel: Rishab Teaches Tech
Views: 32,177
Rating: undefined out of 5
Keywords: RaspberryPi project, Send data to IoT Core, Configuring AWS for IOT core, IOT AWS, IOT Cognito, Register IOT device, IOT raspberry aws, ass iot, rishab kattimani, kattimani boyz, 2020, covi19, corona diaries
Id: kPLafcrng-c
Channel Id: undefined
Length: 23min 13sec (1393 seconds)
Published: Mon Aug 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.