Follow by Email

Blog Archive

Search This Blog

Programming for Raspberry Pi

Share it:
Raspberry Pi Programming
Raspberry Pi Programming

If you want to experiment with Raspberry Pi seriously, you will not be able to avoid editing one or the other program script or even programming something yourself. It's not about developing big programs, just writing a few command lines or commands to a file and running them. Simple to automate certain processes or to process data / input / output.
The following article is intended to provide a first overview of which programming languages can be used on Raspberry Pi. In addition, it should be a first entry to learn programming and desire to do more. The first step to the first executable program should be as frust free as possible.

Programming with the Bash / Shell
The Bash is not a programming language but a user interfaces in the form of a command line for operating a computer system. Like a proper programming language, Bash offers a similar set of commands that automate certain command-line operations.

Bash stands for "Bourne-again shell". It is a traditional user interface where the user can type in commands in an input line, which the shell executes immediately after pressing the Enter or Return key.
The Bash is therefore not a programming language, but a user interfaces in the form of a command line for operating a computer system.
Why program with bash?
When working with the command line, it happens that you have to re-enter several commands in the same order for certain tasks. Alternatively, you can also write the commands line by line in a text file and run if necessary. The bash script is then executed line by line. Just as if you would enter the commands one after the other on the command line.
Like proper programming languages, Bash offers a similar set of commands that can be used to program simple things. These are usually not the right programs. It's all about automating certain processes on the command line.

Source code in Bash: Hello World
First we open an editor and create a file with the following content:
nano helloworld.sh

#! / Bin / bash
echo "Hello World";

Save and close the file with Ctrl + O, Return and Ctrl + X.

Before executing, the file has to be made executable.
chmod + x helloworld.sh
./helloworld.sh

The script outputs the text "Hello World" on the command line.

Step by step: Hello World
#! / Bin / bash

The first line is the so-called Shebang. It contains an indication of which interpreter is responsible for executing the file.
echo "Hello World";

This line outputs the text "Hello World".

Programming with Python
Python is considered an easy-to-learn, universal programming language because it has a clear and concise syntax. Python is widely used in computer science circles, which is why you have to deal with technology-related training and professions again and again.

Python is a scripting language similar to PHP or Javascript. A Python interpreter is responsible for executing the code, which is a plain text file. For newcomers, the language is particularly suitable because it contains only a few keywords and the code can be made relatively clear. At the same time, the required program structure forces us to program cleanly, which is why, as a lateral entrant, we like to stumble over one or the other special feature.
The program structure is formed by indentations with spaces or tab characters. Other languages use parentheses or keywords.
Why program with Python?
Python is a universal programming language. The focus is on the program readability.
Python is an easy-to-learn language because it has a clear and concise syntax. However, one has to object that the Python source code, like other programming languages, can become confusing.
Furthermore, Python is so widespread in computer science circles that one finds it in technology-related training and occupations again.

Should you develop 2 or 3 in Python?
The Linux distribution Raspbian brings along two versions of Python. Generally one differentiates between Python 2.x and 3.x. Unfortunately, Python 3 is not fully backwards compatible and sometimes uses a different syntax than version 2. This raises the question in which version you should develop. The question is quite justified. Python 3 is more modern and some new libraries no longer support Python 2. On the other hand, there are some libraries and programs that do not work with Python 3. Cleverly, both versions can be installed in parallel, which is why the question of the use of a particular version is not so often.
Basically, it is advisable to develop under Python 3 and if necessary through the import of the future module ensure that the programs run with both versions.

Source code in Python 3: Hello World
First we open an editor and create a file with the following content:
nano helloworld.py

#! / Usr / bin / python
print ("Hello World")

Save and close the file with Ctrl + O, Return and Ctrl + X.

Before executing, the file has to be made executable.
chmod + x helloworld.py

./helloworld.py

The script outputs the text "Hello World" on the command line.
Step by step: Hello World
#! / Usr / bin / python

The first line is the so-called Shebang. It contains an indication of which interpreter is responsible for executing the file.
print ("Hello World")

This line outputs the text "Hello World".

Development environment on the Raspbian desktop
If you work with the Raspbian desktop and want to program there with Python, then the Thonny editor is recommended, which is usually preinstalled, but can also be installed later.
sudo apt-get install python3-thonny

Other programming languages
  • C
  • PHP
  • Perl
Share it:

Raspberry Pi

Post A Comment:

0 comments: