Programming the Pi
For this part of the tutorial, we’re going to use a Raspberry Pi 3, loaded with the most common OS distribution, Raspbian (a port of Debian Linux). Follow the instructions that came with the Raspberry Pi to set up the device.
For this walkthrough, we're providing (see below) a simple Node.js application that connects the Raspberry Pi to the EVRYTHNG cloud and shows how to interact with the Nest Cam (although the same process applies to any other Nest Device).
In our application, the Raspberry Pi will function as an independent cloud connected device that will react to the Nest Cam motion events in the different activity zones. When motion is detected in any of the zones, the Raspberry Pi will trigger an alarm in the cloud, represented by a property of our Raspberry Pi device. The alarm will remain on until it is stopped when the spacebar is hit on the keyboard connected to the Raspberry Pi.
Additionally, the Pi will serve a simple HTML page with the status of the alarm and the current snapshot of the Nest Cam.
This will give an overview of how a third party device can be connected to the cloud and react at the same time to Nest events, being able to alter its behaviour based on them.
## Download the Sample Application
Check out the repository evrythng-connectors, and clone it to your Raspberry Pi in an easy to access location. It contains two files:
workswithnest.js
is the sample Node.js application.package.json
contains details of the dependencies needed for the application.
Installing Required Libraries With Node.js and NPM
Although you can access the EVRYTHNG Platform via multiple SDKs and languages, we’re going to use Node.js and our evrythng.js SDK for this walkthrough. If your Pi does not yet have Node.js installed, follow this link to learn how to install it on your device.
To install the dependencies, just run the following command in the same directory as the application and the package.json
file.
npm install
The libraries that are used in the application are:
-
evrythng-mqtt
- This library lets Node.js create and manage connections to the EVRYTHNG Platform using the MQTT protocol. -
evrythng-extended
- This library allows us to manage and control Thng objects. -
keypress
- This library allows you to listen for keys pressed in a keyboard connected to the Pi. -
express
- This library provides the ability to serve web pages and allows us to show the user the latest image from the camera.
## Set Up the Node.js Application
A Node.js application can run when executed by the user, or as a headless application that runs all the time when the Pi is started. For the purposes of this walkthrough, we will start and stop the application manually.
By default, Raspbian comes with either a terminal based application, nano
, or a GUI-based text editor, appropriately called ‘Text Editor’. Use whichever one you are comfortable with.
Open the file workswithnest.js
and find the configuration section. You need to specify the parameters to connect to the EVRYTHNG Platform and to the Nest Cam.
const userAPIKey = ''; // Your Application User API Key
const cameraThngId = ''; // Your Nest Cam Thng ID in EVRYTHNG
const raspberryPiThngId = ''; // The Raspberry Pi Thng ID in EVRYTHNG
-
Replace
userAPIKey
with the Application User API key, and theraspberryPiThngId
with the Thng ID from the Thng Details page for your Raspberry Pi. -
Replace
cameraThngId
with the Thng ID from the details page for your Nest Cam Thng.
Listening for motion events in an Activity Zone
To receive notifications on the motion events from the Nest Cam you just need to subscribe to the last_event
property updates, as you would subscribe to any property of an EVRYTHNG connected device using MQTT or WebSockets.
The sample code below shows how to read information from the Nest Cam that we have configured and subscribe to the motion events, then check for the activity zone name and react by starting the alarm on the Pi.
let nestCamera = null;
user.thng(cameraThngId).read().then((thng) => {
nestCamera = thng;
console.log(`Found Nest Camera!: ${nestCamera.name}`);
// Subscribe to the motion events
nestCamera.property('last_event').subscribe((update) => {
// Filter events with activity zones only
if (update[0].value.activity_zone_ids) {
// Check which activity zones were triggered
update[0].value.activity_zone_ids.forEach((activityZoneId) => {
// Find the activity zone name
nestCamera.properties.activity_zones.forEach((activityZone) => {
if (activityZone.id == activityZoneId) {
console.log('Motion event detected in activity zone: ' +
`${activityZone.name} [${update[0].value.start_time}]`);
}
});
});
// Start the alarm in the Pi and update in the cloud
startAlarm();
}
}).then(() => {
console.log('Subscribed to camera motion events in activity zones');
});
});
## Updating Your Raspberry Pi Status in the Cloud
One of the advantages of connecting your Nest devices to EVRYTHNG is that you have additionally out of the box cloud support for your own devices with the same model and SDK.
How to update properties of your device in the cloud is as simple as updating one of their properties with the new values. In the code below we show a simple status change of the alarm both internally and in the cloud.
function startAlarm() {
if(alarmStatus === 'off') {
alarmStatus = 'on';
// Update device status in the cloud
raspberryPi.property('alarm').update({
value: alarmStatus
}).then(() => {
console.log('Alarm status ON updated in the cloud');
});
}
};
function stopAlarm() {
if(alarmStatus === 'on') {
alarmStatus = 'off';
// Update device status in the cloud
raspberryPi.property('alarm').update({
value: alarmStatus
}).then(() => {
console.log('Alarm status OFF updated in the cloud');
});
}
};
Requesting the Latest Snapshot from the Nest Cam
To work with Nest devices not only can we subscribe to events, we can also access their properties at any time in the same way and combine them with any additional logic in our Raspberry Pi device.
In our sample application we show how to serve a simple HTML page that displays the status of the Raspberry Pi simulated alarm and the last snapshot taken from the Nest Cam. Accessing the properties in real time of the Nest Devices is as simple as using the correspondent EVRYTHNG object. In this case:
nestCamera.properties.snapshot_url
Congratulations, you now have a device that works with the Nest Cam! Let’s give it a try.
Updated about 7 years ago