Part Three Of Building A Motion Detector With Raspberry Pi 3 And Aws
It has been a while, but I am back with the third installment of building a motion detector with AWS and raspberry pi.
First, as always though, I need to make a few assumptions:
Assumptions:
- Everything(and all the assumptions) from part two has been completed/adhered to.
1 Setting up SNS
1.1: In the AWS management console, navigate to Simple Notification Service(SNS).
1.2: Here, select “Create a Topic”. For this use case, select a standard topic rather than the default, first in first out(FIFO).
1.3: For your topic name, choose “Motion-Alert” (or whatever you fancy)
1.4: Finally, press “Create the Topic” to save your changes.
1.5: Now, we have to make sure that the SNS topic subscribes to the correct topic. We can do that by creating an SNS subscription. To do this, first, select your topic.
1.6: In the topic details page, select “create a subscription”.
1.7: You have a plethora of options to select for protocol/endpoint, but for our case, we are going to choose SMS for protocol and phone number for the endpoint. I recommend using your own phone number. I added a friend to the list for testing. They were not entertained.
1.8: Choose “create subscription”.
1.9: Publish a message to test it out!
2 Sending data to our SNS topic
2.1: So….we created a topic, cool. However, that topic does not have any data being sent to it currently. To do that, we have to set up a rule that will look for the payload from our device, and link it with the SNS rule. Let’s refer back to our architectural overview to solidify the connections:
Ok, we sent data from the raspberry pi to IoT Core in part two, right? Yes, hopefully. If this part is working, then payloads should be sent to a topic called “data” in IoT Core. IoT Core is subscribed to changes that are published to a topic(like payloads with motion data!). In SNS, we have this whole other thing going on. A topic was created that has one subscriber(endpoint: phone number). Subscribers will “do something” every time that topic receives a payload! However, these are not connected right now, so we have to find a way to subscribe that SNS topic to changes in the IoT core topic; connecting the two is done by creating the aforementioned rule.
2.2: In the management console, navigate to IoT Core and then select the “ACT” dropdown. There, select “Rules”
2.3: Select “Create A rule”.
2.4: Perfect! You should be here:
2.5: Now, add a name. This can be whatever you want, but setting a relevant name will make dealing with a lot of rules much easier.
2.6: Now, let’s add some SQL. Before just copying the query though, think about the problem and try and construct it yourself.
The problem is: we need to grab certain data from a topic with the name raspberrypi/data
The answer: SELECT VALUE [date, time, motion] FROM 'raspberrypi/data'
Note: this is the way I did it, but if you have a better query, cool. Use that one!
Go ahead and enter that into the “Rule query statement” section.
2.7: Now, add an action! Select “Send a message as an SNS push notification”. Here, choose the SNS topic you created earlier and “RAW” format.
2.8: The rule is good to go, right? No! Not yet, we have to give it the correct permissions! Do this by creating a policy called sns_iot_rule_role
that allows the rule to target the SNS topic.
2.9: Nice! Now, create it!
3 Testing it out:
3.1: Simply run the program on raspberry pi and set off the motion detector. Double-check that data comes in on the IoT Core topic and then wait for a message!
4 Rejoice!
You created a motion detector that sends notifications to users via AWS IoT Core and AWS SNS. If you have any questions or comments, feel free to reach out!