[英]How to send JSON from a local website to AWS IoT?
我正在修改一個網站,當用戶單擊按鈕時,它會下載一個 json 文件。 我想知道是否有一種方法可以自動獲取它,然后我可以在 AWS IoT Core 中訪問它。
基本上,微控制器從機器讀取信息,然后在自己的 IP 上托管網站。 這樣做的缺點是,它僅在用戶與控制器位於同一網絡時才有效。
我已經在 AWS IoT Core 中創建了接收 json 所需的一切(來自之前的項目,其中 json 已經發送到 AWS),我只需要一種方法來獲取那里的信息。 任何幫助將不勝感激。 謝謝!
編輯:我認為這應該是必要的代碼,但是,我不是 100% 確定。
//Save the selected parameter values into the file
$(".SaveImg").click(function () {
//Step 1 : create the JSON for selected parameter values to be saved in file
//var SelectedSubParam = sel;
var str = "{\"ID\":" + sel + ",";
str += "\"param\": [";
//To define whether the selected parameters have value or not
var hasValue = false;
//To check whether parameters has selected or not
var isSelected = false;
$.fn.getSelectedParameterRows().each(function (idx, item) {
if ($(item).css("display") != "none") {
isSelected = true;
var paramValue = $(this).children().eq(1).children("input[type='text']").val().trim();
//Save the parameters which has value otherwise don't save it
if (paramValue != "" && ($(this).children().eq(1).children("input[type='text']").css("visibility") != 'hidden')) {
hasValue = true;
str += "{\"Name\":\"" + $(this).children().eq(0).text().trim() + "\",";
str += "\"Address\":\"" + $(this).children().eq(1).children("input[type='text']").attr('Id') + "\",";
str += "\"Value\":" + paramValue;
str += "},";
}
}
});
//Remove ',' from last param set to make a valid JSON
str = str.slice(0, -1);
str += "]}";
//Step 2: Create and save the Text file
if (hasValue) {
// To save text file in windows default location
// Commented since this code is not working for Mobiles
/*var txt = makeTextFile(str);
console.log($(".SaveImg").attr("href",txt));
$(".SaveImg").attr("download", "ParameterExport.txt");*/
// Create and save the file in downloads folder in client place using File saver API
var blob = new Blob([str], { type: "text/plain;base64" });
saveAs(blob, "ParameterExport.txt");
}
else if (!isSelected) {
if ($.fn.getParameterCount() > 0) {
alert("Please select Parameters !!");
}
else {
alert("No parameters available!!");
}
}
else {
//Show alert if all the selected parameters don't have the value
alert("File cannot be saved since selected variables don't have values !!");
}
});
您可以利用 AWS lambda 的好處,您可以將 json 存儲在 S3 存儲桶中,也可以在運行時從網站獲取它。
從 S3/從站點獲取該 json 后,您可以將消息發布到您的微控制器訂閱的 IoT 核心主題,然后您的微控制器將接收它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.