Raspberry Pi und Linux - Basiswissen - Teil 1

At AZ-Delivery you will find, in addition to exciting projects with our microcontrollers, also some interesting topics with the Raspberry Pi. If necessary, you already have experience with the Raspberry Pi, below pi Called, collected or are still completely inexperienced with this topic?! Vllt. Does the shy away, because you do not know Linux and be afraid to do something wrong?

This blog series should pull the line and to give you the PI and Linux, in this case Raspbian, and also explain a bit of far. The whole thing should not go too deep, but there is appropriate specialist lectures, but it should be laid the foundation for you to deal with the PI and also implement smaller projects in Python.

May I imagine the Raspberry Pi, short pi

You're probably not drummerum, ever have a Raspberry Pi, short pi, kept in your hands or at least seen, see Figure 1.

Figure 1: Raspberry Pi Model 3B

Figure 1: Raspberry Pi Model 3B

The heart of the Pi is the SOC of Broadcom with arm chip. SOC is the short form for system-on-a-chip and describes the design for a (micro) chip that combines all the necessary functions. The first PI was not the fastest with a clock rate of 700MHz, because with the Raspberry Pi 4 Mod. B already has a clock rate of 1.5GHz available. However, and one could still overclock the Pi, which hits himself on the life of the Pi. With initial 256MB or 512MB RAM, which one must also share with the built-in GPU on the SOC chip, the project was initially set strong limits to the project. In comparison, the Raspberry Pi 4 has now proud 8GB RAM in the greatest version! As a storage space for the operating system, a (micro) SD card is used, where it has also been possible since the PI 3 to start the operating system via USB memory. Depending on the model, 2 or 4 USB ports are available, as well as LAN and, if necessary, WLAN. A chamfering feature of the PI are the now 40 GPIO pins, with which you can connect separate hardware, see Figure 2.

Figure 2: The GPIOS of the Raspberry Pi 3b

Figure 2: The GPIOS of the Raspberry Pi 3B

In order to stay short of the individual components, further hardware should be described in addition to the GPIO strip, see Figure 3 and Table 1.

Figure 3: Summary of other interesting hardware

Figure 3: Summary of other interesting hardware



Hardware information
Soc-chip Depending on the model of the PI with more or less GHz. It is installed in the chip next to the CPU also the GPU
R.A.M. Depending on the model and execution, the RAM looks different and has a different size. The current PI 4 can show up to 8 GB RAM
USB ports At the PI3B already with 2xUSB3.0 and 2xUSB 2.0
For the newer PIS also in this configuration, including only USB2.0
Ethernet connection At the pi 3 b a maximum of 300MB / s led via USB2.0
Jack For the sound
Camera port To connect a compatible Raspberry Pi Camera
HDMI connection Depending on the PI model with one or more HDMI connections, must be checked here, whether it is an HDMI, MIDI HDMI or Mini HMID connection
Power-in Via USB mini or USB-C, varies according to PI model
Display port To connect a compatible display


Table 1: Hardware description

But why is that exactly important? Depending on your task, it needs another PI to implement the project. So is the NAS solution OpenMediaVault Especially with a Raspberry P 3 Model B, better a Raspberry Pi 4 with 4GB, or a simple web server with an old Raspberry Pi 2 would be implemented. My private MagicMirror already needed a Raspberry Pi 3 Model B +, as the scope of the data would overwrite an older Raspberry Pi. So you see, it is important for your project to know what minimum requirements are needed in advance.

The right home for the Raspberry Pi

At AZ-Delivery there is currently 10,2021, the Raspberry Pi 3 model B. a housing, power supply or SD card, is not included. Electronics, including the PI counts, just let lies around, is not a good idea. Therefore, the PI should always be packed in a suitable housing, which of course depends on the type of project. As a real maker, you can also simply print a housing yourself, but there is a large selection of different dealers. Since I work a lot with the PI in the area NAS, web servers, DNS filter or 3D printer server, of course, I want to have a useful and high-quality case for my pis. Without wanting to do advertising here, I use in most cases housing from Geekworm.com and also connect the corresponding additional modules for it. Currently I have 3 cases of Geekworm and one of the company Deskpi. The latter has the advantage that I can access all GPIOS, which partly is not the case with the enclosures of Geekworm. Modifications, in the form of unsightly machining on the housing, the optics would destroy. Therefore, always pay attention to the right housing, for the right project.

