One of the main problems boat owners' face (especially if you are forgetful like me) is about forgetting to close navigation lights while leaving the boat. That light stays on until the batteries are depleted. Even if some neighbour notices and calls you; you have to physically go there to turn it off.
So while I was thinking about a solution to that, I took it one step further and decided to build an actual boat hand with Alexa, which does not only open/close lights but also helps you with all the troubles you face while sailing alone.
So I created AlexaBoat, which not only enables remote access to fusebox, but also can help you with releasing the anchor X meters or turning the radio on/off without the need for going down and leaving the rudder.
You can simply ask Alexa to do all these for you.
You can access my published skill at this link: http://alexa.amazon.com/spa/index.html#skills/dp/B01IRA6R2EStep 1: Raspberry Pi, Relays and 3G Dongle
We need to prepare and test our hardware in order to execute commands. We need a 8-Port Relay Board for the project but instead I have 5 1-Port Relays, 3 of which was broken (I found out while doing this project). So I stuck with 2 relays. This is why I have limited number of Intents in Alexa Skill right now. I will increase them when I get an 8-port relay board.
Here are the parts we are using for the project:
I have a Raspbian preinstalled on a SD Card which I will work on.
All we need is some wiring. Just wire Relay VCC and Ground pins to the counterparts and connect input pin to any number you like (don't forget to change pin numbers in the code)
You can find GPIO layout below:
After I made these connections I connected my relays to leds on a breadboard to test my hardware and also test the necessary codes. You can find this final configuration here:
You can use this code to test your parts:
import RPi.GPIO as GPIO # import RPi.GPIO module from time import sleep # lets us have a delay GPIO.setmode(GPIO.BCM) # choose BCM or BOARD GPIO.setup(10, GPIO.OUT) # set GPIO number as an output try: while True: GPIO.output(10, 1) # set GPIO to 1/GPIO.HIGH/True print "HIGH!" sleep(4) # wait 4 seconds GPIO.output(10, 0) # set GPIO to 0/GPIO.LOW/False print "LOW!" sleep(4) # wait 4 seconds except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt GPIO.cleanup() # resets all GPIO ports used by this program
Lastly I connected a Huawei 3G USB Dongle to my Raspberry Pi because I want the system to be accessible 24/7 remotely. It was jus plug and play. No configurations needed but I saw that some devices has problems with Raspbian. There are workarounds for them, just google about your model.
You should be done with hardware part (for now), let’s move on to the software part of the project.Step 2: Create an AWS Lambda ARN for use with Alexa Skill
You need to create an AWS Lambda ARN. You need this for both Alexa to know how to respond to your commands and to execute the command which is asked from Alexa.
- Obviously you need an account for AWS.
- Go to AWS Lambda.
- Select Get Started then Next without selecting any blueprint
- Select the trigger for this Lambda function, which is Alexa Skills Kit.
- Go ahead and fill your configuration info. Select Node.js 4.3 as runtime.
- I supplied the code as attachment. Just create a folder named "certs" and copy your AWS IOT certificates (which you will download in future steps) in that folder. Do not forget to change file names in the index.js.
- Run this node app on your local machine for downloading the packages. Lambda does not download necessary packages when code uploaded so you need to zip node-modules folder and upload it with the code.
- Zip all these files and Upload Zip File. Create a handler role (or choose existing one if you have). In Advanced settings set timeout to 5 sec. 3 secs is enough but I had some timeout issues while testing with 3 secs.
- Then review and create the function.
- You will find Lambda ARN at the top right corner. Copy it for future use.
You need to create an Alexa Skill for speech recognition in our project.
- You need a developer account for Alexa.
- Add a new skill to Alexa.
- Choose a Skill Type, Name and Invocation Name.
- In Interaction Model step fill out the Intent Schema, Sample Utterances and Custom Slot Types. You can find these files attached.
- In Configuration Step, choose Lambda ARN and enter the ARN you copied in previous step.
- Now you are ready to test both your Alexa Skill and your Lambda function. Just enter some commands to text box and review your request and responses. At this point you can try your skill through Alexa enabled devices. Just enable your skill through mobile app then tell Alexa what to do ;)
- After that you can submit your Alexa Skill for certification process.
You need to create Alexa IOT resources for Raspberry Pi.
- Create a “Thing”. From AWS IOT page, click create a resource.
- First, “Create a Thing” and name it.
- Select your thing. Write down the REST API Endpoint on the right panel. Also note the MQTT topic. We'll need these later.
- You need to create a Certificate so that your device can authenticate to AWS IoT. Click “Create a Certificate” and use “1 Click Create”. Download the public key, private key, and certificate. We will also need these later.
- You can download Amazon rootCA keys from here. Copy the text from the browser, paste it into a file.
- Select your newly created certificate and click Activate from Actions dropdown.
- Now, go back to Actions menu, and select “Attach a thing”.
- In the popup form, type the name of your thing and click Attach.
- Best Practice: Create two sets of certificates and attach both of them to your thing. This way you can separate Lambda function and Raspberry Pi. This is a better security practice and it will help troubleshooting logs about which connection is doing what.
- Next we have to Create a Policy so that clients using the certificate will be allowed to connect to the endpoint. We’re going to create a wildcard policy. Click “Create a resource” and then "Create a Policy". Fill out the info. Add it and then click Create button.
- Now your AWS IOT should be ready for use.
Lastly we'll need some coding for our project to be complete. Basically what we'll do is to subscribe to MQTT service; listen to the messages and decode the message; then execute the function according to the message. You can find commented code attached.
Do not forget to create a "certs" folder and put your certificates into that folder. And if you are not running the code as root, it is better for you to create the log folder beforehand.
One thing to remember is to register the code for autostart in case of a restart. Usual methods did not work for me so I used this post for autostart. I recommend it if you have problems with that.Step 6: Real-World connections, wiring and testing
So far we only tested our system with 3 to 5 Volts and Leds. So let's move on to the boat and make some connections and wiring.
Warning: From now on we will be working with higher voltages and physical devices which can cause harm. Be careful while wiring electrical circuits and working with mechanical devices.
We should cut through electrical wiring of the Anchor Winch and install relays in between. Pay attention to cables, colors and poles. You could easily end up wiring wrong cables and cause short-circuits or always running winches (both of which happened to me)
Also we should install a relay between Light circuit of the boat. This is fairly easy because circuit board have spare holes for these installations (I didn't solder them yet).
You can see final assembly here: