Blynk local server を使うとAlexaが答えてくれない

published_with_changes 更新日: event_note 公開日:

labelamazon alexa labelBlynk labelIoT labelRaspberry pi

「アレクサ、部屋の温度を教えて」を実現のための第7歩になります。

Alexaと対話できるようになったのですが、Blynkをローカルサーバーに変えたら使えなくなりました。原因はインターネットからBlynk local serverが見えていなかったことと、Blynkを使う上でのポート設定でした。

しっかりDocumentを読めば書いてあるのですが、そこを飛ばして、見よう見まねとコピペでやっているので、なかなかうまく回りません。

状況

クラウドサーバのBlynkでは、問題なくAlexaが温度をアナウンスしてくれたのですが、BlynkのTokenをlocal server発行のものに変えたら、「スキルがリクエストに正しく応答できませんでした。」となりうまくいきません。

192.168.0.22:9443はBlynk local serverのLAN内のアドレスとポートです。ブラウザで、
https://192.168.0.22:9443/"ローカルのblynkAuthToken"/get/v0
などとやると、温度センサ(BME280)の値は読めています。

LAN内ではOKなので、外からBlynk local serverが見えていないようです。Blynkがそういうことをやってくれていると勝手に思っていたのですが、勝手な思い込みでした。

対策1:Raspberry Piに外部ネットワークからアクセス

このブログを読んでやり方を学びました。
https://camo.qiitausercontent.com/84a041f26da6f1478f6df3f9ee01a78779f865ab/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f39333736332f32643463336436372d623165372d353731342d306331312d3632336632653163346435362e706e67

Raspberry Piに外部ネットワークからアクセスできる様にして携帯でペットを遠隔監視する方法 - Qiita

はじめに 前回、赤外線カメラモジュールを繋げて「mjpg-streamer」を使い、家内のネットワークのブラウザから動画を見れる様にしました。 【Raspberry Piで赤外線カメラモジュールを使ってみる】 今回はその続きで...


1. Raspberry PiのプライベートIPアドレスの固定

Rasbian上でソフト的に固定するのですが、これは実施済みです。

2. 外部にサーバを公開

1)ルータにグローバルIPアドレスを設定する

プロバイダから自動的に割り当てられるグローバルIPアドレスは、ルータを再起動すると変わります。ルータのグローバルIPアドレスはルータ自身が持つので、あえて設定できるものではありません。

2)ポートマッピングの設定 (ルーターのポートを解放する)

外部からRaspberryPiアクセスできる様にルーターのポートを解放し、解放したポートを先ほど固定したRaspberryPiのプライベートIPアドレスと紐づけます。

ブラウザから、192.168.0.1 などとややって、ルータにログインします。


左列の「詳細設定」から「IPv4ポートマッピング設定」を選びます。次の上の画面は既に設定された画面です。新規に設定するときは、右下の「追加」をタップします。

2番目の画面になり、ここで設定をします。


  • LAN側ホスト:Blynk local serverのIPアドレスを指定します。たとえば、192.168.0.22
  • プロトコル:TCPを指定します。
  • ポート番号 :8080 です。8080だけを開けたいので、左・右の空白に8080と入力します。
ポート番号 :8080についてはGithubのblynkkk/blynk-serverに、つぎのような記載がありました。
Run the server on default 'hardware port 8080' and default 'application port 9443' (SSL port) 
  • 優先度:1~50の間で入力します。一番初めにポート開放するなら1です。次は2,3,4….となります。

以上で、ポート開放は完了です。右下の「設定」をタップします。

3)IPパケットフィルタリングの設定(確認のみ)

外部からサーバへのアクセスを許可する設定を行います。また、外部からサーバへの不要なパケットのアクセスをブロックする設定を行います。


4)ルータでも、Raspberry PiのプライベートIPアドレスを固定

Raspberry PiのMACアドレスとRaspberry PiのIPv4アドレスを紐づけ(固定化)します。

コマンドプロンプトで「arp -a」とやるとリストが出てきます。b8-27で始まるのがRaspberry pi のMACアドレスで、その行の左がRaspberry pi のIPアドレスです。

ルーターに戻って、「詳細設定」のなかの「DHCPv4固定割当設定」をクリックして、右下の「追加」をクリックします。

