中国移动物联网开放平台是由中国移动打造的PaaS物联网开放平台。 平台能够帮助开发者轻松实现设备接入与设备连接,提供综合性的物联网解决方案,实现物联网设备的数据获取,数据存储,数据展现。
中移物联网官方网址
https://open.iot.10086.cn/
①注册一个中移物联网的账号
②接下来开始创建产品与产品下的设备
③点击右上角新建产品
记住设备id,之后程序里会用到
为设备添加APIKey,这也是之后程序里要用到的
保存设备APIKey
接下来为设备创建数据流,所有的数据都是被上传到数据流的 这里进行查看数据流
这里添加数据流
接下来就是程序方面的实现 使用http协议,具体的协议内容可以查看
https://open.iot.10086.cn/doc/art/id/190#43
具体二进制文件的传输看这个网址 https://open.iot.10086.cn/doc/art258.html#68
以下http协议代码的实现都是基于这个规定的,一定要打开对照来看
public class OneNETUrlConnectionPost extends Thread {
private int SHOW_REQUEST = 0;
private Handler handler;
// 首先声明设备ID及apikey
private static final String DeviceID = "25857699";
// 个人使用就建议填产品key,设备key在二进制获取那里会权限不足
private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM=";
public OneNETUrlConnectionPost(Handler handler) {
this.handler = handler;
}
@Override
public void run() {
URL url;
HttpURLConnection connection;
try {
//data1代表云端的数据流是data1
String s1 = new String(",;" + "data1" + "," + "哈哈哈,终于成功了");
byte[] data = s1.getBytes();
// 先new出一个URL对象,传入网络地址
// 调用openConnection()方法获取到HttpURLConnection对象
// 自己创建的中移物联网的地址http://api.heclouds.com/devices/25857699/datapoints?type=5
url = new URL("http://api.heclouds.com/devices/" + DeviceID
+ "/datapoints?type=5");
connection = (HttpURLConnection) url.openConnection();
// 下面使一些自由的定制,比如设置连接超时,读取超时的毫秒数,以及服务器希望得到的一些消息头
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("api-key", ApiKey);
connection.setRequestProperty("Content-Length",
String.valueOf(data.length));
connection.setChunkedStreamingMode(5);
// 设置打开输出流
connection.setDoOutput(true);
// 拿到输出流
OutputStream os = connection.getOutputStream();
// 使用输出流往服务器提交数据
os.write(data);
os.flush();
os.close();
// //如果请求发送成功
if (connection.getResponseCode() == 200) {
// 接下来利用输入流对数据进行读取
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}// 正常则返回{"errno":0,"error":"succ"},此函数为void,用不上这个
// 发送数据完毕,接下来用Handler进行提交成功显示
Message message = new Message();
message.what = SHOW_REQUEST;
message.obj = response.toString();
handler.sendMessage(message);
}
// 最后关闭HTTP连接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class OneNETUrlConnectionPostByte extends Thread {
private int SHOW_REQUEST = 0;
private Handler handler;
// 首先声明设备ID及apikey
private static final String DeviceID = "25857699";
// 个人使用就建议填产品key,设备key在二进制获取那里会权限不足
private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM=";
public OneNETUrlConnectionPostByte(Handler handler) {
this.handler = handler;
}
@Override
public void run() {
URL url;
// 自定义的字符数组将它上传到云端
byte[] my_data = { '8', '8', '6' };
HttpURLConnection connection;
try {
// 先new出一个URL对象,传入网络地址
// 调用openConnection()方法获取到HttpURLConnection对象
// 自己创建的中移物联网的地址http://api.heclouds.com/devices/25857699/datapoints?type=5
url = new URL("http://api.heclouds.com/bindata?device_id="
+ DeviceID + "&datastream_id=" + "data123");
connection = (HttpURLConnection) url.openConnection();
// 下面使一些自由的定制,比如设置连接超时,读取超时的毫秒数,以及服务器希望得到的一些消息头
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("api-key", ApiKey);
connection.setRequestProperty("Content-Length",
String.valueOf(my_data.length));
connection.setChunkedStreamingMode(5);
// 设置打开输出流
connection.setDoOutput(true);
// 拿到输出流
OutputStream os = connection.getOutputStream();
// 使用输出流往服务器提交数据
os.write(my_data);
os.flush();
os.close();
// //如果请求发送成功
if (connection.getResponseCode() == 200) {
// 接下来利用输入流对数据进行读取
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}// 正常则返回{"errno":0,"error":"succ"},此函数为void,用不上这个
// 发送数据完毕,接下来用Handler进行提交成功显示
Message message = new Message();
message.what = SHOW_REQUEST;
message.obj = response.toString();
handler.sendMessage(message);
}
// 最后关闭HTTP连接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class OneNETHttpRequestGET extends Thread {
private int SHOW_REQUEST = 0;
private Handler handler;
// 首先声明设备ID及apikey
private static final String DeviceID = "25857699";
// 个人使用就建议填产品key,设备key在二进制获取那里会权限不足
private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM=";
public OneNETHttpRequestGET(Handler handler) {
this.handler = handler;
}
@Override
public void run() {
URL url;
HttpURLConnection connection;
try {
// 先new出一个URL对象,传入网络地址
// 调用openConnection()方法获取到HttpURLConnection对象
url = new URL("http://api.heclouds.com/devices/" + DeviceID
+ "/datastreams/" + "data1");
connection = (HttpURLConnection) url.openConnection();
// 下面使一些自由的定制,比如设置连接超时,读取超时的毫秒数,以及服务器希望得到的一些消息头
connection.setConnectTimeout(15000);
connection.setReadTimeout(15000);
connection.setRequestMethod("GET");
connection.setRequestProperty("api-key", ApiKey);
// 如果网页正确响应
if (connection.getResponseCode() == 200) {
// 接下来利用输入流对数据进行读取
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
// 读取数据完毕,接下来将数据传送到Handler进行显示
Message message = new Message();
message.what = SHOW_REQUEST;
message.obj = response.toString();
handler.sendMessage(message);
// 最后关闭HTTP连接
connection.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class OneNETHttpRequestGETByte extends Thread {
private int SHOW_REQUEST = 0;
private Handler handler;
// 首先声明设备ID及apikey
private static final String DeviceID = "25857699";
// 个人使用就建议填产品key,设备key在二进制获取那里会权限不足
private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM=";
public OneNETHttpRequestGETByte(Handler handler) {
this.handler = handler;
}
@Override
public void run() {
URL url;
HttpURLConnection connection;
try {
// 先new出一个URL对象,传入网络地址
// 调用openConnection()方法获取到HttpURLConnection对象
url = new URL("http://api.heclouds.com/bindata/"
+ "25857699_15201013688560_data123");
connection = (HttpURLConnection) url.openConnection();
// 下面使一些自由的定制,比如设置连接超时,读取超时的毫秒数,以及服务器希望得到的一些消息头
connection.setConnectTimeout(15000);
connection.setReadTimeout(15000);
connection.setRequestMethod("GET");
connection.setRequestProperty("api-key", ApiKey);
// 如果网页正确响应
if (connection.getResponseCode() == 200) {
// 接下来利用输入流对数据进行读取
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
// 读取数据完毕,接下来将数据传送到Handler进行显示
Message message = new Message();
message.what = SHOW_REQUEST;
message.obj = response.toString();
handler.sendMessage(message);
// 最后关闭HTTP连接
connection.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}