Using a Sensor to Trigger a Notification
In this lesson, you’ll use a push button connected to your Arduino to mimic a digital sensor and trigger a notification on your machine.
For this course, you’ll be using Tkinter, which is the standard Python GUI toolkit. For more information on Tkinter, check out the following resources:
For more information about send_email()
and configuring a Gmail account to send emails, check out the following resources:
00:00 In this lesson, you’ll use a sensor to trigger a notification. Now that you’ve covered the basics—and by the basics I mean you’ve configured the Arduino to communicate with your PC using Firmata and pyFirmata—you know how to address digital and analog inputs and outputs and know some of the basics about electronics. So, what are some other uses for adding an Arduino to a PC?
00:24 I’m going to show you a couple examples. One is going to use a digital sensor to trigger a notification—a popup on your computer screen, kind of an alert.
00:34 The other is going to be generating an email based upon a sensor event. So, what are you going to use to display this notification in Python? You’ll use Tkinter, which is part of the standard Python library.
00:49 In fact, it’s the standard Python GUI Toolkit, Graphical User Interface. Using Tkinter, you’ll show a message box when you press a button. And one nice thing about this circuit is it’s going to be the same circuit you’ve used in the digital input example.
01:05 This lesson will only scratch the surface of what Tkinter can do, so I’ll include links for more information on the web and an article on Real Python. So, here’s that circuit again. I won’t spend too much time on this, but again, the 5V comes out of the POWER section, goes into the switch, and into input 10, and it still has that pull down circuit. On the breadboard, same layout as before. If you need to review it, you can find pictures of both the circuit and the breadboard layout below this lesson, but you can go back to Lesson 6 to walk through it step by step.
01:41
This program’s going to look similar to the one that was used for the digital input example, but there’s a few different changes. I’m going to go ahead and create a new file and call it notification.py
. Okay.
01:53
You might remember in switch.py
you imported pyfirmata
, imported time
, set up your board
, and so forth.
02:02
In fact, I’m going to just copy a lot of the stuff that’s in there and put that into notification
. Paste, okay.
02:12
Along with pyfirmata
and time
, you need to import the tkinter
package, and from tkinter
you’re going to import the messagebox
.
02:27
You need to set up a little bit about tkinter
, so root = tkinter
, you’re going to set up Tk()
. Here, you’re setting up the Tkinter main window.
02:37 And then right after that, on line 7, you’re withdrawing that main window because you only want to see the message box. Again, you could find more Tkinter information below this lesson.
02:50
Then you’re going to set up your board
, on line 9, and here you’re still setting up the util
for the board
and iterating on it to again, look for that switch. It still uses the same board input, and it’s still going to use the flashing LED there when the button is pressed.
03:10
So, it’s time to look at the while
loop. This is where a lot of the change is going to happen. Still going to look for the digital_input.read()
but this time if it’s True
, not only are you going to write to the led
, here’s where you’re going to use messagebox
that you imported from tkinter
and .showinfo()
in the messagebox
.
03:30
You have a title
of the box and then a string message
. The title
will be kind of a generic "Notification"
!
03:38
And then the next thing is the "Button was pressed"
! The loop will pause there, waiting for the user to press OK on the message box, and that’ll keep the LED on.
03:48
Then the root.update()
will erase the message box. Don’t need an else
. You could tell the led
to turn off. Again, it’s going to keep looping around looking for that, and then you still want that sleep()
there so it doesn’t tie up your computer too much.
04:06
Go ahead and save. And down here, python
and we’re going to run notification.py
. Oh, yeah I did, I missed something big. I forgot to activate my environment, so pyfirmata
was not available. Try that again.
04:26
python notification.py
. Here it is running.
04:33 I’ve got to press the switch. Okay, there it is. The Tkinter box showing the notification that the Button was pressed.
04:42 Then it happened again, and so forth. Great! Nice! I’m going to quit, Control + C. Breaking out of that, interrupting it.
04:54 For this next code example, you’re going to send an email. Now, setting up email and sending and configuring that in Python is an article of itself. In fact, if you really want to learn much more about this, or you’re having some troubleshooting issues—I’m not going to go too deep into it, I just want to show you that it’s a possibility, but there is a Real Python article on this that goes much more in-depth into it and talks about how to troubleshoot a lot of the issues you may have.
05:20 The code you’ll use is really similar to that article, minus the bit of using the Arduino. And there’s an accompanying Real Python course you can watch that goes over all the concepts also.
05:31 Those will be a great resource for you to learn more about setting up emails if you have deeper questions or have trouble configuring it. For the example configuration that I’m going to show you in this one, I’m going to use a free Gmail account that I’ve already set up, and I’ve already configured the Allow less secure apps options.
05:49 I’m going to include links to all of this information below this lesson. Another issue that I had, since I’ve recently reconfigured my computer and installed Python from scratch on a Mac, it does not install the certificates that are used for sending and receiving email through Python.
06:04 That’s a separate step, that you have to install certificates from the location where Python is installed. There’s a script inside there you have to run.
06:13 All right. Let me have you try it out. To set up the credentials for the email addresses that I’m going to use, I’m going to show you a technique where you could use a module for that information, so that you could keep the email accounts and password separate from your application if you’d like. There are multiple ways to approach this.
06:33
You’re going to create a new file, creds.py
.
06:41
you’ll create variables for sender_email
, which will be the text string of the email account you’re coming from; the receiver_email
address; and then the password
for the sender_email
account. I’ve blocked these out, but you would put your credentials in there for the accounts that you’ve created.
07:10 Then for the mail script that you’re going to create, just create a new script,
07:16
I’ll call it mail_notify.py
. Okay. I’m going to copy most of the notification
script into mail_notify
. Close that one. The next step is to import the credentials, but also remove the imports for tkinter
.
07:32
from creds import sender_email, receiver_email, password
. Great. So that stuff will now be imported. You’re going to use the SMTP library, smtplib
, and you need to import ssl
. Okay. So that stuff’s been imported.
07:49
You’re not using tkinter
, so remove those lines. So here, you’re going to define a function called send_email()
. It’s going to have a port
.
07:57
The port for SSL is 465
.
08:02
The smtp_server
will be using Gmail, so it’s "smtp.gmail.com"
. And then you need a message
.
08:15
Here your message
will be """Subject: Arduino Notification"""
.
08:21
You’re going to put a line break there. """\n The switch was turned on!"""
.
08:28
You need to create a context. context
will be an ssl
, and you’re going to create a default context. That’s the method you’ll use.
08:36
Then you’re going to use a with
block here for containing this all here—a context manager, if you will. smtplib.SMTP_SSL()
, okay. Here you’re using smtp_server
, you’re using the port
you set up a moment ago, and then context
will be equal to the context
.
08:58
So, with smtplib.SMTP_SSL()
as the server
, with all those parameters, you’re going to say print out this, that you’re sending currently an email.
09:10
You need to log into that server
, that’s used with the sender_email
that you imported in and its password
. And then server.sendmail()
. First, you’re going to say the sender_email
, the receiver_email
, and then attached to that is the message
.
09:28
I’m not going too deep into all this. Again, you can learn much more about sending emails in these other tutorials. You’ve still got your board
set up here. It looks good. You got your Iterator
, so it’s looking for the button presses. digital_input
, still coming in D10. This time you don’t need this led
as an indicator.
09:44
During your True
loop down here, you’re going to .read()
for that input, if it is True
. I’m not going to .write()
the led
. In fact, you’re just going to go ahead and send an email. Call your function you set up a moment ago. Okay! That all looks good.
10:01
Go ahead and connect to the Arduino, so python mail_notify.py
,
10:08
and if I press the switch, it says Sending email
and pretty quickly here in my mail program, you can see two messages received. I accidentally pressed it twice, but you see Arduino Notification there.
10:27 Looks good. All right! It looks like you’re ready for the conclusion and a course review. That’s up next.
Become a Member to join the conversation.