# PyPortal Voice Controlled Smart Switch and Time Display

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/085/595/medium800/adafruit_io_IMG_20191125_193404.jpg?1575901936)

How would you like a PyPortal that nicely displays the current date and time? How about using a PyPortal's touch screen to turn on and off lights or appliances? Or, how about a PyPortal that is internet connected so you can turn on and off lights or appliances using your smartphone? Even still, how about a PyPortal that can be controlled using your voice with Alexa or Google Assistant to turn on and off lights or appliances? What would you think of a PyPortal that does all of this in a single device (the the ghost of Steve Jobs is speaking)? Yes, with this project you can do all of this with a PyPortal!

Best of all, this project has no soldering - you only need to plug in a cable from the PyPortal to the IoT relay!

There are several secrets to this magic. The IoT relay module allows us to control appliances using a PyPortal quite easily without worrying about high voltage wiring. Another secret is the use of the PyPortal ESP32 WiFi&nbsp;coprocessor, which we use to connect to the internet and&nbsp;Adafruit IO. We then use the site IFTTT.com ( you can remember it as " **IF** &nbsp; **T** his **T** hen **T** hat") to integrate Adafruit IO with Amazon's Alexa and Google Assistant. The PyPortal touch screen allows us to turn the light on or off simply by touching the screen. As a bonus, the display dims itself in a dark room to be less obtrusive.

## Parts
### Adafruit PyPortal - CircuitPython Powered Internet Display

[Adafruit PyPortal - CircuitPython Powered Internet Display](https://www.adafruit.com/product/4116)
 **PyPortal** , our easy-to-use IoT device that allows you to create all the things for the “Internet of Things” in minutes. Make custom touch screen interface GUIs, all open-source, and Python-powered using&nbsp;tinyJSON / APIs to get news, stock, weather, cat photos,...

In Stock
[Buy Now](https://www.adafruit.com/product/4116)
[Related Guides to the Product](https://learn.adafruit.com/products/4116/guides)
![Front view of a Adafruit PyPortal - CircuitPython Powered Internet Display with a pyportal logo image on the display. ](https://cdn-shop.adafruit.com/640x480/4116-00.jpeg)

### Adafruit PyPortal Desktop Stand Enclosure Kit

[Adafruit PyPortal Desktop Stand Enclosure Kit](https://www.adafruit.com/product/4146)
PyPortal is&nbsp;our easy-to-use IoT device that allows you to create all the things for the “Internet of Things” in minutes. Create little pocket universes of joy that connect to something good.

And now that you've made a cool internet-connected project...

In Stock
[Buy Now](https://www.adafruit.com/product/4146)
[Related Guides to the Product](https://learn.adafruit.com/products/4146/guides)
![Demo Shot of the Assembled Adafruit PyPortal Desktop Stand Enclosure Kit.](https://cdn-shop.adafruit.com/640x480/4146-03.jpg)

### Controllable Four Outlet Power Relay Module version 2

[Controllable Four Outlet Power Relay Module version 2](https://www.adafruit.com/product/2935)
Say goodbye to hazardous high voltage wiring and create the [Internet of Things](https://www.adafruit.com/categories/342) with safe, reliable power control. The **IoT Power Relay** &nbsp;from&nbsp;[Digital...](http://www.digital-loggers.com/iot.html)

In Stock
[Buy Now](https://www.adafruit.com/product/2935)
[Related Guides to the Product](https://learn.adafruit.com/products/2935/guides)
![Controllable Four Outlet Power Relay Module](https://cdn-shop.adafruit.com/640x480/2935-13.jpg)

The power cord that comes with the power relay module is short. If you don't want to use an extension cord, you can purchase this longer power cord in the Adafruit shop.

### STEMMA JST PH 2mm 3-Pin to Male Header Cable - 200mm

[STEMMA JST PH 2mm 3-Pin to Male Header Cable - 200mm](https://www.adafruit.com/product/3893)
This cable will let you turn a JST PH 3-pin cable port into 3 individual wires with high-quality 0.1" male header plugs on the end. We're carrying these to match up with our Hallowing, for extending and connecting sensors or LEDs - and the wires are even color coded!

<a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/3893)
[Related Guides to the Product](https://learn.adafruit.com/products/3893/guides)
![Angled shot of STEMMA JST PH 3-Pin to Male Header Cable - 200mm.](https://cdn-shop.adafruit.com/640x480/3893-03.jpg)

# PyPortal Voice Controlled Smart Switch and Time Display

## Connecting the PyPortal

The PyPortal needs to connect to an IoT Relay in order to control power to the outlets. The IoT relay has 4 outlets:

- 1 outlet normally on
- 2 outlets normally off
- 1 outlet always on

Connect your light or appliance to the outlet marked "Normally Off" and connect the PyPortal USB adapter to the outlet marked "Always On". This allows the PyPortal to be plugged into the same IoT relay as the device it is controlling without turning itself off.

You will need to connect the PyPortal to the IoT Relay using the D4 connector. See the diagram below for the wiring. This connector uses 3 wires (white, red and black) but we only use two of them (white and black). Make sure the black wire is the topmost wire on the PyPortal and the rightmost wire connected to he IoT Relay. If you want to extend the wires so the IoT Relay can be placed further away, [then read this learning guide](https://learn.adafruit.com/on-slash-off-switches/wiring) for some tips about how to solder wires together for a battery switch, where the techniques explained there will work for extending the wires.

![](https://cdn-learn.adafruit.com/assets/assets/000/084/891/medium800/adafruit_io_PyPortal_Smart_Switch_bb.png?1574709499)

The PyPortal needs to connect to the internet to control the switch remotely. Read the next section on configuring the PyPortal for the internet using the **secrets.py** file and installing the CircuitPython files.&nbsp;

# PyPortal Voice Controlled Smart Switch and Time Display

## CircuitPython Code

## Update CircuitPython

First make sure you are running the&nbsp;[latest version of Adafruit CircuitPython](https://circuitpython.org/board/pyportal/)&nbsp;for your board.

See the guide page [Install CircuitPython](https://learn.adafruit.com/adafruit-pyportal/install-circuitpython) in the Adafruit Learning System guide [Adafruit PyPortal](https://learn.adafruit.com/adafruit-pyportal/overview).

## Grab the Project Files
Once you have your CircuitPython libraries installed, you can grab the rest of the project files. Use the **Download Project Bundle** button in the code window below to download the code, fonts, background bitmap and a starter **settings.toml** file.

Copy the contents of the zip file to your PyPortal **CIRCUITPY** drive.

## Code
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/PyPortal/PyPortal_Smart_Switch/code.py

When all the files are loaded onto the PyPortal **CIRCUITPY** drive, the following files should be on there:

![CIRCUITPY](https://adafruit.github.io/Adafruit_Learning_System_Guides/PyPortal_PyPortal_Smart_Switch.png )

## Library Files

Before continuing, please make sure these files and folders are in your board's&nbsp; **lib** &nbsp;folder.

- **adafruit\_bitmap\_font**
- **adafruit\_bus\_device**
- **adafruit\_display\_shapes**
- **adafruit\_display\_text**
- **adafruit\_esp32spi**
- **adafruit\_io**
- **adafruit\_minimqtt**
- **adafruit\_pyportal**
- **adafruit\_connection\_manager.mpy**
- **adafruit\_fakerequests.mpy**
- **adafruit\_minimqr.mpy**
- **adafruit\_pixelbuf.mpy**
- **adafruit\_request.mpy**
- **adafruit\_logging.mpy**
- **adafruit\_minimqtt.mpy**
- **adafruit\_pyportal.mpy**
- **adafruit\_requests.mpy**
- **adafruit\_ticks.mpy**
- **adafruit\_touchscreen.mpy**
- **neopixel.mpy**
- **simpleio.mpy**

## Settings File Setup

Next is to get the PyPortal connected to the internet. To do this, we need to create a _settings_ file. If you have not yet set up a **settings.toml** file in your&nbsp; **CIRCUITPY** &nbsp;drive and connected to the internet using it,&nbsp;[follow this guide and come back when you've successfully connected to the internet](https://learn.adafruit.com/adafruit-pyportal/internet-connect#whats-a-secrets-file-15-1).&nbsp;

Using [Mu](https://learn.adafruit.com/adafruit-pyportal/installing-mu-editor) or any text editor, you should add your Adafruit IO Username and Adafruit IO Key to the **settings.toml** file. This project connects to Adafruit IO to retrieve the current date and time as well as integrating voice commands using Amazon Alexa and Google Assistant. Adafruit IO is free to use, but you'll need to log in with your Adafruit account to use it. If you don't already have an Adafruit login, create&nbsp;[one here](https://accounts.adafruit.com/users/sign_up).

If you haven't used Adafruit IO before,&nbsp;[check out this guide for more info](https://learn.adafruit.com/welcome-to-adafruit-io/getting-started-with-adafruit-io).

Once you have logged into your account, there are two pieces of information you'll need to place in your **settings.toml** file:&nbsp;Adafruit IO username, and&nbsp;Adafruit IO key. Head to&nbsp;[io.adafruit.com](https://io.adafruit.com/)&nbsp;and simply click the&nbsp; **View AIO Key** &nbsp;link on the left hand side of the Adafruit IO page to get this information.

Then, update the&nbsp;`aoi_username`&nbsp;and&nbsp;`aio_key`&nbsp;values in the **settings.toml** file. Your **settings.toml** file should look like this:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/PyPortal/PyPortal_Smart_Switch/settings.toml

## Fonts

Two fonts are used to display the date and time on the PyPortal. You will need to create a **fonts** folder on your CircuitPython **CIRCUITPY** drive and Ensure above you downloaded the font files from the project's GitHub repository

## The Background Image
You will also need a background image for the display. You can use the one included in the project. This background is in the public domain and [can be found at here](https://www.publicdomainpictures.net/en/view-image.php?image=11825&picture=tulip-frame), as well as other similar backgrounds.

The **background.bmp** file is placed in the main (root) directory of the **CIRCUITPY** drive.

In the next pages, we will connect to Adafruit IO for remote controlling the appliance.

# PyPortal Voice Controlled Smart Switch and Time Display

## Adafruit IO Configuration

The project as is works great - it has a nice date and time display with a touch screen switch. However, when combined with Adafruit IO, it can control a connected light or appliance through the internet or voice command. This is done using an Adafruit IO feed. This feed is then used&nbsp; to create a virtual switch. This web based button allows you to turn on and off your smart switch remotely. Later this will be extended to voice commands using Alexa or a Google Assistant. But first is to get the switch working in Adafruit.io.

Read the [Welcome to Adafruit IO learning guide](https://learn.adafruit.com/welcome-to-adafruit-io) to get learn about the features of Adafruit IO.

## Integrating Adafruit IO with CircuitPython

Earlier you created an Adafruit.io account and entered your Adafruit.io credentials `aio_username` and `aio_key` into the **secrets.py** file. Now to create a virtual switch using Adafruit.io. This requires creating a feed at Adafruit.io . The feed name is hard-coded into **code.py** as `pyportal-switch`. You can change this if needed, particularly if you creating more than one switch when you will need different names.

## Creating a New Feed

To create a new feed, navigate to the feed list using "Feeds | View All". Create a new feed from the "Actions" menu and name it "PyPortal Switch". Verify the feed key is `pyportal-switch` since this is the name the PyPortal uses, not the name.

![](https://cdn-learn.adafruit.com/assets/assets/000/084/767/medium800/adafruit_io_create_feed.png?1574426728)

Once the feed is created, next is to create a virtual switch using an Adafruit.io dashboard. This is not necessary for use with Alexa or Google Assistant, but it is a easy method to test the feed using a web browser.

## Creating a New Dashboard

Navigate to "Dashboards | View All", create a new dashboard. You can name it whatever you would like. For our guide we named it "PyPortal Smart Switch".

![](https://cdn-learn.adafruit.com/assets/assets/000/084/768/medium800/adafruit_io_create_dashboard.png?1574427348)

Now to create the switch by clicking the blue plus sign icon on the right side of the dashboard. Create a toggle block from the list of block types. Then, select the "PyPortal Switch" feed from the "Choose Feed" dialog and proceed to the next step by clicking the "next step" button. Finally, give this block a descriptive name and keep the default button text values. Click the "create block" button to save the button.

![](https://cdn-learn.adafruit.com/assets/assets/000/084/837/medium800/adafruit_io_dashboard.png?1574481574)

To verify the switch and the underlying feed is working correctly, you can click on the "ON" or "OFF" button that appears on the dashboard. If your PyPortal is correctly running the **code.py** file and the proper Adafruit.io credentials were entered, You should see your PyPortal's switch icon change as the value is changed on Adafruit.io. If you have a light or appliance plugged in to the relay switch, it should also turn on or off when the virtual button is pressed.

You now have a web controlled smart switch. You can turn your appliance on or off from a web browser on your PC or smartphone. Continue to the next section to configure you smart switch for voice commands using either Amazon Alexa or Google Assistant.

# PyPortal Voice Controlled Smart Switch and Time Display

## IFTTT Configuration

OK, so we have a functioning time display using Adafruit.io and the ability to turn your device on or off using the PyPortal touch screen or from a web page. We could be done here, this is a functional product as it stands. However, we just need one more step to enable the PyPortal to act on voice commands from an Amazon Alexa or Google Assistant. The magic to do this is done using the web site **ifttt.com**. This site, short for "If This Then That", connects the voice device with Adafruit IO. Configure one or both if you happen to have both types of voice assistants.

To begin, click the "Explore" button in the upper right of the page on the IFTTT web site. Next, click the "+" button next to "Make your own applets from scratch".

The first time you connect to a service like Amazon Alexa, Google Assistant, or Adafruit IO, you will be prompted to connect to that service with your login name and password.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/059/medium800/adafruit_io_connect_adafruit.png?1574869330)

Depending on the device you are using, continue with either the "Alexa Setup" or "Google Assistant Setup" page.

# PyPortal Voice Controlled Smart Switch and Time Display

## Alexa Setup

Here are the steps to configure Amazon Alexa with IFTTT. You will need to create two applets, one to turn on the lights and the other to turn off the lights.

Click "This" to create the first half of the applet.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/052/medium800/adafruit_io_ifthis.png?1574862262)

Search for and select "Amazon Alexa". Don't be tempted to select Adafruit yet! We will be selecting that in the second half of the applet.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/053/medium800/adafruit_io_choose_a_service.png?1574862320)

Click on the "Say a specific phrase" trigger. Then enter the phrase "the lights on" in the trigger fields. IFTT requires phrase to start with "Alexa trigger", so adding this text will complete the phrase with "Alexa trigger the lights on".

![](https://cdn-learn.adafruit.com/assets/assets/000/085/044/medium800/adafruit_io_alexa_complete_trigger_fields.png?1574861890)

Click "That" to create the second half of the applet. Notice the Amazon Alexa icon in the "If" part of the applet.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/043/medium800/adafruit_io_alexa_then_that.png?1574861872)

Here is where we connect our applet to Adafruit IO. Select the "Adafruit" icon from the list of action services, and then select the action, "Send data to Adafruit IO".

![](https://cdn-learn.adafruit.com/assets/assets/000/085/049/medium800/adafruit_io_choose_action_service.png?1574862059)

![](https://cdn-learn.adafruit.com/assets/assets/000/085/051/medium800/adafruit_io_choose_action.png?1574862201)

Pick the feed we created earlier on Adafruit IO, "PyPortal Switch". For the data to save, enter "ON" if this applet turns on the appliance, or "OFF" if this applet turns off the appliance.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/050/medium800/adafruit_io_complete_action_fields.png?1574862109)

Review the applet to confirm and click the "Finish" button to complete. You may want to disable notifications when the applet runs to avoid getting notified every time you turn the appliance on or off when using Alexa.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/045/medium800/adafruit_io_alexa_review_and_finish.png?1574861921)

You can now test your applet by speaking the phrase from the previous step. Don't forget you need to create two applets in order to turn the appliance on and off.

# PyPortal Voice Controlled Smart Switch and Time Display

## Google Assistant Setup

Here are the steps to configure Google Assistant with IFTTT. You will need to create two applets, one to turn on the lights and the other to turn off the lights.

Click "This" to create the first half of the applet.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/054/medium800/adafruit_io_ifthis.png?1574862373)

Search for and select "Google Assistant". Don't be tempted to select Adafruit yet! We will be selecting that in the second half of the applet.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/055/medium800/adafruit_io_choose_a_service.png?1574862395)

Click on the "Say a simple phrase" trigger. The Google Assistant trigger here is more customizable than the equivalent Amazon trigger. Here you can enter up to three phrases to run the action, and you can also tell Google Assistant what to say in response. In this example, three phrases are entered: "turn on the lights", "turn the lights on" and "turn on the living room lights". For a response, we used "OK, turning on the living room lights"

![](https://cdn-learn.adafruit.com/assets/assets/000/085/047/medium800/adafruit_io_google_complete_trigger_fields.png?1574861977)

Click "That" to create the second half of the applet. Notice the Google Assistant icon in the "If" part of the applet.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/046/medium800/adafruit_io_google_then_that.png?1574861965)

Here is where we connect our applet to Adafruit IO. Select the "Adafruit" icon from the list of action services, and then select the action, "Send data to Adafruit IO".

![](https://cdn-learn.adafruit.com/assets/assets/000/085/056/medium800/adafruit_io_choose_action_service.png?1574862425)

![](https://cdn-learn.adafruit.com/assets/assets/000/085/058/medium800/adafruit_io_choose_action.png?1574862496)

Pick the feed we created earlier on Adafruit IO, "PyPortal Switch". For the data to save, enter "ON" if this applet turns on the appliance, or "OFF" if this applet turns off the appliance.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/057/medium800/adafruit_io_complete_action_fields.png?1574862461)

Review the applet to confirm and click the "Finish" button to complete. You may want to disable notifications when the applet runs to avoid getting notified every time you turn the appliance on or off when using Google Assistant.

![](https://cdn-learn.adafruit.com/assets/assets/000/085/048/medium800/adafruit_io_google_review_and_finish.png?1574861998)

You can now test your applet by speaking the phrase from the previous step. Don't forget you need to create two applets in order to turn the appliance on and off.


## Featured Products

### Adafruit PyPortal - CircuitPython Powered Internet Display

[Adafruit PyPortal - CircuitPython Powered Internet Display](https://www.adafruit.com/product/4116)
 **PyPortal** , our easy-to-use IoT device that allows you to create all the things for the “Internet of Things” in minutes. Make custom touch screen interface GUIs, all open-source, and Python-powered using&nbsp;tinyJSON / APIs to get news, stock, weather, cat photos,...

In Stock
[Buy Now](https://www.adafruit.com/product/4116)
[Related Guides to the Product](https://learn.adafruit.com/products/4116/guides)
### Adafruit PyPortal Desktop Stand Enclosure Kit

[Adafruit PyPortal Desktop Stand Enclosure Kit](https://www.adafruit.com/product/4146)
PyPortal is&nbsp;our easy-to-use IoT device that allows you to create all the things for the “Internet of Things” in minutes. Create little pocket universes of joy that connect to something good.

And now that you've made a cool internet-connected project...

In Stock
[Buy Now](https://www.adafruit.com/product/4146)
[Related Guides to the Product](https://learn.adafruit.com/products/4146/guides)
### Controllable Four Outlet Power Relay Module version 2

[Controllable Four Outlet Power Relay Module version 2](https://www.adafruit.com/product/2935)
Say goodbye to hazardous high voltage wiring and create the [Internet of Things](https://www.adafruit.com/categories/342) with safe, reliable power control. The **IoT Power Relay** &nbsp;from&nbsp;[Digital...](http://www.digital-loggers.com/iot.html)

In Stock
[Buy Now](https://www.adafruit.com/product/2935)
[Related Guides to the Product](https://learn.adafruit.com/products/2935/guides)
### Power Cord Cable w/ 3 Conductor PC Power Connector Socket

[Power Cord Cable w/ 3 Conductor PC Power Connector Socket](https://www.adafruit.com/product/3311)
The standard 3 prong wall plug (NEMA 5-15p) to PC connector (IEC C13) cable was made commonplace by PC power supplies. Today, they can be found providing power connections for many different products such as computers, monitors, printer, projectors and HDTV's.&nbsp;These cables are great...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3311)
[Related Guides to the Product](https://learn.adafruit.com/products/3311/guides)
### STEMMA JST PH 2mm 3-Pin to Male Header Cable - 200mm

[STEMMA JST PH 2mm 3-Pin to Male Header Cable - 200mm](https://www.adafruit.com/product/3893)
This cable will let you turn a JST PH 3-pin cable port into 3 individual wires with high-quality 0.1" male header plugs on the end. We're carrying these to match up with our Hallowing, for extending and connecting sensors or LEDs - and the wires are even color coded!

<a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/3893)
[Related Guides to the Product](https://learn.adafruit.com/products/3893/guides)

## Related Guides

- [Adafruit PyPortal - IoT for CircuitPython](https://learn.adafruit.com/adafruit-pyportal.md)
- [Adafruit IO Basics: AirLift](https://learn.adafruit.com/adafruit-io-basics-airlift.md)
- [A Logger for CircuitPython](https://learn.adafruit.com/a-logger-for-circuitpython.md)
- [Daily UV Index PyPortal Display](https://learn.adafruit.com/pyportal-uv-index.md)
- [CircuitPython Minesweeper Game](https://learn.adafruit.com/circuitpython-pyportal-minesweeper-game.md)
- [Playing Animated GIF Files in CircuitPython](https://learn.adafruit.com/using-animated-gif-files-in-circuitpython.md)
- [PyPortal Event Countdown Clock](https://learn.adafruit.com/pyportal-event-countdown-clock.md)
- [Cleveland Museum of Art PyPortal Frame](https://learn.adafruit.com/cleveland-museum-of-art-pyportal-frame.md)
- [PyPortal Halloween Countdown](https://learn.adafruit.com/pyportal-halloween-countdown.md)
- [PyPortal Wake-Up Light Alarm Clock](https://learn.adafruit.com/pyportal-wake-up-light.md)
- [Where's My Friend? A Location-Aware Display with PyPortal and ItsASnap](https://learn.adafruit.com/where-s-my-friend-a-location-display-frame-with-pyportal.md)
- [PyPortal Reddit Stats Trophy](https://learn.adafruit.com/pyportal-reddit-stats-trophy.md)
- [Arcada Animated GIF Display](https://learn.adafruit.com/pyportal-animated-gif-display.md)
- [PyPortal Astronauts in Space](https://learn.adafruit.com/pyportal-astronauts-in-space.md)
- [League of Legends Level Trophy for PyPortal](https://learn.adafruit.com/league-of-legends-level-trophy-for-pyportal.md)
- [PyPortal Thingiverse Viewer](https://learn.adafruit.com/pyportal-thingiverse-viewer.md)
