Temperature/Humidity/Pressure Monitor

Temperature/Humidity/Pressure Monitor

View on GitHub
In this project, we are going to use the environmental sensors on Raspberry Pi Sense HAT to record temperature, humidity and pressure, and then upload to the server.

Hardware

Raspberry Pi
Sense HAT


Client Program (Python)

from sense_hat import SenseHat  
import urllib  
import json  
from time import gmtime, strftime  
import time

sense = SenseHat()  
while True:  
        # prepare data
        temperature = sense.get_temperature()
        temperature = round(temperature,2)
        humidity = sense.get_humidity()
        humidity = round(humidity,2)
        pressure = sense.get_pressure()
        pressure = round(pressure,2)
        time_str =  strftime("%Y-%m-%d  %H:%M:%S", gmtime())
        # upload data
        u = urllib.urlopen('your/server/address/'+time_str+'/'+str(temperature)+'/'+str(humidity)+'/'+$
        print "["+time_str+"] uploaded: "+str(temperature)+', '+str(humidity)+', '+str(pressure)
        # upload every 15 min
        time.sleep(60*15)

Server Program (Node.js)

var express = require('express');  
var path = require('path');  
var favicon = require('serve-favicon');  
var cookieParser = require('cookie-parser');  
var bodyParser = require('body-parser');

var routes = require('./routes/index');

// setup mongodb
var monk = require('monk');  
var db = monk('mongodb://localhost:27017/sense');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));  
app.set('view engine', 'jade');

app.set('port', (process.env.PORT || 3000));

app.use(bodyParser.json());  
app.use(bodyParser.urlencoded({ extended: false }));  
app.use(cookieParser());  
app.use(express.static(path.join(__dirname, 'public')));

// Make our db accessible to our router
app.use(function(req,res,next){  
    req.db = db;
    next();
});

// Display all records
app.get('/', function(req, res, next) {  
  res.render('index', { title: 'Sense Record', style: 'main' });
});

// Add a new record
app.get('/add/:time_stamp/:temperature/:humidity/:pressure', function(req, res, next) {  
    var db = req.db;
    var collection = db.get('record');
    collection.insert({'time': req.params.time_stamp,
                    'temperature': req.params.temperature,
                    'humidity': req.params.humidity,
                    'pressure': req.params.pressure
        },function(err, result){
        res.send((err === null) ? { msg: 'Success.' } : { msg: err });
    });
});

// Delete a record
app.delete('/delete/:time', function(req, res, next) {  
    var db = req.db;
    var collection = db.get('record');
    var recordToDelete = req.params.time;
    collection.remove({ 'time' : recordToDelete }, function(err) {
        res.send((err === null) ? { msg: '' } : { msg:'error: ' + err });
    });
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {  
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

app.listen(app.get('port'), function() {  
    console.log('Node app is running on port', app.get('port'));
});

Collect Data

More: IoT