○データ送信用HTMLの作成
次のHTMLコードの入力し、データ送信用HTMLの作成します。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>dweet test</title> | |
<script src="http://dweet.io/client/dweet.io.min.js"></script> | |
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> | |
<script> | |
$(function(){ | |
var thingName = "ichigojam-led-onoff"; //thing nameの設定 | |
// データの送信 | |
function publish(action) { | |
dweetio.dweet_for( | |
thingName, | |
{'action' : action}, | |
function(err, dweet){ | |
console.log(dweet.content); | |
} | |
); | |
} | |
// [ON]ボタンのクリックで文字列「on」を送信 | |
$('#on-button').click(function(){ | |
publish('on'); | |
}); | |
// [OFF]ボタンのクリックで文字列「off」を送信 | |
$('#off-button').click(function(){ | |
publish('off'); | |
}); | |
}); | |
</script> | |
</head> | |
</head> | |
<body> | |
<button id="on-button">ON</button> | |
<button id="off-button">OFF</button> | |
</body> | |
</html> |
「thingName」には任意の固有名を指定します。ここでは、「ichigojam-led-onoff」としています。
○フローの作成
※「debug」ノードは無くても構いません。
③「http request」ノードのプロパティを表示し、[Method]を「GET」、[Return]を「a parsed JSON object」に設定し、[URL]に次の形式のURLを入力します。
「my-thing-name」には任意の固有名を指定します。ここでは、「ichigojam-led-onoff」としています。
④「function」ノードのプロパティを表示し、次のコードを入力します。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var action =msg.payload.with[0].content.action; | |
if(action=="on"){ | |
msg.payload = "LED1"; | |
}else{ | |
msg.payload = "LED0"; | |
} | |
return msg; |

⑤「serial」ノード(output)のプロパティを表示して通信に使用するポートを選択し、[鉛筆]ボタンをクリックし、「add split character to output messages」のチェックをオンにし、[Update]ボタンを」クリックします。
※他の項目が図の設定内容と異なる場合、図と同じになるように変更してください。
⑥「Deploy」をクリックし、フローをデプロイします。
○動作の確認
①ブラウザでデータ送信用HTMLを開きます。
②[ON]ボタンのクリックでLEDが点灯、[OFF]ボタンのクリックでLEDが消灯することを確認します。
0 件のコメント:
コメントを投稿