Building an Automatic Fish Feeder

Hey there! I hope you have some fun Thanksgiving plans that include lots of gorging yourself and relaxing. Today’s song is an old favorite of mine.

I’ve been looking for a project for my Tessel2 microcontroller for a few months now. It’s cool because it allows you to program hardware using Javascript, which is my favorite.

 

With the coming holidays, I’ll be traveling for a few days. Since I’m the proud father of a Betta fish named Rhaegar, I wanted to make sure he stayed alive and happy, so I decided to make him an automatic fish feeder.

 

First, I printed out the model from Thingiverse.

IMG_0448
Top-down view of the final print with the servo attached

 

It’s a pretty simple idea: a tank that gravity-feeds food to an auger. The auger spins with the help of the attached continuous-rotation servo, which is controlled by the Tessel.

 

IMG_0447
3/4 view of the finished piece

Then, I wrote the code. I wanted to make sure the fish was fed every twelve hours, and that I was alerted when he was fed. I used Twilio as my text messaging service, which was super easy.

 

index.js


var tessel = require('tessel');
var servolib = require('servo-pca9685');
var servo = servolib.use(tessel.port['A']);
var twilio = require('twilio');

var servo1 = 1; // We have a servo plugged in at position 1
var TWELVE_HOURS = 60 * 60 * 1000 * 12; /* ms */

servo.on('ready', function () {
	var client = new twilio.RestClient('AC60444a3748dee195b9250b351b25f0f6', 'API_SECRET_GOES_HERE');

	var timeSinceLastFeed = 0;
  
  	var dispenseFood = function(){

  		if(((new Date) - timeSinceLastFeed) > TWELVE_HOURS) {
		//if(((new Date) - timeSinceLastFeed) > 1500) { //for testing at a faster pace
                        client.messages.create({
		            body: 'Fish is about to be fed!',
		            to: 'MY_PHONE_NUMBER',  // Text this number
		            from: '+18455354398' // From a valid Twilio number
		            }, function(err, message) {
		        });
	  		servo.move(servo1, .7); //moves clockwise at full speed
	  		setTimeout(function(){servo.move(servo1, 0.4)}, 75); //moves clockwise at slow speed
	  		
	  		timeSinceLastFeed = new Date();
	  	
	  		return true;
	  	}
	  	else {
	  		console.log('It has been less than twelve hours since the last feeding; can\'t feed yet!');
	  		return false;
	  	}
  	};
  	
  	dispenseFood(); //run once on start
  	setInterval(dispenseFood, 60 * 60 * 1000); //check every hour, just in case
  	//setInterval(dispenseFood, 2000); for testing
});

Then, I did lots of testing. I needed to make sure the amount of food was consistent in each feeding, which was exceedingly difficult. I decreased the time between feedings and tried both worms and pellets to see which was the easiest to produce consistent results, and ended up with a mixture of both.

View post on imgur.com

IMG_0456
Testing the amount of time to spin the auger for optimum food release

 

Next, I pushed the code out to the Tessel to connect to our WiFi and run the program automatically on boot, which was as simple as


t2 push index.js

 

Then, I secured it to the top of the fishtank with some tape (hey, it’s a prototype. Don’t judge me.)

Affixed temporarily to the top of the tank
Affixed temporarily to the top of the tank

We’re still in the testing phase, so hopefully I don’t over/underfeed him. Bettas are pretty hearty fish, though, so I’m not too worried.

 

Thanks for reading! I hope you’re having a fantastic day. See you next time!

How I Made More Interest with $40 Than I Did with $1000

 

 

Everyone knows that interest rates are abysmally low nowadays, especially for savings accounts in the US. When I was younger, I remember speaking with my grandfather, who recommended investing in Certificates of Deposit for 1-to-5-year savings at 5%.

Now, my TD Bank savings account has an interest rate so low, they just round it to 0.00 on my statements, and the best CD I can find today is 1.10%. The best liquid savings I’ve found is Ally, where I store my emergency fund, at 1%. Our government wants to stimulate economic growth by discouraging saving (they call it hoarding to make it sound bad) money, so the Fed sets incredibly low interest rates for everything (except student loans; can I get an Amen?!).

