Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
vincent wong
Published © GPL3+

Alexa, ask Microcontroller Assist

Listen to a list of "10 steps to choose perfect microcontroller" which every designer should perform before making the choice.

IntermediateFull instructions provided1,154
Alexa, ask Microcontroller Assist

Things used in this project

Story

Read more

Code

index.js

JavaScript
'use strict';
module.change_code = 1;
var _ = require('lodash');
var Skill = require('alexa-app');
var CHIP_SESSION_KEY = 'chip';
var skillService = new Skill.app('chip');
var ChipHelper = require('./chip_helper');
var DatabaseHelper = require('./database_helper');
var databaseHelper = new DatabaseHelper();

skillService.pre = function(request, response, type) {
  databaseHelper.createChipTable();
};

var getChipHelper = function(chipHelperData) {
  if (chipHelperData === undefined) {
    chipHelperData = {};
  }
  return new ChipHelper(chipHelperData);
};

var getChipHelperFromRequest = function(request) {
  var chipHelperData = request.session(CHIP_SESSION_KEY);
  return getChipHelper(chipHelperData);
};

var chipIntentFunction = function(chipHelper, request, response) {
  console.log(chipHelper);
  if (chipHelper.lastStep()) {
    response.say(chipHelper.getPrompt() + 'Congratulations! You have complete the steps! Hope you enjoy!');
    response.shouldEndSession(true);
  } else {
    response.say(chipHelper.getPrompt() + " Just say next or continue to go to the next step");
    response.reprompt("I didnt hear you. " + chipHelper.getPrompt() + " Just say next or continue to go to the next step");
    response.shouldEndSession(false);
  }
  response.session(CHIP_SESSION_KEY, chipHelper);
  response.send();
};

skillService.launch(function(request, response) {
  var prompt = 'Welcome to Microcontroller Assist! To start choosing a microcontroller assist, say start the step';
  var reprompt = 'I didnt hear you.  To start choosing a microcontroller assist, say start the step';
  response.say(prompt);
  response.reprompt(reprompt);
  response.shouldEndSession(false);
  response.send();
});

skillService.intent('AMAZON.HelpIntent', {},
	function(request, response){
		var prompt = 'You are using Microcontroller Assist! You can say things like, start the step, or next, or save my step, or load the last step...';
		var reprompt = 'You can say things like, start the step, or next, or save my step, or load the last step... Now, what can I help you with?';
		response.say(prompt);
		response.reprompt(reprompt);
		response.shouldEndSession(false);
		response.send();
	}
);

skillService.intent('AMAZON.StopIntent', {},
	function(request, response){
		response.say('Thank you for using Microcontroller Assist.  Goodbye.');
		response.shouldEndSession(true);		
		response.send();
	}
);

skillService.intent('AMAZON.CancelIntent', {},
	function(request, response){
		response.say('Thank you for using Microcontroller Assist.  Goodbye.');
		response.shouldEndSession(true);		
		response.send();
	}
);

skillService.intent('chipIntent', {
    'utterances': ['{start|begin} {|a|the} step']
  },
  function(request, response) {
    var chipHelper = new ChipHelper({});
    chipIntentFunction(chipHelper, request, response);
  }
);


skillService.intent('saveChipIntent', {
    'utterances': ['{save} {|a|the|my} step']
  },
  function(request, response) {
  //code goes here!
    var chipHelper = getChipHelperFromRequest(request);

    saveChip(chipHelper, request);
    response.say('Your microcontroller choosing progress has been saved!');
    response.shouldEndSession(true).send();
    return false;
  }
);

skillService.intent('loadChipIntent', {
    'utterances': ['{load|resume} {|a|the} {|last} step']
  },
  function(request, response) {
  //code goes here!
    var userId = request.userId;
    databaseHelper.readChipData(userId).then(function(result) {
      return (result === undefined ? {} : JSON.parse(result['data']));
    }).then(function(loadedChipData) {
      var chipHelper = new ChipHelper(loadedChipData);
      return chipIntentFunction(chipHelper, request, response);
    });
    return false;
  }
);

skillService.intent('advanceStepIntent', {
    'utterances': ['{next|advance|continue}']
  },
  function(request, response) {
    var chipHelper = getChipHelperFromRequest(request);
    chipHelper.currentStep++;
	saveChip(chipHelper, request);
    chipIntentFunction(chipHelper, request, response);
  }
);



var saveChip = function(chipHelper, request) {
  var userId = request.userId;
  databaseHelper.storeChipData(userId, JSON.stringify(chipHelper))
    .then(function(result) {
      return result;
    }).catch(function(error) {
      console.log(error);
    });
};




module.exports = skillService;

chip_helper.js

JavaScript
'use strict';
module.change_code = 1;

function ChipHelper(obj) {
  this.currentStep = 0;
  this.steps = [{
    prompt: 'Alexa Microcontroller Assist helps you to choose a perfect microcontroller for your project. When you are ready say next.'
  }, {
    prompt: 'Step 1: Prepare the block diagram of hardware interface. This is the most important and primary action required before going to choose your controller. Make a simple block diagram listing all the hardware interface you are about to use in your project. There are two types of interfaces commonly available. The first one is communication interface where you need to look over the the peripherals such as I2C , SPI, UART and so on. The second one is Digital inputs or analog to digital inputs where you need to look over the ADC feature of your controller. These hardware interfaces will also help to decide the number of pins you need to dedicate for it.'
  }, {
    prompt: 'Step 2: Analyse the software architecture. Analyzing the software architecture can help you to design the system with much precision and avoid possibility of errors. Note down the requirements such as frequency of operation, time required to execute instructions, complexity of algorithm you about to use in your system and so on. This analysis will reveal how much processing power will be needed for your system, whether you go for a simple or robust controller.'
  }, {
    prompt: 'Step 3: Requirement of memory. Memory plays a very critical part in any Microcontroller and you have to determine the memory requirements by your system. You should always make sure that your controller doesn\'t run out of space  and also leave some room for future development. The software architecture and hardware requirements will give you a idea how much of the Flash and RAM needed by application.'
  }, {
    prompt: 'Step 4: Cost of the System. Microcontrollers generally vary in price depending on its features and peripherals inbuilt within. You have to consider the cost factor of a system seriously as people expect their system to be cost efficient providing it performs to the specification. So you must go for a cost efficient controller without comprising the performance of the system.'
  }, {
    prompt: 'Step 5: Power requirement. Power constraints can be real tricky as engineers should keep the power consumption of the system as low as possible. This also depends on the source from which the system obtains the power.  Several companies have started to manufacture Ultra low power controller such as Si101x, MSP430 etc and you can use such controllers if it sits with other parameter requirements. '
  }, {
    prompt: 'Setp 6: Summing up all the above. Now you know all the parameters needed for your particular system. Its time to sum up all the above and prepare a list of requirements such as peripherals, architecture, cost, power constraints etc. Highlighting the most important requirements will further help you to find the most suitable controller by making the search easier.'
  }, {
    prompt: 'Step 7: Search for the right microcontroller. Now its the time to seek for the controller which suits all the above parameters without comprising the performance. Run searches in the internet seeking for the specifications by going through the datasheet or some sites do provide facility to narrow down the list of controllers for the user. Feed the peripherals, I/O, cost , power constraints to the search engine and it will narrow down the list of controllers meeting your demands. This will make your job easier.'
  }, {
    prompt: 'Step 8: Go for a development kit. The selection of Microcontroller doesn\'t complete you job since you need to play with development boards to master the complete working of the controller. Development kit will help you to play, experiment things and will tweak your skills on a certain controller. And if you don\'t find any development boards for your controller its probably get back to previous steps and choose another one. Most of the development boards cost under $100 and you can probably find one which fits in your budget.'
  }, {
    prompt: 'Step 9: Tools and compilers. You are almost done with your Microcontroller selection and this is the last step you need to perform before finishing it. You must search for tools and compilers that are available for your microcontroller. Most controller have many compilers, debugging tools and programmers. You should make sure that all the above necessary tools are available, this makes the designing easy and inexpensive.'
  }, {
    prompt: 'Step 10: Start experimenting. That\'s it you have successfully completed the Microcontroller selection process and its time for experimenting with all the things you got. Build test circuits using the development board and it is advised to design the system by splitting it into several divisions. Experimentation will help you to getting rid of bugs and also to alter if any modifications needed to be done. '
  }, {
    prompt: 'Congratulations! You\'ve complete the steps! Hope you enjoy!'
  }];
  for (var prop in obj) this[prop] = obj[prop];
}

