Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Practical Python Programming for IoT

You're reading from  Practical Python Programming for IoT

Product type Book
Published in Nov 2020
Publisher Packt
ISBN-13 9781838982461
Pages 516 pages
Edition 1st Edition
Languages
Author (1):
Gary Smart Gary Smart

Table of Contents (20) Chapters

Preface Section 1: Programming with Python and the Raspberry Pi
Setting Up your Development Environment Getting Started with Python and IoT Networking with RESTful APIs and Web Sockets Using Flask Networking with MQTT, Python, and the Mosquitto MQTT Broker Section 2: Practical Electronics for Interacting with the Physical World
Connecting Your Raspberry Pi to the Physical World Electronics 101 for the Software Engineer Section 3: IoT Playground - Practical Examples to Interact with the Physical World
Turning Things On and Off Lights, Indicators, and Displaying Information Measuring Temperature, Humidity, and Light Levels Movement with Servos, Motors, and Steppers Measuring Distance and Detecting Movement Advanced IoT Programming Concepts - Threads, AsyncIO, and Event Loops IoT Visualization and Automation Platforms Tying It All Together - An IoT Christmas Tree Assessments Other Books You May Enjoy
IoT Visualization and Automation Platforms

In the previous chapter, we looked at alternative approaches to structuring a Python program that interfaces with electronics. This included an event loop approach, two thread-based approaches showing the use of callbacks and a publisher-subscriber model, and an asynchronous I/O approach.

In this chapter, we will be discussing IoT and automation platforms that you can use with your Raspberry Pi. The terms IoT platform and automation platform are very broad concepts, so for the purpose of this chapter, what I mean by these terms is any software service cloud-based or locally installable that provides you with a ready-made ecosystem to create powerful, flexible, and fun IoT-based projects.

Our primary focus will be on the If-This-Then-That (IFTTT) automation platform, which I suspect many of you will have some familiarity...

Technical requirements

To perform the exercises in this chapter, you will need the following:

  • Raspberry Pi 4 Model B
  • Raspbian OS Buster (with a desktop and recommended software)
  • Python version 3.5 at a minimum

These requirements are what the code examples in this book are based on. It's reasonable to expect that the code examples should work without modification on a Raspberry Pi 3 Model B or a different version of Raspbian OS, as long as your Python version is 3.5 or higher.

You will find this chapter's source code in the chapter13 folder in this book's GitHub repository, which is available here: https://github.com/PacktPublishing/Practical-Python-Programming-for-IoT.

You will need to execute the following commands in a Terminal to set up a virtual environment and install the Python libraries required for the code in this chapter:

$ cd chapter13              # Change into this chapter's folder
$ python3 -m venv venv # Create Python Virtual...

Triggering an IFTTT Applet from your Raspberry Pi

Many of you may already be familiar with the If-This-Than-That (IFTTT) web service (ifttt.com), where you can create simple workflow automation chains called Applets. An Applet responds to changes in one web service (the This), which then triggers an action on another web service (the That).

Here are some common examples of Applet configurations (called Recipes):

  • Send yourself an email whenever a particular Twitter hashtag is published.
  • Turn a smart light bulb on or off at a certain time of the day.
  • Open your internet-connected garage door using your phone's GPS when you are approaching your house.
  • Log how long you spend in the office in a spreadsheet.
  • ...and thousands upon thousands of other examples!

As we will learn in this section and the next, our Raspberry Pi can assume the role of both the This or the That, to either trigger an Applet or perform an action in response to a triggered Applet.

The following...

Creating the temperature monitoring circuit

For this example, we will be reusing the DHT11/DHT22 temperature circuit we created in Chapter 9, Measuring Temperature, Humidity, and Light Levels.

Here is what we need to do:

  1. Build the circuit illustrated in Figure 9.2.
  2. Connect the data pin to GPIO 24 (in Chapter 9Measuring Temperature, Humidity, and Light Levels, we used GPIO 21, but we will use GPIO 21 for a LED later in this chapter).

Once you have built your circuit, we can continue and build our first IFTTT Applet.

Creating and configuring an IFTTT Applet

To create our IFTTT Applet, there are many steps that we need to follow. Many of these steps are simple and generic, irrespective of the type of Applet you are creating. While we will step through these generic steps, we won't go into them in great detail, as I am sure you will be more than capable of understanding what is going on during the process. Instead, what we will focus on is the unique steps and sections of IFTTT that relate to integrating our Raspberry Pi.

Please note that the https://ifttt.com/ free pricing tier limits the number of Applet that you can have active at once. At the time of writing, the maximum was three active Applets. We will be creating four Applets in this and the next chapter combined, so you will need to archive at least one Applet on IFTTT as you work through the next chapter in order to stay on the IFTTT free pricing tier.

Here are the steps we need to follow:

  1. Log into or create your IFTTT account. If...

Triggering an IFTTT Webhook

Now that we have created our IFTTT Applet, we need to take a few more steps to learn how to trigger our Webhook. These steps boil down to knowing where to navigate to within IFTTT to discover your unique Webhook URL.

Here are the steps we need to follow:

  1. First, we need to navigate to the Webhooks page. There are a couple of ways we can do this, and I'll leave it up to you which route you take:
    • Navigate your web browser to the Webhook services URL; that is, ifttt.com/maker_webhook.
    • Alternatively, the navigation steps to take to get to this web page are as follows:
      1. Click on the profile avatar icon (as shown previously in Figure 13.2).
      2. In the menu that appears, choose the My Services item (refer to Figure 13.3).
      3. On the page that appears, find and click on the Webhooks item.

Irrespective of the path you take, you will see the page shown in the following screenshot:

Figure 13.13 – The Webhooks page
  1. Click on the Documentation...

Triggering an IFTTT Applet in Python

We're about to explore a simple application based around the DHT 11/DHT 22 circuits and code we first saw in Chapter 9, Measuring Temperature, Humidity, and Light Levels. You can find this code in the chapter13/ifttt_dht_trigger_email.py file.

This code will monitor the temperature using a DHT 11 or DHT 22 sensor, and if a pre-configured high or low threshold is breached, the code will invoke your IFTTT Webhook URL, which will then send you an email, similar to the one shown in the following screenshot. This corresponds to the email subject and body text you configured in the previous section, in step 13:

Figure 13.15 – Example IFTTT email 

Before we can run our sample application code, there are a few configuration steps we need to perform. Let's take a look:

  1. Open the chapter13/ifttt_dht_trigger_email.py file for editing.
  2. Locate the following segment of code indicated by lines (1) and (2). Confirm that...

Actioning your Raspberry Pi from an IFTTT Applet

The previous section taught us how to trigger an IFTTT Applet from our Raspberry Pi. In this section, we will learn how to action our Raspberry Pi from an IFTTT Applet.

For our example, we will create an IFTTT Applet that will trigger when an email is received. We'll use the subject of this email to control an LED that is connected to a GPIO pin.

We will be using an IFTTT Webhook service, as we did previously, only this time the Webhook service will be installed on the That side of our Applet and will request a URL that we specify. This basic idea is illustrated in the following diagram:

Figure 13.16 – Raspberry Pi assuming the That role in an IFTTT Applet

Let's look at two possible methods we can use with the IFTTT Webhook service to request a URL that can then be seen by our Raspberry Pi's Python code.

Method 1 – using the dweet.io service as an intermediary

One method to integrate the IFTTT with our Raspberry Pi is to use the dweet.io service. We covered dweet.io, along with Python examples, in Chapter 2, Getting Started with Python and IoT.

In brief, here is how we will use dweet.io alongside IFTTT and our Python code:

  1. In our IFTTT Webhook, we'll use a dweet.io URL to publish a dweet (containing an instruction to turn the LED on, off, or make it blink).
  2. Our Raspberry Pi will run Python code to retrieve the dweet published by the IFTTT Webhook.
  3. Our code will then control the LED based on the command specified in the dweet.

This is the method we will use for our example. The advantage of this method is that we do not need to worry about configuring firewalls and port forwarding rules on your router. Plus, it means we can run the example in environments for example, at work where router configurations may not be practical or even possible...

Method 2 creating a Flask-RESTful service

To use this method, we would need to create a RESTful service, similar to what we did in Chapter 3, Networking with RESTful APIs and Web Socket Services Using Flask (the code that can be found in chapter02/flask_api_server.py, which changes the brightness of a LED (rather than setting it to on/off/blinking), would be a great starting point).

We would also need to expose our Raspberry Pi to the public internet, which would require us to open a port and create a port forwarding rule in our local firewall or router. Then, together with our public IP (or domain name), we could construct a URL and use this directly with the IFTTT Webhook service.

For prototyping ideas and creating demos, a simple alternative to opening up firewalls and creating port forwarding rules could be to use a service such as Local Tunnels (localtunnel.github.io/www) or ngrok (ngrok.com), which can help you expose a device to the internet.

Since this...

Creating the LED circuit

Our forthcoming example will require an LED, as well as a series resistor connected to a GPIO pin (GPIO 21, for our example). I'm confident that, given the number of times we've built LED circuits already in this book, you could wire this up on your own with no problems! (And in case you do need a reminder, see Figure 2.7 in Chapter 2, Getting Started with Python and IoT.)

Keep the DHT 11/DHT 22 circuit you created for our first IFTTT Applet example because we will reuse this circuit again later in this chapter.

When you have your circuit ready, we will continue and run our sample program.

Running the IFTTT and LED Python program

In this section, we will run our program and obtain a unique thing name and URL for use with the dweet.io service.

Here are the steps to follow:

  1. Run the code that can be found in the chapter13/dweet_led.py file in a Terminal. You will receive an output similar to the following (your thing name and therefore your URLs will be different):
(venv) $ python dweet_led.py
INFO:main:Created new thing name 749b5e60
LED Control URLs - Try them in your web browser:
On : https://dweet.io/dweet/for/749b5e60?state=on
Off : https://dweet.io/dweet/for/749b5e60?state=off
Blink : https://dweet.io/dweet/for/749b5e60?state=blink

As we mentioned previously, chapter13/dweet_led.py is an exact copy of the same program we discussed in Chapter 2, Getting Started with Python and IoT. If you need more context around how this program works, please revisit that chapter and the code discussion contained therein.

  1. Keep your Terminal open with the program running...

Creating the IFTTT Applet

We are about to create another IFTTT Applet. The overall process is very similar to the one we followed for the Applet we created previously, except our Raspberry Pi (via Webhook integration) will be at the That end of the Applet, as shown in Figure 13.16.

Here are the steps we need to follow to create our next Applet. I've left out many of the common screenshots this time around due to their similarity with our previous IFTTT Applet creation process:

  1. Once logged into IFTTT, click on your profile avatar icon and select Create from the drop-down menu.
  2. On the If + This Then Than page, press the + icon.
  3. In the Choose a service page, search for and select the Email service.
  4. On the Choose trigger page, select Send IFTTT an email tagged (make sure it's the option with the word in it tagged).
  5. On the next page, enter LED as the Tag input and click the Create trigger button:

Figure 13.17 – The Complete...

Controlling the LED from an email

Now that we have created our Applet to control our LED using an email, it's time to test out the integration. 

Here are the steps to create the email:

  1. Make sure the program in the chapter13/dweet_led.py file is still running in your Terminal.
  2. Open your favorite email program and create a new email.
  3. Use trigger@applet.ifttt.com as the email's To address.
When sending a trigger email to IFTTT, it must come from the same email address that you use with IFTTT (you can visit https://ifttt.com/settings to check your email address).
  1. As the subject, use one of the following to control the LED:
  • #LED On
  • #LED Off
  • #LED Blink
IFTTT strips off the #LED tag, so our dweet_led.py program only receives the text On, Off, or Blink. The leading space is stripped off in our Python code.

The following screenshot shows an example email that will make the LED blink:

13.20 – Trigger email example
  1. Send the email.
  2. Wait...

IFTTT troubleshooting

If your IFTTT Applets do not appear to be triggering and actioning, here are a few troubleshooting avenues for you to explore and try:

  • In dweet_led.py, try the following:
    • Turn on debug logging; for example, logger.setLevel(logging.DEBUG).
    • Change the dweet retrieval method located near the end of the source file. If you are using stream_dweets_forever(), try poll_dweets_forever() instead, since it is more resilient to transient connectivity issues.
  • On the IFTTT website, you can inspect the activity log for any Applet by doing the following:
    1. Navigating to the My Services option under the profile menu
    2. Selecting a service (for example, Webhooks)
    3. Selecting the Applet you want to inspect
    4. Clicking the Settings button
    5. Clicking the View activity button and/or trying the Check now button
  • You can also check the following IFTTT resources:

Visualizing data with the ThingSpeak platform

We have just learned how to create simple automation using the IFTTT platform. In this section, we will integrate with the ThingSpeak platform to visually display temperature and humidity data that we'll collect using our DHT 11 or DHT 22 sensors. We will be using the DHT 11/DHT 22 circuit we created earlier in this chapter.

ThingSpeak (thingspeak.com) is a data visualization, aggregation, and analysis platform. We will be focusing on the data visualization aspect, and specifically on how to integrate our Raspberry Pi into this platform. 

I've chosen ThingSpeak for our example in this section for a couple of reasons – it's simple and easy to set up and integrate with, and for simple data visualizations like the ones we will be doing, it's free. There are many other visualization platforms available, and they all have their own unique features, pricing structures, and complexities. I've included a few...

Configuring the ThinkSpeak platform

Configuring ThinkSpeak is relatively simple in fact, it's one of the most straightforward platforms in its class I have come across. Here are the steps that we need to follow:

  1. First, you will need to create a ThingSpeak account for yourself. Visit their website, thingspeak.com, and click on the Sign Up button.
  2. Once you have created your ThinkSpeak account and logged into the platform, you should land on the My Channels page; that is, https://thingspeak.com/channels.
In the ThingSpeak ecosystem, a channel is a virtual place where we store our data, dashboards, and visualizations. It's analogous to a workspace. 
  1. Next, we need to create a new channel by clicking on the New Channel button:

Figure 13.22 – ThingSpeak channel configuration

On the New Channel page, enter the following details:

    • NameEnvironmental Data (or any name of your choice)
    • Field1: temperature
    • Field2: humidity

You...

Configuring and running the ThinkSpeak Python program

We have provided two sample programs that integrate with ThinkSpeak. They are as follows:

  • chapter13/thingspeak_dht_mqtt.py: An example that uses MQTT to send data into a ThinkSpeak channel.

  • chapter13/thingspeak_dht_http.py: An example that uses the Python requests library to make RESTful API calls that send data to a ThinkSpeak channel.

The core concepts of these two programs were discussed in earlier chapters. For your reference, they are as follows:

  • MQTT: We discussed the Paho-MQTT library in Chapter 4, Networking with MQTT, Python, and the Mosquitto MQTT BrokerA key difference for this chapter is that we are using the Paho-MQTT simplifying client wrapper to publish MQTT messages instead of a full life cycle example.
  • We covered RESTful APIs and the requests library in Chapter 2, Getting Started with Python and IoT.
  • The code related to the DHT 11/DHT 22 temperature and humidity sensor was covered in Chapter...

Other IoT and automation platforms for further exploration

So far in this chapter, we have seen IFTTT and ThingSpeak in action, as well as how to integrate them with our Raspberry Pi. We saw how to use IFTTT to create simple workflows and how we can visualize data with ThingSpeak two very different ideas, but nonetheless, they are both IoT platforms.

Both these platforms are immensely powerful and offer a wide range of features and possibilities beyond what we can cover in a single chapter, so I do encourage you to seek out their documentation and examples to advance your learning.

There are many other IoT platforms, applications, and frameworks that are available. This section will provide a short, curated list based on my experience. They all fit in nicely with this book's Python- and Raspberry Pi-based themes.

Zapier

We've already seen IFFF in action. IFTTT is more consumer-focused in terms of the services that it supports, plus as we have seen, we are limited to a single This trigger and a single That action.

Zappier is very similar in principle to IFTTT, but with a more business-orientated focus, including a range of services and integrations not available with IFTTT (there will be services and integrations that are unique to IFTTT also). Furthermore, Zapier is also capable of much more complex workflows for triggering events and actions.

You will find it relatively simple to reimplement our two IFTTT examples from this chapter in Zappier.

Website: https://zapier.com.

IFTTT platform

In this chapter, we used IFTTT as an end user and performed our integrations using Webhooks. If you are a business wishing to create gadgets you want to expose as first-class IFTTT services, then you should check out the IFTTT platform.

Website: https://platform.ifttt.com.

ThingsBoard IoT platform

ThingsBoard is an open source IoT platform that you can download and host on your Raspberry Pi. On the surface, it will allow you to build dashboards and data visualizations, just as we did in ThingSpeak. Compared to ThingSpeak, you will find that ThingsBoard has a steeper learning curve when it comes to creating your first dashboard; however, you will also find that it offers a more extensive set of widgets and customization options. Plus, unlike ThingSpeak, which can only consume data, ThingsBoard allows you to embed controls into a dashboard that lets you interact with your Raspberry Pi using MQTT.

From experience, working your way through the ThingsBoard documentation and tutorials (many are available as videos) is a must if you want to learn how to use this platform since on your first visit to its UI, it's not immediately obvious what you need to do.

Here are a few specific resources from their website:

  • Raspberry Pi installation instructions:...

Home Assistant

Home Assistant is a pure Python home automation suite. Out of the box, Home Assistant can connect with a wide range of internet-enabled devices such as lights, doors, fridges, and coffee machines to mention only a few.

Home Assistant gets a mention here, not only because it is built with Python, but because it allows us to integrate directly with the host Raspberry Pi's GPIO pins, as well as with a remote Raspberry Pi's GPIO pins using PiGPIO's remote GPIO feature. Plus, there are MQTT and RESTful API integration options available.

While simple in concept and end user operation, there is a highish learning curve (and a fair amount of experimentation needed) when it comes to configuring Home Assistant since most of the integrations are performed by manually editing YAML Ain't Markup Language (YAML) files directly.

In relation to GPIO integrations, I have selected some resources from their website to get you started. I recommend reading...

Amazon Web Services (AWS)

Another suggestion is Amazon Web Services, specifically two services IoT Core and Elastic Beanstalk. These options will provide you with immense flexibility and a near-endless number of options when it comes to creating IoT applications. IoT Core is Amazon's IoT platform where you can create dashboards, workflows, and integrations, while Elastic Beanstalk is their cloud platform where you can host your own programs including Python – in the cloud.

Amazon Web Services is an advanced development platform, so you will need to invest weeks into learning how it works and how to build and deploy applications using it, but I can promise you that you will learn a lot during the process! Plus, their documentation and tutorials are very high quality.

Amazon IoT Core: https://aws.amazon.com/iot-core.

Amazon Elastic Beanstalk: https://aws.amazon.com/elasticbeanstalk.

Microsoft Azure, IBM Watson, and Google Cloud

Finally, I do want to mention these other IT giants, who all offer their own cloud and IoT platforms. My suggestion regarding AWS is purely due to my more in-depth experience with this platform. The comparative platforms offered by Microsoft, IBM, and Google are also high quality and backed with excellent documentation and tutorials, so if your personal preference is with one of these providers, you are still in good hands.

Summary

In this chapter, we explored and learned how to use our Raspberry Pi with both the IFTTT and ThinkSpeak IoT platforms. We created two IFTTT examples where our Raspberry Pi performed the This role in an IFTTT Applet to start an IFTTT workflow. We also saw how to use our Raspberry Pi in the That role so that it can be actioned by an IFTTT Applet. Next, we covered an example of how to integrate with the ThinkSpeak IoT platform to visualize temperature and humidity data collected by our Raspberry Pi. Finally, we discussed other IoT platform options that you may like to investigate and experiment with.

We certainly only covered the basics of what is possible with visualization and automation platforms in this chapter. I encourage you to seek our further IFTTT examples and ideas you can experiment with, and also explore the other platforms that we mentioned. And remember, while every platform will be different and have its own integration considerations, the commonly accepted standards...

Questions

As we conclude this chapter, here is a list of questions for you to test your knowledge regarding this chapter's material. You will find the answers in the Assessments section of the Appendix:

  1. With our first IFTTT Applet, where we monitored the temperature, why did we use a different high and low temperature value to trigger our Applet and send an email?
  2. What was the advantage of using an intermediary service such as dweet.io with our IFTTT Webhook service?
  3. What are some of the core differences between IFTTT and Zapier?
  4. Can you control your Raspberry Pi from a ThingSpeak dashboard?
  5. In relation to data, what is the limitation of the IFTTT Webhook service when used as an action (that is, on the That side of an applet)?
  6. You want to prototype the switching on and off of an over-the-counter smart light bulb based on a Raspberry Pi's GPIO pin state. What platforms could you use?
lock icon The rest of the chapter is locked
You have been reading a chapter from
Practical Python Programming for IoT
Published in: Nov 2020 Publisher: Packt ISBN-13: 9781838982461
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}