-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (61 loc) · 2.15 KB
/
Copy pathindex.js
File metadata and controls
66 lines (61 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Import the "get-sensor-readings" module, as well as the firebase admin module
*/
const getSensorReadings = require('./get-sensor-readings')
var admin = require('firebase-admin')
/**
* Read the JSON key that was downloaded from firebase, in this case, it has
* been placed in the "/home/pi" directory, and named "firebase-key.json"
* You can change this to the location where your key is.
*
* Remember, this key should not be accessible by the public, and so should not
* be kept inside the repository
*/
const serviceAccount = require('/home/pi/firebase-key.json')
/**
* The firebase admin SDK is initialized with the key and the project URL
* Change the "databaseURL" to match that of your application.
* Once the admin object is initialized, it will have access to all the
* functionality that firebase provides, and can now write to the database
*/
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://sensor-project-5df04.firebaseio.com'
})
/**
* Initialize the database, and create refs for the temperature
* and humidity keys on our database. This is very similar to the refs we
* created on the client side.
*/
const db = admin.database()
const temperatureRef = db.ref('temperature')
const humidityRef = db.ref('humidity')
/**
* Create a task that runs after a fixed interval of time
*
* Here, we have set the interval to be slightly longer than it was
* before. This is to account for the delay that may occur in the network,
* since we are not running the databas eon the local machine anymore.
* If you find that the application is not communicating with firebase
* as fast as you would like, try increasing this interval based on your
* network speed.
*/
setInterval(() => {
/**
* Retrieve sensor readings
*/
getSensorReadings((err, temperature, humidity) => {
if (err) {
/**
* If any error comes up, log it to the console and return from this
* function call
*/
return console.error(err)
}
/**
* Finally, set the values for the temperature and humidity keys
*/
temperatureRef.set(temperature)
humidityRef.set(humidity)
})
}, 4000)