Be ready with Python and C
We'll use Python because it is a very simple, yet powerful, language and is easy to write and read because of its indentation and standard English keywords.
Note
There are two major versions available and there is a current debate on Python 2 versus Python 3. You can read it at https://wiki.python.org/moin/Python2orPython3.
This book will mostly use Python 2.7.x. If you are beginner and want to learn Python, I advise you to go with Python 3; there is not much difference between the two, but there are noticeable differences and you will observe them.
The C programming language offers ample benefits when developing the projects using already available libraries, such as wiringPi
, which can give you full control of GPIO pins. If you have previously developed a project on C, you can integrate the wiringPi
functions and get the same functionalities as your previous project. Also, you can simultaneously use GPIO.
Let's play around with both the languages; this will not give you the whole idea of the programming, but it will give you a good start and will create interest. We will see both the languages one by one.
Writing and executing the Python program
When you use the RasPi, the Python is already an installed component. In the Linux CLI, you can just type python
and the Python CLI will wait for you to enter the commands. Just type print "this is my first program in Python"
and press Enter. Voilà! You have executed a command of Python. This will not allow you to write full-length code directly now, so what to do if you want to write a long code? There is a better way than this, and we will use that throughout the book.
Type sudo nano example1.py
and you will observe the nano text editor on the screen. Then type the following code:
name = "World" name = "Hello " + name print name for i in range(3): print "Whoa" import this
Now press Ctrl + X and then press Y to save the changes. You will be back to the Linux CLI. Now type python example1.py
. The Python program will be compiled and the output will be displayed in the same window. One thing you should notice is that indentation is very necessary in Python. Remove the indentation before the print "whoa"
script and then execute the program; you will find an error of indentation. In the loops, special care for inserting indentation should be taken while writing the code. This makes the programs easy to read for people other than programmers.
Writing and executing the C program
You should know that the most powerful language existing today is C, and it allows us to fulfil all our coding needs. The C language is very common and is an essential language. Let's go through the procedure of executing a C program, which is almost similar to executing Python programs.
Type sudo nano example2.c
in the LXTerminal or PuTTY. Then you can type any C code you know, or on a beginner basis, you can try this code:
#include<stdio.h> int i; int main() { for(i=0;i<3;i++) printf("Harder you work, Luckier you get"); return 0; }
Press Ctrl + X and press Y to save the changes. Now it's our turn to compile and execute the program. The compiler for C programs is always included in the Linux distribution, which can be cc
or gcc
. Type this command to compile the C program:
gcc -o example2 example2.c
In -o example2
, the example2
part will be the name of the output file and the example2.c
part is the file we saved after writing the C program. Press Enter and check the errors. Correct it by typing sudo nano example2.c
and solve it (if any error occurs). Once it is successfully compiled, type the ls
command to check whether the output file has been created. The output filename will be example2
. You can now type ./example2
to execute the compiled code.
These processes are really helpful in creating sensor projects, and once you practice more codes, it will be easy for you to understand the process.