Python Programming for Arduino Table of Contents Python Programming for Arduino Credits About the Author About the Reviewers www.PacktPub.com Support files, eBooks, discount offers, and more Why subscribe? Free access for Packt account holders Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Downloading the color images of this book Errata Piracy Questions Getting Started with Python and Arduino Introduction to Python Why we use Python When do we use other languages Installing Python and Setuptools Installing Python Linux Ubuntu Fedora and Red Hat Windows Mac OS X Installing Setuptools Linux Windows Mac OS X Installing pip Installing Python packages The fundamentals of Python programming Python operators and built-in types Operators Built-in types Data structures Lists Tuples Sets Dictionaries Controlling the flow of your program The if statement The for statement The while statement Built-in functions Conversions Math operations String operations Introduction to Arduino History Why Arduino? Arduino variants The Arduino Uno board Installing the Arduino IDE Linux Mac OS X Windows Getting started with the Arduino IDE What is an Arduino sketch? Working with libraries Using Arduino examples Compiling and uploading sketches Using the Serial Monitor window Introduction to Arduino programming Comments Variables Constants Data types Conversions Functions and statements The setup() function The loop() function The pinMode() function Working with pins Statements Summary Working with the Firmata Protocol and the pySerial Library Connecting the Arduino board Linux Mac OS X Windows Troubleshooting Introducing the Firmata protocol What is Firmata? Uploading a Firmata sketch to the Arduino board Testing the Firmata protocol Getting started with pySerial Installing pySerial Playing with a pySerial example Bridging pySerial and Firmata Summary The First Project – Motion-triggered LEDs Motion-triggered LEDs – the project description The project goal The list of components The software flow design The hardware system design Introducing Fritzing – a hardware prototyping software Working with the breadboard Designing the hardware prototype Testing hardware connections Method 1 – using a standalone Arduino sketch The project setup The Arduino sketch The setup() function The loop() function Working with custom Arduino functions Testing Troubleshooting Method 2 – using Python and Firmata The project setup Working with Python executable files The Python code Working with pyFirmata methods Working with Python functions Testing Troubleshooting Summary Diving into Python-Arduino Prototyping Prototyping Working with pyFirmata methods Setting up the Arduino board Configuring Arduino pins The direct method Assigning pin modes Working with pins Reporting data Manual operations The write() method The read() method Additional functions Upcoming functions Prototyping templates using Firmata Potentiometer – continuous observation from an analog input Connections The Python code Buzzer – generating sound alarm pattern Connections The Python code DC motor – controlling motor speed using PWM Connections The Python code LED – controlling LED brightness using PWM Connections The Python code Servomotor – moving the motor to a certain angle Connections The Python code Prototyping with the I2C protocol Arduino examples for I2C interfacing Arduino coding for the TMP102 temperature sensor Arduino coding for the BH1750 light sensor PyMata for quick I2C prototyping Interfacing TMP102 using PyMata Interfacing BH1750 using PyMata Useful pySerial commands Connecting with the serial port Reading a line from the port Flushing the port to avoid buffer overflow Closing the port Summary Working with the Python GUI Learning Tkinter for GUI design Your first Python GUI program The root widget Tk() and the top-level methods The Label() widget The Pack geometry manager The Button() widget – interfacing GUI with Arduino and LEDs The Entry() widget – providing manual user inputs The Scale() widget – adjusting the brightness of an LED The Grid geometry manager The Checkbutton() widget – selecting LEDs The Label() widget – monitoring I/O pins Remaking your first Python-Arduino project with a GUI Summary Storing and Plotting Arduino Data Working with files in Python The open() method The write() method The close() method The read() method The with statement – Python context manager Using CSV files to store data Storing Arduino data in a CSV file Getting started with matplotlib Configuring matplotlib on Windows Configuring matplotlib on Mac OS X Upgrading matplotlib Troubleshooting installation errors Setting up matplotlib on Ubuntu Plotting random numbers using matplotlib Plotting data from a CSV file Plotting real-time Arduino data Integrating plots in the Tkinter window Summary The Midterm Project – a Portable DIY Thermostat Thermostat – the project description Project background Project goals and stages The list of required components Hardware design Software flow for user experience design Stage 1 – prototyping the thermostat The Arduino sketch for the thermostat Interfacing the temperature sensor Interfacing the humidity sensor Interfacing the light sensor Using Arduino interrupts Designing the GUI and plot in Python P Pack geometry manager about / The Pack geometry manager paho-mqtt library about / MQTT on Python using paho-mqtt installing / Installing paho-mqtt using / Using the paho-mqtt Python library passive infrared (PIR) sensor about / The project goal using / The list of components URL / The list of components PEP-8 URL / Operators physical systems / Getting started with the IoT pinMode() function about / The pinMode() function pip installing / Installing pip plot() function about / Plotting random numbers using matplotlib portable TFT LCD display using / Using a portable TFT LCD display with the Raspberry Pi connecting, GPIO used / Connecting the TFT LCD using GPIO configuring, with Raspberry Pi OS / Configuring the TFT LCD with the Raspberry Pi OS GUI, optimizing / Optimizing the GUI for the TFT LCD screen potentiometer connections / Connections Python code / The Python code PowerSwitch Tail URL / PowerSwitch Tail Processing about / Introduction to Arduino programming protocols about / Networking fundamentals prototyping about / Prototyping prototyping, thermostat about / Stage 1 – prototyping the thermostat Arduino sketch / The Arduino sketch for the thermostat temperature sensor, interfacing / Interfacing the temperature sensor humidity sensor, interfacing / Interfacing the humidity sensor light sensor, interfacing / Interfacing the light sensor troubleshooting / Troubleshooting prototyping, with I2C protocol about / Prototyping with the I2C protocol Arduino examples / Arduino examples for I2C interfacing TMP102 temperature sensor, using Arduino / Arduino coding for the TMP102 temperature sensor BH1750 light sensor, using Arduino / Arduino coding for the BH1750 light sensor PyMata library, using / PyMata for quick I2C prototyping TMP102 temperature sensor, using PyMata library / Interfacing TMP102 using PyMata BH1750 light sensor, using PyMata library / Interfacing BH1750 using PyMata pySerial commands, using / Useful pySerial commands prototyping templates, using Firmata about / Prototyping templates using Firmata potentiometer / Potentiometer – continuous observation from an analog input buzzer, using / Buzzer – generating sound alarm pattern DC motor, using / DC motor – controlling motor speed using PWM LED / LED – controlling LED brightness using PWM servomotors, using / Servomotor – moving the motor to a certain angle PubSubClient library using / MQTT on Arduino using the PubSubClient library installing / Installing the PubSubClient library URL / Installing the PubSubClient library pulse-width modulation (PWM) about / The Arduino Uno board push button switch using / The list of required components pyFirmata methods working with / Working with pyFirmata methods used, for setting up Arduino board / Setting up the Arduino board used, for configuring Arduino pins / Configuring Arduino pins used, for working with Arduino pins / Working with pins servo_config(pin,min_pulse=544,max_pulse=2400,angle=0) / Additional functions pass_time(seconds) / Additional functions get_firmata_version() / Additional functions exit() / Additional functions pulseIn/pulseOut / Upcoming functions shiftIn/shiftOut / Upcoming functions PyPI URL / Why we use Python, Installing Python packages about / Why we use Python pyplot framework about / Plotting random numbers using matplotlib figure() function / Plotting random numbers using matplotlib show() method / Plotting random numbers using matplotlib pySerial commands using / Useful pySerial commands used, for connecting with serial port / Connecting with the serial port used, for reading a line from port / Reading a line from the port used, for flushing port to avoid buffer overflow / Flushing the port to avoid buffer overflow used, for closing port / Closing the port pySerial library about / Getting started with pySerial installing / Installing pySerial URL / Installing pySerial example / Playing with a pySerial example and Firmata, bridging / Bridging pySerial and Firmata Python about / Introduction to Python benefits / Why we use Python usage considerations / When do we use other languages URL / When do we use other languages installing / Installing Python and Setuptools installing, on Linux / Linux installing, on Ubuntu / Ubuntu installing, on Fedora/Red Hat Linux / Fedora and Red Hat installing, on Windows / Windows URL, for downloading / Windows, Mac OS X installing, on Mac OS X / Mac OS X pip, installing / Installing pip URL, for documentation / Controlling the flow of your program Python code, Tweet-a-PowerStrip about / The Python code Python context manager reference link / The with statement – Python context manager Python data, downloading to Xively about / Python – downloading data from Xively basic method, for retrieving data / The basic method for retrieving data from Xively data retrieving, from web.py web interface / Retrieving data from the web.py web interface custom notifications, from Xively / Triggers – custom notifications from Xively triggers / Triggers – custom notifications from Xively Python data, uploading to Xively about / Python – uploading data to Xively basic method, for sending data / The basic method for sending data web interface used / Uploading data using a web interface based on web.py Python executable files using / Working with Python executable files Python functions using / Working with Python functions def keyword / Working with Python functions Python GUI Tkinter / Learning Tkinter for GUI design first program / Your first Python GUI program Python-Arduino project, remaking / Remaking your first Python-Arduino project with a GUI Python packages installing / Installing Python packages installing, $ pip install *PackageName>=version* command used / Installing Python packages Python programming fundamentals / The fundamentals of Python programming operators / Python operators and built-in types, Operators built-in types / Python operators and built-in types, Built-in types comments / Python operators and built-in types program flow, controlling / Controlling the flow of your program if statement / The if statement for statement / The for statement while statement / The while statement Python software flow, Tweet-a-PowerStrip about / Python software flow Python threading library URL / Using the paho-mqtt Python library Python tutorials URL / The fundamentals of Python programming, The while statement R Radiobutton() widget about / The Checkbutton() widget – selecting LEDs URL / The Checkbutton() widget – selecting LEDs Raspberry Pi about / What is a Raspberry Pi? Raspbian / What is a Raspberry Pi? versions / What is a Raspberry Pi? configuring / Installing the operating system and configuring the Raspberry Pi operating system, installing / Installing the operating system and configuring the Raspberry Pi hardware components / What do you need to begin using the Raspberry Pi? URL / What do you need to begin using the Raspberry Pi?, Preparing an SD card SD card, preparing / Preparing an SD card setup process / The Raspberry Pi setup process read() method using / The read() method real-time Arduino data plotting / Plotting real-time Arduino data remote home monitoring system project overview / Project overview project goals / The project goals project requirements / The project requirements system architecture, designing / Designing system architecture UX flow, defining / Defining UX flow hardware components / The list of required components development stages, defining / Defining the project development stages monitoring station, Arduino used / Stage 1 – a monitoring station using Arduino testing / Testing, Testing and troubleshooting control center, using / Stage 2 – a control center using Python and the Raspberry Pi web application / Stage 3 – a web application using Xively, Python, and Amazon cloud service troubleshooting / Testing and troubleshooting extending / Extending your remote home monitoring system multiple monitoring stations, utilizing / Utilizing multiple monitoring stations sensory capabilities, extending / Extending sensory capabilities UX, improving / Improving UX cloud-based features, expanding / Expanding cloud-based features improved intelligence, for situation awareness / Improving intelligence for situation awareness hardware enclosures, creating / Creating an enclosure for hardware components Representation State Transfer (REST) about / Python web framework – web.py RESTful web applications developing, with Arduino and Python / RESTful web applications with Arduino and Python designing / Designing REST-based Arduino applications GET request, implementing / Working with the GET request from Arduino GET request, generating / The Arduino code to generate the GET request GET request, handling with web.py / The HTTP server using web.py to handle the GET request POST request, implementing / Working with the POST request from Arduino POST request, generating / The Arduino code to generate the POST request POST request, handling with web.py / The HTTP server using web.py to handle the POST request architecture / Exercise 3 – a RESTful Arduino web application Arduino sketch / The Arduino sketch for the exercise web.py web application / The web.py application to support REST requests resource-constrained messaging protocol, using / Why do we need a resourceconstrained messaging protocol? S Scale() widget about / Learning Tkinter for GUI design, The Scale() widget – adjusting the brightness of an LED used, for adjusting brightness of LED / The Scale() widget – adjusting the brightness of an LED SD card reference link / What do you need to begin using the Raspberry Pi?, Preparing an SD card preparing / Preparing an SD card preparing, from Windows / Preparing an SD card preparing, from Mac OS X / Preparing an SD card preparing, from Ubuntu Linux / Preparing an SD card Secure Shell (SSH) protocol / Logging into your virtual instance Serial Clock Line (SCL) about / Prototyping with the I2C protocol Serial Data Line (SDA) about / Prototyping with the I2C protocol serial monitor using / Using the Serial Monitor window serial peripheral interface (SPI) about / Prototyping Server class about / The Server class servomotors using / Servomotor – moving the motor to a certain angle connections / Connections Python code / The Python code setup() function using / The setup() function Setuptools installing / Installing Setuptools about / Installing Setuptools installing, on Linux / Linux installing, on Windows / Windows installing, on Mac OS X / Mac OS X sketch about / What is an Arduino sketch? compiling / Compiling and uploading sketches uploading / Compiling and uploading sketches sketchbook about / What is an Arduino sketch? slicing about / Lists StandardFirmata firmware using / Prototyping statements about / Functions and statements, Statements subnetwork/subnet about / Exercise 1 – a web server, your first Arduino network program reference link / Exercise 1 – a web server, your first Arduino network program system architecture, remote home monitoring system designing / Designing system architecture monitoring station / The monitoring station control center / The control center cloud services / The cloud services system architecture, Tweet-a-PowerStrip about / System architecture T Templetor about / Templates URL / Templates thermostat building / Thermostat – the project description project description / Thermostat – the project description project background / Project background project stages / Project goals and stages project goals / Project goals and stages required components, identifying / The list of required components hardware design / Hardware design software flow, for user experience design / Software flow for user experience design prototyping / Stage 1 – prototyping the thermostat GUI, designing / Designing the GUI and plot in Python plot, designing / Designing the GUI and plot in Python deploying, Raspberry Pi used / Stage 2 – using a Raspberry Pi for the deployable thermostat thermostat, prototyping Arduino interrupts, using / Using Arduino interrupts thermostat, using Raspberry Pi deploying / Stage 2 – using a Raspberry Pi for the deployable thermostat portable TFT LCD display, using / Using a portable TFT LCD display with the Raspberry Pi TFT LCD connection, using GPIO / Connecting the TFT LCD using GPIO TFT LCD, configuring / Configuring the TFT LCD with the Raspberry Pi OS GUI, optimizing for TFT LCD screen / Optimizing the GUI for the TFT LCD screen troubleshooting / Troubleshooting thin-film transistor liquid-crystal display (TFT LCD) about / Hardware design ThingSpeak about / ThingSpeak Tk() widget about / Learning Tkinter for GUI design, The root widget Tk() and the top-level methods Tkinter about / Learning Tkinter for GUI design Pack geometry manager / The Pack geometry manager Grid geometry manager / The Grid geometry manager plots, integrating / Integrating plots in the Tkinter window Tkinter, widgets Tk() / Learning Tkinter for GUI design Label() / Learning Tkinter for GUI design Button() / Learning Tkinter for GUI design Entry() / Learning Tkinter for GUI design Scale() / Learning Tkinter for GUI design Checkbox() / Learning Tkinter for GUI design Tkinter class about / The Label() widget – monitoring I/O pins BooleanVar() method / The Label() widget – monitoring I/O pins update_idletasks method / The Label() widget – monitoring I/O pins update method / The Label() widget – monitoring I/O pins TMP102 temperature sensor interfacing, Arduino used / Arduino coding for the TMP102 temperature sensor interfacing, PyMata library used / Interfacing TMP102 using PyMata transistor terminals reference link / Connections troubleshooting Tweet-a-PowerStrip / Testing and troubleshooting troubleshooting, Arduino board connection about / Troubleshooting Tweet-a-PowerStrip project overview / Project overview project requirements / Project requirements system architecture / System architecture hardware components / Required hardware components user experience flow / User experience flow development stage / Development and deployment stages deployment stage / Development and deployment stages smart power strip with Arduino / Stage 1 – a smart power strip with Arduino and relays hardware design / Hardware design Arduino code / The Arduino code Python code / Stage 2 – the Python code to process tweets, The Python code Python software flow / Python software flow Twitter application, setting up / Setting up the Twitter application testing / Testing and troubleshooting troubleshooting / Testing and troubleshooting multiple features, adding / Extending the project with additional features Twitter application, Tweet-a-PowerStrip setting up / Setting up the Twitter application U Ubuntu Python, installing / Ubuntu matplotlib, setting up / Setting up matplotlib on Ubuntu Ubuntu Linux SD card, preparing / Preparing an SD card Universal Serial Bus (USB) about / Using the Serial Monitor window Uno board about / The Arduino Uno board user experience (UX) flow, Tweet-a-PowerStrip about / User experience flow V variables about / Variables virtual instance, on AWS EC2 service creating / Creating a virtual instance on the AWS EC2 service logging into / Logging into your virtual instance void data type about / Data types W web.py used, for developing web applications / Python web framework – web.py installing / Installing web.py basic concepts / Essential web.py concepts for developing complex web applications URL, handling / Handling URLs GET methods / The GET and POST methods POST methods / The GET and POST methods templates / Templates forms / Forms with Arduino serial interface / Exercise 2 – playing with web.py concepts using the Arduino serial interface web application, remote home monitoring system about / Stage 3 – a web application using Xively, Python, and Amazon cloud service architecture / Architecture of the cloud services Python web application, hosted on Amazon AWS / Python web application hosted on Amazon AWS testing / Testing the web application, Testing and troubleshooting web applications developing, with Python / Developing web applications using Python developing, web.py used / Python web framework – web.py implementing, web.py used / Your first Python web application while statement about / The while statement wide area network (WAN) about / Networking fundamentals Windows Python, installing / Windows Setuptools, installing / Windows Arduino IDE, installing / Windows Arduino board connection, establishing / Windows matplotlib, configuring / Configuring matplotlib on Windows SD card, preparing / Preparing an SD card IP address, obtaining / Windows Wire library about / Prototyping with the I2C protocol URL / Prototyping with the I2C protocol Wiring about / Introduction to Arduino programming with statement using / The with statement – Python context manager World Wide Web (WWW) about / RESTful web applications with Arduino and Python write() method used, for working with files / The write() method X Xively / Architecture of the cloud services Xively, IoT cloud platforms about / Xively – a cloud platform for the IoT account, setting up / Setting up an account on Xively working with / Working with Xively Adruino, interfacing with / Interfacing Arduino with Xively ... Python Programming for Arduino Table of Contents Python Programming for Arduino Credits About the Author About the Reviewers www.PacktPub.com... Testing and troubleshooting Extending the project with additional features Summary Index Python Programming for Arduino Python Programming for Arduino Copyright © 2015 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system,... Connections The Python code Servomotor – moving the motor to a certain angle Connections The Python code Prototyping with the I2C protocol Arduino examples for I2C interfacing Arduino coding for the TMP102 temperature sensor