Let's get back to the GPIOS

I have already addressed the GPIOS, see Figure 2. Unlike a normal PC, the one-board computer 40 connectors, the so-called GPIOS, has to control sensors, displays or actuators. This will be able to develop programs that interact with the outside world. In earlier versions, the PI 28 GPIOS were available, which also severely restricted some projects. It is important to know that although it can take 5V from the PI, however, GPIOS tolerate a maximum of 3.3V. Sensors or actuators returning 5V at the output, mandatory need an intermediate converter, otherwise destroy the GPIOS or even worse, the PI. From the principle you can imagine the GPIOS as well as the pins of one ESP32 NODEMCU Module WLAN WIFI Development Board with CP2102. The difference is that you do not program a microcontroller, but programs based on an operating system. Depending on the version of the PI, you should check exactly which PIN is responsible for what is responsible, see Figure 4.

Figure 4: GPIO occupancy for Raspberry Pi 3 and 4

Figure 4: GPIO occupancy for Raspberry Pi 3 and 4, https://www.raspberrypi.com/documentation/computers/os.html#gpio-and-the-40-pin-header

May I introduce Raspbian or Linux

At this point, there is a topic, which is probably the most common reason why people avoid the PI. The speech is from the frequently used operating system Raspbian, see Figure 5, which uses as a basis for debain. The name Raspbian starts together RaspBerry Pi and DEbian, Whereby it is now more than just this one operating system.

Figure 5: Official Raspbian logo

Figure 5: Official Raspbian logo

Debian is a so-called Linux distribution and just in the word Linux many people get panic or angsticks on the forehead. Reason is that Linux was hard to install and use in its beginnings. This image becomes and has also been shaped by the fact that hackers are shown in many action films with many monitors and many open terminals on which "normal-led" unreadable commands are entered and incomprehensible issues are returned. Mostly then it was claimed that this would be a complicated operating system, mostly Linux. As extremely, as Hollywood wants us to do the white, it's not, but Linux is not a Windows, which, as of 22.09.2021, accounts for just under 75% market share worldwide. Linux also has a graphical interface next to the pure terminal. The great is, the graphical interface and the terminal can be used in parallel, similar to how to use the command line under Windows in parallel.

But why ask the Raspberry Pi Foundation as please Linux, if it seems so complicated to his and with a market share of just under 2.3% worldwide is quite little used. The keyword is Opensource and the Linux as operating system, with some exceptions, is available for free! For a license for Windows or MacOS, you must pay with your money, which is not the case with Linux. In addition, in this case is based on Raspbian, the hardware requirement minimally and can also be used for the so-called arm chips. As a view over the box is also noted that the supercomputers and many large servers of navigable providers, all running with Linux. Problem with the most common operating systems is the hardware requirement, which is currently demanding a 64bit architecture just at the CPU. Other chips, e.g. The SOC chip of Raspberry Pi, are not or only partially supported.

Interesting just for beginners is a Raspbian in the current version, which already brings along all common programs. Here you must pay attention to the SD card or when downloading the image, if you have selected the Lite or Full version. The former is a Raspbian without desktop and there are many programs missing, the latter is quasi for beginners the round-wire package. How to install Raspbian on your pi, can be found in the right one eBook from AZ-Delivery. Meanwhile, there is a simpler tool from the Raspberry Pi Foundation Raspberry Pi Imager, which exists for all common operating systems, See Figure 6.

Figure 6: The Raspberry Pi Imager

Figure 6: The Raspberry Pi Imager

If you use the Pi mostly via SSH only via SSH, the tool offers a special menu via the key combination CTRL + SHIFT + X, see Figure 7 by clicking on the Hostname or directly enabling the SSH.

Figure 7: OS modification

Figure 7: OS modification

The Raspberry Pi Foundation also has a corresponding video released.

This saves you a lot of rework for the configuration during the installation of the operating system. Other operating systems such as Octoprint or Retropi can be easily selected via the Raspberry Pi Imager. In fact, you can also download an image from the internet, e.g. Kali-Linux for the Raspberry Pi. Just try it yourself, then you will already recognize the advantages.

The right system in Linux

Now it gets something very theoretically and in the beginning probably also a confusing topic. Linux uses, similar to MacOS or Windows, a right system, whereby MacOS's rights system is most similar to the Linux or UNIX systems. As in any common operating system, there are users and groups, wherein Linux distinguishes the user between owners and other users. Each user can be added to one or more groups, or special rights are granted for him.

At this point it will be clearly complex at Linux, since Linux drives the right management to the top. As a rule, the simple user may only do in his working directory, the so-called home directory, what he wants. Data can be created, edited or deleted as it is popular. It looks different with the home directories of other users and even other directories in Linux. To access it, appropriate rights must be assigned. Linux distinguishes, as already written above, between the owner, group and other users.

Basically, each of these legislines mentioned above can read, write and execute the attributes. For folders and files, these "rights" can be granted. If you are not the owner or not in the group and may not access files or folders as any other user, access to folders or files will be denied them. At this point, I do not want to go too deep in detail, as this topic could already fill books. I like to refer Wikipediawho have documented the topic very easily and clearly. The followers of Windows I should probably only have angry, since Windows has an extended right system, but to be honest, more than the UNIX file rights does not need it.

The Raspbian surface and a small project

Now the desktop should be considered in more detail, see Figure 8. Insert the microSD card into the PI, plug power supply, monitor, and mouse and keyboard, and wait until the PI is up. The taskbar is, as with many Linux distributions, at the top of the screen. You have a start menu on the left side and on the right important information, like time, volume, Ethernet and other freely configurable widgets you can choose.

Figure 8: The Raspbian surface

Figure 8: The Raspbian surface

On the screenshot you see that I additionally show myself the processor temperature and processor utilization. This personally gives me an overview of how strong the Raspberry Pi is currently busy.

The start menu will, unlike Windows, will vllt. Also strange occurrence, see Figure 9.

Figure 9: Start menu of Raspbian

Figure 9: Start menu of Raspbian

Here is divided for which purpose the program is intended. Under office you will find e.g. LibreOffice and under development The two coveted developer surfaces Geany and Thonny Python IDE. The latter is a development environment for Python, which is now already clarified that Python 2 and 3 is installed on the Raspbian. This programming surface is also great for programming the Raspberry Pi Pico. How exactly you determine which programs are installed and how programs or libraries are installed or deleted should be part of the next post.

In the last section, a 0.96 "-oled display shows information about the PI. Especially if you e.g. Do not know which IP address of the PI has, although a network scanner could be used, but it would be more interesting, but if exactly this information is displayed on the display. This is a great project to deal with the operating system and the GPIO connector.

First, if not already happened, the I2C interface must be activated, see Figure 10. To do this, navigate to Settings in the Start menu and then Raspberry pi configuration.

Figure 10: Activate I2C interface

Figure 10: Activate I2C interface

In the tab interface Activate I2C and restart the Raspberry Pi. Then the interface is available. Afterwards, install still needed libraries and tools, see Figure 11 and Code 1.

Figure 11: Installing needed programs

Figure 11: Installing needed programs

 sudo Apt-Get Install Python SMBus I2C Tools

Code 1: Installation of required libraries

Theoretically, these packages should already be present, but better check again, depending on the age of Raspbian, the packages could also be missing or updating. So that the 0.96 "-OLED display can also be controlled, it still needs a corresponding programming library and examples. For sample scripts, you clone a corresponding repository of Adafruit via the Terminal of GIT, see code 2.

 gun Clone https://github.com/adafruit/adafruit_circuitpython_ssd1306.git

Code 2: Required Git Repository Clon

Afterwards, run code 3 to the required library Adafruit Circuit Python SSD1306 to download and install with all dependencies.

 sudo pip3 install adafruit-circuitpython-ssd1306

Code 3: Installing the required Python library

Now the OLED display is still connected to the PI, which is still manageable at four cables, see Figure 12. Make sure that the display takes 3.3V and connect the pins for SDA and SCL correctly on the Raspberry Pi. Here too, a look helps in the specificationif you should be unsure.

Figure 12: Connect the display to the PI

Figure 12: Connect the display to the PI

To test whether the display and the library are now also functioning, switch to the "examples" directory from the previously cloned repository adafruit_circuitpython_ssd1306 and execute the example shapes.py. This is easiest when using code 4.

 CD Adafruit_circuitpython_sd1306
CD exam
python3 ssd1306_pillow_demo.py

Code 4: Use the code example SSD1306_PILLOW_DEMO.PY

You should now have one on the display Hello World - Read message, see Figure 13.

Figure 13: Hello World from the demoscript

Figure 13: Hello World from the demoscript

Since the preparations are now completed, I would like to use a modified version of the example "stats.py" for my ad. Modified in the sense, because for my PI I use a 750GB HDD as a boot medium and I sometimes want to have other information, such as CPU temperature and properly displayed units for RAM and memory.

To do this, open the file manager and create a new folder called Raspystat, See Figure 14.

Figure 14: Create new folder RaspyStat

Figure 14: Create new folder RaspyStat

In this new folder, you now create a new file named stats.py, see Figure 15.

Figure 15: Create new file stats.py

Figure 15: Create new file stats.py

Open this file, e.g. Thonny Python IDE and paste the modified source code from code 5:


 # SPDX FileCopyright text: 2017 Tony Dicola for Adafruit Industries
 # SPDX FileCopyright text: 2017 James Devito for Adafruit Industries
 # SPDX-License identifier: with
 
 # This example is for use on (linux) Computers That Are Using Cpython with
 # Adafruit Blinka to Support Circuitpython Libraries. Circuitpython Does
 # Not Support Pil / Pillow (Python Imaging Library)!
 
 # Mod by: Joer's way
 # Mod Date: 20.10.2021
 # Some modifications to show wanted information on display
 
 import time
 import subprocess
 
 from board import SCL, SDA
 import busio
 from Pile import image, Imagedraw, Cofront
 import adafruit_ssd1306
 
 
 # Create the I2C Interface.
 i2c = busio.I2c(SCL, SDA)
 
 # Create the SSD1306 OLED class.
 # The first two parameters are the pixel width and pixel height. Change these
 # to the right size for your display!
 disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
 
 # Clear display.
 disp.fill(0)
 disp.show()
 
 # Create blank image for drawing.
 # Make sure to create image with mode '1' for 1-bit color.
 width = disp.width
 height = disp.height
 image = Image.new("1", (width, height))
 
 # Get drawing object to draw on image.
 draw = ImageDraw.Draw(image)
 
 # Draw a black filled box to clear the image.
 draw.rectangle((0, 0, width, height), outline=0, fill=0)
 
 # Draw some shapes.
 # First define some constants to allow easy resizing of shapes.
 padding = -2
 top = padding
 bottom = height - padding
 # Move left to right keeping track of the current x position for drawing shapes.
 x = 0
 
 
 # Load default font.
 font = ImageFont.load_default()
 
 # Alternatively load a TTF font. Make sure the .ttf font file is in the
 # same directory as the python script!
 # Some other nice fonts to try: http://www.dafont.com/bitmap.php
 # font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 9)
 
 while True:
 
     # Draw a black filled box to clear the image.
     draw.rectangle((0,0,width,height), outline=0, fill=0)
 
     # Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
     cmd = "hostname -I | cut -d\' \' -f1"
  # cmd = "hostname -I |cut -f 2 -d ' '"
     IP = subprocess.check_output(cmd, shell = True )
  # cmd = "top -bn1 | grep load | awk '{printf \"CPU: %.2f\", $(NF-2)}'"
  # CPU = subprocess.check_output(cmd, shell = True )
     cmd = "free -m -h| awk 'NR==2{printf \"Mem: %s / %s\", $3,$2}'"
     MemUsage = subprocess.check_output(cmd, shell = True )
     MemUsage = str(MemUsage,'utf-8')
     MemUsage = MemUsage.replace("Mi","MB")
     MemUsage = MemUsage.replace("Gi","GB")
     cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
     Disk = subprocess.check_output(cmd, shell = True )
     cmd = "vcgencmd measure_temp |cut -f 2 -d '='"
     temp = subprocess.check_output(cmd, shell = True )
 
     # Write two lines of text.
 
     draw.text((x, top + 0),       "IP: " + str(IP,'utf-8'),  font=font, fill=255)
     draw.text((x, top + 8),     "Temp: " + str(temp,'utf-8'), font=font, fill=255)
     draw.text((x, top + 16),    MemUsage,  font=font, fill=255)
     draw.text((x, top + 25),    str(Disk,'utf-8'),  font=font, fill=255)
 
     # Display image.
     disp.image(image)
     disp.show()
     time.sleep(2)
 
Code 5: Modifizierter ssd1306_stats.py-Code


Nach dem Ausführen über den grün hinterlegten Start-Button, sollte das Display wie in Abbildung 16 aussehen.

Figure 16: Status display on OLED display

Abbildung 16: Statusanzeige auf OLED-Display

Da mir die Schrift nicht zugesagt hatte, habe ich mir die PixelOperator.ttf über http://www.dafont.com runtergeladen und im selben Verzeichnis wie meinen Quellcode abgelegt. Im gleichen Zuge habe ich die folgenden Modifikationen im Code vorgenommen, siehe Code 6, da sonst der Text nicht lesbar gewesen wäre.

 # SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
 # SPDX-FileCopyrightText: 2017 James DeVito for Adafruit Industries
 # SPDX-License-Identifier: MIT
 
 # This example is for use on (Linux) computers that are using CPython with
 # Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 # not support PIL/pillow (python imaging library)!
 
 
 # Mod by: Joern Weise
 # Mod date: 20.10.2021
 # Some modifications to show wanted information on display
 # Use font ‚PixelOperator.ttf‘ for better reading
 
 import time
 import subprocess
 
 from board import SCL, SDA
 import busio
 from PIL import Image, ImageDraw, ImageFont
 import adafruit_ssd1306
 
 
 # Create the I2C interface.
 i2c = busio.I2C(SCL, SDA)
 
 # Create the SSD1306 OLED class.
 # The first two parameters are the pixel width and pixel height. Change these
 # to the right size for your display!
 disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
 
 # Clear display.
 disp.fill(0)
 disp.show()
 
 # Create blank image for drawing.
 # Make sure to create image with mode '1' for 1-bit color.
 width = disp.width
 height = disp.height
 image = Image.new("1", (width, height))
 
 # Get drawing object to draw on image.
 draw = ImageDraw.Draw(image)
 
 # Draw a black filled box to clear the image.
 draw.rectangle((0, 0, width, height), outline=0, fill=0)
 
 # Draw some shapes.
 # First define some constants to allow easy resizing of shapes.
 padding = -2
 top = padding
 bottom = height - padding
 # Move left to right keeping track of the current x position for drawing shapes.
 x = 0
 
 
 # Load default font.
 # font = ImageFont.load_default()
 
 # Alternatively load a TTF font. Make sure the .ttf font file is in the
 # same directory as the python script!
 # Some other nice fonts to try: http://www.dafont.com/bitmap.php
 font = ImageFont.truetype('PixelOperator.ttf',16)
 
 while True:
 
     # Draw a black filled box to clear the image.
     draw.rectangle((0,0,width,height), outline=0, fill=0)
 
     # Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
     cmd = "hostname -I | cut -d\' \' -f1"
     # cmd = "hostname -I |cut -f 2 -d ' '"
     IP = subprocess.check_output(cmd, shell = True )
  # cmd = "top -bn1 | grep load | awk '{printf \"CPU: %.2f\", $(NF-2)}'"
  # CPU = subprocess.check_output(cmd, shell = True )
     cmd = "free -m -h| awk 'NR==2{printf \"Mem: %s / %s\", $3,$2}'"
     MemUsage = subprocess.check_output(cmd, shell = True )
     MemUsage = str(MemUsage,'utf-8')
     MemUsage = MemUsage.replace("Mi","MB")
     MemUsage = MemUsage.replace("Gi","GB")
     cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
     Disk = subprocess.check_output(cmd, shell = True )
     cmd = "vcgencmd measure_temp |cut -f 2 -d '='"
     temp = subprocess.check_output(cmd, shell = True )
 
     # Write two lines of text.
 
     draw.text((x, top + 2),       "IP: " + str(IP,'utf-8'),  font=font, fill=255)
     draw.text((x, top + 18),     "Temp: " + str(temp,'utf-8'), font=font, fill=255)
     draw.text((x, top + 34),    MemUsage,  font=font, fill=255)
     draw.text((x, top + 50),    str(Disk,'utf-8'),  font=font, fill=255)
 
     # Display image.
     disp.image(image)
     disp.show()
     time.sleep(2)
 
