There are plenty of maker projects out there that show you how to connect a generic PIR motion sensor to a microcontroller. For those interested in learning about PIR motion sensors, then Adafruit's How PIRs Work tutorial is worth a read.
This proof of concept project (POC) takes it one step further, by demonstrating a convenient method of using your smartphone to get the microcontroller to either ignore a PIR event trigger, i.e. when the sensor is set as disarmed, or get the microcontroller to respond with some form of output or automated action (in this case send a user notification via the Internet and trigger a buzzer), when the motion sensor is seen as armed.
The aim of this project, was to only allow smartphone user control when the user was in the vicinity of the PIR sensor. As such, the system is built around a BLE (Bluetooth Low Energy) module, as the actual wireless signal transmission range of the BLE module can then be used to limit how far way the user can be and still control the system via their smartphone. The brains behind the whole system is handled in the cloud and as such requires Internet connectivity using a wifi network.
The control system developed also includes a simple user classification, between a family member (which has full control 24/7) and a friend (which only has control during certain periods of the day etc.). For the purposes of this POC, a "friend" user gets arm/disarm control from 9am till 9pm.
A buzzer is also attached to inform the person in the room that they've been detected. Two buzzer tones - when the user first walks in and nothing has been detected before hand a short warning tone is given. After about 20 seconds this switches to a longer tone to mimic alarming scenario (in video demo you hear this at the beginning). When the user clicks "Enter Room" and enters in their code, the buzzer stops as system is disarmed. When the user clicks on "Leaving Room" there is a short time delay (to allow user to leave) before the warning buzzer kicks in (you see/ hear this at the end of the video demo).
System OverviewThe system is controlled by three elements, namely:
- the Android App, handles user identification and notification that they wish to disarm the PIR sensor (i.e. enter the room) or rearm the sensor (i.e. leave the room), the scanning of the BLE module and the http communication with the Electric Imp cloud service (or agent);
- the Electric Imp microcontroller device, handles the reading of the PIR digital signal output, the activation of the buzzer, the serial communication with the BLE module and the secure comms between device and the Electric Imp cloud server (or agent); and
- the Electric Imp cloud (or agent), handles the uploading of firmware to the Electric Imp device, handles the data transfer with to the Electric Imp device, handles the http communication with the Android App and manages the Slack web service API, which sends notification of events.
MIT's App Inventor was used to create the Android App. This is a free open-source cloud-based app creation tool. There are plenty of tutorials + teaching aids on the website to help you get started. A handy advantage about using App Inventor is that it is really quick to create an app, it provides a connection bridge to allow you to test your app on your phone and it allows you to build and save ".apk" compiled code on your computer which you can then email to your friends and family with specific upload instructions (basically all that a family/friend user does is open up the email on their phone and click on the attachment. Then if the user has the "allow external apps" option ticked, the app will install on their phone).
To create an app, simply click the "Create apps!" button, as shown. You'll then need to login to the system (will leave that up to you).
The first thing to do is create a new project and then you will be presented with a blank layout design, as shown in "Views", for a generic phone. You can then create your User Interface using the component "Palette" on the left of screen. However, before jumping straight into adding components it is generally good practice to define a layout using the "Layout" options provided. The layout and top-down sequence of how the components will be layered on your screen come runtime is shown in the "Components" - Screen1 is typically the first / top component. There is also a range of hidden components offered in the "Palette", such as a wifi connectivity and a clock component, for example.
There is no "one size fits all" approach to App layout design. It is very much up to the individual as to whether they wish to animate, include background images, structure a landscape or portrait layout and how they wish to space out components such as buttons, etc. As such the specifics of each visible component and how they fit together is not included in this write up.
Nevertheless, an overview of the overall design layout is provided, which shows two user buttons (for "Enter Room" and "Leaving Room"), two labels (which are used to provide user feedback) and an image (which is simply there for show).
The power behind the app is provided through added non-visible components. These are as follows:
- Bluetooth LE component. This component is an addon which you import into your App Inventor project. The component is found in App Inventor Extensions. A link to documentation on how to use this BluetoothLE component is also provided here. You may also have noticed in the above diagram that a BluetoothClient component is also included. This is only required to overcome an access rights problem when uploading and running the actual app on the phone (note that this problem is not seen during testing. You can search MIT App Inventor help forum for more info about this). This component has no design-time properties.
- Web component. This component is found within the "Connectivity" palette. The purpose of this component is to handle the http communication with the Electric Imp cloud server. For the purposes of improved readability in the project, this component was renamed "ImpAgent". The component has a number of design-time properties, which includes an option to specify a URL (note this can also be specified / changed at runtime too). In this URL input box, the Electric Imp Agent URL should be specified... as in https://agent.electricimp.com/---insert_unique_agent_id---plus_optional_path_information_here
- Notifier component. This component is found within the "User Interface" palette. This component is used to provide "toasts" or temporary popup messages. It is also used to get the user to enter their unique user code (for determining if family or friend).
- Clock component. This component is found within the "Sensors" palette. This allows for a periodic timer to trigger internally within the app. The timer is used to periodically trigger the BLE scan functionality.
The rules engine driving the app is developed using "Blocks" - you click on the "Blocks" button as shown to enter this part of the app developer.
Screen captures of how the building block were packaged together for this project are provided below. The timer interval is set so that the app scans for BLE devices over 3 seconds and repeats this every 6 seconds. The app uses a simple toast at startup to remind the user to enable Bluetooth on their phone.
When the user clicks on the "Enter Room" button the "StartScanningBtn" click event is triggered. This invokes a request for the user to enter their user code. Once this is entered the app scans for BLE devices until the timer fires. When the timer fires a "Clock1" timer event triggers. The BLE scan information is then sent to the ImpAgent using an http POST. When the ImpAgent responds back, a web "GotText" event is triggered. The response from the imp provides real time user feedback which then updates a screen text label. There are two textual feedback options using the text labels "GreetingLabel" and "ScanResultLabel".
The app also includes some user feedback by changing background colours on app to inform the user that the bluetooth scanning is taking place etc.
Once the app has been constructed, you can test it using the "Connect: AI Companion" menu bar option, which will then link your phone (requires the MIT AI2 Companion App) to your code. Please refer to the MIT App Inventor website if you are not sure how this works.
Once you are happy with your new app, you can build and install it on your phone in two ways. The first is via QR code. This method is probably only suitable for you the coder. The other method is to compile and save an "apk" file to your computer. You can then email this to your family and friends with instruction (as explained in the introduction).
Here are some of the screenshots from the app, showing different feedback messages etc. As you can see, it has a very simple user interface with just two buttons. Note that as most smartphones do not allow for BLE transmissions at present you will see a warning "Unsupported" notice when app loads, even though the App does not use BLE transmissions. According to MIT App Inventor forum this will be resolved at next update of the BLE component.
An Electric Imp IMP002 breakout board from Sparkfun was was used to build the PIR monitoring device (only because I had one spare). Any of the Electric Imp boards (IMP001, IMP003, IMP004, etc.) could be used for this project. In this project the 3.3V board is powered using a 5V DC power supply via the USB mini-B connector as needed a minimum 5V for the PIR sensor.
SeeedStudio Grove modules were chosen as requires no additional hardware interface to be built. It is really plug and play. As the PIR module was not a Grove module it required a voltage divider to make the digital output signal compatible with the Imp GPIO's.
The device was housed in a wooden enclosure only because I had one spare. Then a very clever trick, if you wanted to conceal the purpose of the enclosure in a room, is to wrap the box using a thin white plastic bag (I used an old plastic shopping bag which had a pattern on it, but made sure pattern did not cover the PIR lens). Depending on the thickness of the bag, it does not cause any noticeable impact on PIR detection performance.
Configuring device firmware is quite simple:
// Imp Device Declarations
// UART bus, PIR Trigger and Buzzer GPIO pins
UART <- hardware.uart1289;
PIR <- hardware.pin2;
Buzzer <- hardware.pin7;
// Hardware Configuration
// ----------------------------------------------------------------------
UART.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, BLE_UARTcallbackfnc);
PIR.configure(DIGITAL_IN, PIR_GPIOreadCallbackfnc);
Buzzer.configure(DIGITAL_OUT, 0);
Communicating with the HC-11 BLE Grove module is via AT commands:
UART.write("AT+ADDR?"); // gets the mac address
UART.write("AT+NAME?"); // gets the name of the device (note you can also change the name at runtime if required)
Let's glue together with Imp cloud server and SLACK web serviceFor the purposes of this project, the Imp Agent only has to handle three routines, namely:
- an http request handler: This handles all requests from the user's App.
- an Imp device handler: This handles updating the data table with the BLE mac address and the name. You can no doubt in more functionality here and use some of the many Imp libraries to improve agent-device resilience and manage the communication between the two.
- a SLACK http request handler: This handles the posting of message to the custom SLACK webhook.
Setting up a custom Slack webhook is very straightforward. It uses a common component found within the Slack API (refer to the documentation for more info), called "Incoming Webhooks". First things first though, is that you to create your own "team" or account, if you do not already have one. Just enter your email address and click on the "create a new team button". Then, when you are in your team dashboard, click on team name (top left of screen) and click on "Apps and integrations" option (below Team settings). This will open up a new tab on browser, titled App Directory. Now click on the "Manage" menu option (top right of screen). You will see on left hand side a range of options under the header "Manage". You then click on "Custom Integrations" and this will give you two options "Incoming Webhooks" and "Slash Commands". You can now define your webhook from give it a Channel name and following that a Webhook URL will be generated, which you will need to copy and paste into the Imp Agent. Then you are essentially good to go.
Comments