Terrible economic policy discussions aside, I’ve found a couple ways to help lessen the pain of saving.

As part of diversifying my savings, I have money in a 401k + Roth IRA through my work, and a small percentage in commodities like gold and Bitcoin that I set up by myself.

In May, as an experiment, I set up an account at BitBond, a peer-to-peer lending service, which was advertising an average yield of over 20% APY. Essentially, I view it as a slightly riskier CD. People ask for loans for various business ventures and explain their income and repayment plan, and you can bid as low as .01 Bitcoin (~$4 at the time I bid) on their loan. Each of the loans is assigned a rating based on the history of the loanee and his income, and you can choose to loan in either US Dollars or Bitcoin, with Bitcoin as the source.

I dropped in $40 worth of bitcoin that I didn’t mind losing to see what would happen. Here’s my results, and a comparison to my TD Bank savings account, in which I left exactly $1000 after transferring my savings to Ally, just to keep the account active.

 

Results

After two months, my TD account made 6 cents of interest. Here’s one month’s payment for proof:

 

Now, for Bitbond:

As you can see above, each loan is classified A-F, with A being the highest rating and the lowest interest, and F being the most risky, with greatest chance of reward. I’ve diversified my loans by putting the minimum in most of the loans and spreading out the risk by investing in different classes of loan, A through E.

In the same time on Bitbond, I’ve received 60 cents in interest, or 10 times more interest with 25 times less principal invested. They project that I’ll be getting 23% APY based on my investments spread this year.

 

Final Words

Now that we’ve seen the numbers, I’d like to point out a couple things. This method of investing is significantly riskier than a normal bank account – as you can see above, two of my loanees are late on their payments, which means I’ve gotten no interest on them. There’s a nonzero chance I’ll never get that money back. I justify this by making sure I don’t care about getting this money back, and that it’s a very small portion of my overall portfolio, and by diversifying my loan choices.

 

Secondly, Bitcoin is a relatively new and unstable currency (though, it might be more stable than the Pound right now! Zing.)  It is, however, an incredibly exciting and useful technology that I choose to support with my dollars. There is a chance that the price of Bitcoin will fluctuate enough that the growth of the currency itself might outpace the interest you receive from this investment. If you want to learn more about Bitcoin investment, reddit.com/r/bitcoin, reddit.com/r/cryptocurrencies, and reddit.com/r/bitcoinmarkets are fantastic resources.

Also, I’m not sure if these results will scale linearly with more money invested. There might be a point of diminishing returns with P2P lending, and I might put in some more money to test the waters, but I don’t want to be overly optimistic.

Finally, I want to reiterate that I am not, by any means, an expert in finance, Bitcoin, or cryptocurrency. This is just something that I’ve been studying on my own for a while and wanted to share with you.

 

Thanks for reading! Stay smart. Stay healthy. Peace.

Creating a Wooden 3D Print from a 2D Drawing

Hey there! I hope you’re having a great day. Today, we’re going to take a pencil drawing and convert it into a real, solid, wooden, 3D-printed thing.

Song of the Day!

 

Pencil Drawing to Vector Graphic

I started with this excellent drawing of a platypus by DeviantArt user blueroseval13.

Once I had the drawing I wanted to print, I brought it into Photoshop. I went into the Select menu and chose Color Range, which allowed me to select parts of the drawing based on their color. I brought the Fuzziness up to 200 (the maximum) to select every edge of the drawing.

select-color-range

 

Result after pasting into a new file and cleaning it up
Result after pasting into a new file and cleaning it up

 

I saved this new, cleaned-up drawing, and found an excellent site that converts images to the SVG (Scalable Vector Graphics) format that can be used by our 3D modeling software.

Edit: Reddit user Sonrisa3D says:

MatterControl image converter works well also. Another tool in the toolbox.

Thanks, Sonrisa3D!

Vector Graphic to 3D Model

After converting it to SVG, we can bring it into TinkerCad (a very simple 3D modeling webapp). After opening a new file in TinkerCad, we can select Import and choose our newly-created SVG, and it’ll look something like this:

3d-platypus

 

3D Model to Real, Actual Thing

Now, we can click Design -> Download for 3D Printing and bring it into our slicer software that tells the printer what to print. Then, I scaled it to fit on my printer and hit print!

3D printing works by building up really small layers over a long period of time. Since this model was pretty boring to watch, here’s one of my more interesting prints, a model of the city of Winterfell from Game of Thrones:

 

…And Finally!

Testing the print with various settings.
Testing the print with various settings.

As you can see above, I ran the print job a few times with different settings and materials to finally get the results I wanted. I started with some PETG I had laying around, and then switched to Wood once I knew it would print. I could have probably switched later and saved myself some money, but I didn’t realize how many times it would take to get the look I wanted. I didn’t think about making the walls more pronounced until a bit later, as you can see.

Let’s see how it looks on the computer to which I’m mounting it!

IMG_0223

The print in its temporary home. I'm planning to mount LED lights behind it for a sweet glow, but this will do for now.
The print in its temporary home. I’m planning to mount LED lights behind it for a sweet glow, but this will do for now.

FAQs

Which 3D printer are you using? 

Printrbot Simple Metal with Heated Bed. It’s a great starter printer, and I’m using it to print parts for my next 3D printer. 3D Printers printing 3D printers. Meta.

What materials can you print with?

Wood, BambooIron, Rubber, Hard Plastic, Softer-but-still-firm plastic, and more!

How much did these prints cost?

Total, maybe $7-8 USD.

How long did this take?

I went from drawing to having my first print in-hand in less than 3 hours. To my final print, maybe another 4 hours.

Why do you 3D print things? It would be faster to just buy them.

In some cases, sure, it might be easier to buy a toothbrush holder or coasters, or whatever you might need. But the cool part for me is being able to take an idea and transform it into a real, tangible thing – with just a little time and some fiddling on the computer. Plus, I spend a lot less money on things that are just little pieces of plastic, and can customize them or swap them out really easily whenever I want to suit my decorating desires or when they break.

Every household thing I print is one less that has to be shipped around the world in a wasteful box or cocooned in a plastic shell for safety. The plastic I print with is made from corn waste and other renewable resources. Essentially, it’s the self-sufficient, sustainable, and creative nature of this hobby that fascinates me so much.

 

Full disclosure: I get a small amount of commission if you buy anything through these links. It helps pay for my web hosting.

Thanks for reading! If you want to print this for yourself, here it is! If you have any questions, feel free to leave a comment.

 

Cheers!

UI End-to-End Testing with Nightwatch.js

Good morning and Happy Friday! I’m back after a little prompting from an old friend.

Song of the day:

Quick life update: I’m living near New York City now, and I’m working as a Senior Software Engineer for a team that writes the software that supports such excellent blogs as Engadget, Huffington Post Australia (more international editions to follow!), and Autoblog, among many others.

I’ve also gotten way into 3D printing, Internet of Things development, and am still doing minor development on my open-source fitness project, PPL.fitness.

Today we’re going to talk about testing our front-end code. I know, I know, I’ve used the excuses myself: “I don’t have time! My deadlines are too tight.” “I need to test how something LOOKS, not unit test a function in my code.” “Writing tests is weird and unnatural.” “Deuteronomy says UI testing is an abomination.” Yeah.

Enter stage right: Nightwatch.js. Super simple to set up. I get to use Node.js to simulate clicks, typing, and key presses, and check to see if elements are visible. I also get to check properties of the elements. Plus, it runs against the industry-standard Selenium server.

Though the initial setup took about a day to get my code decently covered by tests, I can now rest a little easier knowing that when I release new feature updates to my software, everything will work.

Setting Up

Install guide
Also download: https://sites.google.com/a/chromium.org/chromedriver/

I set up my folder structure like this:

project folder
…source files…
nightwatch.json
– bin
— chromedriver
— selenium-server-standalone-2.53.0.jar
– tests
— pages
—- pageCreateNew.js
— login.js
— createNew.js

I’m going to put my files up, with my comments inline for explanation.

nightwatch.js

{
  "src_folders" : ["tests"],   
  "output_folder" : "reports",
  "custom_commands_path" : "",
  "custom_assertions_path" : "",
  "page_objects_path" : "tests/pages",
  "globals_path" : "",

  "selenium" : {
    "start_process" : true,
    "server_path" : "bin/selenium-server-standalone-2.53.0.jar",
    "log_path" : "",
    "host" : "127.0.0.1",
    "port" : 4444,
    "cli_args" : {
      "webdriver.chrome.driver" : "bin/chromedriver", //THIS IS A BIG DEAL SO WE CAN TEST IN CHROME
      "webdriver.ie.driver" : ""
    }
  },

  "test_settings" : {
    "default" : { //runs when we don't pass in any options
      "launch_url" : "http://localhost:3000/management/splash/", //point this to whatever URL you want to test
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "desiredCapabilities": {
        "browserName": "firefox",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    },

    "chrome" : { //runs when user runs `nightwatch --env chrome`
      "desiredCapabilities": {
        "browserName": "chrome",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    },

    "production" : { //runs when user runs `nightwatch --env production`
      "launch_url" : "http://production.com/management/splash/",//point this to whatever URL you want to test
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "desiredCapabilities": {
        "browserName": "firefox",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    }
  }
}

Next, I wrote a tiny login function that I can call to get past authentication screens:

login.js

module.exports = function(client){
    return client
        .url('https://cms.aol.com')
        .waitForElementVisible('body', 5000)
        .waitForElementVisible('#signinemail', 3000) //change the selector to whatever the username input is on the site you're testing
        .setValue('#signinemail', email_goes_here) //change the selector accordingly
        .click('#continue_button')
        .waitForElementVisible('#signinpassword', 3000) //change the selector to whatever the password input is on the site you're testing
        .setValue('#signinpassword', password_goes_here) //change the selector accordingly
        .click('#signin_button')  //again, change this to the ID of the login button
};

One thing that’s really cool about Nightwatch is the ability to define pages. You get to define elements here and re-use them later in your testing code:

pageCreateNew.js

module.exports = {
  elements: {
    splashesListContainer: { 
      selector: '.splashes-list-container' 
    },
    startFreshButton: { 
      selector: '.new-button'
    }
  }
};

 

Quick break.

Alright, and now we get to the meat of our testing code!

var login = require('./login.js'); //this is our login function from before

module.exports = {
  '@tags': ['create', 'splash'], //tags are used to run certain groups of tests; I'll talk more about this in a minute

  'can edit the first headline': function (client) { //name your functions like this so that the person running the test knows what's broken or working
  	var createNew = client.page.pageCreateNew(); 

  	login(client); //this is how easy it is to call our login.js script!

  	createNew.navigate(client.launchUrl)
      .waitForElementVisible('@startFreshButton', 15000) //this is how we use the element selectors we defined in pageCreateNew.js
      .click('@startFreshButton')
      .waitForElementVisible('#template-container .headline-1 .splash__header', 1000) 
      .click('#template-container .headline-1 .splash__header')
      .setValue('#template-container textarea', 'Automated Testing Is the Best!') 
      //custom color tests
      .waitForElementVisible('.showCustomColors', 2000)
      .click('.showCustomColors')
      .click('.customColorOption:nth-of-type(2)')
      .click('.showCustomColors') //hide it
      //font size
      .setValue('#font-size-number-input', '80')
      //hyperlink
      .click('.link-button')
      .clearValue('.anchorLink')
      .setValue('.anchorLink', 'http://test.com')
      .click('.accept-item-button')
      .waitForElementVisible('#template-container .headline-1 .splash__header', 1000)
  
   //this is where we make our actual comparisons to see if everything is working!
    client.expect.element('#template-container .headline-1 .splash__header').text.to.equal('Automated Testing Is the Best!');
    client.expect.element('#template-container .headline-1 .splash__header').to.have.css('font-size', '80px');
    client.expect.element('#template-container .headline-1 .splash__header').to.have.css('color', '#2D7061');
    
    client.end();
  }
};

As you can see, the syntax is really simple!


expect(elementSelector).to.have.css(style, value)

expect(elementSelector).text.to.equal(value)

 

I am even able to test the type of my remote data store’s JSON schema with it by running AngularJS commands using client.api.execute(command)!


'is the schema set up correctly?':function (client) {
 var pageData= client.page.pageData();
 var code;

login(client);
 pageSplashLive.navigate(DATA_STORE_URL)
 .waitForElementVisible('body', 10000)
 .api.execute("return angular.element($('.data-editor-form')).scope()['ctrl']['data']['schema']['properties'];", [], function(response) {
 code = response.value;
 var dataType = typeof code;

client.assert.equal(dataType, 'object');
 client.assert.equal(code['Data']['type'], 'array');
 });

client.end();

},

 

I’ve just barely scratched the surface of this fantastic automation framework. I envision being able to automate a ton of online stuff with this tool – it doesn’t just have to be used to test my code.

 

Discussion of the day: what would you automate to make your life easier? I’m currently working on an automatic window blinds project (I’ll write that up soon!)

 

Cheers!

Sam

Happy Saturday!

Good morning everybody! I strongly recommend you let this song melt into your ears while you read. 

It’s been a busy couple of weeks for me! I’ve been working on a big feature release at work, and in my free time, I’ve also been working on arkWatcher and ojo, two open-source Javascript projects. I ran into an issue with some of arkWatcher’s functionality that I posted to StackOverflow, and ended up getting some excellent guidance from my brilliant coworker Michael Timbrook. He suggested using Reactive Extensions to solve my problem, so I’ve been reading up on it the past day or two. It’s really cool, and builds quite simple functions into complex functions to solve big problems. So far, I’ve found this awesome YouTube video and this guide to help me learn more about it.

 

In other, non-programming, news, I’ve been swimming a ton lately. I love it, and it’s fun, but it’s made me noticeably more tired and hungry lately, so I probably have to eat more.

Speaking of eating more, check out this breakfast sandwich I custom-ordered at Lucky Donuts and Deli. Two glazed donuts, scrambled eggs, excellent bacon, and bubbly cheese. This was, without a doubt, the most craving-satisfying, stomach-filling, rich, delicious thing I’ve eaten in a long time.

View post on imgur.com

I’m of the firm belief that you have to have a cheat meal once in a while, at least. This near-obscene, perfect sweet-and-salty combination is the cheatiest of all meals.

This month is just flying by, and I still haven’t come up with a Halloween costume. It’s weird – being on the West Coast for the first time, it doesn’t feel like Fall yet, so some part of me is in denial about what time of year it is. But the days are getting shorter. My friends back at home sent me tons of pictures of hail and snow and 20 degree temperatures last weekend, which seems a little early, even for the Northeast, although this week was apparently a gorgeous one. Here in San Diego, it’s been pretty nice with some occasional rain and cool temperatures. Here was the view on my porch this morning:

View post on imgur.com

Yeah, I live next to a freeway. And a construction site. It’s pretty loud sometimes.

Pool’s nice, though.

This month, I really started pushing myself more physically, mentally, and with my work. I’m starting to enjoy and see great benefit from pushing past the limits of my comfort zone and moving as far into the learning zone as I can. I’m working on finding small optimizations that give me more time, and then using that time to get stronger.

 

 

The other day, my buddy asked my advice about which things he should prioritize in his life and which he should drop. The problem was – all the items he wanted me to choose from were equally important. The only answer I could give him in good conscience was that he had to do them all. If they’re all critically important items, what other choice does he have?

Sometimes, it feels like we have the barest illusion of choice;  really, we have no true choice at all. Sometimes we just have to stay awake later than we want; sometimes we have to concentrate longer and harder, and do more, than we ever thought possible. Sometimes, everything seems to go wrong all at once.

Important in my examination of these ideas is remembering how lucky we are to be given these challenges in the first place. How lucky we are that we have people all around us who’ve gone through similar things! I’m starting to treat each challenge as a gift, a lesson, and a test. A way to glean as much as possible from the brilliant and amazing people around me.

Anyway, that’s about it! Enjoy your weekend. Do something new.

Until next time!

AngularJS – Warn on unsaved form changes

Happy Tuesday everybody! It’s actually raining here in San Diego, wow.

 

Anyway – I needed a way to check if an Angular form had been modified and warn my user. I found this question that offered some help. The caveat: I couldn’t use any specific form names, since the forms already had names and I didn’t want to force users to add IDs to their forms, so I had to modify it a bit.

To use this, we add confirm-on-exit as an attribute to the form element.


<form name="anything" confirm-on-exit>......</form>

Then attach this directive to your application, and you’re all done!


(function() {
'use strict';
 
 angular.module("app").directive('confirmOnExit', function() {
 return {
 link: function($scope, elem, attrs) {
 if (elem[0].nodeName === 'FORM') { //the user wants to check a form
 window.onbeforeunload = function(){
 if (angular.element(elem[0]).hasClass('ng-dirty') && !angular.element(elem[0]).hasClass('ng-submitted')) {
 return "You have unsaved changes - are you sure you want to leave the page?";
 }
 }
 $scope.$on('$locationChangeStart', function(event, next, current) {
 if (angular.element(elem[0]).hasClass('ng-dirty') && !angular.element(elem[0]).hasClass('ng-submitted')) {
 if(!confirm("You have unsaved changes - are you sure you want to leave the page?")) {
 event.preventDefault();
 }
 }
 });
 }
 else { //the user wants to check a specific collection
 if (attrs.confirmOnExit) {
 var loaded = false;
 var hasUnsavedChanges = false;
 $scope.$watchCollection(
 attrs.confirmOnExit,
 function( newValue, oldValue ) {
 if ( !loaded && newValue !== undefined ) { // the collection will change once on initialization; allow for this
 loaded = true;
 }
 else if( newValue !== undefined ){ // the collection has already been initialized; mark it as changed
 hasUnsavedChanges = true;
 }
 }
 );
 
 $scope.$on('$locationChangeStart', function(event, next, current) {
 if ( hasUnsavedChanges ) {
 if(!confirm("Make sure you save your changes before you go!")) {
 event.preventDefault();
 }
 }
 });
 }
 else {
 console.log('Please pass in a collection for confirm-on-exit to work correctly.');
 console.log('Example: <div confirm-on-exit="collectionName">...</div>');
 }
 }
 }
 };
 });
 
 
})();

(Sorry about the line spacing – my code plugin needs to be replaced)

Instead of using the form’s name, we use angular.element(elem[0]) to grab the form because Angular uses jqLite natively. Another cool thing is that we will only warn the user if the form hasn’t been submitted. This way, the user’s good to navigate away after they clicked the submit button.

I just updated this with the ability to pass in a collection and watch that collection for changes – this will allow us to monitor more complex forms for changes.

Hope your day is going great!

 

Cheers,

Sam

 

NodeJS – Wrapping the Request() Module to Point Fingers at Services

Lately, I’ve been looking into tracing the route of service calls through chained services. Since many companies are moving toward a Service-Oriented-Architecture model, tracing when services are called is a crucial step in debugging. This way, when there’s an issue with a service, we know which one is breaking and causing everyone downstream to fail.

As a quick example of what I’m talking about, I’ve made a simple model:

Service A: returns “Hello”

Service B: returns “world”

Service C: calls A and B, then returns “Hello world”

 

Now, imagine service B is taking a long time to respond. Service C will be stuck, and we couldn’t be sure whether it was A or B who is causing the problem!

My goal is to get in the middle of those calls to services A and B and attach headers that track when those calls are made. First, I tried digging into my services’ Bunyan() logging modules and adding my interceptors there. Didn’t work.

Instead, after asking around work and on StackOverflow, I’ve decided to make my own NodeJS module that wraps around Request() and handles all of this for developers with them doing a minimal amount of work. Here’s what I came up with:


'use strict';
var request = require('request');

function spRequest( options, callback ) {

options.incoming_req.headers['id'];

// check for our specific headers
if (options.incoming_req.headers['id'] == undefined) {
   console.log('No id passed to this service');
}
else{
   console.log('I was given id ' + options.incoming_req.headers['id']);
}

// pass those headers boldly forward where no service call has gone before
var forwardHeaders = {
   id: options.incoming_req.headers['id']
   //add other headers here as needed
};

var forwardOptions = { //these are the options we'll use to call request()
   url: options.url,
   headers: forwardHeaders
};

// get start time for the call
var hrTime = process.hrtime()

// we use hrTime because it's more accurate than Date.now()
var start = hrTime[0] * 1000000 + hrTime[1] / 1000;

// called when request() comes back with our data
function _onResponse( start, cb ) {
   var hrTime = process.hrtime()
   var end = hrTime[0] * 1000000 + hrTime[1] / 1000; //since hrTime is an array{milliseconds, nanoseconds} we have to do a bit of conversion
   var responseTime = end - start;
   console.log('request took ' + responseTime + ' milliseconds');

   return cb;
}

// and...GO!
   return request(forwardOptions, _onResponse(start, callback));
}

module.exports = spRequest

What’s really cool about this is that developers really only have to change one thing in their code. When making a HTTP request, they just have to include the request object in the call, like so:

Old way:

request('http://localhost:3000/hello', function (error, response, body) {...});

New way:

request({incoming_req: req, url: 'http://localhost:3000/hello'}, function (error, response, body) {...});

I learned a lot about functional programming today.

Do you like this? Is this something you’d like to read more about? Do you hate this and want me to write more about cooking or building physical things?

Let me know in the comments!

Happy Thursday, everybody!

AngularJS – Form Validation with Angular-UI-Validate

I was recently tasked with validating an input field – comparing whatever someone typed with a list and making sure there aren’t any duplicates. Here’s what I did:

Install Angular-UI-Validate. I chose to use this library because of its ability to skip the hassle of writing custom formatters and easy integration with Angular. Make sure to add it as a module in app.js!

 


angular.module('app', [ui.validate'])

 

In the controller, I had to add a function that checked the value passed to it against a list of names.


$scope.nameHasNotBeenUsed = function( $value ) {
 console.log($value);
 if ($scope.listOfNames) { // check if the names have been loaded yet
 var names = []; // for holding the names of the stacks
 angular.forEach($scope.listOfNames , function(name) { 
 names.push(name.name); // put the names in the array
 });
 return names.indexOf($value) === -1; // returns true if the name doesn't exist in the array; false otherwise
 }
 }

In the view, I just had to attach a couple properties to the input: ui-validate and ng-class.

<form name="formName"> 
   <div class="form-group"> 
      <div class="input-group"> 
         <input name="inputName" ui-validate=" {taken : 'nameHasNotBeenUsed($value)' } " ng-pattern="/^[a-zA-Z0-9-_]*$/" ng-class="{ 'permission-input-wrong': formName.inputName.$error.taken }" required ng-model="inputModel" type="text" class="form-control"> 
      </div> 
      <span ng-show='formName.inputName.$error.taken'>This name is already in use.</span> 
   </div> 
</form>

In the ui-validate property, the input is labeled as taken if the $scope.nameHasNotBeenUsed function returns false. Then, ng-class takes over: it checks for formName.inputName.$error.taken, and if it’s present, it applies the class permission-input-wrong (this just outlines the field in red).

Another cool thing we are doing is selectively hiding and showing error messages based on that formName.inputName.$error.taken.

 

Thanks for reading! If this helps you – if this is a dumb way of doing this, and I need to be corrected – tell me in the comments!

 

Cheers,

Sam

 

Chocolate. Caviar.

Finished Chocolate Caviar
Dessert that pretends it’s dinner.

I cut dates in half and removed the pits. Then, I filled up the date halves with Nutella.

Date halves filled with Nutella.

I crushed up some walnuts with my bare hands and coated them in raw, organic cacao and honey, along with some hazelnut oil. These are our ‘caviar’ eggs. They get sprinkled all over the date-Nutella shells.

Our chocolate caviar.

 

 

 

Some ‘caviar’ spooned onto the date halves

Then, I heated some organic goji berries with water and more hazelnut oil and arranged it all on a pretty plate.

Rehydrated Goji berries

And it was good.

The pictures don’t really do it justice. I need a macro lens.

 

They came out even better than I dared to dream. The Goji berries balance out the intense chocolate sweetness and serve to cleanse the palette between tastes.

 

Please, give me free stuff, Ferrero (Makers of Nutella).

[Tutorial] Taking Automation to the next level

0000159_round-small-sticker-with-the-nfc-wave-logoEven though most of us have an idea about what NFCs are and what they can do, we shun the thought of using them due to their lack
of application in our lives. Well, I’m here to tell you, that’s finally changed. With the help of few dollars, you can take automate your mundane activities and have your mobile phone take care of it for you.

No, this does not require any expensive sensors, hardware or fobs, just a few NFC tags that can hold a little “ID” data for you. NFC tags are pretty great. They are small, paper-like and hold 450 bytes of writable memory. Apologies for getting really technical, but I am here to show you that irrespective of your depth of knowledge in technology, you can do some really nifty things with NFC.

71X4I6-pU3L._SL1100_

 

I am going to write a small tutorial on what you can do and how with some examples to make it abundantly clear.

For this you are going to need:

  1. NFC Tags ( I recommend these tokens) $13 for 10pcs
  2. IFTTT Software (If this then that) (Free and paid available) (I recommend Automagic Premium)

 

Once you buy these NFC tags, we need to tell your phone what to do when the NFC is detected by it. Needless to say, you’re going to require an NFC enabled phone. With the help of apps that implement the IFTTT technology. You can set action triggers, conditions and finally, actions for your phone to do. I use Automatic Premium, but there are other free alternatives too. We are going to define a flow for this, which is nothing but a simple, graph-like structure that gives basic instructions to your phone.

Let’s go ahead and tell our app to listen for a particular NFC tag (each NFC has it’s own unique ID)
Reading for a new Tag will simply open up the wizard UI and wait patiently for you to tap the NFC tag gently on the backside of your phone.

Screenshot_2015-04-15-14-50-04

Once you do, the app reads the “ID” data that uniquely defines an NFC tag. For the sake of this demo, let’s give this NFC ID a nick name, i.e. “Sleep”.

Screenshot_2015-04-15-14-50-20

Next, we tell the app what to do when this NFC Tag is detected. This can be done using the IFTTT app. With the help of simple triggers, conditions and actions to perform we can automate simple tasks.
Here’s an example: Every night before sleeping I turn my data and wifi off and enable battery saving mode. This practice helps to improve my battery life.
Like I mentioned before, if there is absolutely any activity that is performed repeatedly it can be automated complete. So I quickly drag and drop some boxes on the app’s graph UI and tell it what to do.

Screenshot_2015-04-15-14-49-45All I am doing is telling the app to perform the following actions if at all the NFC Tag is detected (Trigger). Check if the time is between 9pm to 7am (Condition), if true set the data / wifi state to off and turn on battery saving mode (action).

Trust me when I say this: making these simple flows is one of the easiest things I have ever done. I encourage everyone to at least give it a shot. There are tons of other applications of these tags and tools: Tapping the tag on your car dashboard starts data, location and opens your maps app for guidance. You’re only limited by your own imagination.