WEBVTT

00:00.110 --> 00:05.540
In this new activity, you will practice more with flask and the web server, and also combining this

00:05.540 --> 00:08.150
with Gpio and this time with LEDs.

00:08.150 --> 00:13.490
So your challenge is to choose which led to turn on from a web browser.

00:13.490 --> 00:19.610
So from a URL here, depending on what you write in the URL, you're going to choose to power on or

00:19.610 --> 00:21.620
to power off a certain LED.

00:21.620 --> 00:22.760
So we have three LEDs.

00:22.760 --> 00:23.900
We're going to choose which LED.

00:24.140 --> 00:27.230
And then if we want to turn it on or turn it off.

00:27.260 --> 00:27.800
All right.

00:27.800 --> 00:31.910
So you can start from what we have written previously.

00:31.910 --> 00:33.950
So here's the code that you can write.

00:33.950 --> 00:36.530
I'm going to save it as activity 11 dot pi.

00:36.530 --> 00:40.250
And I'm going to give you a few tips so you can get started.

00:40.250 --> 00:43.160
So we are going to create a new route here okay.

00:43.160 --> 00:46.100
So app dot route.

00:46.100 --> 00:48.020
And then well I'm going to write the route first.

00:48.020 --> 00:49.280
And I'm going to explain it to you.

00:49.310 --> 01:03.830
So slash LED and then slash with angle brackets int LED number and then states slash and then int state.

01:04.820 --> 01:05.180
All right.

01:05.210 --> 01:05.990
That's the new route.

01:05.990 --> 01:08.210
And then we're going to create a function.

01:08.210 --> 01:09.440
So what is this.

01:09.470 --> 01:10.610
Well we're going to start.

01:10.610 --> 01:11.870
So slash LED.

01:12.020 --> 01:16.730
And then you see we have here int LED number with angle brackets.

01:16.730 --> 01:19.580
This means that we're not going to write this in the URL.

01:19.580 --> 01:21.890
This is going to be replaced by an integer.

01:21.920 --> 01:22.310
Okay.

01:22.310 --> 01:28.580
So if you want to have dynamic URLs that you can write numbers or text or whatever inside, then you

01:28.580 --> 01:32.780
can use this angle bracket with the data type.

01:32.780 --> 01:38.450
And then the kind of the variable name that we're going to have inside our function.

01:38.450 --> 01:45.680
So here when you write the URL you will do slash LED slash you will write a number and then slash state

01:45.710 --> 01:47.390
slash another number.

01:47.420 --> 01:51.320
And actually I need to finish this with the angle brackets.

01:51.350 --> 01:51.680
All right.

01:51.710 --> 01:57.620
So for example slash LED slash zero slash state slash one.

01:57.620 --> 02:02.270
So provided that we start to count at zero zero is going to be the first LED.

02:02.420 --> 02:06.020
And then if we get the state one it means we want to turn on the LED.

02:06.020 --> 02:08.630
So we're going to turn on the first LED for the state.

02:08.660 --> 02:14.270
To keep it simple, we can say that zero means turning off, one means turning on.

02:14.300 --> 02:14.930
All right.

02:14.930 --> 02:18.080
So you have this URL that I already gave you.

02:18.080 --> 02:23.000
And then let's call this function switch LED for example.

02:23.000 --> 02:25.160
And for that one we're going to receive parameters.

02:25.160 --> 02:27.770
So the parameters is the ones that we define here.

02:27.770 --> 02:29.510
So it's going to be called LED number.

02:29.510 --> 02:31.430
And we know it's going to be an integer.

02:31.430 --> 02:36.230
And then we have state okay that's the one here okay.

02:36.260 --> 02:39.770
And then you can write of course the content of the function.

02:39.770 --> 02:42.440
So what you will do we have three LEDs.

02:42.440 --> 02:46.670
So what you can do is as we did previously in this course you can create a list.

02:46.670 --> 02:48.650
So you create a list of LEDs.

02:48.650 --> 02:50.990
Here you can initialize the first state.

02:50.990 --> 02:56.900
And then in this function you will need to well you will need to first validate that those two are correct

02:56.930 --> 02:57.800
numbers okay.

02:57.830 --> 03:03.230
Because I could give whatever number I could give 5000 here I could give minus ten here.

03:03.230 --> 03:06.710
So you want to first verify that the numbers are correct okay.

03:06.740 --> 03:14.660
So that this corresponds to an index in the list and that this is either 0 or 1 okay.

03:14.690 --> 03:20.840
So after you validate that then you can turn on or turn on the chosen LED.

03:20.930 --> 03:21.380
All right.

03:21.380 --> 03:24.290
And I will see you in the next video for the solution.
