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
Internet of Things Programming Projects
Internet of Things Programming Projects

Internet of Things Programming Projects: Build modern IoT solutions with the Raspberry Pi 3 and Python

eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Internet of Things Programming Projects

Writing Python Programs Using Raspberry Pi

In this chapter, we will start writing python programs with Raspberry Pi. Python is the official programming language for Raspberry Pi and is represented by the Pi in the name.

The following topics will be covered in this chapter:

  • Python tools for Raspberry Pi
  • Using the Python command line
  • Writing a simple Python program

Python comes pre-installed on Raspbian in two versions, versions 2.7.14 and 3.6.5 (as of this writing) representing Python 2 and Python 3, respectively. The differences between the two versions are beyond the scope of this book. We will use Python 3 in this book unless otherwise stated.

Project overview

In this project, we will become comfortable with Python development on Raspberry Pi. You may be used to development tools or Integrated Development Environments (IDEs) on other systems such as Windows, macOS, and Linux. In this chapter, we will get our feet wet in terms of using Raspberry Pi as a development machine. We will start off slowly with Python as we get our development juices flowing.

Technical requirements

The following is required to complete this project:

  • Raspberry Pi Model 3 (2015 model or newer)
  • USB power supply
  • Computer monitor
  • USB keyboard
  • USB mouse

Python tools for Raspberry Pi

The following are pre-installed tools that we may use for Python development on Raspberry Pi using Raspbian. This list is by no means the only tools that we may use for development.

The Terminal

As Python comes pre-installed with Raspbian, an easy way to launch it is to use the Terminal. As we can see in the following screenshot, the Python interpreter can be accessed by simply typing python as the command prompt in a Terminal window:

We may test it out by running the simplest of programs:

print 'hello'

Notice the Python version in the line after the command, 2.7.13. The python command in Raspbian is tied to Python 2. In order to access Python 3, we must type the python3 command in...

Using the Python command line

Let's start doing some coding. Whenever I start using a new operating system for development, I like to go through some basics just to get my mind back into it (I'm speaking particularly to those of us who are all too familiar with coding into the wee hours of the morning).

The simplest way to access Python is from the Terminal. We will run a simple program to get started. Load the Terminal from the main toolbar and type python3 at the prompt. Type the following line and hit Enter:

from datetime import datetime

This line loads the datetime object from the datetime module into our instance of Python. Next type the following and hit Enter:

print(datetime.now())

You should see the current date and time printed to the screen:

Let's try another example. Type the following into the shell:

import pyjokes

This is a library that's used...

Writing a simple Python program

We will write a simple Python program that contains a class. To facilitate this, we will use Thonny, a Python IDE that comes pre-installed with Raspbian and has excellent debug and variable introspection functionalities. You will find that its ease of use makes it ideal for the development of our projects.

Creating the class

We will begin our program by creating a class. A class may be seen as a template for creating objects. A class contains methods and variables. To create a class in Python with Thonny, do the following:

  1. Load Thonny through Application Menu | Programming | Thonny. Select New from the top left and type the following code:
class CurrentWeather:
weather_data={'Toronto...

Summary

We began this chapter by discussing the various tools that are available for Python development in Raspbian. The quickest and easiest way to run Python is from the Terminal window. Since Python comes pre-installed in Raspbian, the python command in the Terminal prompt loads Python (Python 2, in this case). There is no need to set environment variables in order to have the command find the program. Python 3 is run from the Terminal by typing python3.

We also took a brief look at IDLE, the default IDE for Python development. IDLE stands for Integrated Development and Learning Environment and is an excellent tool for beginners to use when learning Python.

Thonny is another Python IDE that comes pre-installed with Raspbian. Thonny has excellent debug and variable introspection functionalities. It too is designed for beginning Python developers, however, its ease of use and...

Questions

  1. Which operating systems is Thonny available for?
  2. How do we enter Python 2 from the Terminal command line?
  3. Which tool in Thonny do we use to view what is inside an object?
  4. Give two reasons as to why we are using an object in our weather example code.
  5. What is the advantage of adding a method called getCity to the CurrentWeather class?
  6. What language is IDLE written in?
  1. What are the two steps taken in order to print the current date and time?
  2. In our code, how did we compensate for wind speed directions that are represented by only one letter?
  3. What does the if __name__ =="__main__" statement do?
  4. What does IDLE stand for?

Further reading

Python 3 - Object Oriented Programming by Dusty Phillips, Packt Publishing.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Leverage the full potential of IoT with the combination of Raspberry Pi 3 and Python
  • Build complex Python-based applications with IoT
  • Work on various IoT projects and understand the basics of electronics

Description

The Internet of Things (IOT) has managed to attract the attention of researchers and tech enthusiasts, since it powerfully combines classical networks with instruments and devices. In Internet of Things Programming Projects, we unleash the power of Raspberry Pi and Python to create engaging projects. In the first part of the book, you’ll be introduced to the Raspberry Pi, learn how to set it up, and then jump right into Python programming. Then, you’ll dive into real-world computing by creating a“Hello World” app using flash LEDs. As you make your way through the chapters, you’ll go back to an age when analog needle meters ruled the world of data display. You’ll learn to retrieve weather data from a web service and display it on an analog needle meter, and build a home security system using the Raspberry Pi. The next project has a modern twist, where we employ the Raspberry Pi to send a signal to a web service that will send you a text when someone is at the door. In the final project, you take what you've learned from the previous two projects and create an IoT robot car that you can use to monitor what your pets are up to when you are away. By the end of this book, you will be well versed in almost every possible way to make your IoT projects stand out.

Who is this book for?

Internet of Things Programming Projects is for Python developers and programmers who are interested in building their own IoT applications and IoT-based projects. It is also targeted at IoT programmers and developers who are looking to build exciting projects with Python.

What you will learn

  • Install and set up a Raspberry Pi for IoT development
  • Learn how to use a servo motor as an analog needle meter to read data
  • Build a home security dashboard using an infrared motion detector
  • Communicate with a web service that sends you a message when the doorbell rings
  • Receive data and display it with an actuator connected to the Raspberry Pi
  • Build an IoT robot car that is controlled through the internet

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 31, 2018
Length: 436 pages
Edition : 1st
Language : English
ISBN-13 : 9781789134803
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Oct 31, 2018
Length: 436 pages
Edition : 1st
Language : English
ISBN-13 : 9781789134803
Category :
Languages :

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 107.97
Mastering Arduino
€32.99
Industrial Internet Application Development
€41.99
Internet of Things Programming Projects
€32.99
Total 107.97 Stars icon

Table of Contents

20 Chapters
Installing Raspbian on the Raspberry Pi Chevron down icon Chevron up icon
Writing Python Programs Using Raspberry Pi Chevron down icon Chevron up icon
Using the GPIO to Connect to the Outside World Chevron down icon Chevron up icon
Subscribing to Web Services Chevron down icon Chevron up icon
Controlling a Servo with Python Chevron down icon Chevron up icon
Working with the Servo Control Code to Control an Analog Device Chevron down icon Chevron up icon
Setting Up a Raspberry Pi Web Server Chevron down icon Chevron up icon
Reading Raspberry Pi GPIO Sensor Data Using Python Chevron down icon Chevron up icon
Building a Home Security Dashboard Chevron down icon Chevron up icon
Publishing to Web Services Chevron down icon Chevron up icon
Creating a Doorbell Button Using Bluetooth Chevron down icon Chevron up icon
Enhancing Our IoT Doorbell Chevron down icon Chevron up icon
Introducing the Raspberry Pi Robot Car Chevron down icon Chevron up icon
Controlling the Robot Car Using Python Chevron down icon Chevron up icon
Connecting Sensory Inputs from the Robot Car to the Web Chevron down icon Chevron up icon
Controlling the Robot Car with Web Service Calls Chevron down icon Chevron up icon
Building the JavaScript Client Chevron down icon Chevron up icon
Putting It All Together Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7
(3 Ratings)
5 star 66.7%
4 star 0%
3 star 0%
2 star 0%
1 star 33.3%
Peter Nov 21, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The best way to learn something is by doing! This book will show you real applications for internet enabled electro-mechanical devices and machines. This book was a great intro for me into the Raspberry Pi world and gave me an excellent foundation to implement my own inventions. This book will NOT teach you Python from scratch nor HTML/CSS from scratch but it does a damn good job of getting you coding these languages without a deep dive into code semantics/syntax. I learned heaps about the semantics/syntax just by working through the projects. Oh and yes, you'll have to buy all the sensors and actuators if you want to actually work these projects. Also, the "grand finale" is a robot car ... there's quite a few purchases required in order to implement this one. The author does a great job of informing you where to buy what you need though.
Amazon Verified review Amazon
Frostic7734 Jul 20, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am only on chapter 6 because I have been reading this book at my leisure. I wouldn't recommend this book as a first book for beginners but if you have a little experience with the Pi and Python then this book is great. It is easy to follow along and it has a lot of practical examples. I bought this book because the final chapters teach you how to build a robot car from scratch. I have not been disappointed so far. I would recommend this book to anyone who wants to learn to make their own projects because this book doesn't just teach you how to put together a few projects. Ultimately, it will teach you how to make some of your own projects from scratch.
Amazon Verified review Amazon
Kindle Customer Dec 11, 2020
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Did the author even test the code examples from his book? i.e. 'pip3 install weather-api' does not work. Module name is wrong. Unfortunately it seems it's too late to get my money back.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.