Jonathan van Vlaanderen
Published © GPL3+

AWS IoT Netflix Remote Pause Button Using Raspberry Pi

Remote button that pauses (and un-pauses) Netflix using the Raspberry Pi and Amazon Web Services

EasyFull instructions provided1,621
AWS IoT Netflix Remote Pause Button Using Raspberry Pi

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Male/Male Jumper Wires
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

AWS IoT
Amazon Web Services AWS IoT
Netflix
Netflix

Story

Read more

Schematics

NetflixOnComputer.js

The script you run on the computer

ScriptToRunOnRaspberryPi.js

The script to run on the Raspberry Pi

ButtonTest.py

To test the button and resolve problems

Button setup

To setup the button and the Raspberry Pi
Schermafbeelding%202016 01 31%20om%2009.43.18

Code

ScriptToRunOnRaspberryPi.js

JavaScript
Script to run on Raspberry Pi
var awsIot = require('aws-iot-device-sdk');
require('shelljs/global');
var myThingName = 'RaspberryPiLed';
var thingShadows = awsIot.thingShadow({
   
//To connect to the AWS IoT service 
  keyPath: '/Path/to/AWS/Key',
  certPath: '/Path/to/AWS/certificate',
  caPath: '/Path/to/the/root-CA.crt',
  clientId: myThingName,
  region: 'Your Region'
});

var object = '{\\"state\\":{\\"reported\\":{\\"Pause\\":\\"true\\"}}}'
thingShadows.register( 'Pause' );

mythingstate = {
  "state": {
    "reported": {
      "Pause": "false"
    }
  }
}

var Gpio = require('onoff').Gpio,
  button = new Gpio(14, 'in', 'both')
button.watch(function(err, value) {
//To handle button input
var Unpause = exec('aws iot-data update-thing-shadow update-thing-shadow --thing-name RaspberryPiLed --payload ' + object)
console.log(mythingstate["state"]["reported"]["Pause"])
  console.log(mythingstate["state"])


});
 delete mythingstate['version']; 
 console.log(mythingstate["state"]["reported"]["Pause"])
  console.log(mythingstate["state"])
  var response = thingShadows.update(myThingName,  mythingstate);
  console.log(response)
var networkInterfaces = require( 'os' ).networkInterfaces( );
// PIR sensor connected to pin 7
// You can use any gpio pin 
// install npm button library first
thingShadows.on('connect', function() {
  console.log("Connected...");
  console.log("Registering...");
  thingShadows.register( myThingName );
  console.log("mythingstate = false")
  setTimeout( function() {
    console.log("Updating my IP address...");
    clientTokenIP = thingShadows.update(myThingName, mythingstate);
    console.log("Update:" + clientTokenIP);
    delete mythingstate['version'];

  }, 2500 );
});

ButtonTest.py

Python
Script to test the button
import RPi.GPIO as GPIO
import time
import random

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
button = 14
GPIO.setup(button, GPIO.IN, GPIO.PUD_UP)
while True:
     if GPIO.input(button) == False:
           print("Button pressed")
GPIO.cleanup()

NetflixScriptOnComputer.js

JavaScript
Script to run on your computer
robot = require('robotjs')
require('shelljs/global'); 
var awsIot = require('aws-iot-device-sdk');

var myThingName = 'RaspberryPiLed';


var thingShadows = awsIot.thingShadow({
   
//To connect to the AWS IoT service 
  keyPath: '/Path/to/AWS/Key',
  certPath: '/Path/to/AWS/certificate',
  caPath: '/Path/to/the/root-CA.crt',
  clientId: myThingName,
  region: 'Your Region'
});


mythingstate ={
    "state": {
        "reported": {
            "Pressed": "true"
        }
    }
}

var object = '{\\"state\\":{\\"reported\\":{\\"Pause\\":\\"false\\"}}}'

var networkInterfaces = require( 'os' ).networkInterfaces( );
function CheckState() {
var CheckPause = exec('aws iot-data get-thing-shadow --thing-name ' +myThingName+ ' output.txt && cat output.txt', function(status, output) {
if (output.indexOf("\"Pause\":\"true\"") > -1){
    robot.keyTap('space')
    //To click space
    console.log("Pause")
    //To check if the button is tapped on the Raspberry Pi
    var Unpause = exec('aws iot-data update-thing-shadow update-thing-shadow --thing-name ' +myThingName+' --payload ' + object)
    //To stop continuing to click space
}
});
}
thingShadows.on('connect', function() {
  CheckState();
  console.log("Connected");
  console.log("Registering");
  thingShadows.register( myThingName );
  // An update right away causes a timeout error, so we wait about 2 seconds
  setTimeout( function() {
    console.log("Updating my IP address...");
    UpdatedIP = thingShadows.update(myThingName, mythingstate);
    console.log("Update:" + UpdatedIP);
    CheckState();
  }, 2500 );


	



});

Credits

Jonathan van Vlaanderen

Jonathan van Vlaanderen

1 project • 0 followers
Tech hobbyist
Contact

Comments