0:41
We're going to use it as an IoT device.
What that means is that we're going to use it inside some bigger system and
we will use it to control other devices, observe other devices.
So we'll connect the sensors and actuators and it'll receive data from sensors,
output command, and actuators.
So when you're using it like that, a programming is needed.
Notice that if you use it as a laptop, desktop you don't have to program.
You can sit there and, just like you would with any laptop,
run applications on in it and so forth and you don't have to write code.
But, when you wanna use it as an IoT device you need to write code.
Because the code basically is telling it what to do.
You don't want the user directly interfacing with the device telling it how
to work step-by-step.
You want to have a bunch of code there that just runs and
does what it's supposed to do.
So you need programming, so that's what we're going to do.
So what language can you program in?
Well with the Raspberry Pi, it's a microcontroller, and
you have many choices.
It's a microcontroller with an operating system.
So you have many choices as to what type of programming environment you wanna use.
You can use,
really, any language that you can find the appropriate compiler interpreter for.
So for instance, C, C++, Java, these are all compiled.
Well, Java's in between, you need an interpreter for Java, too, but C, C++,
anything like that, you can compile it,
as long as you have a C compiler, C++ compiler.
And that exists for Raspberry Pi, and you can use that.
Cross compile it, maybe, or you can compile it directly on the machine.
That's fine, too.
So you can do that.
2:08
You can also use an interpreted language or a scripted language like Java which is
interpreted with the Java virtual machine, Python, Perl, any of these type
of languages where there's some type of interpreter there to translate the code.
As long as you have an interpreter for
Raspberry Pi you can use those languages too.
So you have a lot of choice.
We are going to use Python.
Python is the best supported language for Raspberry Pi.
Now, I think that's supported, you can use any language you want, but
Python is the most popular and best supported for Raspberry Pi.
And when I say best supported there is a good programming environment.
There are good programming environments for a lot of languages.
But Python there is a lot of good API's for Python on Raspberry Pi.
API, meaning application programming interface,
basically library functions that are convenient that do things that you want.
So Python is the language that we're going to use on the Raspberry Pi.
3:12
So Python is a high level language.
High level roughly means that it's easy to use.
The details are hidden from the program, some details are hidden from the program.
So there are a lot of details in Python that are hidden from the program, or
a programmer does not have to see.
For instance, in Python you don't have to explicitly declare data types.
You don't have to say this variable.
Say you had a variable x you don't have to say integer, or car, or float, or
whatever it is.
You don't have to say that.
Which by the way in C like with an Arduino we had to say that we had to specify
oh n to x.
With Python you don't have to do that it takes care of that itself.
It basically looks at the interpreter
on the fly can see how the variable;s being used and then cast it accordingly.
So, it says oh this is being used as an integer I'll call this an integer
and so forth.
So, things like that, no pointers.
Now, we haven't talked about pointers, but
pointers are basically pointers to addresses in memory.
C, C++ support these but those are hidden from you in Python.
It's object orientated, Python is object orientated, meaning it has classes.
Now we haven't gone into classes and really what they're about,
we sort of touched on it with Arduino.
There are classes, take my word for it, it's a useful feature.
And you don't get it in, say, C.
In C++ you do get it, but in C you don't get it.
So, Python has a lot of these features that make it really nice to use, and
I use it a lot for various programming assignments.
Now, it has disadvantages.
It is slow, that's the biggest disadvantage.
It is slow compared to C, C++.
It's slow compared to a compiled language in general,
because the interpretation process takes time.
[COUGH] Excuse me.
Takes a certain amount of time.
So compilation is, once you've compiled it, you don't have to convert it
again into machine code, so it's quicker to run C, C++.
So also, one thing that's related to that is that Python,
it's very hard to meet real-time deadlines.
So, this comes up in more sophisticated In IoT devices,
we're not building devices like that right now, but.
Where you have to meet deadlines.
So, if this has to be done, a certain task needs to be completed within this time,
precisely in a certain deadline,
that's much easier to accomplish in C than it is in Python.
The reason why is because the Python interpreter itself which is running and
converting instructions while you're executing the code.
That interpreter takes time and that time is a little hard to predict.
So when you're trying to meet deadlines you also have to consider
the interpreters delay and things like this.
And you don't know exactly how the interpreter is going to convert the code
a priori.
So, you don't know if it'll convert in a fast way or a slow way.
So, it gets difficult to meet real time deadlines with Python.
But that is not our goal here.
We're just trying to use it in a reasonable way.
6:02
So with Python there are roughly two versions.
I say roughly, broadly two versions.
Now, there are refinements on these versions.
There's Python 2 and Python 3.
There's 2.2, 2.3, 2.4 and so on.
They're basically I think on 2.7 something.
Then there's Python 3, 3.43 is the last one I think I remember using,
but they're somewhere up there.
So there's Python 2, Python 3.
Python 2 and 3, they're both supported.
So even though Python 3 exists they haven't gotten rid
of Python 2 support because there's a lot of old code out there, a lot of open
source software that relies, it's written in Python 2 and assumes Python 2.
So they're both supported.
And actually both of them will be present on your Raspberry Pi.
6:45
We're gonna use a Python 3.
There are small differences in syntax, specifically print statements.
Okay, print statements are done differently, or
the syntax is different in Python 2 than in Python 3.
There are a few other little things that'll confuse, so
we're just going to stick with Python 3 throughout this course.
It's probably the best, if you're programming new stuff in Python,
you probably want to go with Python 3,
because one day I suppose, Python 2's gotta get phased out.
Thank you.
[MUSIC]