首页 > 解决方案 > 如何将 JSON 从本地网站发送到 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 !!");
            }
        });

标签: javascripthtmljsonaws-iot

解决方案


您可以利用 AWS lambda,您可以将该 json 存储在 S3 存储桶中,也可以在运行时从网站获取它。

从 S3 获取该 json/从站点获取之后,您可以向您的微控制器订阅的 IoT 核心主题发布一条消息,然后您的微控制器将接收它。


推荐阅读