This project intends to start making "smart home" devices smart. This project uses an LDR circuit to measure how light the room is and uses machine learning techniques to determine when your lights should turn on. The server hosted on the Raspberry Pi provides a user interface to turn lights on and off; Amazon Alexa skills python code to let you know when your lights are set to turn on; and the ability to turn your lights on when it goes dark at night through a machine learning model trained on the LDR data. The Raspberry Pi server offers a simple user friendly method for turning lights on and off. The Alexa skill is made from Python code that allows you to determine at what time your lights will turn on at.
The first step is to download the code from the RaspiPi-Server and place this on your Raspberry Pi in the /var/www/html folder. Following this the permissions need to be modified to allow python files to be read and executed and text files to be read and wrote to, this can be done with the following command (Note: 777 gives read, write, execute previlages):
chmod 777 app.py
This can be run with the command
python app.py
Note you will need to install dependencies for these scripts to run, these can be installed using the command:
sudo apt-get install python-pip
initially, and then either
sudo apt-get install *package*
pip install *package*
A brief explanation of the main code files:
- app.py - This is the main file and all other files are called from this one. It provides the basis of checking the data values from the LDR and controls timings of this. It also provides the web interface that allows you to manually control your lights using Flask. In addition, it proves the Flask-Ask code for your Alexa skill. Finally it calls the python script that uses a regression model to find out what time your lights will turn on and can activate them to turn on when required.
- lightOnfun.py - This is the python function that runs the regression model to determine what time your lights should turn on at. It also has a switch so that regression algorithm can retrain after new data has been acquired, the periodicity of this is still to be determined.
- linearregression.pickle - This is the linear regression model after it is saved to stop the need to retrain the model every time you want to know what time the light will turn on.
- LDRVaueLogger.txt - This file is used to log LDR values.
- /static - This folder is used to store all the static Flask code.
- /templates - This folder is used to store all the template Flask code.
Next you need ngrok to provide a https address that the Alexa skill will require. download ngrok and place the binary file in the same folder as app.py. Then initiate this by typing
ngrok http 5000
Next, build the light bulb. You need to take the 3D printer parts and get these printed using a 3D printer. Then take an LED bulb and remove the base from it, or buy an LED bulb base. Solder this base to the step down voltage converter and to the relay. Put the code for the light bulb on the Wemos D1 mini using the Arduino IDE. Next solder the 5v end of the step down converter to the wemos D1 mini 5v and ground pins. Then you need to take the metal top from an extender and insert it in the case top. Then solder the Wemos D1 mini digital pin to the relay and the relay to the light bulb case top. Finally, glue the case top and base together and a light bulb can be screwed in the top. This is then a smart light bulb that can be controlled using the WiFi functionality of the Wemos D1 Mini. It should end up looking like this:
Next you will need to build the LDR circuit. First, build the casing for the LDR circuit using a 3D printer. Solder the circuit together using a soldering iron to the provided schematic; this involves wiring 5V to a resistor, then the resistor to the LDR, and the LDR to Gnd. Then wire the point inbetwen the resistor and LDR to the Analogue pin. Then place the LDR server code onto the Wemos D1 mini using the Arduino IDE. Next position the circuit inside the case and seal it close. This is then powered up by connecting the Wemos D1 mini to the micro USB. This should end up looking like this:
Last you need to set up the Alexa skill. This will link up with the Python code on the Raspberry Pi to provide the skill with the light on time. First go to the Amazon developer console and make an account, this will need to be the same account that you have set up your Alexa with. Go to add a new skill and choose the language as English (UK); name the skill Smart Smart Home; put the invocation name as smart smart home and click next. On the interaction model page set the intent schema as:
{
"intents": [
{
"intent": "LightOnIntent"
},
{
"intent": "NoIntent"
}
]
}
Then set the sample utterances as:
LightOnIntent what time will the lights turn on
LightOnIntent light on time
LightOnIntent light on
LightOnIntent run
LightOnIntent lights
NoIntent no
NoIntent nothing
NoIntent stop
and click next. On the configuration page change end point to be HTTPS and type into the default page the ngrok address, for example: https://12a34b5c.ngrok.io/Alexa (Note you need to get this from ngrok which you should have started on the Raspberry Pi). Say no to geographical endpoints and click next. under SSL certificate choose "My development endpoint is a sub-domain of a domain that has a wildcard certificate for a certificate authority". Click next and test the Alexa skill.
See the videos of this working below:
Comments