Outils pour utilisateurs

Outils du site


issue103:tutoriel_1

Ceci est une ancienne révision du document !


Welcome back to the new direction of my Python series. In case you missed last month, I am changing the direction of this 5 year series from teaching programming in Python to what is called Physical Computing using Python. When you see the phrase 'Physical Computing', think of buttons, LEDs, motors, sensors (temperature, humidity, motion sensors, barometric sensors, etc.) and more. The reason I decided to do this was that after 5 years, I thought I had shown pretty much everything that you needed for “normal” computing, so let’s focus on what I consider the future of small computer programming and microcontrollers.

This month, I will be going over selecting a Raspberry Pi (yes there are more) that will fit your goals, installing an operating system onto the SD card and starting the RPi for the first time with the new OS.

Next month, we will start learning to respond to switches and control LEDs. In future articles, we will be interfacing with sensors and the Arduino micro-controller.

A brief history of the RPi

Much of this information comes from the official Raspberry Pi website (http://www.raspberypi.org) and my memory of buying my first RPi. When the Raspberry Pi first came out, there were two models – Model A+ and Model B+. The decision tree was fairly easy since the two different versions fit a “simple or full feature” mindset, as you can see in the gross details presented below (They are now called RPi 1 Models)…

In February 2015, both of those models were superseded by the RPi 2 Model B. It shares a good deal with the RPi 1 B+, but has a 900 MHZ Quad-core ARM Cortex-A7 CPI and 1GB Ram.

You can find various models of the RPi at any number of web retailers. My humble suggestion is to get the RPi 2 Model B if you can afford the difference in the price between the P1 Model B (it shouldn’t be that much of a delta). Any of the code that we create in the next few articles should easily work with any version of the RPi.

While you are searching the web for your RPi, you will see various kits and add-on modules like cameras, servo controllers, motor controllers and so on. At this point, the add-ons won’t be needed, but we might use some in the future, so if it is something that you are interested in use your own judgment. As to the kits, here are some things you should consider before you invest in the “ultimate kits”. In the next few articles, we will need :

• A Raspberry Pi computer. • A power supply. For the P1 versions, a 5 VDC 1-1.2 amp cell phone charger with a micro USB connection (normal for many smart phones today) will work well. For the P2 version, I strongly suggest that you get a power supply that has an output of 5 VDC 2.5 amp power supply with a micro USB connector. • A USB Keyboard and Mouse. While many places offer very small keyboard/mouse combos, for programming work and “normal” computer use, you will want a full size version of both. You can move to the small wireless versions later on if you decide to use the RPi for other uses like a multimedia centre or expanded home automation. Normally when I work with the Pi, I use a VNC server on the Pi and a VNC client on my linux machine, so I don't have to have multiple keyboards and mice on the top of my desk. • A 4-8 GB SD Card that is Class 10. Versions P1 A and B used SD cards. P1 Model B+ and above have switched over to a Micro-SD card only support. Keep this in mind when buying a specific version. Of course you can use a bigger card. Officially they say that testing has been done with 32 GB cards and don’t see many issues with most of the larger cards. Please Be careful when buying SD cards, since they are not all created equal. Just because a cheap card is marked “Class 10” doesn’t actually mean that it is going to work like a more expensive card. • Some sort of Internet connection, either USB Dongle or Ethernet cable. • A HDMI monitor/television for output and HDMI cable. If HDMI is not available, the P1 A and B versions provide a RCA Composite Video out and 3.5mm Audio Out connector. The P1 B+ version and later have done away with the RCA Composite Video connector and has replaced it with a 3.5mm jack that combines audio and video in one. You would need a 3.5 mm to 3 RCA connectors to connect to an older TV. • Speakers or headphones (unless the monitor or device you are using supports HDMI audio).

While this is the “minimum” requirement list for this article, for our first project you SHOULD have the following items available…

• Breadboard – The breadboard will be needed to start working with add on discrete components like LEDs, resistors, switches, etc. without having to do any soldering. • GPIO interface board (header) and Ribbon cable. This will connect the GPIO pins from the RPi to the breadboard. Check out http://sparkfun.com or http://www.Adafruit.com for this item. The item you will want to look at from Adafruit is called “Pi T-Cobbler Plus”. Note that this particular item will NOT work with the RPi V1A or B. It will only work with the later versions. It is currently about $8.00 U.S.. If you are using a model A or B, you should get “Pi T-Cobbler” which is about $7.00 U.S. If you are looking at SparkFun, their item is called the “Pi Wedge”. Unless you want to assemble your own (read this as soldering tiny parts), you will want to get the Preassembled version. This one costs about $10.00 U.S.. I believe that they have retired (discontinued) the version for the RPi 1A and 1B. You CAN elect not to get the interface board and ribbon cable and use female (Pi side) to male (breadboard side) jumpers. These will work, however, in some of the things we do later on, if you get the jumper on the wrong pin of the Pi, it could lead to damage to your Pi. • Various Resistors, LEDs and Mini pushbutton switches. I will give you a list before we need them to give you plenty of time to obtain them. You can get these at many places. • One other thing you might consider is a case, but only if you have the breakout boards and ribbon cables. This will protect your Pi from your handling of it.

Setup of your RPi

Now comes what must be for me, the most tedious part of the project… the setup. The steps we will perform are:

• Download the OS image. • Unpack the image file from the archive file. Put it somewhere it's easy to get to. • Installing OS to the SD Card. • Getting the RPi hooked up. • First boot of the RPi with the new OS.

So, let’s get the OS image. Go to the downloads page on the official Raspberry Pi website (https://www.raspberrypi.org/downloads). You will be presented multiple versions of various images that you can download, including 2 versions of Ubuntu (The GUI version is Ubuntu Mate), Windows 10 IOT and more. If you have an older model (original models A or B), neither of the Ubuntu images or the Windows image will run on these models. You need the ARMV7 processor and the extra memory to be able to use these images.

The two we are interested in for this project, are the NOOBS and the RASPBIAN images. I will be using the RASPBIAN Wheezy image dated 05-05-2015 for our first few projects, but if you want to have the option of booting into other OS images on the same card, feel free to download the NOOBS image. Just remember, if you have more than one OS on the card, you have less space available to the RASPBIAN image and you will run into an issue that I always used to, not enough space for all the things you want to try. Assuming that you are doing your work on a Linux machine, you can see the official installation instructions at https://www.raspberrypi.org/documentation/installation/installing-images/linux.md . If you are using a Windows machine or a Mac, follow the links there. I’m going to assume a Linux machine and will give you the instructions here.

Before we get started, you might be asking why, if there is a newer/better version available, am I using the older version. I’ve had some trouble with the ‘Jessie’ release and am more comfortable with the ‘Wheezy’ release at this time. I doubt that this was an issue with the release, probably just a bad download, but I just wanted to let you know. For the purpose of the next few articles, use ‘Wheezy’ and feel free to play with other versions.

Unpack the archive and have it be sent to a folder that will be easy for you to remember.

Installing the OS Image to the SD Card

If you are using an early version of the Pi, you will be using a standard sized SD card. If you are using a later version you will be using a Micro-SD card. To save me having to type the distinction every time, I will use “SD” in the documentation. One more thing before we start. I STRONGLY SUGGEST that you do not use a device connected to an external USB hub for the imaging of the SD card. I know the specs say you can, but I've never had very good luck doing this.

OK, here we go. Before inserting the SD card into your Linux box, open a terminal and do:

sudo -i

Most of the commands don't actually need the sudo level permissions, but it won't hurt and neither you or I have to remember when they do. Now run “df -h” to see what devices are currently mounted in the system. My system responds as shown below. Yes, I've named my machine Slartibartfast.

Thank goodness! However /dev/sde1 has two partitions. This will be important in the next step. If you are me, please write down the drive information so you don't make a mistake. Now you will want to unmount the SD card drive.

Slartibartfast ~ # umount /dev/sde2

Slartibartfast ~ # umount /dev/sde1

Slartibartfast ~ # df -h

Notice that I started yet another “df -h” just to verify that the device is unmounted.

If you have ever used this SD card for anything before, you will want to remove the partitions before proceeding further. Some people might argue that this is not necessary, but why not? It only takes a few seconds and it keeps us from having problems. Use “gparted” to remove all the partitions.

We are about to write the Raspbian image to the SD card. There are two ways to do this. First is to use the “dd” command AS SUDO, which I'm sure will be the first thing that comes to everyone's mind. However, remember when we use “dd”, we don't get any information coming back to tell us what is going on and if it takes 5 minutes or longer to write the image, we won't see anything that entire time in the way of progress. While there are other methods I'm going to suggest that instead, you can use the “dcfldd” command (shown top right). Once it gets started (which could take a minute or so) it will give a progress report about how much has been written. Pick your “weapon” of choice. I'm going to show “dcfldd”. Now, as SUDO, please change to where ever you have unpacked the image you are going to use.

I show (below) an “ls” command here. I do this so I can remember the name of the file that I'm just about to work with, and I have the exact spelling.

On my machine, the process took about 10 minutes total.

This next step (above) is totally optional, but if you are like me, you want to verify the write so that you can be sure that this matches the image. We will make an image of the SD card we just did and write it to a temporary image file back to the hard drive. Since your SD card will likely be bigger than the one they used to create the distribution image, we will need to truncate our copy to match the size of the original and finally use diff to verify that both images are the same. Remember this could take a rather long time if you have a card larger than about 8Gb. I'm using a 32Gb card and it looks like it's going to take probably 30+ minutes to copy the image to the drive.

As you can see, the images are the same, so if there is anything wrong from here until we log in, it's not our fault. This process could be a useful process as you go along and want to make a backup image of your Pi's “drive”, just in case something happens.

Finally, we want to run the sync command which will make sure that anything remains uncommitted in the write cache is flushed and that is ok to unmount the SD card.

Now we can move on to some more “exciting” things. Powering on the Pi.

Getting ready to power up your RPi

Notice how I worded the heading for this portion of the instructions. There are certain things you should do before you apply power to your RPi. There are possibilities you can damage your RPi if you don't do the steps in order.

Plug in the Keyboard and Mouse into the USB port/ports.

Plug in the Ethernet cable into the Ethernet port or Wireless dongle into the USB Port.

Switch on your monitor or TV and get it set to the proper mode (HDMI or Composite).

Plug in the video cable (HDMI or Composite).

Put the SD card (or Micro-SD card) into place. It doesn't matter if you are using a full size SD card or a Micro-SD, you will insert it with the label facing down, not up towards the bottom of the Pi. And whatever you do, DO NOT remove the SD card while the RPi is powered on.

At this point, we are ready to plug in the power, so take a deep breath and cross your body parts. Plug it in.

If it worked, then we'll move on. If not, please retry the instructions above.

Once you get Pi booted into a distribution for the first time, you will presented with the raspi-config application. We are going to want to tweak some of the settings. We only really need to do this once.

You will see a screen with 9 options on it. We will work with numbers 1,3 and 4. • Option #1 - Asks about expanding the file-system. You really want to do this so you can get the most space you can. It will take effect at the next reboot. • Option #3 - Enable boot to Desktop/Scratch. You should go ahead and set this to Desktop Login as User 'Pi' at the Graphical Desktop. • Option #4 – This sets various things that we take for granted by our automated setup systems. They include Locale, Timezone and Keyboard Layouts. • First select Locale. Since this computer comes from the UK, its default is to select things that someone living there would need. I, on the other hand, need to change some settings. I have to let the window scroll down to EN_US.UTF-8 UTF-8 and select it. Follow the prompts and you'll be fine. • Next I need to set my time-zone. Since I live in Colorado, USA, I would select America under the Geographic area, and Denver for the Time Zone. • Finally I have to select the keyboard layout I wish to use. It asks a lot of questions, so I would select “Generic”, “US”, “US”, “Default”, ”No Compose Key” and “No” to Xserver Termination key.

Finally I'm ready to set it up, so I select “Finish” and “yes”. Your Pi should reboot and you should see the normal desktop. Now we want to update the system to the latest, add a couple of applications that we'll need right away and then let it reboot once again.

Open a terminal off the top menu bar and do:

sudo apt-get update

sudo apt-get dist-upgrade

Now we want to install TightVNCServer. While this is an optional step, I find it much more constructive to use the Remote window on my Linux desktop than be forced to have 2 monitors, keyboards and mice. It always gets me confused about what/where I am.

sudo apt-get install tightvncserver

Once that's set up, it will ask you to create a password, so no one can just jump into your screen. Make it easy for you to remember.

The very next thing we want to do is set the tightvncserver to automatically startup on boot. That way we don't have to have a mouse or a keyboard.

• Change to the home directory if you aren't already there. $ cd /home/pi • Next, change to the .config directory. $ cd .config • Now we will make a new directory here called 'autostart'. $ mkdir autostart • Change to the autostart directory we just created. $ cd autostart • Now create a new configuration file. $ nano tightvnc.desktop And enter the following lines: [Desktop Entry] Type=Application Name=TightVNC Exec=vncserver :1 StartupNotify=false • Save the file (^O) and exit (^X).

Almost done now. The last thing we will need to do is install the IDE we will be using for our code development, which is Geany.

sudo apt-get install geany

Move over to your normal computer and load VNCViewer software on it. Once that's all done, you will probably want to spend a moment or two by rebooting the computer and making sure that the VNC really did start up and connect. If everything works, you are done.

You will need (as I said earlier) a few things for next month. Some male to male jumpers, female to female jumpers, the breadboard, interface and cable and a handful of things from the electronics store…

• Some small LEDs. Try to get around 10 of each Red, Green, Yellow and Clear. • Some small ¼ watt resistors. 220 ohm, 4.7K ohm, 10K ohm, and some other “normal” hobbyist resistors. About 10 each will do you and the salesperson at the local shop should be able to get you what you need. • A couple of small switches (spst) that will fit on the breadboard. (usually comes with 4 pins).

Really that's about all you will need for the next article. In the meantime, enjoy playing with Linux on the Pi. I think you will be surprised by the power of this tiny device.

So until next month, the last thought I will leave you with is something we hear here in the U.S. all the time…

“But wait … there's more!!!!!!!”

issue103/tutoriel_1.1448967895.txt.gz · Dernière modification : 2015/12/01 12:04 de auntiee