This is the solution for the activity 11 where you have to choose which led to turn on from a web browser.
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 route.
But first things first let's initialize our LEDs.
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 pushbutton.
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 number 17 then LED.
The second was on the Gpio number 27.
And then we have led with 22 for the third Gpio.
Great.
That's going to initialize the LEDs.
So that's going to initialize all of those pins to output mode.
And it should be turned off.
But just to be sure let's do a for loop for LED in LED list.
We do LED dot off okay.
So 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 routes.
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 if it's not correct, we're going to return an error message directly.
And we're not going to go further with the function okay.
So if.
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.
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 a 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 is 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.
Three is out of the list.
So I check checked 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 conditions, 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 but the number that we have provided.
So I'm going to do 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 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 than zero and state different than one okay.
And I use the end keyword here.
So we just have two values.
We check 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 is not always just one solution to a problem.
There are as many solutions as there are people writing a solution.
So now with this I can do return and let's say state must be 0 or 1.
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 0 or 1.
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 0 or 1.
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
We've led member.
Dots 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 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 Ctrl 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 were still running something on the terminal, for example, and you run something on
Tony, 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 three activity 11.
Let's run that.
And the server is running.
And to test that let's open a web browser.
Okay.
So I have the localhost here is going to work but also with the IP address and the port 5000.
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 0 or 1.
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 second LED.
Let's do the same thing with two.
Great.
And now let's see with three for example we're going to get an error.
So it's working but we get the 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 ten 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 a URL not found anyway.
So let's go back to zero here okay.
And then state we can have state zero.
We can have state one.
That's going to work.
But now let's do state two.
And you see state must be 0 or 1 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.
Great.
And that's the end of this activity and also the end of this section.
So with what you've done here in this section with Flask and Python, you see that you can remotely
control the hardware components of your Raspberry Pi.
Because this while we are still running the web browser from the Raspberry Pi.
But you can try the same from any other device.
So computer or phone or whatever that is connected to the same network as the Raspberry Pi.