ChipHelper.prototype.lastStep = function() {
  return this.currentStep === (this.steps.length - 2);
};

ChipHelper.prototype.completed = function() {
  return this.currentStep === (this.steps.length - 1);
};

ChipHelper.prototype.getPrompt = function() {
  return this.getStep().prompt;
};

ChipHelper.prototype.getStep = function() {
  return this.steps[this.currentStep];
};

module.exports = ChipHelper;

Intent Schema

snippets
{
  "intents": [
    {
      "intent": "chipIntent"
    },
    {
      "intent": "saveChipIntent"
    },
    {
      "intent": "loadChipIntent"
    },
    {
      "intent": "advanceStepIntent"
    },
    {
      "intent": "AMAZON.HelpIntent"
    },
    {
      "intent": "AMAZON.StopIntent"
    },
    {
      "intent": "AMAZON.CancelIntent"
    }
  ]
}

database_helper.js

JavaScript
'use strict';
module.change_code = 1;
var _ = require('lodash');
var CHIP_DATA_TABLE_NAME = 'chipData';
var dynasty = require('dynasty')({});

// var localUrl = 'http://localhost:8000';
// var localCredentials = {
  // region: 'us-east-1',
  // accessKeyId: 'fake',
  // secretAccessKey: 'fake'
// };

// var localDynasty = require('dynasty')(localCredentials, localUrl);
// var dynasty = localDynasty;

function ChipHelper() {}
var chipTable = function() {
  return dynasty.table(CHIP_DATA_TABLE_NAME);
};

ChipHelper.prototype.createChipTable = function() {
  return dynasty.describe(CHIP_DATA_TABLE_NAME)
    .catch(function(error) {
      return dynasty.create(CHIP_DATA_TABLE_NAME, {
        key_schema: {
          hash: ['userId', 'string']
        }
      });
    });
};

ChipHelper.prototype.storeChipData = function(userId, chipData) {
  return chipTable().insert({
    userId: userId,
    data: chipData
  }).catch(function(error) {
    console.log("storeChipData -> " + error);
  });
};

ChipHelper.prototype.readChipData = function(userId) {
  return chipTable().find(userId)
    .then(function(result) {
      return result;
    })
    .catch(function(error) {
      console.log("readChipData -> " + error);
    });
};

module.exports = ChipHelper;

package.json

JSON
{
  "name": "chip",
  "version": "1.0.0",
  "description": "alexa helps you choose a microcontroller!",
  "main": "index.js",
  "author": "wesee",
  "license": "ISC",
  "dependencies": {
    "alexa-app": "^2.3.2",
    "dynasty": "^0.2.4",
    "lodash": "^4.3.0"
  }
}

Sample Utterances

snippets
chipIntent	start step
chipIntent	begin step
chipIntent	start a step
chipIntent	begin a step
chipIntent	start the step
chipIntent	begin the step
saveChipIntent	save step
saveChipIntent	save a step
saveChipIntent	save the step
saveChipIntent	save my step
loadChipIntent	load step
loadChipIntent	resume step
loadChipIntent	load a step
loadChipIntent	resume a step
loadChipIntent	load the step
loadChipIntent	resume the step
loadChipIntent	load last step
loadChipIntent	resume last step
loadChipIntent	load a last step
loadChipIntent	resume a last step
loadChipIntent	load the last step
loadChipIntent	resume the last step
advanceStepIntent	next
advanceStepIntent	advance
advanceStepIntent	continue

Credits

vincent wong

vincent wong

81 projects • 205 followers

Comments