首先搭建一个web测试环境, 目的是以什么方式POST/GET提交,以及提交间隔,
<?php
header('content-type:application/json;charset=utf8');
$method = $_SERVER['REQUEST_METHOD'];
$requestData = $_REQUEST;
$data_time = date("h:i:sa");
$dataArray = array(
"method" => $method,
"data" =>array(
"requestData" => $requestData,
"returnData"=> $data_time
)
);
$dataJson = json_encode($dataArray);
$fileJson = 'hotel.json';
$fp_license = fopen($fileJson,'a');
$flag=fwrite($fp_license,$dataJson.'\r\n');
fclose($fp_license);
echo $dataJson;
?>
configuration.yaml 建立
sensor:
- platform: rest
name: a1
unique_id: "sensor_a1_1"
resource: http://192.168.18.83/a/?POST=test
method: POST
scan_interval: 1860
payload: 'a=1&b=2&c=3'
headers:
Content-Type: 'application/x-www-form-urlencoded;charset=utf-8'
value_template: >
{{ value_json["data"] }}
json_attributes:
- data
- platform: template
sensors:
b1:
value_template: '{{states.sensor.a1.attributes.data}}'
friendly_name: 'c1'
unique_id: "sensor_c1_1"
结论:
服务器保存
{"method":"POST","data":{"requestData":{"POST":"test","a":"1","b":"2","c":"3"},"returnData":"03:25:57am"}}
是我想要的以POST方式发送数据, 也返回了服务器响应的JSON
服务器收到请求时间为 scan_interval 参数决定频率.
问题: 怎么做一个开关或按钮, 人工主动点击再次刷新, 场景如: 向web 发送JSON, 响应到解析的JSON
|