Code 6: Modifiziertes stat-Skript mit Font

Figure 17: New font in the directory of script

Abbildung 17: Neues Font im Verzeichnis vom Skript

Zuletzt soll dieses Skript noch ausgeführt werden, sobald der Pi eingeschaltet und Raspbian hochgefahren ist. Damit das funktioniert, öffne ich ein Terminal und gebe Code 7 ein.

 sudo nano /etc/rc.local

Code 7: rc.local öffnen

Anschließend füge ich am Ende, jedoch vor exit 0, Code 8 hinzu und schließe den Editor mittels Tastenkombination Strg + X.

 cd /home/pi/RaspyStat
sudo python3 stats.py &

Code 8: stats.py beim Start ausführen

Eine Abfrage ob „Geänderter Puffer speichern“ bestätigen Sie mit J oder Y, je nach Spracheinstellung, siehe Abbildung 18.

Figure 18: Adjusting RC.Local Save

Abbildung 18: Anpassung rc.local speichern

Um nun zu testen, ob das Skript auch funktioniert, starten Sie den Raspberry Pi über das Startmenü einmal neu. Bedenken Sie bitte, dass die Pfade von meinen Angaben abweichen könnten, daher passen Sie die Kommandos gemäß ihrem Pfad und Dateinamen an. Dass alles korrekt funktioniert sehen Sie, wenn ihr Display ähnlich Abbildung 19 aussieht.

Figure 19: System Infos on the display after reboot

Abbildung 19: Systeminfos auf dem Display nach Neustart

Zusammenfassung

So they have learned a lot about the Raspberry Pi together with me. You now know the background of the Raspberry Pi, which operating system is normally used and how it comes to the microSD card. In addition, you know the graphical surface of Raspbian a bit and have implemented a simple project using Python3. To this end, it was a lot of input and the learning curve was probably very steep. It does not remain out of the site, as well as in microcontroller programming, to deal with further reading or internet contributions with the topic. In the next part, it should be explained again how the (file) system is built up, which has the word "sudo" on how to use the command line with important commands and how you can protect your raspberry pi a bit wide . At the same time, I would like to introduce even exciting projects that could be realized with the corresponding instructions and the necessary time, of them.

Source codes and other projects for AZ-Delivery from me, see https://github.com/M3taKn1ght/Blog-Repo.


Projects for beginnersRaspberry pi

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN