getting started with raspberry pi 3

Upload: vivek

Post on 05-Jul-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/16/2019 Getting Started with Raspberry Pi 3

    1/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    2/133

    Copyright

    Getting Started with Raspberry Pi 3

    Agus Kurniawan

    1st Edition, 2016

    Copyright © 2016 Agus Kurniawan

    * Raspberry Pi Logo is trademark and copyright from Raspberry PiFoundation, https://www.raspberrypi.org .

  • 8/16/2019 Getting Started with Raspberry Pi 3

    3/133

    Table of ContentsCopyri ght

    Preface

    1. Intro duction to Raspberry Pi 3

    1.1 Raspberry Pi 3

    1.2 Getting Hardware

    1.3 Unboxing

    2. Operating System2.1 Raspberr y Pi 3 Operating System

    2.2 Preparation

    2.2.1 S etup MicroSD Card

    3. Pow ering Up and R unning3.1 Put Them All!

    3.2 Expanding File System

    3.3 C onfigure Timezone

    3.4 Configure Keyboard

    3.5 Rebooting

    3.6 Shutdown

    3.7 Change Password

    3.8 Configure All Settings

    4. Connecting to a Network4.1 Getting Started

  • 8/16/2019 Getting Started with Raspberry Pi 3

    4/133

    4.2 Attaching a Network Module

    4.3 Connecting to a Network

    4.3.1 Connecting a Network via Ethernet

    4.3.2 Connecting a Network via WiFi4.4 Auto Connect to WiFi

    4.5 Configuring Static IP Address

    4.6 Browsing Internet

    4.7 SSH

    4.8 Update Package Repository

    4.9 Upgrading Package and Firmware

    4.10 Remote Desktop

    5. Raspberry Pi Programming

    5.1 Getting Started

    5.2 Python

    5.3 C/C++

    5.4 Node.js

    5.5 Scratch5.6 Wolfram Mathematica

    5.7 Java

    6. Working with Bluetooth and iBeacon

    6.1 Getting Started

    6.2 Setting up Bluetooth

    6.3 Ping Bluetooth

  • 8/16/2019 Getting Started with Raspberry Pi 3

    5/133

    6.4 Working with Blueman Bluetooth Manager

    6.4.1 Setting up

    6.4.2 Pairing

    6.4.3 Transferring File6.5 Building your own iBeacon

    7. Deploying LAMP Stack

    7.1 Getting Started

    7.2 Installing Apache Server

    7.3 Installing MySQL

    7.4 Installing PHP and MySQL Driver for PHP

    7.5 Testing PHP

    7.6 Testing PHP and MySQL

    8. Accessing GPIO

    8.1 Introduction to GPIO

    8.2 Accessing GPIO

    8.3 Demo

    8.4 Further Reading9. Raspberry Pi 3 Serial Debugging

    9.1 Preparation

    9.2 Enabling Serial Debugging

    9.3 Wiring

    9.4 Testing

    Contact

  • 8/16/2019 Getting Started with Raspberry Pi 3

    6/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    7/133

    Preface

    This book was written to help anyone who wants to get started in Raspberry Pi 3. It

    describes all the basic elements of the Raspberry Pi 3 with step-by-step approach.

    Agus Kurniawan

    Berlin, March 2016

  • 8/16/2019 Getting Started with Raspberry Pi 3

    8/133

    1. Introduction to Raspberry Pi 3

  • 8/16/2019 Getting Started with Raspberry Pi 3

    9/133

    1.1 Raspberry Pi 3

    The Raspberry Pi is a low cost, credit-card sized computer that plugs into a computermonitor or TV, and uses a standard keyboard and mouse(source: https://www.raspberrypi.org/help/what-is-a-raspberry-pi/ ).

    Built on the latest Broadcom 2837 ARMv8 64bit processor the new generation RaspberryPi 3 Model B is faster and more powerful than its predecessors. It has improved powermanagement to support more powerful external USB devices and further to customerfeedback based development the new Raspberry Pi 3 now comes with built-in wireless andBluetooth connectivity.

    The following is technical specification of Raspberry Pi 3 device:

    Broadcom BCM2837 64bit ARMv8 Quad Core Processor powered Single BoardComputer running at 1.2GHz1GB RAMBCM43143 WiFi on boardBluetooth Low Energy (BLE) on board40pin extended GPIO4 x USB2 ports4 pole Stereo output and Composite video portFull size HDMICSI camera port for connecting the Raspberry Pi camera

    DSI display port for connecting the Raspberry Pi touch screen displayMicroSD port for loading your operating system and storing dataUpgraded switched Micro USB power source (now supports up to 2.4 Amps)Same form factor as the Raspberry Pi 2 Model B, however the LEDs will changeposition

    You can see Raspberry Pi 3 model B device on the Figure below.

    https://www.raspberrypi.org/help/what-is-a-raspberry-pi/

  • 8/16/2019 Getting Started with Raspberry Pi 3

    10/133

    1.2 Getting Hardware

    How to get Raspberry Pi 3 device?

    Officially you can buy it from the official distributoron https://www.raspberrypi.org/products/raspberry-pi-3-model-b/ .

    You also buy Raspberry Pi peripheral devices for instance, keyboard, mouse, HDMI cable,SD card, USB hub, etc.

    I tried to look for buying Raspberry Pi 3 device and found that there are another options tobuy

    The Pi Hut, http://thepihut.comEXP-Tech, http://www.exp-tech.de/Sparkfun, https://www.sparkfun.com

    adafruit, https://www.adafruit.com Ebay, http://www.ebay.com (personal transaction)

    You also can buy this board at your local electronics stores.

    http://www.ebay.com/https://www.adafruit.com/https://www.sparkfun.com/http://www.exp-tech.de/http://thepihut.com/https://www.raspberrypi.org/products/raspberry-pi-3-model-b/

  • 8/16/2019 Getting Started with Raspberry Pi 3

    11/133

    1.3 Unboxing

    After bought Raspberry Pi 3 from The Pi Hut (http://thepihut.com), I get the board asfollows.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    12/133

    2. Operating System

    This chapter explains how to work with Operating System for Raspberry Pi 3.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    13/133

    2.1 Raspberry Pi 3 Operating System

    Raspberry Pi provides some Operating Systems you can use and run on the top ofRaspberry Pi 3. You can download these OS files onhttp://www.raspberrypi.org/downloads .

    In this book, I use Raspbian “Jessie” OS.

    http://www.raspberrypi.org/downloads

  • 8/16/2019 Getting Started with Raspberry Pi 3

    14/133

    2.2 Preparation

    Raspbian is an Operating system based on Debian Linux for the Raspberry Pi hardware. Irecommend you to download OS image file on http://www.raspberrypi.org/downloads .For illustration, I use Raspbian Jessie OS.

    After extracted this file, you will obtain *.img file, for instance, 2016-02-26-raspbian-essie.img file.

    2.2.1 Setup MicroSD Card

    If we are working with Raspberry Pi 3 board, we need MicroSD card to extract this OSimage file. I use MicroSD Card 16 GB.

    http://www.raspberrypi.org/downloads

  • 8/16/2019 Getting Started with Raspberry Pi 3

    15/133

    Insert this card into your computer.

    For Linux users:

    You can mount it, for instance, /dev/sdd1

    umount /dev/sdd1

    Then, you can copy all img file into MicroSD card.

    dd bs =1M if =~/2015-02-16-raspbian-wheezy.img of =/dev/sdd1

    For Mac user, you can use a script from thistool, https://github.com/RayViljoen/Raspberry-PI-SD-Installer-OS-X .

    Navigate file install with Jessie image file.

    $ ./install 2016-02-26-raspbian-jessie.img

    Wait it until done.

    https://github.com/RayViljoen/Raspberry-PI-SD-Installer-OS-X

  • 8/16/2019 Getting Started with Raspberry Pi 3

    16/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    17/133

    For Windows users:

    Download Win32DiskImager on https://sourceforge.net/projects/win32diskimager/ .

    Run Win32DiskImager and navigate Raspberry Pi image file.

    The following is a sample of Win32 Disk Imager with Raspbian Wheezy image.

    Click Write button to start for copying files.

    Click Yes to confirm overwrite files

    Then, Win32DiskImager app will copy all files into Micro SD card.

    If success, you can see all files in Micro SD card.

    Plug out SD card from computer. Then, plug in it into Raspberry Pi 3.

    https://sourceforge.net/projects/win32diskimager/

  • 8/16/2019 Getting Started with Raspberry Pi 3

    18/133

    Now your Raspberry Pi 3 is ready to be deployed OS.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    19/133

    3. Powering Up and Running

    In this chapter we start to run and configure Raspberry Pi 3.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    20/133

    3.1 Put Them All!

    Now you are ready to boot your Raspberry Pi. Connect Monitor via HDMI, a keyboard viaUSB and power into Raspberry Pi.

    Turn on the power for your Raspberry Pi. Raspbian OS will boot for the first time.

    If success, you will get the first screen of Raspberry Pi Jessie desktop as below

  • 8/16/2019 Getting Started with Raspberry Pi 3

    21/133

    On desktop mode, if you want to work with Terminal, you can click black monitor icon,shown in Figure below.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    22/133

    3.2 Expanding File System

    After running Jessie OS, you can configure it by expanding Filesystem via raspi-config tooptimize your micro SD storage. Type this command on Terminal.

    sudo raspi-config

    Then, you get the following form.

    Select 1 Expand Filesystem. After that, you are be required to restart Raspbian.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    23/133

    3.3 Configure Timezone

    If you want to change timezone in Raspberry Pi 3, you can do it in console with typing

    sudo raspi-config

    Then, you will get a dialog as below

    Select 5 Internationalisation Options.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    24/133

    Then, I2 Change Timezone.

    Select your Geographic area.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    25/133

    Then, select your city or region.

    After that, Raspbian will configure your Timezone.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    26/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    27/133

    3.4 Configure Keyboard

    You may change your keyboard configuration. You can type this command into Terminal.

    sudo raspi-config

    Then you will get a dialog as below

    Select 5 Internationalisation Options.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    28/133

    Select I3 Change Keyboard Layout. Choose your keyboard type and model.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    29/133

    3.5 Rebooting

    If you want to reboot your Raspberry Pi, write this script on Terminal.

    sudo shutdown -r now

    You also can do it with writing this script on Terminal.

    sudo reboot

  • 8/16/2019 Getting Started with Raspberry Pi 3

    30/133

    3.6 Shutdown

    It’s better to shutdown your Raspberry Pi If you don’t use it. Please don’t turn off thepower directly.

    Write this script to shutdown and turn off your Raspberry Pi

    $ sudo shutdown -h -P now

  • 8/16/2019 Getting Started with Raspberry Pi 3

    31/133

    3.7 Change Password

    By default, Raspbian provides username: pi and password: raspberry. If you want tochange password, you can do it by calling passwd

    $ passwd

    Then type the current password and new password

  • 8/16/2019 Getting Started with Raspberry Pi 3

    32/133

    3.8 Configure All Settings

    We can configure all settings on Raspbian by typing the following command.

    $ raspi-config

    Configure what you want to edit.

    Further information about this command, you can read it onhttps://www.raspberrypi.org/documentation/configuration/raspi-config.md .

    https://www.raspberrypi.org/documentation/configuration/raspi-config.md

  • 8/16/2019 Getting Started with Raspberry Pi 3

    33/133

    4. Connecting to a Network

    This chapter explains how to work with networking in Raspberry Pi 3.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    34/133

    4.1 Getting Started

    In this chapter, we learn how to connect Raspberry Pi 3 to a network through built-inEthernet and WiFi.

    Let’s start.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    35/133

    4.2 Attaching a Network Module

    Raspberry Pi 3 has built-in Ethernet and WiFi so we can connect to a network directly. Wedon’t need additional network module.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    36/133

    4.3 Connecting to a Network

    4.3.1 Connecting a Network via Ethernet

    Connect Ethernet USB to Raspberry Pi and a network.

    Turn on Raspberry Pi 3. You can see the monitor and access it via Keyboard. You alsoaccess it via SSH, read section 4.7.

    Now you can check your current IP Address by writing this script

    $ ifconfig -a

    You should see your current IP address.

    4.3.2 Connecting a Network via WiFi

    Raspberry Pi 3 has built-in WiFI so you can connect it to a network. on Raspbian Jessie

    desktop, you can click on the right tool (Network icon) so you can see the list of WiFiHotspot.

    Select your SSID.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    37/133

    Fill SSID key if your WiFi Hotspot needs authentication key.

    If succeed, WiFi icon will show on the right toolbar.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    38/133

    You can configure the WiFi setting by right-click on WiFi icon. Select WiFi Networks(dhcpcdui) Settings.

    Then, you should get Network dialog.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    39/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    40/133

    4.4 Auto Connect to WiFi

    You may want to to connect to WiFi automatically after rebooting. You canopen /etc/network/interfaces file.

    $ sudo nano /etc/network/interfaces

    Then, on wlan0 you change from manual to dhcp.

    The following is the final configuration on /etc/network/interfaces file.

    After that, you can edit /etc/wpa_supplicant/wpa_supplicant.conf file to configure SSIDincluded its key.

    $ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

    Add these settings. Change ssid and psk values.

    network ={ ssid ="SSID_WIFI" psk ="SSID_Key"

    key_mgmt =WPA-PSK}

  • 8/16/2019 Getting Started with Raspberry Pi 3

    41/133

    Save all settings. Now try to reboot Raspbian.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    42/133

    4.5 Configuring Static IP Address

    By default, Raspberry Pi configures IP address in DHCP mode. If you want to change tostatic IP Address, you edit interfaces file.

    Type these commands on Terminal.

    $ cd /etc/network$ sudo nano interfaces

    Then, you will see a content of file interface. Replace iface eth0 inet dhcp with

    iface eth0 inet staticaddress 192.168.1.10netmask 255.255.255.0gateway 192.168.1.1

    After that, you can verify your current IP Address now. You may reboot your RaspberryPi.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    43/133

    4.6 Browsing Internet

    If your Raspberry Pi already connected to Internet, you can browse the Internet. RaspberryPi provides Epiphany as browser. Click its icon, shown in Figure below.

    Furthermore, a browser is opened. Navigate to a specific URL. If success, it show thetarget URL. A sample output of browser can be seen in Figure below.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    44/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    45/133

    4.7 SSH

    By default, Raspbian Jessie has installed and enabled SSH service so we can use itdirectly.

    For testing, I used PuTTY, http://www.putty.org , application in Windows platform to

    remote Raspberry Pi via SSH.Fill IP Address of Raspberry Pi.

    You can get IP address your Raspberry Pi 3 board by checking it on your router. Forinstance, my router detected my board MAC.

    http://www.putty.org/

  • 8/16/2019 Getting Started with Raspberry Pi 3

    46/133

    Raspberry Pi usually has MAC address with prefix B8:27:EB . You also can fill RaspberryPi hostname. By default, the Pi hostname is raspberrypi .

    Then, click Open button. If connected, you will get a security alert.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    47/133

    Click Yes button.

    Entry username and password. If success, you will get Raspberry Pi console.

    For Mac and Linux, you can use SSH on Terminal. For instance, IP address of RaspberryPi is 192.168.1.41, you can type this command.

    $ ssh [email protected]

  • 8/16/2019 Getting Started with Raspberry Pi 3

    48/133

    4.8 Update Package Repository

    If you want to update your package repository, you can execute by writing this script

    $ sudo apt-get update

    Note: It needs the Internet connection.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    49/133

    4.9 Upgrading Package and Firmware

    You can update your current packages/libraries using this command.

    $ sudo apt-get update

    You also can update Raspberry Pi firmware using this command.

    $ sudo raspi-update

  • 8/16/2019 Getting Started with Raspberry Pi 3

    50/133

    4.10 Remote Desktop

    We can remote our Raspbian desktop using remote desktop (RDP). By default, it’s not beinstalled yet so you can install xrdp . Type this command on Terminal.

    $ sudo apt-get install xrdp

    To test, I use Remote Desktop on Windows platform. Fill IP address of Raspberry Pi 3.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    51/133

    Click Connect button. If you will get a warning dialog. Click Yes button.

    If you success, you will get xrdp dialog. Fill Raspberry Pi account.

    If done, you will get Raspbian desktop.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    52/133

    On Mac, I use Microsoft Remote Desktop

    Fill IP address for your Raspberry Pi 3.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    53/133

    Then, connect it.

    If succeed, you should see Jessie desktop.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    54/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    55/133

    5. Raspberry Pi Programming

    This chapter explains how to work with development environment on Raspberry Pi 3.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    56/133

    5.1 Getting Started

    In this section, we learn several programming on Jessie OS on Raspberry Pi 3.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    57/133

    5.2 Python

    Raspberry Pi Jessie provides Python for development by default so you can executePython code inside Raspberry Pi console.

    $ python

    After that, you run Python command.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    58/133

    5.3 C/C++

    Raspberry Pi also provides GCC inside package distribution. You can check your currentGCC version by typing this command.

    $ gcc --version

    For illustration, we create a simple code, hello world. Create a file

    $ nano hello.c

    Write this code.

    #include

    main(){ printf("Hello World");

    }

  • 8/16/2019 Getting Started with Raspberry Pi 3

    59/133

    Save it.

    Now you can compile C code using GCC.$ gcc hello.c -o hello

    To run, you type this script.

    $ ./hello

  • 8/16/2019 Getting Started with Raspberry Pi 3

    60/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    61/133

    5.4 Node.js

    If you are node.js lovers, Raspbian Jessie has installed for you.

    Try to check the node.js version

    $ node -v

    How to get started? Ok, we can create a file, called mynode.js.

    $ nano mynode.js

    and write this code

    console.log('hello node.js');

  • 8/16/2019 Getting Started with Raspberry Pi 3

    62/133

    Save it.

    Now you can execute mynode.js file using node.js$ node mynode.js

  • 8/16/2019 Getting Started with Raspberry Pi 3

    63/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    64/133

    5.5 Scratch

    Scratch is a free desktop and online multimedia authoring tool that can be used bystudents, scholars, teachers, and parents to easily create games and provide a steppingstone to the more advanced world of computer programming or even be used for a rangeof educational and entertainment constructivist purposes from math and science projects,including simulations and visualizations of experiments, recording lectures with animatedpresentations, to social sciences animated stories, and interactive art and music.

    Raspbian already installed it for you. You can run Scratch by clicking scratch logo (see itin Figure below).

    After that, you will get a Scratch application.

    If you don’t see Scratch application, you can type this command on Terminal.

    $ sudo scratch

    A sample output of Scratch application can be seen in Figure below.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    65/133

    Further information about Scratch, you can read and learn it on https://scratch.mit.edu/ .

    https://scratch.mit.edu/

  • 8/16/2019 Getting Started with Raspberry Pi 3

    66/133

    5.6 Wolfram Mathematica

    Now you can get Wolfram Mathematica for Raspberry Pi for free license. You can findWolfram Mathematica on desktop.

    You can read how to use Wolfram Mathematica on thissite, http://www.wolfram.com/raspberry-pi/ .

    I also wrote a book, Getting Started with Wolfram Language and Mathematica for

    http://www.wolfram.com/raspberry-pi/

  • 8/16/2019 Getting Started with Raspberry Pi 3

    67/133

    Raspberry Pi . You can reach it on http://blog.aguskurniawan.net/post/pimath.aspx .

    http://blog.aguskurniawan.net/post/pimath.aspx

  • 8/16/2019 Getting Started with Raspberry Pi 3

    68/133

    5.7 Java

    Java also has installed on Raspbian Jessie. You can verify it typing this command onTerminal.

    $ java -version

    You should see Java version which installed on Raspberry Pi 3.

    Raspbian Jessie has BlueJ for Java IDE too.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    69/133

    For testing, you can write these codes on text editor such as nano or vi.

    public class HelloWorld {public static void main (String[] args) {

    System. out . println ("Hello, World"); }

    }

  • 8/16/2019 Getting Started with Raspberry Pi 3

    70/133

    Save these codes into a file, called HelloWorld.java. Now you can compile and run it on

    Terminal.$ javac HelloWorld.java$ java HelloWorld

    Program output:

  • 8/16/2019 Getting Started with Raspberry Pi 3

    71/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    72/133

    6. Working with Bluetooth and iBeacon

    This chapter explains how to work with built-in Bluetooth on Raspberry Pi 3.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    73/133

    6.1 Getting Started

    In this chapter, we learn how to use built-in Bluetooth on Raspberry Pi 3 board.

    Let’s start.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    74/133

    6.2 Setting up Bluetooth

    Raspbian Jessie already installed Bluetooth library so we can use it.

    Now we check status of Bluetooth device using this command.

    $ /etc/init.d/bluetooth status

    By default, Bluetooth device is enabled and running.

    I use Macbook to verify existence of Raspberry Pi Bluetooth. I don’t see Raspberry Pi

    Bluetooth.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    75/133

    This means Raspberry Pi Bluetooth is not discoverable.

    We can set it using this command.$ sudo hciconfig hci0 piscan

    Then, we can our Raspberry Pi Bluetooth using hciconfig.

    $ sudo hciconfig -a

  • 8/16/2019 Getting Started with Raspberry Pi 3

    76/133

    Now my Macbook detected Raspberry Pi Bluetooth.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    77/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    78/133

    6.3 Ping Bluetooth

    In this section, we want to pair my Macbook Bluetooth to Raspberry Pi Bluetooth.

    Firstly, I scan my Macbook Bluetooth using hcitool to retrieve MAC address of MacbookBluetooth.

    $ sudo hcitool scan

    To ping, you can use l2ping with passing target MAC address.

    $ sudo l2ping -c 1 MAC_ADD_BLUETOOTH

    If succeed, you should get a ping report.

    A sample of pinging on Macbook Bluetooth.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    79/133

    6.4 Working with Blueman Bluetooth Manager

    In this section, we learn to manage Raspberry Pi 3 Bluetooth using GUI, BluetoothManager.

    Let’s start.

    6.4.1 Setting up

    Firstly, you run Raspbian Jessie in desktop mode. You should see Bluetooth Manager onPreferences menu, shown in Figure below.

    If you don’t see it, Raspbian Jessie may not install blueman. You can install it using thiscommand on Raspberry Pi Terminal.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    80/133

    $ sudo apt-get install bluetooth bluez blueman

    Then, add your account into member of bluetooth.

    $ sudo usermod -G bluetooth -a pi

    After installed, please reboot bluetooth service.

    $ sudo service bluetooth restart

    Now you should Bluetooth Manager now. You can open it and the get the followingdialog.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    81/133

    You can click Search icon to scan Bluetooth beacons.

    For example, Bluetooth Manager detected my Macbook Bluetooth.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    82/133

    We can configure Raspberry Pi Bluetooth in always visible mode. You can click Bluetoothicon. Then, a menu is shown.

    Select Adaptors. Next, you can select Always visible.

    6.4.2 Pairing

    In this section, we learn how to pair Raspberry Pi Bluetooth to other Bluetooth device.How to pair?

    Open Bluetooth Manager. Search and find Bluetooth device which you want to pair. Afterfound, select menu Device -> Pair.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    83/133

    Then, you should get a dialog as follows.

    You can select Random Passkey or Custom Passkey.

    On target Bluetooth machine, it should get notification for pairing.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    84/133

    After confirmed for pairing, we try to connect to Group Network

    Click Close if done.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    85/133

    Now you can see paired Bluetooth on Bluetooth Manager.

    On target Bluetooth, for instance, my Mackbook shown paired with Raspberry PiBluetooth.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    86/133

    6.4.3 Transferring File

    After paired with other Bluetooth device, we can transfer a file. In this scenario, I useMacbook. I should configure my Bluetooth can receive a file.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    87/133

    On System Preferences, select Sharing.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    88/133

    Checked Bluetooth Sharing. Select Ask What to Do on When receiving items.

    Now we send a file from Raspberry Pi. Select Bluetooth icon, select Send Files to Device.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    89/133

    You should get a dialog. Click OK for targeted Bluetooth.

    Choose a file.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    90/133

    This will send our file.

    On target machine, Macbook, I got a confirmation, shown in Figure below.

    Click Accept so you will get a file.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    91/133

    6.5 Building your own iBeacon

    The last section, we try to build iBeacon using hcitool. Firstly, we configure Bluetooth foriBeacon mode. Type these commands.

    $ sudo hciconfig hci0 up$ sudo hciconfig hci0 leadv 3$ sudo hciconfig hci0 noscan

    Then, verify it using hciconfig.

    $ hciconfig

    For testing, I use Beacon Scan tool to listen iBeacon packet.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    92/133

    To send iBeacon, you can type this command.

    $ sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 63

    It will generate iBeacon data.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    93/133

    On Beacon Scan application, it shows iBeacon data.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    94/133

    7. Deploying LAMP Stack

    This chapter explains how to deploy LAMP stack on Raspberry Pi 3 board.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    95/133

    7.1 Getting Started

    In this section, we try to deploy LAMP on our Raspberry Pi. The following is a list of required component which mustbe installed:

    Web Server, Apache

    Database, MySQLPHPMySQL Database driver for PHP

    We will install these components on next section.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    96/133

    7.2 Installing Apache Server

    Firstly, we install Apache Server.

    $ sudo apt-get install apache2

  • 8/16/2019 Getting Started with Raspberry Pi 3

    97/133

    7.3 Installing MySQL

    The second step is to install MySQL. Execute this command

    $ sudo apt-get install mysql-server

    In the middle of installing process, you will be asked to fill root password for MySQL

  • 8/16/2019 Getting Started with Raspberry Pi 3

    98/133

    If installation process is done, you can verify your MySQL by executing this command

    $ mysql --version

  • 8/16/2019 Getting Started with Raspberry Pi 3

    99/133

    Now you can connect to MySQL server. Execute this command

    $ mysql -u root -p

    Note: you may change MySQL user and password.

    Type this command to retrieve databases in MySQL.

    mysql> show databases;

  • 8/16/2019 Getting Started with Raspberry Pi 3

    100/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    101/133

    7.4 Installing PHP and MySQL Driver for PHP

    You need MySQL driver for PHP to access MySQL database. Execute this command toinstall PHP-MySQL driver

    $ sudo apt-get install php5 php5-mysql

  • 8/16/2019 Getting Started with Raspberry Pi 3

    102/133

    7.5 Testing PHP

    For testing, we are going to write “Hello world” PHP. Create a file, called hello.php , in/var/www/

    $ sudo nano /var/www/hello.php

    Then, write this code.

    PHP from Raspberry

    PHP Test

    Hello world PHP

    PHP Information:

    Save it.

    You test it now. Open your browser and navigate to URL where hello.php file located.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    103/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    104/133

    7.6 Testing PHP and MySQL

    In this section, we will create a PHP and MySQL application. We create a file,hellodb.php .

    $ sudo nano /var/www/hellodb.php

    Write all code below.

    Gettting MySQL database list from Raspberry

    List of database:

    Note: change username and password for MySQL.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    105/133

    Save it.

    You test it now. Open your browser and navigate to URL where hellodb.php file islocated.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    106/133

    8. Accessing GPIO

    This chapter explains how to work with GPIO on Raspberry Pi 3.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    107/133

    8.1 Introduction to GPIO

    General-purpose input/output (GPIO) is a generic pin on an integrated circuit whosebehavior, including whether it is an input or output pin, can be controlled by the user atrun time. GPIO pins have no special purpose defined, and go unused by default.

    To understand GPIO on Raspberry Pi 3 board, you can see it in Figure below.

    (source: https://www.element14.com/community/docs/DOC-73950/l/raspberry-pi-3-model-b-gpio-40-pin-block-pinout )

    See the pin 1 and pin 2 on physical board on the following Figure.

    https://www.element14.com/community/docs/DOC-73950/l/raspberry-pi-3-model-b-gpio-40-pin-block-pinout

  • 8/16/2019 Getting Started with Raspberry Pi 3

    108/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    109/133

    8.2 Accessing GPIO

    In this section, we will focus on Raspberry Pi GPIO programming using Python. Pythonand RPi.GPIO library, http://pypi.python.org/pypi/RPi.GPIO , have installed for you soyou can use them directly to access Raspberry Pi GPIO.

    Now you read all GPIO configuration using gpio command. Type this command onTerminal.

    $ gpio readall

    You can see pin mapping for Raspberry Pi 3 GPIO.

    You also can install RPi.GPIO library manually. How to install?

    For illustration, we will install RPi.GPIO 0.6.2. Firstly, we need Python developmentlibrary. Type the following command.

    $ sudo apt-get install python-dev

    Make sure your Raspberry Pi already connected to Internet network.

    Now you can download RPi.GPIO and install it. For instance, I install RPi.GPIO 0.6.2.

    http://pypi.python.org/pypi/RPi.GPIO

  • 8/16/2019 Getting Started with Raspberry Pi 3

    110/133

    $ wget $ https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.6$ tar -xvzf RPi.GPIO-0.6.2.tar.gz$ cd RPi.GPIO-0.6.2/$ sudo python setup.py install

    Make sure your Raspberry Pi already connected to Internet network.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    111/133

    8.3 Demo

    In this section, we learn how to write data using GPIO on Raspberry Pi. We can use oneLED to illustrate our case.

    The LED is connected to GPIO pin 11 (GPIO17). LED ground pin is connected to GPIO

    GND.The following is my wiring.

    Now we create Python application to write data on GPIO. We can use GPIO.output() towrite data, GPIO.HIGH and GPIO.LOW.

    Create a file, called leddemo.py , and write the following code.

    import RPi.GPIO as GPIO

    import time

    led_pin = 17GPIO.setmode(GPIO.BCM)GPIO.setup(led_pin, GPIO.OUT)

    try : while 1 : print ("turn on led") GPIO.output(led_pin, GPIO.HIGH) time.sleep( 2 ) print ("turn off led") GPIO.output(led_pin, GPIO.LOW)

  • 8/16/2019 Getting Started with Raspberry Pi 3

    112/133

    time.sleep( 2 )

    except KeyboardInterrupt : GPIO.output(led_pin, GPIO.LOW) GPIO.cleanup()

    print ("done")

    Save this code.

    You can run the application using the following command.

    $ sudo python leddemo.py

    If success, you can see the output as below.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    113/133

    The LED also is blinking.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    114/133

    8.4 Further Reading

    You can learn Raspberry Pi GPIO by trying more practices. I have written a book,Raspberry Pi I/O Programming Using Python. This book helps you to get startedRaspberry Pi I/O programming. This book uses Raspberry Pi 1 but it’s compatible withRaspberry Pi 2. Further information about this book, you can visit to my blog,http://blog.aguskurniawan.net/post/Raspberry-Pi-IO-Programming-using-Python.aspx .

    http://blog.aguskurniawan.net/post/Raspberry-Pi-IO-Programming-using-Python.aspx

  • 8/16/2019 Getting Started with Raspberry Pi 3

    115/133

    9. Raspberry Pi 3 Serial Debugging

    This chapter explains how to access Raspberry Pi 3 via Serial Port.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    116/133

    9.1 Preparation

    To debug Raspberry Pi using GPIO serial through computer, we need USB TTL device.There are a lot of USB TTL device, for instance, USB to TTL Serial Cable - Debug /Console Cable for Raspberry Pi from Adafruit, http://www.adafruit.com/products/954 .

    Another device, you can buy USB to TTL on Cooking-Hacks, http://www.cooking-hacks.com/index.php/usb-to-ttl-converter-cp210.html .

    In this section, I used a Foca V2.1 FT232RL Tiny Breakout USB to Serial UART Interfacefrom iteadstudio. I bought it on

    http://www.exp-tech.de/Shields/Foca-V2-1-FT232RL-Tiny-Breakout-USB-to-Serial-

    http://www.exp-tech.de/Shields/Foca-V2-1-FT232RL-Tiny-Breakout-USB-to-Serial-UART-Interface.htmlhttp://www.cooking-hacks.com/index.php/usb-to-ttl-converter-cp210.htmlhttp://www.adafruit.com/products/954

  • 8/16/2019 Getting Started with Raspberry Pi 3

    117/133

    UART-Interface.html

  • 8/16/2019 Getting Started with Raspberry Pi 3

    118/133

    9.2 Enabling Serial Debugging

    By default, Raspbian disables serial debugging so we need to enable this feature. OnTerminal type the following command.

    $ sudo raspi-config

    You will get a dialog. Select 8 Advanced Options menu.

    Then, select A8 Serial menu.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    119/133

    Furthermore, you will get a confirmation. Please select to enable serial debuggingfeature.

    If success, you will get a success confirmation.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    120/133

    Close this config dialog.

    Now you can reboot your Raspbian by typing the command on Terminal.

    $ sudo reboot

  • 8/16/2019 Getting Started with Raspberry Pi 3

    121/133

    9.3 Wiring

    How to implement?

    It’s easy. You can just connect Tx from USB TTL to Raspberry Pi UART0_TXD and USBTTL RX to Raspberry Pi UART0_RXD. Some USB TTL must change them. It meansUSB TTL TX should be connected to Raspberry Pi UART0_RXD and USB TTL RX toRaspberry Pi UART0_TXD. (Optional) You can connect GND from USB TTL to GND ofRaspberry Pi board.

    Here is a sample of connected hardware.

    Now your USB cable of USB TTL device is be connected to your computer. You can useany serial application to execute.

    In this book, I used PuTTY,http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html , and run it on myWindows OS.

    Run PuTTY and choose Serial for connection type. Fill Serial line name, for instance, myWindows detected it on COM6 as below.

    http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

  • 8/16/2019 Getting Started with Raspberry Pi 3

    122/133

    Set 115200 for speed serial.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    123/133

    Click Serial on side menu and choose None for Parity and Flow control.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    124/133

    You also can use CoolTerm to view serial data. You can download and install it onhttp://freeware.the-meiers.org .

    http://freeware.the-meiers.org/

  • 8/16/2019 Getting Started with Raspberry Pi 3

    125/133

    To use it, you can open Options menu and select Port and Baudrate on 115200.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    126/133

    On Terminal tab, you don’t configure anything.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    127/133

    Click OK button to save the configuration.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    128/133

    9.4 Testing

    If you’re ready, you can click Open button on PuTTY. You may press Enter on keyboardwhen you see blank screen.

    If success, you will get the authentication form.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    129/133

    Then, try to logon to Raspberry Pi.

    Here is a sample of serial debugging output.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    130/133

    If you use CoolTerm, click Connect menu. After that, you can see Login Terminal.

    Type username and password.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    131/133

    You also can execute common commands such as ls.

  • 8/16/2019 Getting Started with Raspberry Pi 3

    132/133

  • 8/16/2019 Getting Started with Raspberry Pi 3

    133/133

    Contact

    If you have question related to this book, please contact me at [email protected] . My

    blog: http://blog.aguskurniawan.net

    http://blog.aguskurniawan.net/