Reputation: 2419
I have a small web server written using node.js, express and serial port which would constantly listen to an temperature sensor attached to the mac via USB. The code is as follow:
var serialport = require("serialport"), // include the serialport library
SerialPort = serialport.SerialPort, // make a local instance of serial
app = require('express')(), // start Express framework
server = require('http').createServer(app), // start an HTTP server
io = require('socket.io').listen(server); // filter the server using socket.io
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('sensordatabase', server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'sensordatabase' database");
db.collection('tempsensor', {safe:true}, function(err, collection) {
if (err) {
console.log("The 'values' collection doesn't exist. Creating it with sample data...");
}
});
}
});
var serialData = {}; // object to hold what goes out to the client
server.listen(8080); // listen for incoming requests on the server
console.log("Listening for new clients on port 8080");
// open the serial port. Change the name to the name of your port, just like in Processing and Arduino:
var myPort = new SerialPort("/dev/cu.usbmodem5d11", {
// look for return and newline at the end of each data packet:
parser: serialport.parsers.readline("\r\n")
});
// respond to web GET requests with the index.html page:
app.get('/', function (request, response) {
myPort.on('data', function (data) {
// set the value property of scores to the serial string:
serialData.value = data;
response.send(data);
// for debugging, you should see this in Terminal:
console.log(data);
});
});
As it can be seen from the above code, my sensor values are stored in "data".
Now I would like to save this data into my tempsensor collection which has the following format:
{
"Physicalentity": "Temperature",
"Unit": "Celsius",
"value": "",
"time": "",
"date": ""
},
My question is:
1: How can I save the "data" in the value object, using the mongodb driver for node.js? 2: How can I add the time when the data is added automatically?
I know there's a function called new Date()
for the date, is there a similar function for time?
I would really appreciate any help.
Thanks a ton in advance.
Upvotes: 0
Views: 2252
Reputation: 23047
Please, remember - this is not learning tutorial place, this is place where people have technical or software issues not related to their learning abilities or so. In order to improve that, please read examples working with mongodb and in overall working with node.js.
Here is some details over your situation to guide you:
In callback function on myPort.on('data'), you have access to your data, this is exact place where you have to save your data to database.
In meantime, when you initialize database connection, and collection, you need to get handle of the collection in order to use it in application after. In callback function on db.collection('tempsensor') you have object collection - this is what you need to execute mongodb functions to work with data in that collection.
So save this variable somewhere in shared scopes (can be global variable or array of collections).
Then in callback on data received, use this collection and pass data like suggested by Serdar Dogruyol.
Upvotes: 1
Reputation: 8548
> db.c.insert({'date': new Date(), 'time_hours': new Date().getHours(), 'time_mi
n': new Date().getMinutes()})
Inserted 1 record(s) in 31ms
> db.c.find({})
{ "_id" : ObjectId("50d30884059dc377c6ff66ec"), "date" : ISODate("2012-12-20T12:
45:56.493Z"), "time_hours" : 16, "time_min" : 45 }
Fetched 1 record(s) in 0ms
>
Using Mongo shell. it means you could using it in any mongo driver.
Upvotes: 1
Reputation: 5157
You can do something like this to insert your document into your collection.
db.collection('tempsensor',{safe:true}, function(err, collection) {
collection.insert({
"Physicalentity": "Temperature",
"Unit": "Celsius",
"value": "",
"time": "",
"date": ""
}, function(err, doc) {
if(err){
console.log("Error on document insert");
}else{
//Document saved succesfuly
}
});
});
Upvotes: 1