Introduction: Smart Home Assistant With Motion Sensed Lamp and Temperature/Humidity Sensor

Do you want to learn how to build a Smart Home Assistant (Alexa) using a Raspberry Pi? Want to further your project and learn how to build a skill for Alexa? Well, look no further because I have built an Alexa with a motion sensor lamp and a room temperature/humidity sensor skill. This project was inspired by adylinn's post and this instructable is a part of my SIDE project for Ms. Berbawy's POE class. To see more projects from her class, check out her website.

Shout out to Ms. Berbawy for providing guidance throughout this project and allowing me to use her maker space!

Special thanks to Patrick Fromaget and Nick Triantafillou for their amazing posts that guided me through this passion project of mine!

Supplies

Step 1: Setting Up the Raspberry Pi

First, you have to set up your raspberry pi in order to begin your project.

  1. Insert your Micro SD card into the SD card slot that is located underneath your raspberry pi.
  2. Attach the Raspberry Pi DAC Hat on top of your raspberry pi by sliding the GPIO pins (the sharp spiky needle-like things) into the slots provided on the DAC Hat.
  3. Attach your microphone (USB Port) and speaker (Audio Jack) to the raspberry pi.
  4. Connect your keyboard and mouse to the USB Port. **I used a Bluetooth keyboard and mouse and so they are all connected to the dongle that is connected to the USB Port.
  5. Next, using the HDMI cable, connect your raspberry pi to a monitor.
  6. Connect the power cord to a power outlet and connect the other end to your raspberry pi (Micro USB Port).

Now, your pi is connected and running.

Step 2: Building Your Assistant Speaker (Alexa) + Troubleshooting Tips

The steps for building this project are outlined clearly on the Amazon Voice Service Website. This process took around 2 hours for me as the installation can be pretty long. Even though this website does outline the important steps, it does not address a few common issues that I faced. An issue that I faced when installing the Alexa Assistant Speaker was that the installation would sometimes freeze midway. I found two different solutions that have worked for me.

First Solution:

cd /home/pi/build 
sudo make VERBOSE=1  

Then run the installation command again which is provided on the Amazon Voice Service website.

sudo bash setup.sh config.json [-s DSN] 

Second Solution:

Run the following commands:

sudo -s
swapoff -a
dd if=/dev/zero of=/swapfile bs=1M count=512
mkswap /swapfile
swapon /swapfile
swapon -s<br>

Then run the installation command again which is provided on the Amazon Voice Service website.

sudo bash setup.sh config.json [-s DSN]

After installation, connect your speaker and microphone to your raspberry pi and run it using the following code:

cd /home/pi
sudo bash startsample.sh

Step 3: Setting Up the Motion Sensor

Before connecting the PIR Motion Sensor, it is important to make sure that the raspberry pi is not plugged in. The first step is to connect the PIR Motion Sensor to the Raspberry Pi which can be accomplished by using the GPIO pins on the raspberry pi (the sharp spiky needle-like things). I have outlined the steps below.

**Refer to the Raspberry Pi GPIO Pin Diagram above from the Raspberry Pi Website.

1. First connect one end of three different colored jumper wires to the pins on the motion sensor.

2. The pin on the left will be connected to Pin 9 (Ground).

3. The one in the middle will be connected to Pin 11 (GPIO17)

4. The pin on the right will be connected to Pin 2 (5V power).

Now that I have successfully connected my PIR motion sensor to my raspberry pi I can start coding the motion sensor.

Step 4: Setting Up Your PHUE Light Bulb

I need to be able to control the lights using the raspberry pi which is why I am using a smart light bulb. I followed the instructions on Patrick Fromaget and configured my light bulb. Now, it is important to take note of the light bulb number (for whichever lightbulb you are using). My code will include LIGHT# which should be replaced with the correct light bulb number.

Step 5: Motion Sensor Lamp

Now after configuring the lightbulb I am going to add the code for the motion sensor to the code for the light bulb configuration. I want the light to turn on when there is motion detected and to turn off when there is no more motion. It is vital to configure the lightbulb before running this code. Replace the previous code provided by Patrick Fromaget for the lightbulb with the code given below.

#!/usr/bin/python

from phue import Bridge
import logging
Import time
logging.basicConfig()

b = Bridge('192.168.1.79')
#b.connect()

#Change the light state
b.set_light(LIGHT#, 'on', False)

While True:
	pir.wait_for_motion()
	print(“motion detected”)
	b.set_light(LIGHT#, ‘on’, True)
	time.sleep(5)


	pir.wait_for_no_motion()
	b.set_light(LIGHT#, ‘on’, False)
	print(“motion not detected”)
	time.sleep(1)  

You should be able to run your code now and the light should work now.

**You can remove the print statements as it is not necessary and is there only for testing and debugging purposes.


Step 6: Setting Up the DHT22 Temperature/Humidity Sensor

I have to connect the DHT22 Temperature/Humidity Sensor to my raspberry pi now. The process is similar to that of the PIR Motion Sensor only the GPIO pins are different now. Making sure that the raspberry pi is not plugged in, connect three different colored jumper wires to the pins on the DHT22. I have outlined the steps below.

**Refer to the Raspberry Pi GPIO Pin Diagram above from the Raspberry Pi Website.

1. First connect one end of three different colored jumper wires to the pins on the motion sensor. Make sure the colors of these wires are different from the wires for the PIR Motion Sensor.

2. Connect the jumper wire from the left pin to Pin 4 (5V power).

3. Connect the one in the middle to Pin 15 (GPIO22).

4. Connect the right one to Pin 6 (Ground).

Step 7: DHT22 Temperature Sensor

Nick Triantafillou explains the whole process very clearly but some of the steps are a little outdated and so I will cover those steps and show a way around them.

The first step was pretty confusing because the DHT22.py code was not working for me. So I grabbed Joan's code and copied that as my code for DHT22.py and it started working then.

The next error I ran into was when running the first command in Step 5 of "Setting up the IoT." The certificate from Symantec was nowhere to be found and so my code kept running an error. This is how I worked around that issue:

1. Download the RSA 2048 bit key: VeriSign Class 3 Public Primary G5 root CA certificate from the Amazon Server Authentication Website on your raspberry pi.

2. Rename the file aws-iot-rootCA.crt. It is vital to name the file exactly as it is provided here or else your code could run an error.

Step 8: Designing the Box

I built a box with fingers for my Smart Home Assistant and light bulb. I designed it on Adobe Illustrator and used a Laser Cutter to cut my box.

I used P95 Acrylic for the top as it is translucent and allows for light to pass through. The bottom of the box for the raspberry pi, speaker, microphone, and the different sensors was cut on 1/8-inch wood.

I designed small holes (0.125 in. x 0.125 in.) on the bottom of the box to allow for sound to travel to the microphone and from the speaker.

I also added a small hole in front of the box for the PIR Motion Sensor (0.9055 in. x 0.9055 in.) and a rectangular hole on the back for the DHT22 Humidity/Temperature Sensor (0.7874 in. x 0.5906 in.).

On the back, I also cut out a hole (1.57 in. x 1.57 in.) for the wires to pass through for the raspberry pi and the light bulb.

In the middle, I added a piece of wood separating the light bulb and the raspberry pi. I added a hole on that piece (1.48 in. x 1.48 in.) for the light bulb to rest on.

I have attached my files for the box. The one labeled 1/8-inch_Wood should be cut on 1/8-inch wood and the one labeled P95 Acrylic has to be cut on P95 Acrylic. After cutting them out, just use super glue to glue them all back together.

**I also added my initials in front of the box and you could change it to personalize your project more.

Step 9: Putting It All Together

I have added a simple image of my electronics layout on top and how I arranged it all inside the case. It is important to make sure that all the wires are tight and intact, especially the DHT22 Humidity/Temperature Sensor as it tends to disconnect easily.

Step 10: Conclusion

While this project may have been started as a project for Ms. Berbawy's class, it quickly turned into one of my passion projects. This was a project that I have been wanting to do for a while and so I would like to thank Ms. Berbawy again for providing the opportunity.

I have learned so much about raspberry pi and the different sensors from this project. I was also able to expand my software skills with this project and explore new technologies with this project. I learned more about the different Amazon Web Services and I also learned how to create a skill of my own.

I would really like to expand this project in the future and add different sensors. I have already started planning the different sensors I would like to add and I hope that I have the opportunity to build upon this project in the future.

If you are building this project, hope you enjoy it as much as I did!