perl on raspberry pi - svperl 2017-02-02

18
Perl on the Raspberry Pi Presented by Ian Kluft Silicon Valley Perl (SVPerl) Santa Clara, California February 2, 2017

Upload: ian-kluft

Post on 07-Feb-2017

338 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Perl on Raspberry Pi - SVPerl 2017-02-02

Perl on the Raspberry Pi

Presented by Ian KluftSilicon Valley Perl (SVPerl)

Santa Clara, CaliforniaFebruary 2, 2017

Page 2: Perl on Raspberry Pi - SVPerl 2017-02-02

About this Presentation

● A lot of this is introducing the Raspberry Pi (“RasPi”) computer● Once you have your Raspberry Pi running it’s just Linux

– Or whatever you installed on it

● Only a small number of slides are specifically about Perl– Any language on Linux will work on the RasPi

– Since Perl is a tool in your toolbox, this shows how to use it too

Page 3: Perl on Raspberry Pi - SVPerl 2017-02-02

History of the Raspberry Pi

● Started by UK CompSci professor Eben Upton

● College freshman didn’t have experience playing with computers

● Needed an inexpensive computer for kids to learn

● Raspberry Pi released 2012

● Low cost: US$25 without Ethernet, $35 with net

● Original expected demand was 10,000 units total

● 10 millionth shipped in 2016● Developed by non-profit

Raspberry Pi Foundation● Licensed to manufacturers

Page 4: Perl on Raspberry Pi - SVPerl 2017-02-02

Popularity soars with Maker Community

● “Maker” is generally anyone who builds things

● Anyone can be a Maker – you too

● Raspberry Pi instantly became wildly popular as a cheap embedded computer

● Best uses when power of Linux needed

● Hardware video acceleration made it good for displaying video on HDMI

● GPIO (general purpose I/O) pins can be optionally used for electronics interfacing

● Networking makes RasPi devices able to remote sense or control

● For analog/digital electronic projects, Arduino is popular competing project board

Page 5: Perl on Raspberry Pi - SVPerl 2017-02-02

Based on ARM processor

● This is different if you’re used to Intel x86 computers

● ARM is a “microcontroller” aimed at low-power devices

● Most phones & embedded computers use ARM today

● Raspberry Pi originally used 32-bit ARM6– limited OS support mostly Debian

Linux, OpenElec

● RasPi 2 & 3 are 64-bit ARM8– wider OS support: more Linux

distros, FreeBSD, Win10 IoT

● binaries must be compiled for ARM – can’t use x86 binaries– Debian Linux → Raspbian– Ubuntu & Fedora: ARM spins– FreeBSD → RaspBSD– Windows 10 → Win10 IoT

Page 6: Perl on Raspberry Pi - SVPerl 2017-02-02

Raspberry Pi Models

Model: RasPi 1 RasPi 1+ RasPi 2 RasPi Zero RasPi 3

Introduced 2012 2014 2015 2015 2016

Processor ARM6 32bit ARM6 32bit ARM8* 64bit ARM6 32bit ARM8 64bit

CPU speed 700MHz 700MHz 900MHz 700MHz 1.2GHz

memory 256MB 512MB 1GB 512MB 1GB

USB2 ports 1 on A, 2 on B 4 4 1 4

Flash boot SD MicroSDHC

* RasPi 2 before v1.2 was 64bit ARM7 CPU, upgraded to ARM8 same as RasPi 3

Page 7: Perl on Raspberry Pi - SVPerl 2017-02-02

Downloadable System Boot Images

● NOOBS– “New Out of the Box System” by

Raspberry Pi Foundation

– For beginners, contains multiple OS images that it can install for you

– Bigger image because it includes lots of OS’s

● Raspbian– Debian Linux for Raspberry Pi– Official RasPi reference system– Works on all RasPi models

● Ubuntu & Fedora– Both support RasPi 2 & 3 on ARM versions

● RaspBSD– FreeBSD configured for RasPi 2 & 3

● OSMC & LibreElec– Entertainment system distros based on

Linux kernel

– Work on all RasPi models

● Microsoft Windows 10 IoT Core– RasPi 3 only

– Not free - requires software license

Page 8: Perl on Raspberry Pi - SVPerl 2017-02-02

Types of Projects

● Desktop– Conventional computer

● RasPi 3 powerful enough for low-end desktop replacement

– USB keyboard & mouse● Or TV remote for entertainment

– HDMI monitor● Composite video available, not common

– Network – Ethernet or WiFi● WiFi built in on RasPi 3● USB WiFi common on others

● Headless– No keyboard, mouse or monitor

– Software developed on desktop

– Network common but optional

– Applications:● Sensing/measurement/control● Robots● Drones● Etc

– “Internet of Things” (IoT) device

Page 9: Perl on Raspberry Pi - SVPerl 2017-02-02

How to set up a Raspberry Pi

● Flash SD card with boot system image– More on that coming up

● Connect cables, monitor & network● Apply power via microUSB power input

– 1A for RasPi 1, 2A for RasPi 2, 2.4A for RasPi 3

– Maybe more for your specific project needs

– Mysterious malfunctions may result from insufficient power

● Boot and configure whatever system it is

Docs from raspberrypi.org

Page 10: Perl on Raspberry Pi - SVPerl 2017-02-02

Flashing an SD card for Raspberry Pi

● Raspberry Pi needs SD card to boot– RasPi 1 uses bigger SD card

– All others use smaller MicroSD

– RasPi 3 also has a new USB boot procedure● It is not for beginners

● 8GB SD card minimum recommended● Get the right boot image for your system

– If you have a RasPi 1/1+/Zero, there’s usually a separate image for ARM6

● RaspberryPi.org has SD flash tools for Windows & Mac– Instructions for Windows

– Instructions for MacOS

● Linux instructions provide no tools– Biggest point is to identify the correct

device

– You don’t want to accidentally erase another disk on your system

– You can use my “PiFlash” tool for Linux

● In all cases…– Flashing gigabytes of data will require at

least some minutes of wait, maybe many

– Be patient once you start

– Let it finish

Page 11: Perl on Raspberry Pi - SVPerl 2017-02-02

PiFlash script

● I wrote piflash in Dec 2016● Source code and instructions are on

GitHub– github.com/ikluft/ikluft-tools/tree/master/piflash

● Safety for beginners:– Only writes to an SD card

● Automation for experts:– No need to unzip/extract binary image

Docs screendump from piflash

Page 12: Perl on Raspberry Pi - SVPerl 2017-02-02

Internals of the PiFlash script

● You don’t need to know this to use PiFlash– But we at SVPerl want to hear about Perl

● Lowest-level “State” class stores all parameters from– Command line

– Operating system configuration

– Input file

– Output device

– It’s all the info needed for support requests

– --verbose parameter dumps the State

● Contains a fork/exec wrapper to capture status or output of commands it launches– Eliminating qx() made perlcritic happy at the -4 level

● Maintains list of command locations rather than use $PATH – just being careful since it needs root privs

● “lsblk” command inspects output device so we can tell if it’s an SD card– Differences tested on PCI and USB SD readers

– That’s expected to cover most cases

– If a device isn’t detected, I’d like to see the verbose output so I can add it

● Main() is exception-catching wrapper

Demo: flash an SD card with Raspbian and boot it on a RasPi 3

Page 13: Perl on Raspberry Pi - SVPerl 2017-02-02

CPAN modules for Raspberry Pi

● If you’re running Linux or FreeBSD, most CPAN modules should work just like on servers or desktops

● There are some modules specific to Raspberry Pi...

● GPIO interface – RasPi General Purpose I/O– Rpi::*

● Read/write SPI bus● Read/write GPIO pins

– Device::SMBus

– Device::I2C

– Device::PiGlow

● Embedded electronics– More RPi::* modules for...

● Temperature● Ultrasonic motion detection● Digital potentiometer (variable resistor)● Analog/digital conversion

– Device::PaPiRus– UAV::Pilot has RasPi backend– Device::BCM2835::Timer

● With these you can control just about any electronic hardware

Page 14: Perl on Raspberry Pi - SVPerl 2017-02-02

IoT security issues

● IoT has earned a bad reputation for security

● Don’t be part of that problem● So think about security from the start,

especially on networked devices● Example from Sept 2016: botnet took

over thousands of IoT devices to attack Krebs on Security site– DDoS: distributed denial-of-service

– Overwhelmed Akamai servers, who then declined to host Krebs’ non-profit site

● Insecure devices with poor/no password or other vulnerabilities can be harvested to run botnet software

● Coordinated attack from compromised IoT devices around the world

● If you’re not securing your IoT devices, you can easily be part of that problem

● If the device has private/valuable data, that can also be a target

Page 15: Perl on Raspberry Pi - SVPerl 2017-02-02

IoT Security Tips

● Securing your system– Change passwords from default

– Use encryption (SSH, TLS)● Deny SSH root logins

– Use existing software when you can● don’t reinvent the wheel● Install software updates

– Close unused socket ports● “netstat -ap”● Shut down unneeded servers● Use iptables/nftables firewalls● This minimizes opportunities to attack

– Set SELinux to “enforcing”

● Securing your network– Use encryption

● RasPi is powerful enough to use encryption

– Secure the client and server● RasPi can be on either end

– Don’t use protocols that send passwords “in the clear”

– Assume anyone could be packet-sniffing the network

● A compromised RasPi can● And you just installed a RasPi

Page 16: Perl on Raspberry Pi - SVPerl 2017-02-02

Video demo

● For this demo we will...– Switch to Raspberry Pi display to see Raspberry Pi with LibreElec

entertainment system play sample videos downloaded from YouTube● The main point is to show the RasPi is great at playing video

– Those viewing the slides online can find these videos on YouTube● Bloomberg “Hello World” UK episode – 0:00-12:00 timepoints cover RasPi

– https://youtu.be/OHF2xDrq8dY

● Cnet Top 5 Raspberry Pi Projects (a bit dated – when RasPi 2 was new)– https://youtu.be/nAvZMgXbE9c

Page 17: Perl on Raspberry Pi - SVPerl 2017-02-02

Where to get a Raspberry Pi

● Local stores, if you can’t wait...– Central Computers

● Santa Clara● Sunnyvale● Fremont● San Francisco

– Fry’s Electronics● San Jose● Sunnyvale● Campbell● Palo Alto● Everywhere...

● Many “starter kit” packages include everything you need (cables, microSD, etc)

● Online retailers– Lowest prices (i.e. $35 for bare RasPi Model

B board) are only from the manufacturers● Newark Electronics● MCM Electronics

– Also popular● SparkFun Electronics (Boulder)● AdaFruit (NYC) – slow UPS shipping to CA

– Beware of markups by Amazon retailers!

Page 18: Perl on Raspberry Pi - SVPerl 2017-02-02

Questions?