|
本帖最后由 九天 于 2018-9-24 14:00 编辑
使用了 STM32+GSM+GPS 开发板,向移动IOT平台发送GPS定位数据,然后读取这个数据,向HA提交
流程如下:
IOT.PHP 上传到自己的服务器
<?php
$header[]="api-key:XXXXXXXXX";//此处写成自己的API-KEY值
$url="http://api.heclouds.com/devices/XXXXXXXXX/datastreams/location";/*获取数据流为**的数据值,注意此处设备ID号以及申请获取的数据流ID号都应根据自己的OneNet平台设备ID号,希望获取的数据流ID号进行更改*/
//用于获取从OneNet平台返还的数据
function get($url, $header)
{
//1.初始化,创建一个新cURL资源
$ch=curl_init();
//2.设置URL和相应的选项
curl_setopt($ch,CURLOPT_URL,$url);//需要获取的URL地址,也可以在curl_init()函数中设置
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);//启用时会将头文件的信息作为数据流输出
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//将curl_exec()获取的信息以字符串返回,而不是直接输出。
curl_setopt($ch,CURLOPT_HEADER,0);//启用时会将头文件的信息作为数据流输出。
if(curl_exec($ch)=== false) //curl_error()返回当前会话最后一次错误的字符串
{
die("Curlerror: ".curl_error($ch));
}
$response =curl_exec($ch);//获取返回的文件流
curl_close($ch);
return $response;
}
$output=get($url,$header);
$output_array=json_decode($output,true);
print_r($output_array["data"]["current_value"]);
?>
在NODE-RED里添加
[{"id":"c0524b72.d57be8","type":"tab","label":"流程3","disabled":false,"info":""},{"id":"f0a81a3f.e51e78","type":"http request","z":"c0524b72.d57be8","name":"","method":"GET","ret":"txt","url":"http://www.xxx.com/iot.php","tls":"","x":240,"y":200,"wires":[["697c5394.614c9c"]]},{"id":"a25b6c09.876dc","type":"inject","z":"c0524b72.d57be8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":200,"wires":[["f0a81a3f.e51e78"]]},{"id":"5f41bf45.6eead","type":"http request","z":"c0524b72.d57be8","name":"","method":"GET","ret":"txt","url":"","tls":"","x":800,"y":200,"wires":[["2daf12e.33f24ee"]]},{"id":"2daf12e.33f24ee","type":"debug","z":"c0524b72.d57be8","name":"GPS请求输出","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":980,"y":200,"wires":[]},{"id":"b965d3b4.0cd37","type":"function","z":"c0524b72.d57be8","name":"构造GPS请求","func":"const device = 'tuguan'\nconst lat = msg.lat\nconst lon = msg.lon\nreturn {\n url:`[url=https://www.xx.com:8123/api/gpslogger?device=]https://www.xx.com:8123/api/gpslogger?device=[/url]${device}&latitude=${lat}&longitude=${lon}&accuracy=0&battery=100`\n}","outputs":1,"noerr":0,"x":620,"y":200,"wires":[["5f41bf45.6eead"]]},{"id":"697c5394.614c9c","type":"function","z":"c0524b72.d57be8","name":"解析GPS数据","func":"const gps = msg.payload.substring(21,58)\nconst lon = gps.substring(0,12)\nconst lat = gps.substring(26,41)\nreturn {lon,lat}","outputs":1,"noerr":0,"x":420,"y":200,"wires":[["b965d3b4.0cd37"]]}]
感谢所有代码提供者。
NODE-RED解析GPS数据 这个里面我用了个笨办法,希望有人改进下
const gps = msg.payload.substring(21,58)
const lon = gps.substring(0,12)
const lat = gps.substring(26,41)
return {lon,lat}
iot.rar
(1.6 KB, 下载次数: 11)
|
评分
-
查看全部评分
|