Skip to content
This repository was archived by the owner on Sep 16, 2019. It is now read-only.

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
plokk committed Jun 9, 2015
1 parent cc5ed12 commit 640d4a5
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 57 deletions.
247 changes: 190 additions & 57 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var moment = require('moment');
var firebase = require('./js/firebase_service.js');
var Controller = require('./js/controller.js');
var Sensor = require('./js/sensor.js');
var URM37 = require('./js/urm37.js');
var Twitter = require('twitter');

var twitterClient;
Expand Down Expand Up @@ -57,19 +58,64 @@ var readQueue = [];

var lastData = {};


/**
* Read sensor values
* Read values from URM37
*/
function readSensors() {
function readURM37(sensorName) {
async.series(
[
URM37.init,
URM37.openPort,
function(callback) {
URM37.readSensorValues(function(error, values) {
if (error) {
callback(error);
return;
}

//Add timestamp to values
values.timestamp = moment().format();

// Get sensor from reading queue
var sensorName = readQueue.shift();

// No sensor queued to be read
if (!sensorName) {
return;
}
if (!sensorName in lastData) {
lastData[sensorName] = {};
}

lastData[sensorName] = values;

firebase.addItem(sensorName, values);
console.log(TITLE.INFO + sensorName + ':');
console.log(values);

// 28.8 = depth of the water reservoir
// 100-((0/28.8) * 100)

// Math.round(10-((13/28.8) * 10));

callback();
});
},
URM37.closePort,
function(callback) {
config.sensors[sensorName].previousReading = moment().format();
callback();
},
writeConfig
],
function(error) {
if (error) {
console.error(TITLE.ERROR + error);
}
readSensors();
}
);
}


/**
* Read values from SHT1x
*/
function readSHT1x(sensorName) {
// Select sensor to be used
Sensor.select(sensorName);
async.series(
Expand Down Expand Up @@ -122,28 +168,68 @@ function readSensors() {
);
}

/**
* Read sensor values
*/
function readSensors() {

// Get sensor from reading queue
var sensorName = readQueue.shift();

// No sensor queued to be read
if (!sensorName) {
return;
}

if (sensorName in config.sensors) {
switch (config.sensors[sensorName].type) {
case 'SHT1x':
readSHT1x(sensorName);
break;
case 'URM37':
readURM37(sensorName);
break;
}
} else {
if (sensorName == 'twitter') {
doGrabWebcam();
}
}
}

/**
* Application loop
*/
function loop() {
setInterval(function () {

var sensors = config.sensors || {};

var readingInterval;
var previousReading;

for (var sensor in sensors) {

var readingInterval = sensors[sensor].readingInterval;
var previousReading = moment(sensors[sensor].previousReading);
readingInterval = sensors[sensor].readingInterval;
previousReading = moment(sensors[sensor].previousReading);

if (moment().diff(previousReading, 'seconds') >= readingInterval) {
console.log(TITLE.INFO + sensor + ' requires reading');
readQueue.push(sensor);
}
}

readSensors();
// Twitter
if ('twitter' in config) {
var actionInterval = config.twitter.actionInterval;
var previousAction = moment(config.twitter.previousAction);
if (moment().diff(previousAction, 'seconds') >= actionInterval) {
readQueue.push('twitter');
}
}

readSensors();

}, 5000);
}, 20000);
}

function doGrabWebcam() {
Expand All @@ -154,9 +240,9 @@ function doGrabWebcam() {
function(error, stdout, stderr) {
if (error) {
console.error(TITLE.ERROR + error);
readSensors();
return;
}

}
console.log(TITLE.INFO + 'Snapshot captured');
doMediaTweet();
}
Expand Down Expand Up @@ -185,60 +271,103 @@ function doPourWater() {

firebase.addItem(sensorName, values);
console.log(TITLE.INFO + 'Water poured for 5000ms');

}
);
}

function doMediaTweet() {
var data = fs.readFileSync(webcamImage);

twitterClient.post('media/upload',
{ media: data },
function(error, media, response) {
console.log(TITLE.INFO + 'Last data:');
console.log(lastData);

if (error) {
console.error(TITLE.ERROR + 'Error uploading media:');
console.error(error);
return;
}
var tweet = '';
var hasData = false;

console.log(media);
if ('soil-temperature-and-humidity' in lastData) {
var st = Math.round(lastData['soil-temperature-and-humidity'].temperature * 10) / 10 + ' ℃';
var sh = Math.round(lastData['soil-temperature-and-humidity'].humidity * 10) / 10 + '%';
var sd = Math.round(lastData['soil-temperature-and-humidity'].dewpoint * 10) / 10 + ' ℃';

tweet += '🌱' + ' Soil at ' + st + ' (' + sh + ', ' + sd + ')';
tweet += '\n';
hasData = true;
}

// Lets tweet it
var status = {
status: '🌱 Yeah, I am a media tweet.',
media_ids: media.media_id_string // Pass the media id string
}
if ('air-temperature-and-humidity' in lastData) {
var at = Math.round(lastData['air-temperature-and-humidity'].temperature * 10) / 10 + ' ℃';
var ah = Math.round(lastData['air-temperature-and-humidity'].humidity * 10) / 10 + '%';
var ad = Math.round(lastData['air-temperature-and-humidity'].dewpoint * 10) / 10 + ' ℃';

twitterClient.post('statuses/update', status,
function(error, tweet, response) {
if (error) {
console.error(TITLE.ERROR + 'Error in tweet:');
console.error(error);
}
tweet += '☁️' + ' Air at ' + at + ' (' + ah + ', ' + ad + ') ';
tweet += '\n';
hasData = true;
}

if ('water-level-and-temperature' in lastData) {
var wt = Math.round(lastData['water-level-and-temperature'].temperature * 10) / 10 + ' ℃';
var wd = Math.round(lastData['water-level-and-temperature'].distance * 10) / 10;

var amount = Math.round(10-((wd/28.8) * 10));
var percent = Math.round(100-((wd/28.8) * 100));

if (percent >= 0 && percent <= 100) {
var str = '[';
for (var i = 0; i < 10; i++) {
if (i < amount) {
str += '💦';
} else {
str += '_';
}
);

}

str += ']';

tweet += '💧' + ' Water reservoir at ' + wt + '\n' + str + ' (' + percent + '%)';
hasData = true;
}
);
}
}

function doTweet() {
twitterClient.post('statuses/update',
{
status: '🌱 Doing great! Air temperature is +23℃ and soil moisture is 96%'
},
function(error, tweet, response) {
if (error) {
console.error(TITLE.ERROR + 'Error in tweet:');
console.error(error);
if (hasData) {

twitterClient.post('media/upload',
{ media: data },
function(error, media, response) {

if (error) {
console.error(TITLE.ERROR + 'Error uploading media:');
console.error(error);
readSensors();
return;
}

console.log(media);

// Lets tweet it
var status = {
status: tweet,
media_ids: media.media_id_string // Pass the media id string
}

twitterClient.post('statuses/update', status,
function(error, tweet, response) {
if (error) {
console.error(TITLE.ERROR + 'Error in tweet:');
console.error(error);
}
config.twitter.previousAction = moment().format();
writeConfig(function() { return; });
readSensors();
}
);

}
}
);
);
} else {
readSensors();
}
}


/**
* Load config file and initialize application
*/
Expand Down Expand Up @@ -268,7 +397,14 @@ function initialize() {
console.log(' ' + TITLE.INFO + 'Previous reading: ' + sensors[sensor].previousReading);

// Add sensor
Sensor.add(sensor, sensors[sensor].pinData, sensors[sensor].pinSck);
switch (sensors[sensor].type) {
case 'SHT1x':
Sensor.add(sensor, sensors[sensor].pinData, sensors[sensor].pinSck);
break;
case 'URM37':

break;
}
}

console.log('\n' + TITLE.INFO + 'Controllers:');
Expand Down Expand Up @@ -307,9 +443,6 @@ function initialize() {

loop();
//doPourWater();
//doTweet();
//doMediaTweet();
//doGrabWebcam();
}
);
}
Expand Down
5 changes: 5 additions & 0 deletions config.json.sample
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"pinSck": 13,
"readingInterval": 60,
"previousReading": "2015-05-19T12:16:42+00:00"
},
"water-level-and-temperature": {
"type": "URM37",
"readingInterval": 60,
"previousReading": "2015-05-19T12:16:42+00:00"
}
},
"controllers": {
Expand Down
Loading

0 comments on commit 640d4a5

Please sign in to comment.