Choose Which LED to Turn On with Flask (Challenge 3) - Raspberry Pi 5 Tutorial (#15)

Choose Which LED to Turn On with Flask (Challenge 3) - Raspberry Pi 5 Tutorial (#15)

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI

Оглавление (4 сегментов)

Segment 1 (00:00 - 05:00)

and welcome back this is episode number 15 of this tutorial series on Raspberry Pi for complete beginners you can find the series playlist Link in the description and let's get started in this tutorial we are going to control the LEDs on the Raspberry Pi circuit from a web application using flask and Python and once again like I did previously in this series I'm going to show you the problem we want to solve as a challenge and then I'm going to write the solution with you step by step so your challenge is to choose which led to turn on from a web browser so from a URL here depending on what you write in the URL you're going to choose to power on or to power off a certain LED so we have three LEDs we're going to choose which led and then if we want to turn it on or turn it off all right so you can start from what we have written previously so here's the code that you can write I'm going to save it as activity 11. py and I'm going to give you a few tips so you can get started so we are going to create a new root here okay so app do root and then well I'm going to write the root first and I'm going to explain it to you so slash LED and then slash with angle brackets int LED number and then States slash and then int State all right that's the new route and then we're going to create a function so what is this well we're going to start so slash LED and then you see we have here int LED number with angle brackets this means that we're not going to write this in the URL this is going to be replaced by an integer okay so if you want to have Dynamic URLs that you can write numbers or text or whatever inside then you can use this angle bracket with the data type and then the kind of the variable name that we're going to have inside our function so here when you write the URL you will do slash LED slash you will write a number and then slash stat slash another number and actually I need to finish this with the angle brackets all right so for example SL LED sl0 SL state slash1 so provided that we start to count at zero is going to be the first LED and then if we get the state one it means we want to turn on the LED so we're going first led for the state to keep it's simple we can say that zero means turning off one means turning on all right so you have this URL that I already gave you and then let's call this function switch LED for example and for that one we're going to receive parameters so the parameters is the ones that we Define here so it's going to be called LED number and we know it's going to be an integer and then we have state okay that's the one here okay and then you can write of course the content of the function so what you will do we have three LEDs so what you can do is as we did previously in this course you can create a list so you create a list of LDS here you can initialize the first state and then in this function you will need to well First validate that those two are correct numbers okay because I could give whatever number I could give 5,000 here I could give minus 10 here so you want to First verify that the numbers are correct okay so that this corresponds to an index in the list and that this is either zero or one okay so after you validate that then you can turn on or turn on the uh chosen LED all right and now you can press pause on the video try to do the challenge by yourself and then watch the solution so I give you the beginning of the code so we're going to start from what we had before I give you the beginning of the code for the root but first things first let's initialize our LED so we're going to import LED also from the gpio zero and then we can initialize our LEDs we're going to do that for example just after the push button so we could do LED one two and three and then add them into a list but let's just create a list directly so LED list is equal to and then let's put brackets and let's initialize the LEDs directly inside the list so LED like that with so the first led was on pin so on gpio 17 then led the Sig was on the GP number 27 and then we have led with 22 for the third GP great that's going to initialize the LEDs so all of those pins to output mode and they should be turned off but just to be sure let's do a follow Loop for Led in LED list we do LED dot off okay so

Segment 2 (05:00 - 10:00)

it's quite quick to do and this we've done it already previously great now all the LEDs are initialized so you see we initialize all the hardware here and the app then we create the roots and we can use the hardware in the different functions here and then we run the application so let's now write the content of this switch LED function here first thing we want to do is to validate that the LED number is correct and that the state is correct let's do one by one and actually instead of checking if it's correct let's check if it's not correct and we're going to return an error message directly and we're not going to go further with the function okay so if what well the LED number here is going to correspond to an LED so to an index in that list so it's not correct first if the index is lower than zero I'm going to write this so if LED number is strictly lower than zero then it's not going to be valid okay we cannot have a negative index and I'm going to put or so if the LED number so if the index is lower than zero or if the index is also greater than well the last index how to check that well we can use LED number greater or equal than the length of the LED list and why that because well you can see the last index actually corresponds to the length minus one okay the length is three here so the index is going to be zero one and two all right so two is the last index if you have three is out of the list so I check that the LED number if it's greater or equal than the length then we are out of the list so if we have one of those two condition we know that the index is not in the list and is not correct so we can return for example wrong LED number and why not put the number that we have provided so I'm going to do St Str because it's an integer LED number great so now I go back here and I know that after I pass this if okay so if the condition here is false it means that the LED number is correct I'm going to do the same thing with the state so if and I'm going to check if the state is not correct so we know the state must be zero or it must be one so there are just two values to check that easily I can do if state is different zero and state different than one okay and I use the end keyword here so we just have two values which if it's different than that one and different than this one then we're going to enter the if and return an error message for this you could have written it differently for example you could have said if the state is strictly lower than zero or state is strictly greater than one then it's going to be basically the same thing okay so for those validations maybe you have come up with something that's different than me if you have written the activity by yourself and if it works then it's completely fine okay there's not all always just one solution to a problem there are as many solutions are there people writing a solution so now with this I can do return and let's say state must be zero or one all right and we are done with data validation so we know that at this point if we didn't return anything it means that the LED number corresponds to an index in the list and the state is either zero or one so what I can do now is I can check for example if state is equal to let's start with zero then we want to turn off an LED and which led the one that corresponds to the index here so I will do LED list and then put the index which is Led number here okay so I get access to the element of the list which is an LED and I do dot off and that's it then I can do so I go back here I can do L if state is equal to one and actually I don't really need to do this because if it's not zero it's going to be one we just validated here that the state is only going to be zero or one okay so I don't even need to write this L if I can just write else it's going to be the same LED list with LED number

Segment 3 (10:00 - 15:00)

dot on so we turn off or we turn on the LED and finally let's not forget and this is quite important that's not the end of the function we need to return something if we don't return anything we might get an error so let's return and well we don't really need to return anything let's just say okay so we return a string here if we didn't validate the LED number we also return a string if we didn't validate the state and after that we'll return a string to say okay so that we have performed the action and just remove the Extra Spaces here and let's have a last check so we initialize stuff here and then we have the root validation the action the return all right looks good so I'm going to save the file with contrl s and now I could run it from here I'm going to choose to run it from the terminal okay so let's go back to the terminal and well let's just make sure that you don't have a flask server running anywhere okay because if you are still running something on the terminal for example and you're run something on thy you might get an error all right so here I'm going to choose to only run this from the terminal so I am in my python programs let's do Python 3 activity 11 let's run that and the server is running and to test that let's open a web browser okay so have the Local Host here is going to work but also with the IP address and the port 5,000 let's just make sure it's working okay at least the homepage is working so the server is working and now I'm going to add a slash LED slash I will need to give a number let's start with the first led zero and then slash state and slash I need to give zero or one let's use one to turn on the first led so let's press enter and let's look at what's going to happen here you see the first led was turned on all right I can replace the number here with one that's going to be the second LED because that's the index one all right we turn on the seg LED let's do the same thing two great and now let's see with three for example we going to get an error so it's working but we get wrong LED number three okay now let's say that I want to turn off the second LED so that's going to be the index number one I do index one here and then state zero I press enter and you see I turn off the second LED great so it's correctly working we could turn on and off any LED that we want on the circuit and let's just finish to verify that everything is working here so you have seen if I put LED that's outside of the range for example 10 we have an error let's put a negative number as well like minus one well actually you can't even put minus one here because the minus is going to be considered as a dash so you're going to get to URL not found anyway so let's go back to zero here okay and then State we can have state zero one it's going to work but now let's do state two and you see state must be zero or one okay so you can see that validating the data that you get in a web server is very important okay so you only execute an action with an input that is valid hey this is Edward I hope you enjoyed this tutorial series a lot and that you could get some real value out of it now this series is actually a free extract from a much larger course named Rasberry Pi for beginners this complete course contains 10 hours of content and will take you from a complete beginner to a Raspberry Pi maker with a strong intermediate level where you become really autonomous and confident to start any project that you want so if you like the way I teach and if you want to go further with Raspberry Pi starting from this last tutorial well I recommend that you check the course out the link is in the description and just in case you're wondering what you're going to learn well here is a sneak peek of what's inside the course so here I'm inside the course and we're going to check on the right here the curriculum we have 15 sections so the first section we're going to learn what is Raspberry Pi we've seen that already in this uh series we are then going to install the Raspberry p OS which is what we've done as well but here you can see we also have some extra steps for example with Raspberry Pi connect which seems very promising okay on section three and four then we have kind of a mini course on Python 3 so if you need a refresher of the basics in Python 3 then I got you covered with those about 2 hours of

Segment 4 (15:00 - 17:00)

Python 3 we are then going to build the circuit like we did in this series okay and practice so build the circuit learn how to use the gpio okay there's additional activities here and practice more with the gpio we've done only one activity so far in this series but I have two more activities for you to practice then we are going to detect in Section 8 detect movement with a p sensor that's going to be a very interesting feature to add to our circuit so here you see we see how to add it to the Circuit how to control with python and an extra activity then we have two sections on how to use the terminal on the Raspberry Pi so if you don't know anything about the terminal or Linux well you're going to also get kind of a mini Crush course on that so how to use the terminal specifically for the Raspberry Pi and Linux and then also how to interact with the terminal from Python 3 and there is more then we're going to see how to send an email directly from the Raspberry Pi that's super useful for example to send allets when something is happening on your P we also then have another section on the Raspberry Pi camera we're going to see how to well plug the camera and then take photos and videos both from the terminal and python plus an extra activity then we come back to the flask server that's what we've done here in this series and finally there is a final project so here's the basically what we're going to do in this final project is we're going to take all the foundation that we have with all the sensors and functionalities and knowledge that you got in this course and build a final project to practice on everything and in this project well you see we're going to start some scripts from the terminal we have the Rasberry Pi with the camera with few things on the breadboard and when we detect a movement we are going to take a photo and send that photo by email so you see we receive a new photo by email and then we also put that photo on a web server so it's going to be available for us to get using a web server so from a web page you can see the photos that were taken from your Raspberry Pi okay so that's a complete alarm system that we're going to build in this final project for the course all right so you can just click in the link in the description to also see this outline few preview that you can check you can watch the reviews so the course is already quite popular here on Udi and I encourage you to check it out all right thank you for watching and I hope to see you in the course

Другие видео автора — Robotics Back-End

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник