I have a fish tank and I want to go on a vacation and need to feed the fish or sometimes I keep forgetting to feed them regularly or I'm just too lazy to feed the fish but i don't want to see dead skeletons in my fish tank so from time to time I have to interrupt whatever I am doing and go to feed the fish twice a day everyday. Why they just don't feed themselves, so I came up with this idea I will make them feed themselves and guess want I won't touch the fish tank unless once a week to fill the food.
Magical, isn't it? Also I can control the fish tank lights remotely.
Thanks to this guy in the picture I call him "Mr Feeder" and yes he's a plastic cup with some decorations I made for him to look like a fish (I assume) :D .
I can command Alexa echo device saying "Alexa, ask fish tank to feed the fish" and mr feeder will thankfully do the rest of the job and by connecting it AWS IoT I can ask Alexa to feed the fish from anywhere in the world.
The idea is that Alexa sends the command which is "feed the fish" to a Raspberry Pi through AWS IoT and Raspberry Pi controls the servo attached to "Mr feeder" and a relay module that turn lights on/off. And here is the result:
Steps to Achieve this
First of all this is the GPIO pins for Raspberry Pi 3 we will need it to know how to connect the servo and relay module:
1. The servo is attached to the plastic cup "Mr feeder" to rotate it with an opening that allows food to be dropped while rotation and the code makes 3 rotation for each command to be sure that enough food is dropped.
The connection is as follow or look at the schematics below:
3 wires PWR, GND, pulse of servo connected to pin 11.
This is important because the code use this pin to control the servo this part of the code is:
import time; import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(11,GPIO.OUT) p =GPIO.PWM(11,50) p.start(12.5) count=0 try: while count<=3: p.ChangeDutyCycle(12.5) time.sleep(1) p.ChangeDutyCycle(2.5) time.sleep(1) count+=1 except KeyboardInterrupt: p.stop() GPIO.cleanup()
Testing the code:
2. Relay module code:
import time; import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(11,GPIO.OUT) GPIO.output(11,GPIO.HIGH) time.sleep(2); GPIO.output(11,GPIO.LOW) time.sleep(2); GPIO.cleanup()
Note (in testing same pin 11 was used but in the final code we use pin 13 as 11 was used by the servo):
Connect the relay output to the lights wire it should be very easy, wire it all up as the schematic below and move to the next step.
Login to AWS or create an account and choose Lambda service:
Create a function:
Skip blueprints and click next:
Add Alexa Skill Kit:
Add function name and upload zip file from source code attached below and click next after configuring lambda role with basic execution role:
Review it and click create function:
Remember this section or take a note of ARN this is the endpoint to be used in next step:
Login to amazon developer console or create an account then choose Alexa:
Click Alexa Skill kit:
Add a new skill:
I have already submitted the skill to certification so no testing and data will be filled in the pictures:
Here we are defining interaction model and 2 intents are defined "feedintent" and "lightsIntent" with their sample utterance:
Configuration add ARN from previous step I have blurred it here:
Icons (Isn't it cute :D )
Now submit your skill to certification:
Return to AWS console again and choose AWS Iot:
Click on create a resource:
We need 3 resources: a thing, a policy, and a certificate
First create a thing name it fish_tank and click create:
Create a certificate, tick activate and click 1 click certificate create and download the certificates we will need to add them to the Raspberry Pi in folder called "certs" on Desktop to be able to connect to IoT:
Create a policy as follows:
Click add statement then click create:
Now we have 3 resources we should attach the thing and policy both to the certificate we created so after clicking on the certificate click actions and choose attach policy and enter it's name then add thing and enter it's name:
Now put the code file fishtank.py attached below on your /home/pi/Desktop/ destination and create folder ins same destination called certs if you haven't created it with certificates downloaded inside it and change their names to be used as in the code:
For rootCA.key you can download it from this link: https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem
Their names should be as follows:
root_cert = cert_path + "rootCA.key" cert_file = cert_path + "cert.pem.crt" key_file = cert_path + "private.pem.key"
Finally all is done, hope you enjoy it :D