Kelsey Breseman
Published

A Sleep Tracker for Your Dog Using Tessel and Twilio

A Sleep Tracker for Your Dog Using Tessel and Twilio

Full instructions provided2,370
A Sleep Tracker for Your Dog Using Tessel and Twilio

Things used in this project

Story

Read more

Code

file_9257.js

JavaScript
// your Twilio AccountSid and AuthToken
var account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var auth_token = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
var twilio_num = "5558675309";
var number = "55555555555"; // The number you want to text the information to
var client = require('twilio')(account_sid, auth_token);

file_9258.txt

SH
npm install twilio

file_9259.js

JavaScript
sendText( number, twilio_num, "This text message was sent by Tessel. Cool, right?!");
function sendText(to,from,msg) {
  client.sms.messages.create({
    to: to,
    from: from,
    body:msg
  }, function(error, message) {
    if (!error) {
      console.log('Success! The SID for this SMS message is:');
      console.log(message.sid);
      console.log('Message sent on:');
      console.log(message.dateCreated);
    } else {
      console.log('Oops! There was an error.', error);
    }
  });
}

file_9260.txt

SH
tessel run twilio.js

file_9261.txt

SH
npm install accel-mma84

file_9262.js

JavaScript
var accel = require('accel-mma84').use(tessel.port['A']);

file_9263.js

JavaScript
// Initialize the accelerometer
accel.on('ready', function () {
  // Stream accelerometer data
  accel.on('data', function (xyz) {
    console.log( xyz );
  });
});

accel.on('error', function(err){
  console.log('Error:', err);
});

file_9264.js

JavaScript
var last_movement = 0.0;
var last_movement_time = Date.now();

file_9265.js

JavaScript
// Initialize the accelerometer
accel.on('ready', function () {
  // Stream accelerometer data
  accel.setOutputRate(1.56, function rateSet() {
    accel.setScaleRange( 8, function scaleSet() {
      accel.on('data', function (xyz) {
        if( last_movement !== xyz[0].toFixed(1) ) {
          last_movement = xyz[0].toFixed(1);
          var minutes = ( (Date.now() -  last_movement_time)/1000) / 60 ;
          last_movement_time = Date.now();
          if( minutes > 5 ) {
            // send text
            sendText(number, twilio_num, "Your dog slept for " + Math.round( minutes ) + " minutes");
          }
        }
      });
    });
  });
});

file_9266.txt

SH
tessel run twilio.js

file_9267.txt

SH
tessel push twilio.js

Gist

Credits

@rickyrobinett

Posted by Fouu3goh51as9ed.large Kelsey Breseman

Comments

Add projectSign up / Login