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

数据模型:

手机或电脑浏览器<======>厂家乐鑫云端<======>用户路由器<======>用户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);
?>

结果:成功


 


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值