Table Of ContentPython Programming
for Arduino
Develop practical Internet of Things prototypes
and applications with Arduino and Python
Pratik Desai
BIRMINGHAM - MUMBAI
Python Programming for Arduino
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: February 2015
Production reference: 1230215
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78328-593-8
www.packtpub.com
Credits
Author Project Coordinator
Pratik Desai Milton Dsouza
Reviewers Proofreaders
Juan Ramón González Safis Editing
Marco Schwartz Maria Gould
Josh VanderLinden Ameesha Green
Paul Hindle
Commissioning Editor
Saleem Ahmed
Indexer
Mariammal Chettiyar
Acquisition Editor
James Jones
Graphics
Abhinash Sahu
Content Development Editor
Priyanka Shah
Production Coordinator
Manu Joseph
Technical Editor
Ankita Thakur
Cover Work
Manu Joseph
Copy Editors
Jasmine Nadar
Vikrant Phadke
About the Author
Pratik Desai, PhD, is the Principal Scientist and cofounder of a connected devices
start-up, Imbue Labs, where he develops scalable and interoperable architecture for
wearable devices and Internet of Things (IoT) platforms during the day. At night, he
leads the development of an open source IoT initiative, the Semantic Repository of
Things. Pratik has 8 years of research and design experience in various layers of the
IoT and its predecessor technologies such as wireless sensor networks, RFID, and
machine-to-machine (M2M) communication. His domains of expertise are the IoT,
Semantic Web, machine learning, robotics, and artificial intelligence.
Pratik completed his MS and PhD from Wright State University, Ohio, and
collaborated with the Ohio Center of Excellence in Knowledge-enabled Computing
(Kno.e.sis) during his doctoral research. His doctoral research was focused on
developing situation awareness frameworks for IoT devices, enabling semantic
web-based reasoning and handling the uncertainty associated with sensor data.
In his personal life, Pratik is an avid DIY junkie and likes to get hands-on experience
on upcoming technologies. He extensively expresses his views on technology and
shares interesting developments on Twitter (@chheplo).
I would like to dedicate the book to my parents, who were responsible
for building the foundation of what I am today. The book would not
have been possible without the patience, support, and encouragement
from my beloved wife, Sachi. I would also like to thank her for landing
her photography skills that were used in development of some of the
important images used in the book. I would also like to extend my
sincere gratitude to the editors for their valuable feedbacks.
About the Reviewers
Juan Ramón González is a technical engineer of computer systems and lives
in Seville (Andalusia, Spain). For the past 9 years, he has been working on free
software-based projects for the regional Ministry of Education by using Python,
C++, and JavaScript, among other programming languages.
He is one of the main members of the CGA project in Andalusia (Centro de Gestión
Avanzado or Advanced Management Center), which manages a network with more
than 4,000 servers with Debian and 500,000 client computers that run Guadalinex,
a customized Ubuntu-based operating system for Andalusian schools.
As a software developer who has a passion for electronics and astronomy, he started
one of the first projects to control a telescope with the Arduino microcontroller by
using a computer with the Stellarium software and a driver developed with Python
to communicate with the telescope. This project's sources are published on the
collaborative platform GitHub. You can see the whole code and the prototype at
https://github.com/juanrmn/Arduino-Telescope-Control.
Marco Schwartz is an electrical engineer, entrepreneur, and blogger. He has
a master's degree in electrical engineering and computer science from Supélec,
France, and a master's degree in micro engineering from EPFL, Switzerland.
Marco has more than 5 years of experience working in the domain of electrical
engineering. His interests gravitate around electronics, home automation, the
Arduino and the Raspberry Pi platforms, open source hardware projects, and
3D printing.
He runs several websites around Arduino, including the Open Home Automation
website that is dedicated to building home automation systems using open
source hardware.
Marco has written a book on home automation and Arduino called Arduino Home
Automation Projects, Packt Publishing. He has also written a book on how to build
Internet of Things projects with Arduino called Internet of Things with the Arduino
Yun, Packt Publishing.
Josh VanderLinden is a lifelong technology enthusiast who has been programming
since the age of 10. He enjoys learning and becoming proficient with new technologies.
He has designed and built software, ranging from simple shell scripts to scalable
backend server software to interactive web and desktop user interfaces. Josh has been
writing software professionally using Python since 2007, and he has been building
personal Arduino-based projects since 2010.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.PacktPub.com
and as a print book customer, you are entitled to a discount on the eBook copy. Get in
touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles,
sign up for a range of free newsletters and receive exclusive discounts and offers
on Packt books and eBooks.
TM
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view 9 entirely free books. Simply use your login credentials for
immediate access.
Table of Contents
Preface 1
Chapter 1: Getting Started with Python and Arduino 7
Introduction to Python 7
Why we use Python 8
When do we use other languages 9
Installing Python and Setuptools 10
Installing Python 10
Linux 10
Ubuntu 11
Fedora and Red Hat 11
Windows 11
Mac OS X 14
Installing Setuptools 14
Linux 15
Windows 15
Mac OS X 16
Installing pip 16
Installing Python packages 17
The fundamentals of Python programming 18
Python operators and built-in types 19
Operators 20
Built-in types 20
Data structures 22
Lists 22
Tuples 24
Sets 25
Dictionaries 25
Controlling the flow of your program 25
The if statement 26
The for statement 26
The while statement 26