Sricharan BachuCharles Huang
Published © GPL3+

Amazon Echo to Arduino Connection

We are using AWS lambda functions to update firebase. Then we will use an Arduino to carry out a task whenever anything on firebase changes.

IntermediateWork in progress1,287
Amazon Echo to Arduino Connection

Things used in this project

Story

Read more

Custom parts and enclosures

Final Product Working

Code

index.js

JavaScript
Store data from Echo to firebase
'use strict';
//https://stackoverflow.com/questions/37325775/amazon-lambda-to-firebase
//sice me
var firebase = require("firebase");

exports.handler = (event, context, callback) => {
    context.callbackWaitsForEmptyEventLoop = false;  //<---Important

    var config = {
    apiKey: "foo",
    authDomain: "foo",
    databaseURL: "foo",
    projectId: "foo",
    storageBucket: "foo",
    messagingSenderId: "foo"
};

if(firebase.apps.length == 0) {   // <---Important!!! In lambda, it will cause double initialization.
        firebase.initializeApp(config);
    }

var defaultDatabase = firebase.database();
// if(event.request.type) == "IntentRequest"{
//     if(event.request.intent.name) == "changeLightState"{
//         firebase.database().ref('lightState/').set({
//             state: event.request.intent.slots.command.value
//         });
//     }
// }
    
    if(event.request.intent.name == "changeLightState")
    var lState = event.request.intent.slots.command.value;
    firebase.database().ref('lightState/').set({
         state: lState
    });
    var response = "Mission Accomplished";
    var res = buildRes(response)

    setTimeout(function(){context.succeed(res);}, 1000);

}
//change the lambda function on the skill

// Helpers



function buildRes(response) {
    return {
        version: "1.0",
        "response": {
        "outputSpeech": {
            "type": "PlainText",
            "text": response,
            },
            "shouldEndSession": true
        }  
    } 

}

Credits

Sricharan Bachu
1 project • 1 follower
Charles Huang
0 projects • 0 followers

Comments