Skip to content

Instantly share code, notes, and snippets.

@silverjam
Created August 10, 2016 18:12
Show Gist options
  • Save silverjam/7196a2bd52b1a6eb94cad55813e3f511 to your computer and use it in GitHub Desktop.
Save silverjam/7196a2bd52b1a6eb94cad55813e3f511 to your computer and use it in GitHub Desktop.
Alexa Lambda Arduino
{
"intents": [
{
"intent": "YourDino"
},
{
"intent": "HasTeradactyl"
}
]
}
/**
* This sample demonstrates a simple skill built with the Amazon Alexa Skills Kit.
* The Intent Schema, Custom Slots, and Sample Utterances for this skill, as well as
* testing instructions are located at http://amzn.to/1LzFrj6
*
* For additional samples, visit the Alexa Skills Kit Getting Started guide at
* http://amzn.to/1LGWsLG
*/
const net = require('net');
// Route the incoming request based on type (LaunchRequest, IntentRequest,
// etc.) The JSON body of the request is provided in the event parameter.
exports.handler = function (event, context) {
try {
console.log("event.session.application.applicationId=" + event.session.application.applicationId);
/**
* Uncomment this if statement and populate with your skill's application ID to
* prevent someone else from configuring a skill that sends requests to this function.
*/
/*
if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.[unique-value-here]") {
context.fail("Invalid Application ID");
}
*/
if (event.session.new) {
onSessionStarted({requestId: event.request.requestId}, event.session);
}
if (event.request.type === "LaunchRequest") {
onLaunch(event.request,
event.session,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === "IntentRequest") {
onIntent(event.request,
event.session,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === "SessionEndedRequest") {
onSessionEnded(event.request, event.session);
context.succeed();
}
} catch (e) {
context.fail("Exception: " + e);
}
};
/**
* Called when the session starts.
*/
function onSessionStarted(sessionStartedRequest, session) {
console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId +
", sessionId=" + session.sessionId);
}
/**
* Called when the user launches the skill without specifying what they want.
*/
function onLaunch(launchRequest, session, callback) {
console.log("onLaunch requestId=" + launchRequest.requestId +
", sessionId=" + session.sessionId);
// Dispatch to your skill's launch.
getWelcomeResponse(callback);
}
/**
* Called when the user specifies an intent for this skill.
*/
function onIntent(intentRequest, session, callback) {
console.log("onIntent requestId=" + intentRequest.requestId +
", sessionId=" + session.sessionId);
var intent = intentRequest.intent,
intentName = intentRequest.intent.name;
// Dispatch to your skill's intent handlers
if ("YourDino" === intentName) {
const client = net.connect({host: "XX", port: 1337}, () => {
// 'connect' listener
console.log('connected to server!');
client.write('y');
handleDino(intentRequest, session, callback);
});
} else if ("HasTeradactyl" === intentName) {
const client2 = net.connect({host: "XX", port: 1337}, () => {
// 'connect' listener
console.log('connected to server!');
client2.write('n');
handleDino(intentRequest, session, callback);
});
} else if ("AMAZON.HelpIntent" === intentName) {
getWelcomeResponse(callback);
} else if ("AMAZON.StopIntent" === intentName || "AMAZON.CancelIntent" === intentName) {
handleSessionEndRequest(callback);
} else {
throw "Invalid intent";
}
}
/**
* Called when the user ends the session.
* Is not called when the skill returns shouldEndSession=true.
*/
function onSessionEnded(sessionEndedRequest, session) {
console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId +
", sessionId=" + session.sessionId);
// Add cleanup logic here
}
// --------------- Functions that control the skill's behavior -----------------------
function getWelcomeResponse(callback) {
// If we wanted to initialize the session to have some attributes we could add those here.
var sessionAttributes = {};
var cardTitle = "Welcome";
var speechOutput = "Welcome to the Alexa Skills Kit sample. " +
"Please tell me your favorite color by saying, my favorite color is red";
// If the user either does not reply to the welcome message or says something that is not
// understood, they will be prompted again with this text.
var repromptText = "Please tell me your favorite color by saying, " +
"my favorite color is red";
var shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
function handleSessionEndRequest(callback) {
var cardTitle = "Session Ended";
var speechOutput = "Thank you for trying the Alexa Skills Kit sample. Have a nice day!";
// Setting this to true ends the session and exits the skill.
var shouldEndSession = true;
callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}
/**
* Sets the color in the session and prepares the speech to reply to the user.
*/
function handleDino(intent, session, callback) {
var cardTitle = intent.name;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = true;
var speechOutput = "";
speechOutput = "ok, I asked Jarvis, check out his screen";
repromptText = "sorry, Jarvis didn't understand the question";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
function createFavoriteColorAttributes(favoriteColor) {
return {
favoriteColor: favoriteColor
};
}
function getColorFromSession(intent, session, callback) {
var favoriteColor;
var repromptText = null;
var sessionAttributes = {};
var shouldEndSession = false;
var speechOutput = "";
if (session.attributes) {
favoriteColor = session.attributes.favoriteColor;
}
if (favoriteColor) {
speechOutput = "Your favorite color is " + favoriteColor + ". Goodbye.";
shouldEndSession = true;
} else {
speechOutput = "I'm not sure what your favorite color is, you can say, my favorite color " +
" is red";
}
// Setting repromptText to null signifies that we do not want to reprompt the user.
// If the user does not respond or says something that is not understood, the session
// will end.
callback(sessionAttributes,
buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession));
}
// --------------- Helpers that build all of the responses -----------------------
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
return {
outputSpeech: {
type: "PlainText",
text: output
},
card: {
type: "Simple",
title: "SessionSpeechlet - " + title,
content: "SessionSpeechlet - " + output
},
reprompt: {
outputSpeech: {
type: "PlainText",
text: repromptText
}
},
shouldEndSession: shouldEndSession
};
}
function buildResponse(sessionAttributes, speechletResponse) {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
};
}
#include "Arduino.h"
#include "SSD1306.h"
#include <ESP8266WiFi.h>
SSD1306 display(0x3c, D3, D5);
char ssid[] = "xx"; // your network SSID (name)
char pass[] = "xx"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
WiFiServer server(1337);
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println();
// Initialising the UI will init the display too.
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
//WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
// attempt to connect to Wifi network:
while ( WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
String szStatus = String(WiFi.status());
Serial.println("status: " + szStatus);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
Serial.println("setup done, ip address: " + WiFi.localIP().toString());
}
void drawCommand(char cmd) {
// Font Demo1
// create more fonts at http://oleddisplay.squix.ch/
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
/*
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, "Hello world");
*/
if (cmd == 'y') {
display.drawString(0, 0, "Why, yes...\n it is my dinosaur...");
} else if (cmd == 'n') {
display.drawString(0, 0, "No, I don't\n own a teradactyl.");
} else {
display.drawString(0, 0, "Awaiting...");
}
/*
display.setFont(ArialMT_Plain_24);
display.drawString(0, 26, "Hello world");
*/
}
void loop()
{
display.setTextAlignment(TEXT_ALIGN_RIGHT);
WiFiClient client = server.available();
if (!client) {
Serial.println("no client");
drawCommand('x');
}
bool gotCommand = false;
if (client) {
while (client.connected()) {
Serial.println("got client");
if (client.available()) {
char command = client.read();
drawCommand(command);
gotCommand = true;
}
client.stop();
}
}
display.drawString(10, 128, String(millis()));
display.display();
if (gotCommand) {
delay(5000);
} else {
delay(500);
}
}
YourDino is that your dinosaur
YourDino if that's his dinosaur
HasTeradactyl do you have a teradactyl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment