WEBVTT

00:00.080 --> 00:07.100
This is the solution for the activity 11 where you have to choose which led to turn on from a web browser.

00:07.100 --> 00:09.170
So I give you the beginning of the code.

00:09.170 --> 00:11.450
So we're going to start from what we had before.

00:11.480 --> 00:14.030
I give you the beginning of the code for the route.

00:14.120 --> 00:18.890
But first things first let's initialize our LEDs.

00:18.890 --> 00:24.110
So we're going to import LED also from the Gpio zero.

00:24.950 --> 00:27.320
And then we can initialize our LEDs.

00:27.320 --> 00:30.680
We're going to do that for example just after the pushbutton.

00:30.680 --> 00:34.790
So we could do LED one two and three and then add them into a list.

00:34.790 --> 00:36.890
But let's just create a list directly.

00:36.890 --> 00:39.410
So led list is equal to.

00:39.500 --> 00:45.740
And then let's put brackets and let's initialize the LEDs directly inside the list.

00:45.740 --> 00:48.530
So LED like that with.

00:49.220 --> 00:55.730
So the first LED was on pin so on Gpio number 17 then LED.

00:55.940 --> 00:59.510
The second was on the Gpio number 27.

00:59.570 --> 01:04.380
And then we have led with 22 for the third Gpio.

01:04.410 --> 01:05.040
Great.

01:05.040 --> 01:07.890
That's going to initialize the LEDs.

01:07.890 --> 01:12.000
So that's going to initialize all of those pins to output mode.

01:12.000 --> 01:14.100
And it should be turned off.

01:14.100 --> 01:20.670
But just to be sure let's do a for loop for LED in LED list.

01:20.700 --> 01:23.580
We do LED dot off okay.

01:23.580 --> 01:24.750
So it's quite quick to do.

01:24.750 --> 01:26.760
And this we've done it already previously.

01:26.760 --> 01:27.150
Great.

01:27.180 --> 01:29.490
Now all the LEDs are initialized.

01:29.490 --> 01:32.160
So you see we initialize all the hardware here and the app.

01:32.190 --> 01:33.450
Then we create the routes.

01:33.450 --> 01:37.350
And we can use the hardware in the different functions here.

01:37.350 --> 01:39.270
And then we run the application.

01:39.270 --> 01:44.430
So let's now write the content of this switch LED function here.

01:44.460 --> 01:50.760
First thing we want to do is to validate that the LED number is correct and that the state is correct.

01:50.760 --> 01:52.620
Let's do one by one.

01:52.620 --> 01:58.050
And actually, instead of checking if it's correct, let's check if it's not correct.

01:58.050 --> 02:01.890
And if it's not correct, we're going to return an error message directly.

02:01.890 --> 02:04.950
And we're not going to go further with the function okay.

02:04.950 --> 02:06.330
So if.

02:06.330 --> 02:07.140
If what?

02:07.170 --> 02:10.560
Well, the LED number here is going to correspond to an LED.

02:10.980 --> 02:13.140
So to an index in that list.

02:13.140 --> 02:14.970
So it's not correct.

02:15.000 --> 02:19.320
First if the index is lower than zero I'm going to write this.

02:19.320 --> 02:26.100
If LED number is strictly lower than zero then it's not going to be valid.

02:26.130 --> 02:26.310
Okay.

02:26.340 --> 02:28.590
We cannot have a negative index.

02:28.590 --> 02:32.280
And I'm going to put a or so if the LED number.

02:32.280 --> 02:38.640
So if the index is lower than zero or if the index is also greater than well the last index.

02:38.640 --> 02:39.510
How to check that.

02:39.510 --> 02:49.200
Well we can use LED number greater or equal than the length of the LED list.

02:49.200 --> 02:50.250
And why is that?

02:50.250 --> 02:56.820
Because well you can see the last index actually corresponds to the length minus one okay.

02:56.850 --> 02:58.830
The length is three here.

02:58.830 --> 03:01.350
So the index is going to be zero one and two.

03:01.380 --> 03:01.620
All right.

03:01.650 --> 03:03.120
So two is the last index.

03:03.120 --> 03:04.440
If you have three.

03:04.470 --> 03:06.600
Three is out of the list.

03:07.260 --> 03:13.720
So I check checked that the LED number, if it's greater or equal than the length, then we are out

03:13.720 --> 03:15.160
of the list.

03:15.160 --> 03:22.690
So if we have one of those two conditions, we know that the index is not in the list and is not correct.

03:22.690 --> 03:30.610
So we can return, for example, wrong LED number.

03:30.610 --> 03:35.560
And why not but the number that we have provided.

03:35.560 --> 03:40.390
So I'm going to do str because it's an integer LED number.

03:40.630 --> 03:41.140
Great.

03:41.140 --> 03:46.780
So now I go back here and I know that after I pass this if okay.

03:46.810 --> 03:51.700
So if the the condition here is false, it means that the LED number is correct.

03:51.700 --> 03:54.490
I'm going to do the same thing with the state.

03:54.490 --> 03:57.580
So if and I'm going to check if the state is not correct.

03:57.580 --> 04:01.480
So we know the state must be zero or it must be one.

04:01.480 --> 04:04.030
So there are just two values to check that easily.

04:04.030 --> 04:13.840
I can do if state is different than zero and state different than one okay.

04:13.870 --> 04:16.240
And I use the end keyword here.

04:16.240 --> 04:17.770
So we just have two values.

04:17.770 --> 04:21.850
We check if it's different than that one and different than this one.

04:21.850 --> 04:25.450
Then we're going to enter the if and return an error message.

04:25.450 --> 04:27.910
For this you could have written it differently.

04:27.910 --> 04:33.970
For example, you could have said if the state is strictly lower than zero or state is strictly greater

04:33.970 --> 04:37.030
than one, then it's going to be basically the same thing.

04:37.060 --> 04:42.820
Okay, so for those validations, maybe you have come up with something that's different than me.

04:42.820 --> 04:47.290
If you have written the activity by yourself and if it works, then it's completely fine.

04:47.320 --> 04:47.530
Okay.

04:47.560 --> 04:50.350
There is not always just one solution to a problem.

04:50.350 --> 04:53.950
There are as many solutions as there are people writing a solution.

04:53.950 --> 05:02.680
So now with this I can do return and let's say state must be 0 or 1.

05:02.890 --> 05:03.550
All right.

05:03.550 --> 05:06.430
And we are done with data validation.

05:06.430 --> 05:13.150
So we know that at this point if we didn't return anything, it means that the LED number corresponds

05:13.150 --> 05:17.710
to an index in the list, and the state is either 0 or 1.

05:17.710 --> 05:22.600
So what I can do now is I can check, for example, if state is equal to.

05:23.020 --> 05:24.430
Let's start with zero.

05:24.730 --> 05:31.150
Then we want to turn off an LED and which led the one that corresponds to the index here.

05:31.150 --> 05:37.480
So I will do LED list and then put the index which is LED number here.

05:39.490 --> 05:39.730
Okay.

05:39.730 --> 05:43.210
So I get access to the element of the list which is an LED.

05:43.510 --> 05:48.700
And I do dot off and that's it.

05:48.730 --> 05:50.020
Then I can do.

05:50.050 --> 05:55.330
So I go back here I can do l if state is equal to one.

05:55.330 --> 06:00.640
And actually I don't really need to do this because if it's not zero it's going to be one.

06:00.640 --> 06:05.080
We just validated here that the state is only going to be 0 or 1.

06:05.110 --> 06:05.650
Okay.

06:05.650 --> 06:15.040
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

06:15.040 --> 06:18.170
We've led member.

06:20.540 --> 06:22.310
Dots on.

06:22.310 --> 06:25.640
So we turn off or we turn on the LED.

06:25.640 --> 06:28.880
And finally, let's not forget and this is quite important.

06:28.880 --> 06:30.440
That's not the end of the function.

06:30.440 --> 06:31.940
We need to return something.

06:31.940 --> 06:34.730
If we don't return anything we might get an error.

06:34.730 --> 06:36.350
So let's return.

06:36.440 --> 06:39.200
And well, we don't really need to return anything.

06:39.230 --> 06:40.580
Let's just say okay.

06:40.580 --> 06:42.980
So we return a string here.

06:42.980 --> 06:48.470
If we didn't validate the LED number, we also return a string if we didn't validate the state.

06:48.470 --> 06:54.110
And after that we return a string to say okay, so that we have performed the action and just remove

06:54.110 --> 06:57.770
the extra spaces here and let's have a last check.

06:57.770 --> 07:00.200
So we initialize stuff here.

07:00.830 --> 07:06.440
And then we have the root validation the action the return.

07:06.470 --> 07:06.890
All right.

07:06.920 --> 07:08.000
Looks good.

07:08.000 --> 07:11.300
So I'm going to save the file with Ctrl s.

07:11.300 --> 07:15.350
And now I could run it from here I'm going to choose to run it from the terminal okay.

07:15.380 --> 07:16.880
So let's go back to the terminal.

07:16.880 --> 07:22.860
And well, let's just make sure that you don't have a flask server running anywhere, okay?

07:22.890 --> 07:27.000
Because if you were still running something on the terminal, for example, and you run something on

07:27.030 --> 07:29.250
Tony, you might get an error.

07:29.280 --> 07:29.550
All right.

07:29.580 --> 07:33.630
So here I'm going to choose to only run this from the terminal.

07:33.630 --> 07:35.970
So I am in my Python programs.

07:35.970 --> 07:41.070
Let's do Python three activity 11.

07:41.190 --> 07:42.660
Let's run that.

07:42.660 --> 07:44.790
And the server is running.

07:44.790 --> 07:48.030
And to test that let's open a web browser.

07:48.060 --> 07:48.360
Okay.

07:48.390 --> 07:54.150
So I have the localhost here is going to work but also with the IP address and the port 5000.

07:54.180 --> 07:56.610
Let's just make sure it's working okay.

07:56.640 --> 07:58.410
At least the homepage is working.

07:58.410 --> 07:59.940
So the server is working.

07:59.970 --> 08:07.110
And now I'm going to add a slash LED slash I will need to give a number.

08:07.140 --> 08:15.240
Let's start with the first LED zero and then slash state and slash I need to give 0 or 1.

08:15.240 --> 08:18.000
Let's use one to turn on the first LED.

08:18.810 --> 08:20.010
So let's press enter.

08:20.010 --> 08:21.780
And let's look at what's going to happen here.

08:21.780 --> 08:25.080
You see the first LED was turned on.

08:25.380 --> 08:29.400
All right I can replace the number here with one.

08:29.400 --> 08:32.550
That's going to be the second LED because that's the index one.

08:32.970 --> 08:33.450
All right.

08:33.450 --> 08:35.400
We turn on the second LED.

08:35.640 --> 08:38.760
Let's do the same thing with two.

08:39.480 --> 08:40.410
Great.

08:40.470 --> 08:44.250
And now let's see with three for example we're going to get an error.

08:44.250 --> 08:49.110
So it's working but we get the wrong LED number three okay.

08:49.500 --> 08:54.180
Now let's say that I want to turn off the second LED.

08:54.180 --> 08:58.500
So that's going to be the index number one I do index one here.

08:58.500 --> 09:01.170
And then state zero I press enter.

09:01.170 --> 09:03.720
And you see I turn off the second LED.

09:03.990 --> 09:04.620
Great.

09:04.620 --> 09:05.970
So it's correctly working.

09:05.970 --> 09:10.680
We could turn on and off any LED that we want on the circuit.

09:10.680 --> 09:14.400
And let's just finish to verify that everything is working here.

09:14.400 --> 09:20.940
So you have seen if I put LED that's outside of the range for example ten we have an error.

09:21.060 --> 09:26.160
Let's put a negative number as well, like minus one.

09:26.880 --> 09:32.910
Well, actually you can't even put minus one here because the minus is going to be considered as a dash.

09:32.910 --> 09:35.910
So you're going to get a URL not found anyway.

09:35.910 --> 09:38.880
So let's go back to zero here okay.

09:38.910 --> 09:40.800
And then state we can have state zero.

09:40.830 --> 09:41.850
We can have state one.

09:41.880 --> 09:42.810
That's going to work.

09:42.810 --> 09:45.120
But now let's do state two.

09:45.150 --> 09:48.450
And you see state must be 0 or 1 okay.

09:48.450 --> 09:54.300
So you can see that validating the data that you get in a web server is very important okay.

09:54.330 --> 10:00.150
So you only execute an action with an input that is valid.

10:00.150 --> 10:00.570
Great.

10:00.570 --> 10:03.870
And that's the end of this activity and also the end of this section.

10:03.870 --> 10:09.150
So with what you've done here in this section with Flask and Python, you see that you can remotely

10:09.150 --> 10:12.480
control the hardware components of your Raspberry Pi.

10:12.510 --> 10:16.680
Because this while we are still running the web browser from the Raspberry Pi.

10:16.680 --> 10:19.680
But you can try the same from any other device.

10:19.680 --> 10:25.680
So computer or phone or whatever that is connected to the same network as the Raspberry Pi.
