This challenge was created to demonstrate the power of Amazon Alexa and Raspberry Pi for IoT purposes. IoT stands for Internet of Things and is currently taking the world by storm. Seamless intergeneration of devices and gadgets has become very important to consumers and the industry has been forced to respond. Amazon has now joined the market with the invention of Alexa and its ability to have control of voice commands. When you ask Siri the temperature, she will give you a good response and shut off. However, when you ask Alexa the temperature, you have the authority to turn on air conditioning if the temperature is too hot, or heating when it is too cold.
Our SubmissionFor our submission we wanted to mimic something like IBM Bluemix so we interfaced Amazon Alexa with the mighty workflow, drag and drop power of Node-Red. By transforming a Raspberry Pi 2B+ into a secure apache web server Amazon Alexa skills could be directed towards the Pi. Thus, leaving endless possibilities to any level of developer. We believe a very important aspect of IoT is the ability to customize, this becomes difficult with the vast amount of APIs and platforms needed to be explored in order to create integration of products. This takes hours of searching through documentation, learning new programming languages, and copying and pasting Stack Overflow code ;). By using Node-Red, we eliminate the need to learn a bunch of APIs as there are custom nodes already built for most devices and social medias.
Our project has the functionality to log weights during a workout, log cardio, and to play music on command. There is a MYSQL database setup on the pi that contains weeks of your current workout plan. When you ask Alexa for the workout of the day, she will give you the muscle group and the exercises you will be working on. Once you are at the gym/home working out, all you have to say is "log 25" and it will insert that weight into the database, and respond with telling you the next exercise you have to do. The point of this was to give a hands-free experience to gym users like ourselves who hate having to bring in a book and pen to the gym. Pages start getting torn out, the pen gets crushed by your max deadlift, and who wants to write anything down after you've done your third set of pull-ups to failure? No, you don't. Alexa knows where you are in your workout from the moment it begins, all you have to say is "log {weight}" and she will tell you what is coming next. The next thing our project does is log runs using the Fitbit API. My Fitbit Flex has UI on the device and I have to manually put in my cardio, because of this we have another database set up for logging runs. Just tell Alexa when you start your cardio and she will log your current steps, distance, and calories of the day. Once the run is over she will enter the new current steps, distance and calories to take the difference with the starting information. The result is a response from Alexa telling you how well your cardio went! It will also keep the logs in the database to track your progress. Last but not least, we have the ability to play music off the pi for home workouts. Tell Alexa you are starting a workout and she will play your workout playlist, tell Alexa to plat mood music and she will check the weather to plat a specific playlist based on the forecast.
ConclusionOur main goal of this project was modularity and customization for IoT developers. Dynamically being able to add more and more functionality to any unique project. We used fitness as the basis of our project because this is what we are interested in, but with the system we set up you can do so much more. Using Node-Red and Raspberry Pi does not just aide our fitness applications, it helps any developer for any purpose. We hope that developers follow our tutorial and realize how easy it to swap out nodes and add functionality. If you are open-minded, you won't just see voice controlled workflows that allow you to log workouts, turn on playlists, and interface with Google, Fitbit, or Twitter. You will see the potential to replace nodes to log recipes, turn on sprinklers, and even control drones with the power of Amazon Alexa and Raspberry Pi.
Setting Up Everything!There was a lot of setup in this project, we will go through each process individually to reduce confusion and maintain repeatability of making this system from scratch again. So hold on to your hats! This is gonna be a learning experience for everyone.
Setting up Secure Apache Server on Raspberry Pi- First write the Raspbian Jessie image onto a SD card and put it into the Raspberry Pi
- We then followed this tutorial to get the apache server running on the Raspberry Pi. Please refer to the link for this step as it was well documented. I will list some important notes.
- Keep a copy of the original files you manipulate for this step (such as the files in the etc/apache folder) because if something goes wrong it is much easier to replace the files then to redo step 1.
- When you set up your port forwarding, test it is actually working properly by going to http://www.canyouseeme.org/
- Make sure to also set up port forwarding on port 443 as this is the secure http port that is needed to work with Amazon Alexa
- If you use DNS Dynamic for your free hostname (like in the tutorial) make sure you go to navigation bar ->Webclient and then click Start, in order to properly activate your domain. If you do not do this it will not properly connect with other online devices.
- We bought our SSL certificate from NameCheap.com for $11/year , but you can make your own for free using openssl.
- When you buy your SSL Certificate, NameCheap will give you very clear instructions on how to setup your SSL Certificate on your computer. It is a very quick and painless process. You can see my final configuration file at the end of this section.
- When you setup all the SSL components you may realize that it is not activated yet. To update the apache server with the SSL certificate you must go into the raspberry pi terminal and type the command sudo a2enmod ssl, followed by typing the command sudo service apache2 restart
- Bam! You should now have secure apache web server running on your pi that can be accessed by the world wide web! :D The last step is to go into the file where you put the SSL certificate information (my path is /etc/apache2/sites-enabled/000-default.conf ) and change the DocumentRoot variable to /var/www/php, as shown below.
- To do this go to var/www/ directory through the terminal. Create a folder called php. Within the php folder create a file called index.php and insert this code into the file.
- If you have not already, you must become an amazon developer
- Once you have done so, go to the Alexa tab and select the "get started" for Alexa Skill Kit
- I followed the steps of making a Amazon Skill, implementing an invocation name and overall name for the skill
- Then I added the intents (here) and utterances (here), along with slots to actually be able to send variable information to the raspberry pi. For logging of the workouts I used the type AMAZON.NUMBER to be able to receive a number through the request. There are many different types of requests you can make, check them all out here
- With the pi setup as a apache server, with a domain name and https access, I was able to use it as my endpoint. This was very important, as without this I would not be able to make requests to the pi.
- I bought an SSL certificate from https://www.namecheap.com/ and therefore, I selected the option as my endpoint has a trusted certificate authority.
- Then came testing, making sure I was getting the responses I was supposed to.
- If you have not already, you must become an amazon developer
- Once you have done so, go to the Alexa tab and select the "get started" for Alexa Voice Service
- Choose to register an application or a device. If you want to put Alexa Voice Service on your phone like we did, select application
- Now fill in all of the information such as Company Name, Application Type ID, etc and click Next
- Now you should have your Security Profile ID, Client_ID and Client_Secret
- If you would like to use Xamarin like we did, download the demo source code for Xamarin Alexa Service here. Depending on your mobile device, you will need to follow different instructions on the Xamarin Alexa Service website Getting Started section
- After placing your client_ID and client_secret in the appropriate files, you should be able to make Alexa commands off of your mobile device!
- To install Node-Red on the raspberry pi I followed the Node-Red on Raspberry Pi tutorial
- It is very important that when you install extension nodes you run npm install <extention node name> in the .node-red folder on the raspberry pi. For us, the file was in ~/.node-red. Remember the "." before the name means its hidden
- Now we move on to communication between the secure Apache web server on port 443 and the Node-Red server running on port 1880. To do this do this, we will use a simple php script
- The php script (here) uses file_get_contents( 'php://input') to get the request sent by Alexa, and then writes this information into a file. Then we make the php script sleep for 2 seconds. This is to give the node-reed flow enough time to complete and put the response information into a response file.
- Once the php sleep is done, it will read from the response file, that node-red has just put the response in, and echo it back for Alexa to read out loud
- On the node-red side there is a lot more work to be done. We needed some way to be able to listen for information coming from the apache server on port 443. To do this we use the "watch" node in the advanced section of node-red. The "watch" node is setup to watch for changes on the request file that gets written to by the php script. When the php file writes to the request file, the watch node will trigger the main Alexa flow to begin
- Once the flow has been triggered it does the following steps
- Reads the content from the request file and converts it to JSON format
- We must then filter the type of request that was sent by Alexa. We are only interested in "IntentRequest", so we use a "switch" node setup as shown below
- Now that we have filtered for Intent, we have to filter for type of intent, so the output of the switch node from step 2 will go into another switch node setup as shown below. Remember, the intent names are determined by what you called your intents when setting up your Alexa Skill
- For each intent you should have a different flow; this is where we now put the logging workout flow, logging cardio flow, etc. What is important is at the end of the intent flow, you put what you want to be responded back to the user. To do this, in your last function node of the intent flow write msg.payload = <stuff you want Alexa to say>.
- All intent flows go to the same place in the end; the response you want Alexa to say will be put in the proper format to then be written to the response file for our php script. This is done by adding a "template" node setup as shown below. As you can see the payload that we fed our flow in step 4 will be the text for Alexa to read back to the user
- We then write the contents from step 5 into the response file using a "file" node
- The response file gets read from the php script and echoes it back to Alexa to be read out loud. And bam! You have now completed the hard part of this tutorial. Now everything else is up to you! You can follow the rest of this blog to see how we created our project, or you can make your new flows for your own projects.
I followed this tutorial to get MySQL running on the pi.
- Install mysql on the pi by running sudo apt-get install mysql-server in the terminal
- Follow the instructions for install
- Open MySQL by running mysql -uroot -hlocalhost -p in the terminal. You have opened the SQL command line and make sql requests now.
- I exported my database (here) used in this project and put it on github for easy import. You can follow this tutorial (here) to import the database.
- Now that the database is setup, we need to get it on Node-Red. Run sudo npm install node-red-node-mysql into the terminal to install the mysql node.
- Restart Node-Red and navigate to the side-menu. You should see the mysql node under storage. For an example lets use a basic input and debug flow.
- Now lets configure the node with our credentials from mysql.
- The queries for the mysql node must be sent through msg.topic not msg.payload. So in the Input node put an example query such as SELECT * FROM exercises, workouts where workout_date = "2016-08-21" and day_id = day (NOTE: make sure you make a proper query based on the names of your database and tables).
- You should get an output to the debug node should be like this.
- And thats it for setting up the mysql node!
- The first thing that you need to do is become a Fitbit developer. https://dev.fitbit.com/apps
- Sign in and follow the guided steps. Eventually you will end up at a page that looks like this, where you should be able to see your skill.
- When you click on your skill you will see your Apps client_id, client_secret and everything else you would ever need to know about your app (I did not post picture for security reasons). Click on the link OAuth 2.0 tutorial page. Using your client_id and client_secret follow the steps to get your access key and refresh token. Now comes the fun part!
- To use the Python Fitbit API you need the UPDATED python-fitbit library. Make sure PIP is up-to-date. then run the command sudo pip install fitbit to get the python-fitbit library
- Next setup your fitbit python script like this, using your own client_id, client_secret, access_token, and refresh_token you got from the step before..
- With the authd_client initialized you can now use any of the python-fitbit functions available to grab all sorts of data! You can see my examples used in this project on my github.
- Next run the script in the terminal by navigating to the location of the script and type python *script_name* you should receive information back in JSON format of your request.
This was used to play the local playlists off of the pi, this was fairly simple.
- Pygame libraries were already included with the pi, so no install needed there (if you do not have it installed, just use PIP or APT-GET)
- Setup a folder on your pi that the playlists will be in held in. we made a directory on my pi called “Songs”, (using mkdir Songs). Within the Songs folder we made other folders that are the playlists. So for the Hot, Cold, and Workout playlists used in the workflows, we made a folder called Hot, Cold, and Workout.
- The python script musicplayer.py takes in a folder name as a parameter. The program will loop through all the songs in the folder (playlist).
- You will notice in the script, I use the full path of the folder, not just the relative path from the python script. This is important for the Node-Red flow, as the relative path to the playlist folder is not the same when using the Exec node in Node-Red and will cause python script to crash. To make sure this is not happening to you, implement a try except statement that catches the OSERROR.
OpenWeatherMap node is used to retrieve a weather report (Current weather data) from Open Weather Map website.
- Run the following command in the Node-Red directory. npm install node-red-node-openweathermap
- In order for the OpenWeatherMap node to work you will need to create a free account so you can generate an API key.
- Sign up at: www.openweathermap.org
- Once you have an account you will be prompt to select one of the following weather options (under the API tab). For our project we selected "Current weather data".
- Once you selected what weather API you wish to work with, make sure you select "Get API key and Start" - under the "Free" column.
- You will receive a default API key, however id you wish you can generate your own (unlimited).
- Now restart Node-Red and look for the openweathermap node in the left panel. Setup a small flow with an inject, the openweathermap node, and a debug node (just like we did for the MySQL node).
- Double click on the node and put in your API key you got from openweathermap, as well as the location of where you want to get your weather readings.
- When you hit deploy, you should see the debug node receiving JSON data about the current weather of that location.
That is it for this tutorial! I hope you were successful in all of your installations and configurations. If so, you now have a powerful, drag and drop Amazon Alexa and Raspberry Pi Development Platform for creating Smart Home systems. If you have any questions or comments, feel free to post about them :)
Comments