Fibaro网关控制若琪播报天气

日期:2018-05-21    阅读:600

1.在若琪APP的家庭页面,依次点击设置—Webhook—创建一条Webhook(名称随意)--得到一段URL



2.在若琪设置页面,依次点击若琪的更多设置—设备信息—长按序列号复制

 

3.在心知天气(https://www.seniverse.com/account)注册一个账号,并依次点击产品—上方的天气数据API—中间的查看API文档—左侧的天气实况

复制 蓝色的请求地址示例

 

 

拿到了这3个参数,就可以创建Fibaro的Lua场景来控制了。

把以下代码复制进去,并把对应颜色的参数替换一下。

 

 

--[[

%% properties

%% globals

--]]

 

local command ='https://api.seniverse.com/v3/weather/now.json?key=cwe2cszx82jkqxmv&location=shenzhen';

 

sn = '0201021739002069';

webHookURL = 'https://homebase.rokid.com/trigger/with/H1fh81OTnf';

 

texts = '';

local current_day =os.date("%d");

local current_month =os.date("%m");

 

local HC2 = net.HTTPClient()

HC2:request(command,

    {

       success =

         function(response)

           local result = response.data;

                     localluaTable = json.decode(result);

 

                area =luaTable["results"][1]["location"]["name"];

                text =luaTable["results"][1]["now"]["text"];

                 temp =luaTable["results"][1]["now"]["temperature"];

               

         

                texts = '今天' .. current_month ..'月'.. current_day ..'日,' ;

                    texts = texts .. '天气:'..area..text..temp..'摄氏度';

     

           local data = "{\"type\":\"tts\",\"devices\": {\"sn\": \"";

                data = data ..sn;

                data = data .."\"},\"data\": {\"text\": \"" ; 

                data = data .. texts;

                data = data .."\"}}";

     

                fibaro:debug(data);

     

                putMethod(webHookURL,data,function(resp)

           print('Status: ' .. resp.status)

           end,

           function(err)

           print('error' .. err)

           end

           )

           end,

         error = function(err)

          -- fibaro:debug("HTTP call error: " .. err)

         end,

         options = {method = "GET" }

   })

 

     

 

   -------------------------------------------

 

 

function putMethod(requestUrl, data,successCallback, errorCallback)

local http = net.HTTPClient()

 

http:request(requestUrl, {

options = {

method = 'POST',

headers = {

['content-type'] = 'application/json;charset="utf-8"'

},

data = data

},

success = successCallback,

error = errorCallback

})

End

 


 

 

再创建一个Fibaro图形场景定时启用这个Lua场景即可。


 




本文分享来自公众号“  Oliver IOT极智客“

Copyright © 2019 麦蕊科技 保留公司所有权利

关于我们  |  联系我们  |  网站地图  |  

麦蕊科技