設定画面がでるので、先ほど調べたRaspberry PiのMACアドレスとRaspberry PiのIPv4アドレスを入力します。最後に「設定」をクリックして記録します。

ルーターの設定を変えたときは、最終的に左上の「保存」(オレンジに変わっている)をクリックします。さらにルーターの再起動を行います。

3. ファイアウォール設定

ここを参考にして設定しました。

【パクろう】ラズパイでファイアーウォールを設定する方法

【パクろう】ラズパイでファイアーウォールを設定する方法

みなさんラズパイでファイアウォールの設定をしていますか?ここではその設定方法や、そのまま使えるお勧め設定を載せました。ラズパイのファイアウォール設定はこれを見れば完璧です。

開けたポートは
SSH:ポート22
VNC:ポート5900
Blynk:ポート8080, 9443

Blynk側のセキュリティは、次のように記されています。

Blynkサーバーには、さまざまなセキュリティレベル用に開いている5つのポートがあります。
  • 80-ハードウェアのプレーンTCP接続(セキュリティなし)
  • 8080-ハードウェア用のプレーンTCP接続(セキュリティなし)
  • 443-モバイルアプリとSSLを使用したハードウェアのSSL / TLS接続
  • 9443-モバイルアプリとSSLを使用したハードウェアのSSL / TLS接続
ハードウェアは、その機能に応じて、443(9443)または80(8080)への接続を選択する場合があります。アプリとサーバー間の接続は常にSSL / TLSを介して行われるため、常に保護されています。ハードウェアとサーバー間の接続は、ハードウェアの機能によって異なります。引用元:https://docs.blynk.cc/Security

対策2:AWS Lambda関数の変更

クラウドのBlynkサーバー用に作ったLambda関数をBlynk local server用に変更します。変更箇所はクラウドサーバーをローカルサーバーに変え、加えてポートを変更します。

変更前

http.get({
  host: 'blynk-cloud.com',
  path: '/' + blynkAuthToken + '/get/' + blynkPin,
  port: '80'
}

 変更後

http.get({
  host: globalIP, // globalIPはルータのグローバルIPアドレス
  path: '/' + blynkAuthToken + '/get/' + blynkPin,
  port: '8080'
}

 portについては、上の方にも書きましたがGitHubにつぎのようにあるので'8080'にしたらうまくいきました。

 Run the server on default 'hardware port 8080' and default 'application port 9443' (SSL port)


Alexa Skill ローカルサーバー用スクリプト

対話モデルのjsonコード

alexa developer consoleで作る対話モデルのjsonコードです。
taiwa_skill.json
{
    "interactionModel": {
        "languageModel": {
            "invocationName": "ローカル",
            "intents": [
                {
                    "name": "AMAZON.CancelIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "samples": []
                },
                {
                    "name": "HelloWorldIntent",
                    "slots": [],
                    "samples": [
                        "hello",
                        "ハロー",
                        "こんにちは"
                    ]
                },
                {
                    "name": "AMAZON.NavigateHomeIntent",
                    "samples": []
                },
                {
                    "name": "GetMeasurement",
                    "slots": [
                        {
                            "name": "Location",
                            "type": "LIST_OF_LOCATIONS"
                        },
                        {
                            "name": "Measurement",
                            "type": "LIST_OF_MEASUREMENTS"
                        }
                    ],
                    "samples": [
                        "GetMeasurement {Location} ",
                        "GetMeasurement {Location} は",
                        "GetMeasurement {Location} はいくつ",
                        "GetMeasurement {Location} を教えて",
                        "GetMeasurement {Location}  の  {Measurement} はどんだけ",
                        "GetMeasurement {Measurement} はどんだけ",
                        "GetMeasurement {Location} は  {Measurement}",
                        "GetMeasurement {Location} の  {Measurement} を教えて",
                        "GetMeasurement {Location}  の  {Measurement} はいくつ",
                        "GetMeasurement {Location}  の {Measurement} は",
                        "GetMeasurement {Location} の  {Measurement}",
                        "GetMeasurement {Measurement} を教えて",
                        "GetMeasurement {Measurement} はいくつ",
                        "GetMeasurement {Measurement} は",
                        "GetMeasurement {Measurement}"
                    ]
                }
            ],
            "types": [
                {
                    "name": "LIST_OF_LOCATIONS",
                    "values": [
                        {
                            "name": {
                                "value": "屋根裏",
                                "synonyms": [
                                    "天井裏"
                                ]
                            }
                        },
                        {
                            "name": {
                                "value": "仏間",
                                "synonyms": [
                                    "和室"
                                ]
                            }
                        },
                        {
                            "name": {
                                "value": "二階"
                            }
                        }
                    ]
                },
                {
                    "name": "LIST_OF_MEASUREMENTS",
                    "values": [
                        {
                            "name": {
                                "value": "熱中度指数",
                                "synonyms": [
                                    "暑さ度",
                                    "熱中度",
                                    "暑さ指数"
                                ]
                            }
                        },
                        {
                            "name": {
                                "value": "気圧",
                                "synonyms": [
                                    "大気圧"
                                ]
                            }
                        },
                        {
                            "name": {
                                "value": "湿度",
                                "synonyms": [
                                    "湿気"
                                ]
                            }
                        },
                        {
                            "name": {
                                "value": "気温",
                                "synonyms": [
                                    "何度",
                                    "室温",
                                    "温度"
                                ]
                            }
                        }
                    ]
                }
            ]
        }
    }
}

AWS Lambda

index.js


'use strict';

/**
 * 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
 */

var http = require('http');

// --------------- 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,
        response: speechletResponse,
    };
}


// --------------- 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.
    // セッションを初期化していくつかの属性を持たせたい場合は、それらをここに追加できます。
    const sessionAttributes = {};
    const cardTitle = 'Welcome';
    const speechOutput = "二階の温度を教えてなどと問いかけると、それを答えます。";
    // 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.
    // ユーザーがウェルカムメッセージに返信しないか、理解できないことを言った場合、このテキストが表示されます。
    const repromptText = "私はこの部屋を見てるわよっ!何が知りたいのっ!   ってかっ";
    const shouldEndSession = false;

    callback(sessionAttributes,
        buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}

function handleSessionEndRequest(callback) {
    const cardTitle = 'Session Ended';
    const 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.
    const shouldEndSession = true;

    callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}

function createLocationAttributes(location) {
    return {
        location: location
    };
}

/**
 * Read temperature in the session and prepares the speech to reply to the user.
 */
function readTemperatureInSession(intent, session, callback) {
    const cardTitle = intent.name;
    const LocationSlot = intent.slots.Location;
    const MeasurementSlot = intent.slots.Measurement;
    let repromptText = '';
    let sessionAttributes = {};
    const shouldEndSession = false;
    let speechOutput = '';
	var body = '';
    var globalIP = '**.***.**.***'; // host: ルータのグローバルIPアドレス。ルータを再起動すると変わる
    var blynkport = '8080'; // port: blynk local serverのhardware port software port 9443 ではNGだった
	var blynkAuthToken; // projectの token
	var blynkPin; // 欲しいセンサ値を書き込んだblynkのvirtual pin番号

    var location = LocationSlot.value;
    if (location == '' && session.attributes) {
        location = session.attributes.location;
    }
    if (location === '') {
        location = '二階';
    }
    switch (location) {
        case '二階':
            blynkAuthToken = '****_nikaino_BME280_notoken_****'; // 二階のBME280のtoken ローカル
            break;
        case '仏間':
            blynkAuthToken = '***_butsumano_BME280_notoken_***'; // 仏間のBME280のtoken ローカル
            break;
        case '屋根裏':
            blynkAuthToken = '***_yaneurano_BME280_notoken_***'; // 屋根裏のBME280のtoken ローカル
            break;
    }

    var measurement = MeasurementSlot.value;
    if (measurement === '') {
        measurement = '温度';
    }
    switch (measurement) {
        case '何度':
        case '気温':
        case '室温':
        case '温度':
            blynkPin = 'V0';
            break;
        case '湿気':
        case '湿度':
            blynkPin = 'V1';
            break;
        case '大気圧':
        case '気圧':
            blynkPin = 'V2';
            break;
        case '熱中度指数':
        case '熱中度':
        case '暑さ指数':
        case '暑さ度':
        case '暑さ':
            blynkPin = 'V4';
            break;
    }

	var httpPromise = new Promise( function(resolve,reject){
		http.get({
      host: globalIP,
			path: '/' + blynkAuthToken + '/get/' + blynkPin,
      port: blynkport
		}, function(response) {
			// Continuously update stream with data
			response.on('data', function(d) {
				body += d;
			});
			response.on('end', function() {
				// Data reception is done, do whatever with it!
				console.log(body);
				resolve('Done Sending');
			});
		});
	});
	httpPromise.then(
		function(data) {
			console.log('Function called succesfully:', data);
			var info = parseFloat(JSON.parse(body));
			if (measurement === '何度' ) {
			speechOutput = location + 'は ' ;
			} else {
			speechOutput = location + 'の ' + measurement + 'は ';
			}
        	repromptText = speechOutput;
			switch (measurement) {
                default:
                case '何度':
                case '気温':
                case '室温':
                case '温度':
        			speechOutput = speechOutput + info.toFixed(1) + '度 です。';
		        	repromptText = repromptText + info.toFixed(1) + '℃です。';
                    break;
                case '湿気':
                case '湿度':
        			speechOutput = speechOutput + info.toFixed(1) + 'パーセント です。';
		        	repromptText = repromptText + info.toFixed(1) + '%です。';
                    break;
                case '大気圧':
                case '気圧':
        			speechOutput = speechOutput + info.toFixed(0) + 'ヘクトパスカル です。';
		        	repromptText = repromptText + info.toFixed(0) + 'hPaです。';
                    break;
                case '熱中度指数':
                case '熱中度':
                case '暑さ指数':
                case '暑さ度':
                case '暑さ':
                	var annotation = '';
                    if ( info >= 31 ){
                        annotation = '危険! ブラックです。';
                    } else if ( info >= 28){
                        annotation = '厳重警戒! レッドです。';
                    } else if ( info >= 25){
                        annotation = '警戒! オレンジです。';
			        } else if ( info >= 21){
                        annotation = '注意です。';
		        	} else {
		        	    annotation = '';
		        	}
        			speechOutput = speechOutput + info.toFixed(0) + '度 です。' + annotation;
		        	repromptText = repromptText + info.toFixed(0) + '℃です。'  + annotation;
                    break;
			}
			console.log(speechOutput);
			sessionAttributes = createLocationAttributes(location);
			callback(sessionAttributes,buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
		},
		function(err) {
			console.log('An error occurred:', err);
		}
	);
}
    
// --------------- Events -----------------------

/**
 * 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}");

    const intent = intentRequest.intent;
    const intentName = intentRequest.intent.name;

    // Dispatch to your skill's intent handlers
    if (intentName === 'GetMeasurement') {
        readTemperatureInSession(intent, session, callback);
    } else if (intentName === 'AMAZON.HelpIntent') {
        getWelcomeResponse(callback);
    } else if (intentName === 'AMAZON.StopIntent' || intentName === 'AMAZON.CancelIntent') {
        handleSessionEndRequest(callback);
    } else {
        throw new Error('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
}


// --------------- Main handler -----------------------

// Route the incoming request based on type (LaunchRequest, IntentRequest,
// etc.) The JSON body of the request is provided in the event parameter.
exports.handler = (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);
    }
};

Alexa Skills Kitでテスト

local serverでも、クラウドサーバと同じようにちゃんと対話できるようになりました。


課題

(1) オリジナルスキルは「ローカルを開いて」とかの呼び出し名から始めなければならず。2回のやり取りが必要です。温度を知りたいだけならば、Alexaに聞くのは面倒です。Blynkアプリを開く方が簡単です。

(2) ルーターのグローバルIPアドレスはルーターが再起動すると変わります。私の環境では何かの理由でルーターが再起動することがよくあり、Alexaが答えなくなります。そのたびに、AWS Lambdaを開いて、index.jsでグローバルIPアドレスを書き換える必要があります。

(3) Lambdaの問題かもしれませんが、グローバルIPアドレスが変わって新しい温度を読み込んでいなくても、Alexaは覚えていた温度を答えます。

以上の理由から、「アレクサ、部屋の温度を教えて」をほとんど使わなくなりました。グローバルIPアドレスを都度見に行くようなLambda関数を作れれば問題解決なのですが、そのような能力はありません。


Powered by Blogger | Designed by QooQ

keyboard_double_arrow_down

keyboard_double_arrow_down