Outils pour utilisateurs

Outils du site


issue116:tuto2

In this series of articles, I will be building a text-based application with Free Pascal, using its text-based interface for user interaction. This will be combined with other, more modern, technologies such as database access using SQL and Web access with HTTP. The final aim of the project is to demonstrate how Pascal can be used to build a modern application, while avoiding the overhead associated with a graphical interface that uses a widget set such as GTK or Qt. In the previous part of this series, we put our complete application together using Free Vision for the user interface, Sqlite to create a local database, and CURL and XML to retrieve fresh data from an RSS feed from the Web to update our database. In this part, we will see various ways in which our application can run on a Raspberry Pi.

Dans cette série d'articles, je construirai une application en mode texte avec Free Pascal, en utilisant son interface en mode texte pour l'interaction avec l'utilisateur. Ceci sera combiné avec d'autres technologies, plus modernes, telles que l'accès à une base de données en utilisant SQL et l'accès au Web avec HTTP. Le but final du projet est de démontrer comment Pascal peut être utilisé pour construire une application moderne, tout en évitant le surpoids associé à une interface graphique qui utilise un jeu de gadgets comme GTK ou Qt.

Dans l'article précédent de la série, nous avons assemblé une application complète en utilisant Free Vision pour l'interface utilisateur, SQLite pour créer une base de données locale, et CURL et XML pour rafraîchir les données à partir d'un lien RSS sur le Web et mettre à jour notre base de données. Dans ce numéro, nous verrons plusieurs façons de faire tourner notre application sur un Raspberry Pi.

The Raspberry Pi family and Ubuntu The Raspberry Pi family of single-board computers is gaining popularity as an inexpensive means of learning more about computers, about electronics, and how to interface these two worlds together. However, they are actually complete computers in their own right, and with processors starting at a 32-bit, and running at a clock speed of 600 MHz upwards, they can arguably outperform any type of Personal Computer sold up to and including a 1997 Intel Pentium II. So, while having substantially less computational power than a modern desktop, they are still well capable of running an interface such as Free Vision – that was originally designed for computers equipped with the Intel 8086 and 80386 processors. The Raspberry Pi family is actually based on two different series of processors: the RPi versions A, B, and B+ have a Broadcom System-on-a-Chip (SOC) based on the ARMv6 architecture, while the RPi 2 and RPi 3 models are based on the ARMv7 architecture. This has implications for the operating system, since Ubuntu is available only for the ARMv7 architecture. So, for models RPi 2 and 3, we can download an Ubuntu 16.04 Server image from the official downloads section http://cdimage.ubuntu.com/releases/16.04/release/; at the time of writing the latest version was file ubuntu-16.04.1-preinstalled-server-armhf+raspi2.img.xz. This will give us access to the apt command and repositories, and we can install the latest Free Pascal version 3.0 packages as described for a normal desktop or laptop computer.

La famille Raspberry Pi et Ubuntu

La famille d'ordinateurs mono-carte Raspberry Pi gagne une popularité croissante comme moyen bon marché d'en apprendre plus sur les ordinateurs, sur l'électronique et sur la connexion entre ces deux mondes. Cependant, ce sont vraiment des ordinateurs complets à part entière et, avec des processeurs commençant à 32 bits et tournant avec des vitesses d'horloge au minimum de 600 MHz, ils peuvent sans doute dépasser en performances n'importe quel type de PC vendu avec un Pentium II d'Intel de 1997. Aussi, tout en ayant notablement moins de puissance de calcul qu'un ordinateur de bureau moderne, ils sont encore capables de faire tourner une interface telle que Free Vision, qui a été conçue au départ pour des ordinateurs équipés de processeurs Intel 8086 et 80386.

La famille Raspberry Pi est, en fait, basée sur deux types différents de processeurs : les versions A, B et B+ du RPi ont un SOC (System-on-Chip - système sur une puce) Broadcom sur une architecture ARMv6, alors que les modèles RPi 2 et RPi 3 sont basés sur une architecture ARMv7. Ceci a des conséquences pour le système d'exploitation, car Ubuntu n'est pris en charge que sur l'architecture ARMv7. Ainsi, pour les modèles RPi 2 et 3, nous pouvons télécharger une image d'Ubuntu 16.04 Server depuis la section de téléchargement officiel : http://cdimage.ubuntu.com/releases/16.04/release/ ; au moment où j'écrivais ces lignes, la dernière version du fichier était ubuntu-16.04.1-preinstalled-server-armhf+raspi2.img.xz. Ceci nous donnera accès à la commande apt et aux dépôts, et nous pourrons installer les paquets de la dernière version 3.0 de Free Pascal comme indiqué pour un ordinateur de bureau ou portable ordinaire.

On the other hand, for the earlier ARMv6 versions of the RPi, we will need to get a Debian (Raspbian) image. Raspbian is a very acceptable alternative for Ubuntu Server, with only two major differences. The first one is the default user (login name: pi / password: raspberry) instead of Ubuntu’s (ubuntu/ubuntu). The second is that Debian’s repositories provide a slightly older version 2.6 of Free Pascal, which may present a few changes compared to version 3.0 - though nothing of consequence unless we use the graphical programming environment known as Lazarus. However, for both versions, we find clues to indicate that the main target of the Free Pascal project is, indeed, the Intel 32-bit architecture. A few of the libraries do not seem to have been carried over correctly to the ARM build. For our project, the only one of note is the XMLReader unit, so we will not be able to implement the RSS reader, while the other libraries (Free Vision, Database and CURL) seem to work correctly. This would no doubt be a minor inconvenience, but one to take into account and check – if the RPi is important for the reader’s plans.

D'un autre côté, pour les versions plus anciennes du RPi en ARMv6, nous aurons besoin d'une image Debian (Raspbian). Raspbian est une alternative très acceptable à Ubuntu Server, avec seulement deux différences majeures. La première est l'utilisateur par défaut (identifiant : pi ; mot de passe raspberry) au lieu de ubuntu/ubuntu sur Ubuntu. La seconde est que les dépôts de Debian fournissent une version 2.6 légèrement plus ancienne de Free Pascal, qui peut présenter quelques modifications en comparaison de la version 3.0 - mais rien de conséquent sauf si nous utilisons l'environnement graphique de programmation connu sous le nom de Lazarus.

Cependant, pour les deux versions, vous trouvez des indications pour signaler que la cible principale du projet Free Pascal est, bien sûr, l'architecture Intel 32-bit. Quelques bibliothèques ne semblent pas avoir été portées correctement pour la compilation ARM. Pour notre projet, la seule notable est l'unité XMLreader et nous ne pourrons pas implémenter le lecteur RSS, alors que les autres bibliothèques (Free Vision, Database et CURL) semblent fonctionner correctement. C'est sans aucun doute un inconvénient mineur, mais à prendre en compte et à vérifier - si le RPi est important pour votre projet.

In any case, it may be worth noting that programs compiled for the Intel 32- or 64-bit architectures will not work directly on an RPi, since the processor architecture is different and the machine language instruction sets are not compatible. This means a re-compile will be required. There are two techniques to do so. • The first is to install the Free Pascal compiler on the target RPi, then simply transfer our Pascal source code files over. They are recompiled on the RPi itself. Once done, the binary files (executable programs) may be copied over to and used directly on further RPis, since they are now binary-compatible with the ARM processor. • A second technique may be of interest when compiling applications for a very low-powered platform. Known as cross-compiling, the application is compiled on a more powerful computer such as a desktop, but specifying that output must be in the machine language of the second, low-powered architecture. This is often the case when designing apps destined for mobile phones and tablets. It is also possible to use our desktop to compile for the RPi, however the process is rather involved and documentation is scarce. Since the RPi has sufficient power to compile its own programs on-board without being too slow about it, I would suggest readers go this route and avoid delving into cross-compiling.

De toute façon, il vaut mieux garder en tête que les programmes compilés pour les architectures 32- et 64-bit d'Intel ne fonctionneront pas directement sur le RPi, car l'architecture du processeur est différente et les jeux d'instructions en langage machine ne sont pas compatibles. Ceci signifie qu'une re-compilation sera nécessaire. Il y a deux techniques pour cela. • La première consiste à installer le compilateur Free Pascal sur le RPi cible, puis à simplement transférer dessus nos fichiers source en Pascal. Ils sont recompilés par le RPi lui-même. Une fois terminé, les fichiers binaires (les programmes exécutables) peuvent être copiés et utilisés directement sur d'autres RPi, car ils ont maintenant une compatibilité binaire avec le processeur ARM. • Une seconde technique peut présenter de l'intérêt pour la compilation des applications pour une plateforme de faible puissance. Connue comme la compilation croisée (cross-compiling), l'application est compilée sur un ordinateur plus puissant tel qu'un ordinateur de bureau, mais en spécifiant que la sortie doit être faite dans le langage de la seconde architecture, moins puissante. C'est souvent le cas quand des applis sont conçues pour des téléphones mobiles ou des tablettes. Il est aussi possible d'utiliser notre ordinateur de bureau pour compiler à destination du RPi ; cependant, le procédé est plutôt long et complexe et la documentation est insuffisante.

Comme le RPi est assez puissant pour compiler lui-même ses propres programmes sans être trop lent, je suggère aux lecteurs de suivre cette solution et d'éviter de se perdre dans la compilation croisée.

Using the Raspberry Pi directly All generations of the RPi have an output to connect a screen, analog composite video (yellow RCA jack) for versions A and B, and HDMI for all. There is also at least one USB port to connect an external keyboard, so nothing more is needed to use an RPi as a terminal to run a Free Vision program. The advantage of starting out from Ubuntu Server or an equivalent version of Debian is that the RPi comes up directly in text mode. The user simply logs in at the console, and starts up the application from the command line. Applications are quite responsive, even on an earlier B model. The RPi’s 512 MBytes of RAM should not be overtaxed, though this will depend on the actual data usage of our program - here, I was getting by on less than 90 MBytes for the operating system and the application altogether. On the other hand, the screen image over an analog feed, while working, is not too satisfactory. HDMI should be preferred if at all possible, even if we are working just with a text console. Depending on the foreseen use of the device, a user could even be automatically logged into the console, and the Free Vision application launched immediately upon login.

Utiliser directement le Raspberry Pi

Toutes les générations du Raspberry Pi ont une sortie pour connecter un écran, une sortie vidéo composite (le jack jaune RCA) pour les versions A et B, et une HDMI sur tous. Il y a aussi un port USB pour connecter un clavier externe ; ainsi, rien de plus n'est nécessaire pour utiliser un RPi comme terminal pour faire tourner un programme Free Vision.

L'avantage de démarrer d'un Ubuntu Server ou d'une version équivalente de Debian, c'est que le RPi démarre directement en mode texte. L'utilisateur se connecte simplement à la console et commence l'application en ligne de commande.

Les applications sont assez réactives, même sur un vieux modèle B. Les 512 Mo de RAM du RPi ne sont pas surchargées, bien que cela dépendra de l'usage réel des données par le programme - ici, je prenais moins de 90 Mo pour, à la fois, le système d'exploitation et l'application. En revanche, l'image d'écran par un lien analogique, bien que fonctionnelle, n'est pas très satisfaisante. La connexion HDMI est préférable si possible, même si vous ne travaillez qu'en texte à la console.

Suivant l'usage prévu du dispositif, un utilisateur pourrait même se connecter automatiquement à la console et l'application se lancer immédiatement après.

The Raspberry as an application server One of the nicer characteristics of Free Vision applications is that they work well over an SSH connection. Since our RPi already comes with an SSH server (both in Ubuntu Server and in Raspbian), it can immediately become an application server by simply installing our application’s binary file in the user’s home directory. One can then SSH over from one’s preferred computing device such as a computer or a tablet, and access the application. The low network overhead also helps application responsivity. The Raspberry Pi 3 allows us to go one step further, and avoid the need for a physical network connection through the use of the inbuilt WiFi adaptor. To set this up from the command line, we cannot depend on Network Manager that may be familiar to Ubuntu desktop users. Instead, we will need to install wpa-supplicant by hand - since we are all using WPA encryption by default on our networks, are we not? So: apt install wpasupplicant

Le Raspberry Pi comme serveur d'application

Une des belles caractéristiques des applications Free Vision est qu'elles fonctionnent bien via une connexion SSH. Puisque notre RPi arrive déjà avec un serveur SSH (dans Ubuntu Server comme dans Raspbian), il peut devenir immédiatement un serveur d'application si le fichier binaire de notre application est présent dans le répertoire home de l'utilisateur. On peut alors utiliser SSH depuis notre appareil informatique préféré tel qu'un ordinateur ou une tablette, et accéder à l'application. La faible surcharge du réseau aide aussi au bon temps de réponse de l'application.

