A Self-Made Smart Mirror
How we made a smart mirror using Raspberry Pi
With the huge growth of IoT devices, people’s daily necessities could be variously given more amazing functions. Today, I’m gonna show you guys the process of making a smart mirror with Raspberry Pi 3.
Functions:
There are two modes of this smart mirror, security mode and active mode: In the security mode, the mirror keeps detecting the change of the environment with based on light and motions in front of it. If something is detected, the mirror will automatically send an email to the owner. In the active mode, the mirror could show the time, date and current room temperature. There is no physical button on this mirror, an user is able to switch between these two modes by voice control.
Team Memembers:
Silver Zhang, Iman Elsayed
Things we used:
1 Raspberry Pi 3, 1 Sunfounder USB microphone, 1 RobotDyn Photosensitive Light Sensor (LDR), 1 Elegoo Ultrasonic Sensor, 1 two-way mirror, 1 Dell monitor, bunch of jump wires (primarily female-male wire), wood frames.Process:
1. We have to measure the sizes the two-way mirror in order to cut the wood frames, here, a big shout-out to my partner’s brother, who did a really brilliant job on cutting the woods and putting them together. Our monitor’s has the pretty much the same height as the two-way mirror and horizontally smaller than it so that we have space to fit our raspberry pi and circuit.
2. Then we move onto the circuit part, based on our objectives, we put all the electronic elements together on one bread board and have them correctly interact with our Raspberry pi.
3. Then, looks like we have all the hardware set up so far, the next thing we need to do is design a software system that could both display information and interact with sensors. Here we used python 3 since it is easy and has a lot of libraries. For the interface, we used python's tkinter to construct a full screen background containing those labels which load the information from the internet and sensors.
The picture above is the software structure is very clear, each source of information is an independent module which is called in the graphic function every specific amount of time. Besides the sensor parts, we also have the voice controling function, which keeps listening the voice from surroundings and if the keywords "switch" or "security" are heard, then the mirror will switch the mode to active or security mode, therefore, we don't need any physical button to do the operations. Also for the voice recognition, we used google's voice recognition API.
Note:
For the thermistor part, we used is the thermistor module and ADC converter(PCF8591) module of Sunfounder (usually like 6 or 7 dollars), but there are also other options you can find online, if you are using the same things as we did, the ADC converter libarary is provided by Sunfounder.(See our source code for more information)
4. Another function module we have is an automatic email-sending funtion, in security mode, when the ultrasonic or light sensors have detected something
from the environment, the program would send an alarm email to the owner.
Here is a piece of code to implement email sending with python's email library, you also need to input username and password of your own email account and the email will be sent from that account:
from email.mime.text import MIMEText
import smtplib
def sendemail():
FROM = 'fromemail'
SENTTo = 'toemail@gmail.com'
SUBJECT = "Alarm"
TEXT = "INTRUDER INTRUDER!"
msg = MIMEText('body')
msg['Subject'] = 'subject'
msg['From'] = "..."
msg['Reply-to'] = "..."
msg['To'] = "..."
mail = smtplib.SMTP("smtp.gmail.com", 587)
mail.ehlo()
mail.starttls()
mail.ehlo()
mail.login("email", "password")
mail.sendmail('affinemirror@gmail.com', SENTTo, TEXT)
print("Sent Succesffuly")
mail.close()
5. One of the problems might be how to have several processes going on in the python since tkinter is already a loop that is running
all the time, we cannot have another loop at the same time to keep checking sensor's data and voice around.
Here, tkinter has a function:
self.root.after(time, function)
Where the second parameter "function" you want to keep executing and the first parameter "time" is the frequency the function is executed.
With this line of code, I can say my function paramter refers to the function that is checking the GPIO ports of sensors constantly
so that we can keep updating the data from sensors.
But for voice listening, there is a small difference, we used multi-threading using python's threading library.
def thr(self):
t1 = threading.Thread(target=self.listen, daemon=True)
t1.start()
6. After every part was working correctly, we start putting all the parts together, we stick our monitor and circuit onto the back of the mirror.
7. Yes, now a basic smart mirror has been made! Click here to get the source code of our software system.
Closing Thoughts:
This is just the first version of smart mirror made by me and my partner, but obviously it is not what we have imagined about a really great smart mirror. In the future, we are planning to remove all the sensors, improve the system and develop a better voice interaction for the mirror.