You are previewing Python Programming for Arduino.
O'Reilly logo
Python Programming for Arduino

Book Description

Develop practical Internet of Things prototypes and applications with Arduino and Python

In Detail

The future belongs to applications and services that involve connected devices, requiring physical components to communicate with web-level applications. Arduino combined with the popular open source software platform Python can be used to develop the next level of advanced Internet of Things (IoT) projects with graphical user interfaces and Internet-connected applications.

Starting with designing hardware prototypes using Arduino, this book will then show you everything you need to know to be able to develop complex cloud applications. You will delve into domain-specific topics with incremental complexity, ending with real-world projects. You will quickly learn to develop user interfaces, plots, remote access, messaging protocols, and cloud connectivity. Each successive topic, accompanied by plenty of examples, will help you develop your cutting-edge hardware 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 hands-on 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 cloud-based services

  • Create advanced Internet of Things applications through projects in the book

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

    1. Python Programming for Arduino
      1. Table of Contents
      2. Python Programming for Arduino
      3. Credits
      4. About the Author
      5. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. Getting Started with Python and Arduino
        1. Introduction to Python
          1. Why we use Python
          2. When do we use other languages
        2. Installing Python and Setuptools
          1. Installing Python
          2. Linux
            1. Ubuntu
            2. Fedora and Red Hat
          3. Windows
          4. Mac OS X
          5. Installing Setuptools
            1. Linux
            2. Windows
            3. Mac OS X
          6. Installing pip
          7. Installing Python packages
        3. The fundamentals of Python programming
        4. Python operators and built-in types
          1. Operators
          2. Built-in types
            1. Data structures
            2. Lists
            3. Tuples
            4. Sets
            5. Dictionaries
          3. Controlling the flow of your program
            1. The if statement
            2. The for statement
            3. The while statement
          4. Built-in functions
            1. Conversions
              1. Math operations
              2. String operations
        5. Introduction to Arduino
          1. History
          2. Why Arduino?
          3. Arduino variants
          4. The Arduino Uno board
          5. Installing the Arduino IDE
            1. Linux
            2. Mac OS X
            3. Windows
        6. Getting started with the Arduino IDE
          1. What is an Arduino sketch?
          2. Working with libraries
          3. Using Arduino examples
          4. Compiling and uploading sketches
          5. Using the Serial Monitor window
        7. Introduction to Arduino programming
          2. Variables
          3. Constants
          4. Data types
          5. Conversions
          6. Functions and statements
            1. The setup() function
            2. The loop() function
            3. The pinMode() function
            4. Working with pins
            5. Statements
        8. Summary
      9. 2. Working with the Firmata Protocol and the pySerial Library
        1. Connecting the Arduino board
          1. Linux
          2. Mac OS X
          3. Windows
          4. Troubleshooting
        2. Introducing the Firmata protocol
          1. What is Firmata?
          2. Uploading a Firmata sketch to the Arduino board
          3. Testing the Firmata protocol
        3. Getting started with pySerial
          1. Installing pySerial
          2. Playing with a pySerial example
        4. Bridging pySerial and Firmata
        5. Summary
      10. 3. The First Project – Motion-triggered LEDs
        1. Motion-triggered LEDs – the project description
        2. The project goal
          1. The list of components
          2. The software flow design
          3. The hardware system design
            1. Introducing Fritzing – a hardware prototyping software
            2. Working with the breadboard
            3. Designing the hardware prototype
          4. Testing hardware connections
        3. Method 1 – using a standalone Arduino sketch
          1. The project setup
          2. The Arduino sketch
            1. The setup() function
            2. The loop() function
          3. Working with custom Arduino functions
          4. Testing
          5. Troubleshooting
        4. Method 2 – using Python and Firmata
          1. The project setup
          2. Working with Python executable files
          3. The Python code
          4. Working with pyFirmata methods
          5. Working with Python functions
          6. Testing
          7. Troubleshooting
        5. Summary
      11. 4. Diving into Python-Arduino Prototyping
        1. Prototyping
        2. Working with pyFirmata methods
          1. Setting up the Arduino board
          2. Configuring Arduino pins
            1. The direct method
            2. Assigning pin modes
          3. Working with pins
            1. Reporting data
            2. Manual operations
              1. The write() method
              2. The read() method
          4. Additional functions
          5. Upcoming functions
        3. Prototyping templates using Firmata
          1. Potentiometer – continuous observation from an analog input
            1. Connections
            2. The Python code
          2. Buzzer – generating sound alarm pattern
            1. Connections
            2. The Python code
          3. DC motor – controlling motor speed using PWM
            1. Connections
            2. The Python code
          4. LED – controlling LED brightness using PWM
            1. Connections
            2. The Python code
          5. Servomotor – moving the motor to a certain angle
            1. Connections
            2. The Python code
        4. Prototyping with the I2C protocol
          1. Arduino examples for I2C interfacing
            1. Arduino coding for the TMP102 temperature sensor
            2. Arduino coding for the BH1750 light sensor
          2. PyMata for quick I2C prototyping
            1. Interfacing TMP102 using PyMata
            2. Interfacing BH1750 using PyMata
          3. Useful pySerial commands
            1. Connecting with the serial port
            2. Reading a line from the port
            3. Flushing the port to avoid buffer overflow
            4. Closing the port
        5. Summary
      12. 5. Working with the Python GUI
        1. Learning Tkinter for GUI design
        2. Your first Python GUI program
          1. The root widget Tk() and the top-level methods
          2. The Label() widget
          3. The Pack geometry manager
        3. The Button() widget – interfacing GUI with Arduino and LEDs
        4. The Entry() widget – providing manual user inputs
        5. The Scale() widget – adjusting the brightness of an LED
        6. The Grid geometry manager
        7. The Checkbutton() widget – selecting LEDs
        8. The Label() widget – monitoring I/O pins
        9. Remaking your first Python-Arduino project with a GUI
        10. Summary
      13. 6. Storing and Plotting Arduino Data
        1. Working with files in Python
          1. The open() method
          2. The write() method
          3. The close() method
          4. The read() method
          5. The with statement – Python context manager
        2. Using CSV files to store data
        3. Storing Arduino data in a CSV file
        4. Getting started with matplotlib
          1. Configuring matplotlib on Windows
          2. Configuring matplotlib on Mac OS X
            1. Upgrading matplotlib
            2. Troubleshooting installation errors
          3. Setting up matplotlib on Ubuntu
        5. Plotting random numbers using matplotlib
        6. Plotting data from a CSV file
        7. Plotting real-time Arduino data
        8. Integrating plots in the Tkinter window
        9. Summary
      14. 7. The Midterm Project – a Portable DIY Thermostat
        1. Thermostat – the project description
          1. Project background
          2. Project goals and stages
          3. The list of required components
          4. Hardware design
          5. Software flow for user experience design
        2. Stage 1 – prototyping the thermostat
          1. The Arduino sketch for the thermostat
            1. Interfacing the temperature sensor
            2. Interfacing the humidity sensor
            3. Interfacing the light sensor
            4. Using Arduino interrupts
          2. Designing the GUI and plot in Python
            1. Using pySerial to stream sensor data in your Python program
            2. Designing the GUI using Tkinter
            3. Plotting percentage humidity using matplotlib
            4. Using button interrupts to control the parameters
              1. Changing the temperature unit by pressing a button
              2. Swapping between the GUI and the plot by pressing a button
          3. Troubleshooting
        3. Stage 2 – using a Raspberry Pi for the deployable thermostat
          1. What is a Raspberry Pi?
          2. Installing the operating system and configuring the Raspberry Pi
            1. What do you need to begin using the Raspberry Pi?
            2. Preparing an SD card
            3. The Raspberry Pi setup process
          3. Using a portable TFT LCD display with the Raspberry Pi
            1. Connecting the TFT LCD using GPIO
            2. Configuring the TFT LCD with the Raspberry Pi OS
          4. Optimizing the GUI for the TFT LCD screen
          5. Troubleshooting
        4. Summary
      15. 8. Introduction to Arduino Networking
        1. Arduino and the computer networking
          1. Networking fundamentals
          2. Obtaining the IP address of your computer
            1. Windows
            2. Mac OS X
            3. Linux
          3. Networking extensions for Arduino
            1. Arduino Ethernet Shield
            2. Arduino WiFi Shield
            3. Arduino Yún
          4. Arduino Ethernet library
            1. The Ethernet class
            2. The IPAddress class
            3. The Server class
            4. The Client class
          5. Exercise 1 – a web server, your first Arduino network program
        2. Developing web applications using Python
          1. Python web framework –
            1. Installing
            2. Your first Python web application
          2. Essential concepts for developing complex web applications
            1. Handling URLs
            2. The GET and POST methods
            3. Templates
            4. Forms
          3. Exercise 2 – playing with concepts using the Arduino serial interface
        3. RESTful web applications with Arduino and Python
          1. Designing REST-based Arduino applications
          2. Working with the GET request from Arduino
            1. The Arduino code to generate the GET request
            2. The HTTP server using to handle the GET request
          3. Working with the POST request from Arduino
            1. The Arduino code to generate the POST request
            2. The HTTP server using to handle the POST request
          4. Exercise 3 – a RESTful Arduino web application
            1. The Arduino sketch for the exercise
            2. The application to support REST requests
          5. Why do we need a resource-constrained messaging protocol?
        4. MQTT – A lightweight messaging protocol
          1. Introduction to MQTT
          2. Mosquitto – an open source MQTT broker
          3. Setting up Mosquitto
          4. Getting familiar with Mosquitto
        5. Getting started with MQTT on Arduino and Python
          1. MQTT on Arduino using the PubSubClient library
            1. Installing the PubSubClient library
            2. Developing the Arduino MQTT client
          2. MQTT on Python using paho-mqtt
            1. Installing paho-mqtt
            2. Using the paho-mqtt Python library
          3. Exercise 4 – MQTT Gateway for Arduino
            1. Developing Arduino as the MQTT client
            2. Developing the MQTT Gateway using Mosquitto
            3. Extending the MQTT Gateway using
            4. Testing your Mosquitto Gateway
        6. Summary
      16. 9. Arduino and the Internet of Things
        1. Getting started with the IoT
          1. Architecture of IoT web applications
          2. Hardware design
        2. The IoT cloud platforms
          1. Xively – a cloud platform for the IoT
            1. Setting up an account on Xively
            2. Working with Xively
          2. Alternative IoT platforms
            1. ThingSpeak
            2. Carriots
        3. Developing cloud applications using Python and Xively
          1. Interfacing Arduino with Xively
            1. Uploading Arduino data to Xively
            2. Downloading data to Arduino from Xively
            3. Advanced code to upload and download data using Arduino
          2. Python – uploading data to Xively
            1. The basic method for sending data
            2. Uploading data using a web interface based on
          3. Python – downloading data from Xively
            1. The basic method for retrieving data from Xively
            2. Retrieving data from the web interface
            3. Triggers – custom notifications from Xively
        4. Your own cloud platform for the IoT
          1. Getting familiar with the Amazon AWS platform
            1. Setting up an account on AWS
            2. Creating a virtual instance on the AWS EC2 service
            3. Logging into your virtual instance
          2. Creating an IoT platform on the EC2 instance
            1. Installing the necessary packages on AWS
            2. Configuring the security of the virtual instance
            3. Testing your cloud platform
              1. Testing the Mosquitto service
              2. Configuring and testing basic security
              3. Uploading and testing a project on the instance
        5. Summary
      17. 10. The Final Project – a Remote Home Monitoring System
        1. The design methodology for IoT projects
        2. Project overview
          1. The project goals
          2. The project requirements
          3. Designing system architecture
            1. The monitoring station
            2. The control center
            3. The cloud services
          4. Defining UX flow
          5. The list of required components
          6. Defining the project development stages
        3. Stage 1 – a monitoring station using Arduino
          1. Designing the monitoring station
          2. The Arduino sketch for the monitoring station
            1. Publishing sensor information
            2. Subscribing to actuator actions
            3. Programming an interrupt to handle the press of a button
          3. Testing
        4. Stage 2 – a control center using Python and the Raspberry Pi
          1. The control center architecture
          2. The Python code for the control center
            1. Creating the GUI using Tkinter
            2. Communicating with the Mosquitto broker
            3. Calculating the system's status and situation awareness
            4. Communicating with Xively
            5. Checking and updating the buzzer's status
          3. Testing the control center with the monitoring station
          4. Setting up the control center on the Raspberry Pi
        5. Stage 3 – a web application using Xively, Python, and Amazon cloud service
          1. Architecture of the cloud services
          2. Python web application hosted on Amazon AWS
          3. Testing the web application
        6. Testing and troubleshooting
        7. Extending your remote home monitoring system
          1. Utilizing multiple monitoring stations
          2. Extending sensory capabilities
          3. Improving UX
          4. Expanding cloud-based features
          5. Improving intelligence for situation awareness
          6. Creating an enclosure for hardware components
        8. Summary
      18. 11. Tweet-a-PowerStrip
        1. Project overview
          1. Project requirements
          2. System architecture
          3. Required hardware components
            1. Relays
            2. PowerSwitch Tail
          4. User experience flow
          5. Development and deployment stages
        2. Stage 1 – a smart power strip with Arduino and relays
          1. Hardware design
          2. The Arduino code
        3. Stage 2 – the Python code to process tweets
          1. Python software flow
          2. Setting up the Twitter application
          3. The Python code
        4. Testing and troubleshooting
        5. Extending the project with additional features
        6. Summary
      19. Index