Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Python Programming for Arduino
Python Programming for Arduino

Python Programming for Arduino: Develop practical Internet of Things prototypes and applications with Arduino and Python

eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Python Programming for Arduino

Chapter 2. Working with the Firmata Protocol and the pySerial Library

In the previous chapter, you learned the fundamentals of the Python programming language and the Arduino hardware platform so that you could get started. If you are reading this chapter directly without going through the previous chapter, it is assumed that you have some level of expertise or working experience with these technologies. This chapter describes two important components that are required to bridge Arduino with Python:

  • The Arduino Firmata protocol
  • Python's serial library called pySerial

Although the Firmata protocol is useful to interface Arduino with Python, it can also be used as an independent tool to develop a large variety of applications.

It is time to take your Arduino hardware out and start getting your hands dirty. During the course of this chapter, you will require an LED, a breadboard, and a 1 kilo-ohm resistor as well as the components that you already used in the previous chapter, that...

Connecting the Arduino board

As mentioned in the previous chapter, this book supports all major operating systems, and this section will provide you with steps to connect and configure the Arduino board for these operating systems. In the previous chapter, we utilized example code to get started with the Arduino IDE. If you were unable to successfully communicate with Arduino by following the information given in the previous chapter, follow the instructions provided in this section to establish a connection between your computer and your Arduino. First, connect your Arduino board to your computer's USB port using a USB cable and follow the steps according to your operating system.

Linux

If you are using the latest version of Ubuntu Linux, once you connect the Arduino board and open the Arduino IDE, you will be asked to add your username to the dailout group, as displayed in the following screenshot. Click on the Add button and log out from the system. You don't need to restart...

Introducing the Firmata protocol

Before Arduino, the domain of microcontroller-based applications was limited to hardware programmers. Arduino made it simple for developers that came from other software fields and even for the non-coding community to develop microcontroller-based hardware applications. Arduino consists of a simple hardware design with a microcontroller and I/O pins to interface external devices. If one can write an Arduino sketch that can transfer the control of the microcontroller and these pins to an external software mechanism, then it will reduce one's efforts to upload Arduino sketches for every modification. This process can be performed by developing such an Arduino program that can then be controlled using a serial port. There exists a protocol called Firmata, which does exactly that.

What is Firmata?

Firmata is a generic protocol that allows communication between the microcontroller and the software that is hosted on a computer. Any software from any computer...

Getting started with pySerial

You learned about the Firmata protocol in the previous section. This is an easy and quick way to start working with Arduino. Although the Firmata protocol helps you to develop complex applications from your computer without modifying the Arduino sketch, we are not ready to start coding these applications.

The first step towards writing these complex applications is to provide an interface between your programming environment and the Arduino via a serial port. In this book, you will be required to establish a connection between the Python interpreter and Arduino for every project that we develop.

Writing your own library, which includes implementation of functions and specifications to enable communication on a serial protocol, is an inconvenient and time consuming process. We are going to avoid that by using an open source, well maintained Python library called pySerial.

The pySerial library enables communication with Arduino by encapsulating the access for the...

Bridging pySerial and Firmata

In the Firmata section, we already learned how useful it is to use the Firmata protocol instead of constantly modifying the Arduino sketch and uploading it for simple programs. pySerial is a simple library that provides a bridge between Arduino and Python via a serial port, but it lacks any support for the Firmata protocol. As mentioned earlier, the biggest benefit of Python can be described in one sentence, "There is a library for that." So, there exists a Python library called pyFirmata that is built on pySerial to support the Firmata protocol. There are a few other Python libraries that also support Firmata, but we will only be focusing on pyFirmata in this chapter. We will be extensively using this library for various upcoming projects as well:

  1. Let's start by installing pyFirmata just like any other Python package by using Setuptools:
    $ sudo pin install pyfirmata
    

    In the previous section, while testing pySerial, we uploaded the DigitalSerialRead...

Summary

By introducing the Firmata library, we avoided writing any custom Arduino sketches in this chapter. We will continue this practice during the remaining part of this book and will only use or make custom sketches when required. In this chapter, you interacted with the Arduino board by making the LED blink, which is the easiest way to get started on a hardware project. Now it's time for your first project, where we are also going to make some more LEDs blink. One might ask the question that if we have already done it, then why do we need another project to make LEDs blink? Let's find out.

Left arrow icon Right arrow icon

Description

This is the book for you if you are a student, hobbyist, developer, or designer with little or no programming and hardware prototyping experience, and you want to develop IoT applications. If you are a software developer or a hardware designer and want to create connected devices applications, then this book will help you get started.

Who is this book for?

This is the book for you if you are a student, hobbyist, developer, or designer with little or no programming and hardware prototyping experience, and you want to develop IoT applications.

What you will learn

  • Design and develop your own hardware prototypes using Arduino
  • Interface Arduino to a computer using the Firmata protocol and Python
  • Acquire handson experience in developing applications for a variety of popular sensors
  • Develop graphical user interfaces to control your components and plots to visualize sensor data
  • Access your Arduino prototype from the Internet
  • Implement messaging protocols for Arduino communication
  • Connect your hardware project to cloudbased services
  • Create advanced Internet of Things applications through projects in the book

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 27, 2015
Length: 400 pages
Edition : 1st
Language : English
ISBN-13 : 9781783285945
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Feb 27, 2015
Length: 400 pages
Edition : 1st
Language : English
ISBN-13 : 9781783285945
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 103.97
Arduino Development Cookbook
€36.99
Arduino Essentials
€24.99
Python Programming for Arduino
€41.99
Total 103.97 Stars icon

Table of Contents

12 Chapters
1. Getting Started with Python and Arduino Chevron down icon Chevron up icon
2. Working with the Firmata Protocol and the pySerial Library Chevron down icon Chevron up icon
3. The First Project – Motion-triggered LEDs Chevron down icon Chevron up icon
4. Diving into Python-Arduino Prototyping Chevron down icon Chevron up icon
5. Working with the Python GUI Chevron down icon Chevron up icon
6. Storing and Plotting Arduino Data Chevron down icon Chevron up icon
7. The Midterm Project – a Portable DIY Thermostat Chevron down icon Chevron up icon
8. Introduction to Arduino Networking Chevron down icon Chevron up icon
9. Arduino and the Internet of Things Chevron down icon Chevron up icon
10. The Final Project – a Remote Home Monitoring System Chevron down icon Chevron up icon
11. Tweet-a-PowerStrip Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.6
(22 Ratings)
5 star 72.7%
4 star 13.6%
3 star 13.6%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Loris Mar 27, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I had an Arduino Uno board at home for some time. I had few projects in my head but I didn’t have the required knowledge of electronics and C programming to bring them to an end. I have been programming in Python for a long time and I bought this book with the hope of being able to somehow simplify the projects by using a more familiar programming language. The book was extremely useful as it clearly explains which part of the code should be written in C and when it is possible to use Python.The first chapter of the book introduces the reader to both Python and Arduino and can be safely skipped if the reader is already familiar with both of them. The author provides detailed step by step instructions on how to install what is needed. Throughout the book instructions are given for Linux, Mac OS X, and Windows.The next chapter is about the Arduino Firmata protocol and the pySerial Python library. This chapter was probably one of the most interesting to me as it shows how to interact with the Arduino board by using Python.The following chapters are a little bit more complex but they are still easy to follow, and the author shows how to use various electronic components such as a passive infrared (PIR) sensor to detect movements, a potentiometer, a buzzer, a DC motor, a servomotor, a temperature sensor, a humidity sensor, and a light sensor. Almost every chapter proposes little projects that can be as easy as blinking an LED or rather complex. The instructions of the projects are always very clear and the provided schemes are (at least for me that I have no experience in electronics) extremely useful.A portion of the book is devoted to provide a GUI to the projects. The author chose to use Tkinter. This framework is probably not as features rich and appealing as other frameworks but it has the advantage to be part of the Python standard library, so there is no need to install anything else to use it.It was a welcome surprise to discover that the book also explained how to include both the Arduino and a Raspberry Pi into the same project. I own a Raspberry Pi and I have been interested in mating the two boards for a long time.Other topics that are explained in the book are how to provide network capabilities to the Arduino and how to build a simple and RESTful web application in Python.In the final chapters are presented two big (and more complex) projects that make use of all what have been explained in the previous chapters.Needless to say, I highly recommend this book
Amazon Verified review Amazon
Don L. Jackson Mar 24, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
First my background: I'm an old software/hardware guy who first used the Intel 4004 microprocessor back in 1973. Since thenI have been professionally involved with microprocessors and microcontrollers, even after retirement. I've taughtcollege level courses on the micro. I know and have used quite a few languages from assembly to C++.Going all the way through this book would keep anyone out of trouble for a long time. I must admit not gettingall the way through at this writing, although I have skimmed the remainder.The author, in his easy to follow style, is honest and points out when Python should be used and when other "static"(compiled) languages such as C would be better choice. He demonstrates use of both C and Python in the examples.Fair treatment of the three major operating systems is refreshing. Those are Windows, Mac, and Linux. Linuxmay not be the most popular, but many experimenters who would find this book useful do use Linux. He alsospends a good amount of time bringing new users of Python and the Arduino gently up to speed. Some backgroundin writing computer programs and elementary knowledge of electronics is desirable, but may not be necessaryas he explains most things in detail.The "Firmata" protocol was totally foreign to me, and I finally figured out (correctly?) that the more commonterm might be "debugger". First time encountering that term, but of course "sketch" (meaning source code) was alsoforeign to me before encountering the Arduino. I did not see the common term "bootloader" mentioned. Theauthor is using the terminology of the Arduino enthusiast rather than those of mainstream hobbyists andprofessionals.The reader is led through some varied and interesting projects including mating with a Raspberry Pi and otherI/O modules and making a web server. Since the Pi is a substantial outlay of money, I believe the advertisingfor the book should give some indications of what is required to fulfill the books projects. To that end, Iwould suggest at beginning of book a complete list of materials needed for all projects so reader can orderthem before needing them. Many can be ordered through Amazon or eBay from China for little expense and free shipping,but it takes about 2-4 weeks to receive them.There is a good intro to Fritzing and how to put it to good use in the projects. Fritzing produces more ofa pictorial view of a project which is great for beginners, but addition of an actual schematic diagramwould be more satisfactory for seasoned users and a good learning tool for the beginner.Wow! This book goes far beyond my expectations in the projects to include ethernet, wifi, IOT -- expect tospend some money on the items needed, but the education will be worth it. I do not believe I've seen abook cover this much territory with such easily understood directions. The author even managed to get Twitterinvolved! The book seems quite suitable for a class textbook, easily for middle school grades, and maybefor advanced elementary school students, but is certainly not dumbed down to insult an adult.I highly recommend this book -- the author has managed to write for nearly all persons of various backgrounds.
Amazon Verified review Amazon
ruben Mar 13, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It is an excellent book very new and it has interesting topicsIt helps me a lot because I am learning a lot of information for my projects and for my students
Amazon Verified review Amazon
Raúl Apr 03, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Are you into developing your hardware ideas or fast prototyping using Arduino?? Do you love Python?? then this book is for you, because it will introduce you to the easy interfaces that connect Arduino with the development tools necessary to bring your scripts into real world action, from LED blinking to a whole monitoring station that will tweet you “Good Morning, today is a shiny day”, which means a lot.As the author mentions, with python you will be able to create easy to read and understand code, bring from ideas to market prototypes that would take as much as twice when using Arduino’s native environments and a plus, thousands of libraries already available for the developers.Going back to the final project, following the guidelines mentioned in the book, you will be able to establish foundational knowledge around the Internet Of Things (IoT) which is becoming a booming trend between “Do it Yourself” people and companies. From here on, you will have enough tools and skills to make your home smarter, automatize tedious life actions or even create a food dispenser for your grumpy cat.Also a big topic touch in the book will be regarding UI interfaces, which will use Tkinter for desktop apps, but also will touch the surface of RESTful services, which will allow the communication of the Arduino board to the world, which will allow you to place your board, connect to the internet and then forget about it wherever in the map it is.I would totally recommend this book, mostly for intermediate to advance user; novices will have some troubles following up but with enough practice, it won’t be a problem.
Amazon Verified review Amazon
Matteo Apr 23, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is excellent if you plan to start doing complex coding with arduinos and Python, codebase is done using 2.7 but this is not a major issue. Everything is explained thoroughly without being hard to read/understand, quite a bit of the starting part of the book is dedicated on python basics that may be redundant depending on your code skills but its not a big deal. Quite a lot of the exercises depend on acquiring daughter boards and possibly a raspi so check if you have all the required parts to run the code samples, if you have zero parts on hand you can source most of it on rs/digikey.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.