TECHNOLOGY IN ACTION™ Raspbian OS Programming with the Raspberry Pi IoT Projects with Wolfram, Mathematica, and Scratch — Agus Kurniawan Raspbian OS Programming with the Raspberry Pi IoT Projects with Wolfram, Mathematica, and Scratch Agus Kurniawan Raspbian OS Programming with the Raspberry Pi: IoT Projects with Wolfram, Mathematica, and Scratch Agus Kurniawan Depok, Indonesia ISBN-13 (pbk): 978-1-4842-4211-7 ISBN-13 (electronic): 978-1-4842-4212-4 https://doi.org/10.1007/978-1-4842-4212-4 Library of Congress Control Number: 2018964829 Copyright © 2019 by Agus Kurniawan This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Natalie Pao Development Editor: James Markham Coordinating Editor: Jessica Vakili Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit http://www.apress. com/rights-permissions. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/978-1-4842-4211-7. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper Table of Contents About the Author ���������������������������������������������������������������������������������ix About the Technical Reviewer �������������������������������������������������������������xi Acknowledgments �����������������������������������������������������������������������������xiii Introduction ����������������������������������������������������������������������������������������xv Chapter 1: Introduction to Raspberry Pi �����������������������������������������������1 1.1 What Is Raspberry Pi? .....................................................................................2 1.2 Reviewing the Raspberry Pi Board Models .....................................................3 1.3 Introducing the Raspbian OS ..........................................................................5 1.4 Setting Up Raspbian OS on Raspberry Pi Boards ............................................7 1.5 Headless Raspbian SSH Network Setup .........................................................8 1.6 Expanding Storage ........................................................................................10 1.7 Configuring a WiFi Network ..........................................................................12 1.8 Exploring the Raspbian OS Desktop and Terminal ........................................13 1.9 Configuring the VNC Server...........................................................................16 1.9.1 Installing the VNC Server ......................................................................17 1.9.2 Enabling the VNC Server .......................................................................17 1.9.3 Configuring Boot Options for the Desktop ............................................19 1.9.4 Testing the Server .................................................................................21 1.10 Powering Off the Raspbian .........................................................................24 1.11 Summary.....................................................................................................25 iii TTaabbllee ooff CCoonnTTeennTTss Chapter 2: The Raspbian OS Command Line ���������������������������������������27 2.1 Introducing Raspbian Shells .........................................................................28 2.2 The Basic Raspbian Shell ..............................................................................30 2.2.1 Rebooting Raspbian OS ........................................................................30 2.2.2 Shutting Down ......................................................................................30 2.2.3 Configuring the Timezone .....................................................................31 2.2.4 Configuring the Keyboard .....................................................................33 2.3 Manipulating Files and Directories ...............................................................34 2.3.1 Getting the Current Working Directory ..................................................34 2.3.2 Listing Files and Directories .................................................................34 2.3.3 Understanding the ls Parameters .........................................................37 2.3.4 Changing the Current Working Directory ..............................................38 2.3.5 Creating a Directory ..............................................................................42 2.3.6 Removing Files and Directories ............................................................44 2.3.7 Copying Files and Directories ...............................................................46 2.3.8 Moving Files and Directories ................................................................50 2.3.9 Creating Symbolic Links .......................................................................52 2.4 Redirection ....................................................................................................53 2.4.1 Standard Output ...................................................................................53 2.4.2 Standard Input ......................................................................................54 2.4.3 Standard Error ......................................................................................55 2.4.4 Pipelines ...............................................................................................56 2.5 Permissions ..................................................................................................57 2.5.1 Understanding Permissions ..................................................................57 2.5.2 chmod ...................................................................................................61 2.5.3 chown ...................................................................................................63 iv TTaabbllee ooff CCoonnTTeennTTss 2.6 Manipulating Processes ................................................................................64 2.6.1 ps ..........................................................................................................64 2.6.2 top ........................................................................................................68 2.6.3 Killing Processes ..................................................................................70 2.6.4 Viewing Memory ...................................................................................71 2.7 Media Storage ...............................................................................................72 2.7.1 Displaying Free Disk Space ..................................................................72 2.7.2 mount ...................................................................................................73 2.7.3 umount .................................................................................................77 2.8 Summary.......................................................................................................77 Chapter 3: Programming on Raspbian OS ������������������������������������������79 3.1 Understand the Programming Model in Raspbian OS ...................................79 3.2 Setting Up the Programming Environment ...................................................83 3.3 Python Programming ....................................................................................83 3.3.1 Python 3 (IDLE) .....................................................................................84 3.3.2 Thonny Python IDE ................................................................................85 3.3.3 Python Shell from Remote SSH ............................................................88 3.3.4 What’s Next? .........................................................................................90 3.4 Node.js Programming ...................................................................................90 3.5 C/C++ Programming .....................................................................................94 3.6 Summary.......................................................................................................96 Chapter 4: Computational Mathematics with the Wolfram Language and Mathematica ���������������������������������������������������������������97 4.1 Introducing Wolfram Language and Mathematica ........................................98 4.2 Setting Up Wolfram and Mathematica ........................................................101 4.3 Developing a Hello World Program ..............................................................102 v TTaabbllee ooff CCoonnTTeennTTss 4.4 Basic Programming .....................................................................................103 4.4.1 Data Types and Declaring Variables....................................................104 4.4.2 Arithmetic Operators ..........................................................................105 4.4.3 Relational and Logical Operators ........................................................106 4.4.4 Conditional Statements ......................................................................108 4.4.5 Looping ...............................................................................................111 4.4.6 Adding Comments ..............................................................................116 4.4.7 Functions ............................................................................................117 4.5 Computational Mathematics .......................................................................119 4.5.1 Calculus ..............................................................................................119 4.5.2 Matrix .................................................................................................132 4.5.3 Quadratic Equations ...........................................................................137 4.5.4 Linear Equations .................................................................................138 4.6 Summary.....................................................................................................140 Chapter 5: Visual Programming with Scratch ����������������������������������141 5.1 Introducing Visual Programming and Scratch .............................................142 5.2 Setting Up Scratch ......................................................................................143 5.3 Developing a Hello Program ........................................................................143 5.3.1 Creating a Project ...............................................................................143 5.3.2 Building a Scratch Program ................................................................145 5.3.3 Running the Program..........................................................................148 5.3.4 Saving a Program Into a File ..............................................................149 5.4 Working with Sprites ...................................................................................150 5.5 Working with the Background .....................................................................155 vi TTaabbllee ooff CCoonnTTeennTTss 5.6 Working with Motion ...................................................................................159 5.6.1 Coordinate System and Direction .......................................................160 5.6.2 Demo 1: Movement .............................................................................162 5.6.3 Demo 2: Direction ...............................................................................163 5.7 Working with Control Blocks .......................................................................165 5.7.1 Building a Decision .............................................................................165 5.7.2 Looping ...............................................................................................173 5.8 Summary.....................................................................................................175 Index �������������������������������������������������������������������������������������������������177 vii About the Author Agus Kurniawan is a lecturer, researcher, IT consultant, and an author. He has 17 years of experience in various software and hardware development projects, delivering materials in training and workshops, and technical writing. He has been awarded the Microsoft Most Valuable Professional (MVP) award 14 years in a row. His topic interests are software engineering, embedded system, networking, and security system. He has been working as lecturer and researcher at the Faculty of Computer Science, Universitas Indonesia. Currently, he is pursuing a PhD in computer science at the Freie Universität Berlin, Germany. He can be reached on his blog at http://blog.aguskurniawan.net and Twitter at @agusk2010. ix About the Technical Reviewer Massimo Nardone has more than 22 years of experiences in security, web/ mobile development, cloud, and IT architecture. His true IT passions are security and Android. He has been programming and teaching how to program with Android, Perl, PHP, Java, VB, Python, C/C++, and MySQL for more than 20 years. He holds a Master of Science degree in Computing Science from the University of Salerno, Italy. He has worked as a Project Manager, Software Engineer, Research Engineer, Chief Security Architect, Information Security Manager, PCI/ SCADA Auditor, and Senior Lead IT Security/Cloud/SCADA Architect for many years. Technical skills include: Security, Android, Cloud, Java, MySQL, Drupal, Cobol, Perl, web and mobile development, MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll, Scratch, etc. He currently works as Chief Information Security Office (CISO) for Cargotec Oyj. xi