Chuck Norris and Internet of Things
I have been tinkering around with the new AWS IoT button that i picked up a few months ago but due to work and tons of bot projects that i have been working on, never got the chance to work on this device until yesterday. It opened my eyes to many different use cases that could possibly be made happen, in near future. Very excited about this. The AWS IoT button if you dont know is a generic version of the Dash buttons that have been floating around on amazon.com for $4.99. You push a button to order your favorite detergent such as Tide or even diapers for your baby..etc..etc..Pretty sweet idea right ? So what if this button could be programmed to trigger a webhook that can be posted to any destination of your choice...That is what the AWS IoT button does. It does take a little bit of infrastructure to be setup before it will fully bloom to its full function. Thanks to AWS Lambda.
![](https://static.wixstatic.com/media/653e9f_438e9c8be99346eaa52fbcd48a623282~mv2.jpg/v1/fill/w_259,h_195,al_c,q_80,enc_auto/653e9f_438e9c8be99346eaa52fbcd48a623282~mv2.jpg)
AWS Lambda ?? what is that ? Its basically a serverless compute infrastructure in the cloud and lets you run code based on events that trigger the code. You do not need any server infrastructure to be maintained in order to run this. You pay by usage. AWS infact has a free 12 month tier that let you use it for free..What more do you need ? Sign up for Lambda and check it out. https://aws.amazon.com/lambda/.
The beauty of AWS Lambda is that you do not pay when your code is not running. Although all this looks a little too easy to setup, AWS can be the most overwhelming place unless you get accustomed to the environment and get used to their documentation and read it ten times..I will try to simplify things for you here in this blog post and see if it gets easy for you to grasp the specifics behind setting up the AWS IoT button and Lambda to do things on the Internet.
In the mean time, you must be thing what does Chuck Norris have to do with all this ? As you know he is the bomb and he is the man who made IoT possible..just kidding. I have been working on a Chuck Norris Bot for Cisco Spark. Feel free to use the bot by adding chuck-norris@sparkbot.io to your Cisco Spark room and just ask him "sup". See what he has to say to you. The Chuck Norris bot is hosted in a server at my home that snarks back a random Chuck Norris fact back to your 1-1 or Group Cisco Spark room. All you have to do is ask Chuck "sup". If you are in a group room @mention the bot followed by "sup" or if you are in a 1-1 room, just ask "sup".
My goal for the day was to setup the IoT button so that when i push the button, a random Chuck Norris fact would be posted into a Cisco Spark room. The way this has to work is AWS IoT button can only connect to AWS Lambda service and then Lambda has to do the integration to Cisco Spark. So
AWS IoT button---> Internet ---> AWS Lambda Code -->Get Chuck Norris fact -->Post to Cisco Spark Room
Setting up the AWS IoT button with Lambda
First off you have to setup your AWS IoT button with AWS Lambda. For this sign into your AWS Management Console and select AWS IoT.
![](https://static.wixstatic.com/media/653e9f_61ed4a5c804740ee866c0b9245b6b0e6~mv2.png/v1/fill/w_448,h_162,al_c,q_85,enc_auto/653e9f_61ed4a5c804740ee866c0b9245b6b0e6~mv2.png)
It will ask you for your IoT button serial number on the back of the device and this will then ask you to download a Private key as well as a Certificate PEM file. Download these files and save it safely as we will need it soon. Next you press the IoT button for 5 seconds and it will blink blue and goes into Setup mode. Once in Setup mode you will notice that it has setup a wifi network for you to connect to , so from your computer connect to it and then you can browse to the IoT button using its IP address. It will ask you to set the Wifi (at your home - choose a network of your choice), as well as upload the private key and certificate file that you downloaded in previous steps.
![](https://static.wixstatic.com/media/653e9f_c0c9053be5e64c9692a27f39745c7172~mv2.png/v1/fill/w_980,h_542,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/653e9f_c0c9053be5e64c9692a27f39745c7172~mv2.png)
Once this is all set, click save and your IoT button has been registered to AWS cloud.
The next step is to connect the AWS IoT button that is registered in the cloud and tie it to a Lambda function. A Lambda function is code that will run when the button is pushed. You can edit the code inline or you can edit it offline , zip it up and upload it with all the node-modules needed for your project.
![](https://static.wixstatic.com/media/653e9f_c36bfe0939db46f8b85e42d3cf4aafec~mv2.png/v1/fill/w_980,h_442,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/653e9f_c36bfe0939db46f8b85e42d3cf4aafec~mv2.png)
My code requires me to go fetch a random Chuck Norris Joke from internet and then post that into a Cisco Spark Room That is two separate HTTP request. One is a GET and one is a POST. To make things easier i decided to use the Request module. Now Lambda does not have any of these modules. So i took an offline editor like Atom and wrote the following code.
To create code and upload to AWS you need a linux machine of some sort. You can either use an AWS EC2 instance or if you have a linux machine like Ubuntu or Debian. We need the request module installed so that we can use it for HTTP GET / POST activities. So if you have not installed request before try logging into the linux machine and Create a folder called Lambda. Inside this folder create a file called index.js. index.js will host the code that you will run under the lambda function. To install request module simply use "npm install request". The node-modules will show up inside this folder at this point with request module installed.
![](https://static.wixstatic.com/media/653e9f_85fec9e206524523bd891ba934aa7bb4~mv2.png/v1/fill/w_926,h_182,al_c,q_85,enc_auto/653e9f_85fec9e206524523bd891ba934aa7bb4~mv2.png)
Now lets go back to creating the code we need for posting the joke to the Cisco Spark Room. Keep in mind that your code has to be wrapped in exports.handler. So if your file is named index.js, Lambda looks for exports.handler.
![](https://static.wixstatic.com/media/653e9f_e903a77769d746f394c2a78f2d9580b6~mv2.png/v1/fill/w_980,h_472,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/653e9f_e903a77769d746f394c2a78f2d9580b6~mv2.png)
This first step above is a HTTP GET that will retreive a random joke from ICNDB.com and the joke itself is retrieved as JSON data and is stored in the body.value.joke key.
Next i have to take that data and then craft a message to post into a Static Cisco Spark Room ID. This room ID is in the HTTP POST Body as you can see below. I am using markdown here to highlight the message in bold.
![](https://static.wixstatic.com/media/653e9f_5ff0b30348894c79b89b44a41b5f55c1~mv2.png/v1/fill/w_980,h_379,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/653e9f_5ff0b30348894c79b89b44a41b5f55c1~mv2.png)
Once we have this code written and saved, zip the entire contents of the Lambda folder into a tar.gz (Please remember not to compress the Lambda folder. You have to be inside the Lambda folder on command line and zip only the contents of the Lambda folder).
![](https://static.wixstatic.com/media/653e9f_55474a5184494370950426ca413e0f23~mv2.png/v1/fill/w_980,h_125,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/653e9f_55474a5184494370950426ca413e0f23~mv2.png)
Once you have zipped up the file come back to AWS Console and upload it here.
![](https://static.wixstatic.com/media/653e9f_12df4804442549369951426c0f91ca8d~mv2.png/v1/fill/w_980,h_517,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/653e9f_12df4804442549369951426c0f91ca8d~mv2.png)
Also make sure that you Save and Test it using the Test button. Also make sure that in the triggers tab you see the IoT button you setup earlier.
![](https://static.wixstatic.com/media/653e9f_e92fdc07b8f142ba9f750584a043f6e1~mv2.png/v1/fill/w_980,h_334,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/653e9f_e92fdc07b8f142ba9f750584a043f6e1~mv2.png)
While testing, any run time errors will be posted on the web console. Now you are all set to Test the IoT button to Cisco Spark integration. When you push the AWS IoT button, it triggers the Lambda function which in-turn runs my Lambda node.js code which in turn fetches the random joke / fact related to CHuck Norris and Posts it to my Cisco Spark Room of choice.
![](https://static.wixstatic.com/media/653e9f_59cacb02c9be41e39cc173354cdb4a16~mv2.png/v1/fill/w_980,h_377,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/653e9f_59cacb02c9be41e39cc173354cdb4a16~mv2.png)
There you go, there is your Chuck Norris IoT button. I hope you enjoyed this blog post and the marvels of IoT that are yet to come in near future...