ESP8266浏览器发送数据到远程设备测试OK

来源:互联网 发布:体育场馆播放软件 编辑:程序博客网 时间:2024/05/17 01:45

数据模型:

手机或电脑浏览器<======>厂家乐鑫云端<======>用户路由器<======>用户esp8266模块<======>串口

用到的硬件:https://detail.tmall.com/item.htm?id=520327020054&spm=a1z09.2.0.0.0PMKGa&_u=b3jva391c93

开发工具:phpstudy,esp8266编程工具v4.5(需注册,直接使用固件就不用了)

1.自己写固件程序 烧录进去

esp8266烧录插上就行
先用一键烧录工具试


2.在云端https://iot.espressif.cn配置

参考:https://iot.espressif.cn/#/help/ioc-switch-demo-zh-cn/
公开
一维数据

数据模型名称为plug-status  ,测试数据对应如下 
查询状态
curl -X GET -H "Content-Type:application/json" -H "Authorization: token d426e199d39ea63dc713241488d3d0b579ca017f" https://iot.espressif.cn/v1/datastreams/plug-status/datapoint/
切换开关为1
curl  -H "Authorization: token 6bdc9da4134c862199005c672f32817d5173a68a" -d "{\"datapoint\":{\"x\":1}}" http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/?deliver_to_device=true


3.http pos监听
发送
POST https://iot.espressif.cn/v1/datastreams/plug-status/datapoint/?deliver_to_device=true HTTP/1.1
Host: iot.espressif.cn
Connection: keep-alive
Content-Length: 21
Time-Zone: Asia/Shanghai
Origin: https://iot.espressif.cn
Language: zh-cn
Authorization: token 4faa67aa888e01f065d95f791a2b12ca58b6c4af
Content-Type: application/json;charset=UTF-8
Accept: application/json, text/plain, */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Referer: https://iot.espressif.cn/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8
Cookie: Authorization=token-7cb04556dda5ddc3bf0ebc01d716bc869727930f


{"datapoint":{"x":0}}


返回
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Sun, 11 Jun 2017 11:12:11 GMT
Content-Type: application/json
Content-Length: 229
Connection: keep-alive


{"datapoint": {"id": 5295096, "created": "2017-06-11 19:12:11", "updated": "2017-06-11 19:12:11", "visibly": 1, "datastream_id": 12318, "datatype": 0, "at": "2017-06-11 19:12:11", "x": 0.00000}, "nonce": 28772519, "status": 200}




4.浏览器发送数据到远程设备测试
发送
GET https://iot.espressif.cn/v1/device/rpc/?deliver_to_device=true&action=1122 HTTP/1.1
Host: iot.espressif.cn
Connection: keep-alive
Accept: application/json, text/plain, */*
Language: zh-cn
Authorization: token 4faa67aa888e01f065d95f791a2b12ca58b6c4af
Time-Zone: Asia/Shanghai
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Referer: https://iot.espressif.cn/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: Authorization=token-7cb04556dda5ddc3bf0ebc01d716bc869727930f


返回
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Sun, 11 Jun 2017 11:22:56 GMT
Content-Type: application/json
Content-Length: 86
Connection: keep-alive


{"status": 200, "datapoint": {"x": 1}, "nonce": 250233347, "deliver_to_device": true}


5.ERROR:使用curl命令出错,说工具不支持https
curl -H "Authorization: token HERE_IS_THE_DEVICE_KEY" 'https://iot.espressif.cn/v1/device/rpc/?deliver_to_device=true&action=your_action'






6.php发送数据到设备测试成功
<?php
$header= array(                                                                                                                                             
    "Authorization: token 4faa67aa888e01f065d95f791a2b12ca58b6c4af",


    );
    //初始化
    $curl = curl_init();
 //  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");                                         
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, 'http://iot.espressif.cn/v1/device/rpc/?deliver_to_device=true&action=1122');
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //显示获得的数据
  ///  print_r($data);
  
  $obj = json_decode($data);
//强制转化为数组格式
$arr = (array) $obj;
//按数组方式调用里面的数据
print_r($arr['status']);
echo '</br>';
print_r($arr['deliver_to_device']);
echo '</br>';
$arrin=(array)$arr['datapoint'];
print_r($arrin['x']);
echo '</br>';
//输出数组结构
print_r($arr);
?>

结果:成功




















阅读全文
0 0