Software apps and online services
Hand tools and fabrication machines
Your home is your Castle. Increasingly, we enjoy faster and faster deliveries to our homes, but we cannot always be there to take delivery of our packages (since we cannot all afford to hire a personal doorman!) So what happens with all those packages we need delivered?
Do we try to pick up our parcels elsewhere and add another stop to our already over-busy days? Do we put the burden of accepting deliveries on one of our neighbors? Can we trust enough to let random delivery personnel into our homes? Or do we just cross our fingers and hope that delivered packages will still be there, safe and sound (and dry!) when we get home?
We need a better and more secure solution for unattended package deliveries that balances our need for security without sacrificing home delivery convenience, that reduces redeliveries, and that gives us control and insight into our deliveries. The "Castle Locker" secure, voice-controlled, trunk project is my prototype for securing package deliveries at the front door of our castles, err, homes!
This project is broken into 6 major steps/phases:
1. Build the Trunk
2. Set-up an AWS IOT Thing
3. Create the Arduino Trunk Controller
4. Create the nodeMCU IOT Communication Controller
5. Create website/page to enable Alexa Account linking (optional)
6. Create Alexa Custom SkillSystem Architecture
Your trunk could be made from almost any materials and of whatever scale you choose. For the Castle Locker, I selected a toy chest I had purchased last year from Amazon, but never used.
Then I "pimped" the trunk with two linear actuators actuators from Firgelli Automations to automate the opening/closing - and to make it hard to force the trunk open.
I also added a solenoid lock, close button, 4x3 matrix keypad, PIR motion sensor, open/close magnetic contact switch/sensor, keypad passive buzzer (for key pad sounds) and a speaker (for "talking to" delivery people). These items will be mounted all dependent on the trunk that you use. Below are a few shots so you get the idea with regard to my chosen layout on this trunk.
I will not reproduce Amazon's very good documentation on this. Just follow the steps at https://docs.aws.amazon.com/iot/latest/developerguide/register-device.html to create a thing. Here are some notes that helped:
1. Copy the Thing Name, AWS Endpoint and Region for your thing. You will need these in the Arduino code and Alexa skill in future steps.
2. You will need to create an IAM user and attach an IOT services policy that permits access to the IOT thing. Be sure to copy the IAM access key ID and secret access key when you create the user, because you cannot get the secret access key later (you will have to recreate the user if you lose it!).
3. If you want, you can create a thing shadow document now and play around with changing the desired and reported states. The desired and reported values will be created later by the code, if you have not already defined the values you want to track in the shadow document. We are going to use three settings including:
- "voiceCmd2CastleLocker", for sending voice commands from Alexa to the trunk
- "castleLockerState", for tracking the open/close status of the trunk
- "noticeFromCastleLocker", for future notices like an incorrect code entry from the trunk to Alexa
See the attached code and circuit diagrams for the set-up for this step. The purpose of the Arduino Trunk Controller is to provide standalone (no WIFI connection) control of the trunk. The Arduino Uno Board is a good choice for the many inputs/outputs for sensors and feedback to the user. The trunk controller provides these major functions:
1. Detects motion when someone approaches the trunk.
2. Plays a greeting message directing the delivery person to enter an access code on the trunk's keypad to leave the package in the secure trunk.
3. Handles the keypad access code entry, providing feedback, as needed, for correct/incorrect entries.
4. Opens the trunk when a correct code is entered (disengages the solenoid lock, and powers the actuators to open the lid).
5. Monitors the trunk open state for 15 seconds and closes and locks the trunk automatically if the red button has not been pushed.
6. Listens for and communicates with the nodeMCU IOT Controller to maintain status between the boards and to accept commands to open/close the trunk initiated via Alexa.
As part of this step, you will need to generate the .mp3 files that the trunk uses to engage with the delivery person. I elected to use Amazon Polly to generate these files rather than trying to record them from a live voice actor. Amazon Polly has the advantage of creating sound files at consistent volumes and without background noise. Also, it is VERY fast to create professional sounding recordings and tweak them as needed simply by editing your script and regenerating the mp3. I found "Matthew's" voice to be the most natural for this project.
TIP: As noted in the code, name the mp3 files sequentially and copy them to the microSD card one at a time, and in order, so the DFRobot mp3 player plays the correct file. Also know that if you change the number or order of the files, you need to update the Arduino code statements to call the appropriate file number.
TIP: Furthermore, the DFRobot player is VERY temperamental about power. Make sure you fully power it with 5V and enough current. Otherwise the board will behave erratically, sometimes playing files and even stalling the main program loop. Pay particular attention to sudden power draws or, better yet, power the DFRobot mp3 player breakout board separately from other components.
TIP: It would be better to store the Access Codes in the EEPROM rather than hard coding them into the software. That will enable future features such as dynamically adding codes, deleting codes, and expiring codes from Alexa.STEP 4. Create the nodeMCU IOT Communication Controller
See the attached code and circuit diagrams for the set-up for this step. The purpose of the nodeMCU IOT Communication Controller is to provide internet accessibility for the trunk and to communicate trunk status to/from the AWS IOT thing shadow via MQTT pub/sub messages. Alexa will also update the thing shadow in order to send along voice commands to the trunk. After trying to put all the WIFI and MQTT IOT functions into the Arduino Uno Board with a WIFI breakout, I finally settled on the need for a separate board. By separating the WIFI and IOT functions onto the nodeMCU board, I also gained the advantage of the Arduino controller being more stable and less likely to fail due to a loss of internet signal. The nodeMCU IOT controller provides the following major functions:
1. Sets up and maintains a WIFI connection/reconnection.
2. Sets up and maintains subscriptions to the AWS IOT thing.
3. Uses MQTT messages and callbacks to process changes to the thing shadow - such as a voice command to open the trunk from Alexa. Note: I am using a variety of libraries in this board's program to provide these functions (see code include statements).
4. Powers the trunk open/close magnet contact switch/sensor to know whether the trunk is open or closed. The nodeMCU board also ensures we do not send an open command to the trunk if it is already open and vice versa for the closed state.
5. Listens for and communicates with the Arduino Trunk Controller to maintain status between the boards and to accept notices initiated from the Arduino controller that may need to be sent to IOT and Alexa (or other devices - like a text message via Twillio).
TIP: The nodeMCU board labels pins differently from the Arduino numbering of boards like the Uno. Use the pinout reference above to translate the pins into their GPIO numbers you define in the Arduino code.
TIP: The nodeMCU board can sometimes get itself into a state where you cannot flash the board from the Arduino IDE. First, disconnect all the pin leads and just use the microUSB port to try and upload code. If that still fails, you will probably need to re-flash the board from the manufacturer's GitHub site: https://github.com/nodemcu/nodemcu-flasher
TIP: If you want to see what is happening on both the Arduino Board and the nodeMCU Board from the serial monitors on your development PC, you will need to launch two separate instances of the Arduino IDE (use the MS Windows Programs menu to open the program twice!) so you can have each sketch have its own Board core. You will also need a second serial monitor program to watch one of the boards, as you cannot open two serials monitors from the Arduino IDE at the same time. I found PuTTY to be the perfect choice for this scenario.STEP 5. Create website/page to enable Alexa Account linking [OPTIONAL]
If you are only going to control your own trunk and keep your Alexa skill private, you will not need to complete this step. If, however, you want to have multiple trunks or users, your Alexa skill will need someway of distinguishing between the users, trunks, or locations, and this is where account linking may come into play. I published the Castle Locker skill as a public one, anticipating that I may want to try and bring this product to market (I also knew that going through the certification process helps point out potential problems in the skill and ensure the skill follows voice-first best practices). As a result, I needed to create an account linking webpage to work with the Alexa account linking function. Suffice it to say, but the webpage just needs to allow a user to login and then return a token that identifies (see the linkAlexa.aspx page and the codebehind file linkAlexa.cs both attached) the user to Alexa and then within your skill when someone invokes the skill. I chose an Azure website with a simple SQL Server based user table for my purposes, but you could accomplish this in almost any website platform, so long as the website can be secured with an SSL certificate.
The final step in this build is to create the custom Alexa skill to control the trunk with voice commands. The attached JSON intent schema and Lambda node.js code provides the functionality for Alexa to understand voice commands related to the Castle Locker trunk and to communicate to/from the AWS IOT thing. You will create the Lambda Intent Handler and use it's ARN as the endpoint in the Alexa Skill.
The attached skill code has passed certification, so it should serve as a good template for building similar skills that have an IOT interface as well (examples of which I found to be limited or one-sided in their interaction with IOT services). The following vocal commands are included in the skill at this time:
1. Alexa, open Castle Locker
2. Open my trunk
3. Close my trunk
4. Is my trunk open (or closed)
I envision extending these functions to be able to add/delete access codes for the trunk using your voice. I also see opportunities for receiving notifications on Alexa devices as to trunk access, incorrect entries, detected motion, the doorbell function from the keypad, and other delivery and porch related activities.
There are many enhancements that would bring this project along to a robust complete product. I hope my work-to-date can serve as a good foundation for a crowdfunding project. To bring the Castle Locker secure trunk to market I envision adding/refining the following things:
1. Move all electronic components from prototype breadboards and long wires to a custom PCB.
2. Design a truly waterproof trunk that provides a more robust integrated locking mechanism.
3. Add more event logging and control features to the controller code to allow temporary, one-time, and expiring access codes.
4. Enhance the Alexa skill to enable notifications from the trunk
5. Develop the Arduino and nodeMCU controllers into a stand-alone control unit that could be fitted to multiple trunks or other containers from 3rd party manufacturers.