Skip to content

Commit

Permalink
Raspberry Pi
Browse files Browse the repository at this point in the history
  • Loading branch information
gkca committed Feb 28, 2019
1 parent 5ca14cf commit 69d2be9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Raspberry Pi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Raspberry-Relay-Control",
"version": "1.0.0",
"description": "Raspberry Pi Code",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "AG & SA",
"license": "ISC",
"dependencies": {
"firebase-admin": "^6.4.0"
}
}
58 changes: 58 additions & 0 deletions Raspberry Pi/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Firebase Admin SDK Configuration */
var admin = require("firebase-admin");
var fs = require('fs');

const ACTIVATE = "activate";
const DEACTIVATE = "deactivate";
const RELAY = "number";

const PATH_TO_RELAYS = {
1: "/sys/class/gpio/gpio23/value",
2: "/sys/class/gpio/gpio24/value"
};

var serviceAccount = require("<PATH to Your serviceAccountKey.json File>");

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "<PATH to Your Database URL>"
});

// Database Init
var defaultDatabase = admin.database();
var ref = defaultDatabase.ref();

// You can retrieve services via the defaultApp variable...
var defaultDatabase = admin.database();
var ref = defaultDatabase.ref();

entitiesFromAssistant = {}

var ref = defaultDatabase.ref();

ref.on('value', function (snapshot) {
snapshot.forEach(function (childSnapshot) {

var key = childSnapshot.key;
var val = childSnapshot.val();

entitiesFromAssistant[key] = val;

}); // snapshot.forEach(function (childSnapshot)

console.log(entitiesFromAssistant);

Object.entries(PATH_TO_RELAYS).forEach(([relay, path]) => {
if (relay == entitiesFromAssistant[RELAY]) {
if (entitiesFromAssistant[ACTIVATE]) {
fs.writeFileSync(path, entitiesFromAssistant[ACTIVATE], function (err) {
if (err) throw err;
}); // fs.writeFileSync
} else {
fs.writeFileSync(path, entitiesFromAssistant[DEACTIVATE], function (err) {
if (err) throw err;
}); // fs.writeFileSync
} // else
} // if (relay == entitiesFromAssistant[RELAY])
}); // Object.entries(PATH_TO_RELAYS).forEach
}); // ref.on('value', function (snapshot)

0 comments on commit 69d2be9

Please sign in to comment.