Skip to content

Instantly share code, notes, and snippets.

@jiankaiwang
Last active December 6, 2017 06:48
Show Gist options
  • Save jiankaiwang/635f40bb5609033e922337b7aa2a44e6 to your computer and use it in GitHub Desktop.
Save jiankaiwang/635f40bb5609033e922337b7aa2a44e6 to your computer and use it in GitHub Desktop.
It's the firebase API template in Nodejs server side, and also continuous sending iot sensor data to the firebase.
/*
* author : jiankaiwang
* platform : nodejs
* feature : the firebase API template in NodeJS server side
* description : continuously send iot sensor data to the firebase
*/
var firebase = require("firebase");
function get_time(type) {
function __formatMDHMS(getValue) {
return (getValue < 10 ? '0' + getValue : getValue);
}
var varTime = new Date(),
varYears = varTime.getFullYear(),
varMonths = varTime.getMonth()+1,
varDate = varTime.getDate(),
varHours = varTime.getHours(),
varMinutes = varTime.getMinutes(),
varSeconds = varTime.getSeconds();
switch(type) {
case "Y":
return(varYears);
case "M":
return(__formatMDHMS(varMonths));
case "D":
return(__formatMDHMS(varDate));
case "h":
return(__formatMDHMS(varHours));
case "m":
return(__formatMDHMS(varMinutes));
case "s":
return(__formatMDHMS(varSeconds));
case "YMD":
return(varYears + "/" + __formatMDHMS(varMonths) + "/" + __formatMDHMS(varDate));
case "hm":
return(__formatMDHMS(varHours) + "/" + __formatMDHMS(varMinutes));
case "hms":
return(__formatMDHMS(varHours) + ":" + __formatMDHMS(varMinutes) + ":" + __formatMDHMS(varSeconds));
}
}
var config = {
apiKey : "",
authDomain : "",
databaseURL : "https://(xxx).firebaseio.com/",
storageBucket : ""
}
firebase.initializeApp(config);
var db = firebase.database();
var deviceID = "dev1";
setInterval(function(){
var ref = db.ref("/cluster1/" + deviceID + "/" + get_time("YMD") + "/" + get_time("h"));
var value = {
humi: "28",
temp: "26",
time: get_time("hms")
}
ref.set(value);
}, 10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment