Project 2 Smart Home
We have already controlled all kinds of modules via app and Bluetooth in previous chapters. In this chapter, we will integrate all these functions and make a comprehensive smart home app.
Task Navigation
1. App function integration, and interface optimization.
2. Complete the smart home app
Hands-On
App production
1. Interface design
1) Open app inventor and create a new project.
2) Under " Screen1 " component properties, we can edit some properties of the app to make it closer to common app design, such as: app icon, app name, background color, etc.
3) Now design the component for Bluetooth connection. First set horizontal layout, then change vertical align to center and change width to fill. Then add a button and label to the horizontal layout.
4) Change the display text of Button 1 to " Connect Bluetooth" and the display text of Label 1 to "Not Connected" (color changes to orange). Rename the component name of button 1 to "connect or disconnect", and rename the component name of label 1 to "Bluetooth connection status".
5) Then make the home appliance control part layout. Add three horizontal layouts below ;
After changing the width of the horizontal layout to Fill:
add an image, a label, a horizontal layout (the width and height are changed to Fill), and a button;
The three horizontal layouts are the same.
6) Set the label text as shown.
7) Upload images to appinventor to set the icons of appliances. Set the image size to 30*30 pixels.
8) Modify the button graphic; upload the switch status image to the app inventor, and set the button graphic to the state "Off". Delete the button display text.
9) The interface layout is completed. Now add the non-visual components (the Bluetooth addition steps are the same as in Chapter IX; the aix file of the Bluetooth module needs to be uploaded). The timer needs to set the timing interval to 500 milliseconds; and the launcher needs to set the Action to: "android.bluetooth.adapter.action.REQUEST_ENABLE", the same as before.
A dialog is added here to alert the user when Bluetooth is not successfully connected, "Bluetooth is not connected". The setting of it will be in the logic section. All other components have been used in previous cases.
10) Complete the interface design. (You can adjust the color and pattern of the icon / text / background according to your preferences. )
2. Logical design
1) Define global variables. The first two are the same as the previous case, as they fixed parameters. Here we add a sign for a successful connection. Because the app we use this time are capable of both sending and receiving. We need this sign to check status.
2) Set the screen initialization parameters, which are as before. Use the launcher to start Bluetooth when the screen is initialized.
3) Set the function to connect to Bluetooth when the Connect button is pressed, similar to previous cases.
4) Set the status display and checking when the Bluetooth connection is successful and disconnected.
5) Set a timer. When the timer reaches the time, it will check. If the current Bluetooth connection is successful, it will receive Bluetooth data. The timer we set is 500ms, so every 500ms it will scan for data received.
6) Define a process or a function to send data. When the Bluetooth is connected, close the timer for receiving data, then send data. Re-enabled timer after completing data sending. Same with previous cases.
7) Set the function of several buttons.
This completes the logic design.
Package and install it on your phone.
Hardware connection
Connect the relay to Digital Pin 5 on the UNO board, the LED light module to Digital Pin 6, the servo to Digital Pin 11.
Hardware connection
(Relay -- 5, LED -- 6, Servo -- 11)
Please match the colors when plugging
Programming
1. Reference program
2. Program’s effect:
The latest temperature, humidity, and light data can be obtained on the phone by refreshing, and the corresponding servo / relay / LED can be started by pressing the corresponding buttons.
Further Exercise
We are now mainly controlling lights in our homes. Can we actually use Bluetooth to control other home appliances?
There are a lot of fun projects in the DFRobot community, go search and find out! Community URL: www.dfrobot.com