Le Raspberry Pi nous permet d'aller un cran plus loin, et d'éviter le besoin d'une connexion physique au réseau, en utilisant l'adaptateur WiFi intégré. Pour le paramétrer en ligne de commande, nous ne pouvons pas dépendre du gestionnaire du réseau, qui est sans doute familier aux utilisateurs d'Ubuntu sur ordinateur de bureau. À la place, nous aurons besoin d'installer wpa-supplicant à la main, car nous utilisons tous le cryptage WPA par défaut sur nos réseaux, n'est-ce pas ? Aussi :

apt install wpasupplicant

Now, we need to create a hash of our WiFi’s WPA key: wpa_passphrase essid key where “essid” should be replaced with our network’s identification, and “key” with the WPA key. This command should reply with the required hash code: # wpa_passphrase myessid 12345678 network={ ssid=“myessid” #psk=“12345678” psk=407c7d4b572bf701afc06ddd6886cf33ce4f4f2e924d7f12cebe54628d74013e } We can now edit file /etc/network/interfaces to make the WiFi connection come up automatically, each time the RPi is started up. Add lines: auto wlan0 iface wlan0 inet dhcp wpa-ssid myessid wpa-psk 407c7d4b572bf701afc06ddd6886cf33ce4f4f2e924d7f12cebe54628d74013e naturally, replacing these values with your own as appropriate.

Maintenant, nous devons créer un hachage de notre clé WPA du WiFi :

wpa_passphrase essid key

où « essid » devra être remplacé par l'identification du réseau et « key » par la clé WPA. Cette commande devrait répondre avec le hachage de code demandé :

# wpa_passphrase myessid 12345678 network={

  ssid="myessid"
  #psk="12345678"
  psk=407c7d4b572bf701afc06ddd6886cf33ce4f4f2e924d7f12cebe54628d74013e

}

Nous pouvons maintenant éditer le fichier /etc/network/interfaces pour que la connexion WiFi se fasse automatiquement, à chaque démarrage du RPi. Ajoutez les lignes :

auto wlan0 iface wlan0 inet dhcp

wpa-ssid myessid
wpa-psk 407c7d4b572bf701afc06ddd6886cf33ce4f4f2e924d7f12cebe54628d74013e

en remplaçant, naturellement, ces valeurs par les vôtres.

The final installation is rather minimal, consisting just of the RPi 3 itself, micro-SD card and a telephone charger for power. It can be placed in a suitable location, but please do make sure it has sufficient ventilation since the more powerful processor on this model can have issues with overheating. Such a setup would be sufficient for light database work, such as entering a sale or a bill of lading in a business, or data entry on the move for a scientific application. The data entered can then be transferred periodically to a larger computer and be integrated into its database. If large data handling capacities are needed, the RPi can still work as a front end, but the data repository will need to be moved to a separate database system and accessed over the network. In this part of our series on using Free Pascal with Ubuntu, we saw various ways in which our application can run on a Raspberry Pi, turning it either into a lightweight terminal or an equally lightweight application server. In the next part, we will focus on what makes the Raspberry Pi unique, and will build a Free Vision interface to make use of the General Purpose I/O ports on this small board computer.

L'installation finale est plutôt minimale, comprenant simplement le RPi 3 lui-même, une carte micro-SD et un chargeur de téléphone pour l'alimentation. Il peut être placé dans un endroit adapté, mais assurez-vous, s'il vous plaît, que la ventilation est suffisante, car le processeur plus puissant de ce modèle peut avoir des dysfonctionnements s'il surchauffe.

Un tel paramétrage peut être suffisant pour le travail avec une base de données légère, telle que la saisie d'une vente ou un connaissement dans une entreprise, ou des saisies de données à la volée pour une application scientifique. Les données entrées peuvent ensuite être transférées périodiquement vers un plus gros ordinateur et intégrées dans sa base. Si un besoin de gestion d'une grande quantité de données est nécessaire, le RPi peut encore travailler comme frontal, mais le dépôt des données devra être déplacé dans un système de base de données séparé, accessible par le réseau.

Dans cette partie de la série sur l'utilisation de Free Pascal avec Ubuntu, nous avons vu plusieurs façons de faire tourner notre application sur un Raspberry Pi, le transformant, soit en terminal léger, soit en serveur d'application, tout aussi léger. Dans la prochaine partie, nous nous concentrerons sur ce qui rend le Raspberry Pi unique, et nous construirons une interface en Free Vision pour utiliser les ports d'entrée/sortie généraux (GPIO) de ce petit ordinateur mono-carte.

issue116/tuto2.txt · Dernière modification : 2017/01/13 14:31 de andre_domenech