1. Introduction to OneNET
China Mobile Internet of Things Open Platform is a PaaS Internet of Things Open Platform created by China Mobile.
The platform can help developers easily access and connect devices, provide comprehensive Internet of Things solutions, and realize data acquisition, data storage and data display of Internet of Things devices.
Official website of China Mobile Internet of Things
https://open.iot.10086.cn/
Android Platform Access to OneNET Method:
1. Register an account for the IOT
(2) Next start creating products and equipment under products
(3) Click on the top right corner to create a new product
(4) Start to create the device next, after clicking Submit
Remember the device id, which will be used later in the program
Add APIKey to the device, which will be used later in the program
Save device APIKey
Next, create a data stream for the device, all data is uploaded to the data stream
View data streams here
Add data stream here
Next comes the implementation of the program
Using the http protocol, specific protocol content can be viewed
https://open.iot.10086.cn/doc/art/id/190#43
See this web address for specific binary file transfers
https://open.iot.10086.cn/doc/art258.html#68
The implementation of the following http protocol codes is based on this requirement and it is important to turn on the comparison
(1) Uploading text data with POST
public class OneNETUrlConnectionPost extends Thread { private int SHOW_REQUEST = 0; private Handler handler; // First declare the device ID and apikey private static final String DeviceID = "25857699"; // Product key is recommended for personal use, device key will not have sufficient permissions in binary access private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM="; public OneNETUrlConnectionPost(Handler handler) { this.handler = handler; } @Override public void run() { URL url; HttpURLConnection connection; try { //Data1 represents the data flow in the cloud as data1 String s1 = new String(",;" + "data1" + "," + "Hahaha, finally succeeded"); byte[] data = s1.getBytes(); // First new out a URL object, pass in the network address // Call the openConnection() method to get the HttpURLConnection object // I created my own address for MIT 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(); // Here are some free customizations, such as setting connection timeouts, milliseconds of read timeouts, and some headers the server wants 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); // Set Open Output Stream connection.setDoOutput(true); // Get the output stream OutputStream os = connection.getOutputStream(); // Submit data to the server using an output stream os.write(data); os.flush(); os.close(); // //If the request is sent successfully if (connection.getResponseCode() == 200) { // Next, read the data using the input stream 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); }// Normally return {"errno":0,"error":"succ"}, this function is void, do not use this // Send data, and then submit with Handler to display successfully Message message = new Message(); message.what = SHOW_REQUEST; message.obj = response.toString(); handler.sendMessage(message); } // Finally close the HTTP connection connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }
(2) Uploading byte data with POST (including binary files such as pictures and videos)
public class OneNETUrlConnectionPostByte extends Thread { private int SHOW_REQUEST = 0; private Handler handler; // First declare the device ID and apikey private static final String DeviceID = "25857699"; // Product key is recommended for personal use, device key will not have sufficient permissions in binary access private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM="; public OneNETUrlConnectionPostByte(Handler handler) { this.handler = handler; } @Override public void run() { URL url; // Custom character array to upload it to the cloud byte[] my_data = { '8', '8', '6' }; HttpURLConnection connection; try { // First new out a URL object, pass in the network address // Call the openConnection() method to get the HttpURLConnection object // I created my own address for MIT 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(); // Here are some free customizations, such as setting connection timeouts, milliseconds of read timeouts, and some headers the server wants 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); // Set Open Output Stream connection.setDoOutput(true); // Get the output stream OutputStream os = connection.getOutputStream(); // Submit data to the server using an output stream os.write(my_data); os.flush(); os.close(); // //If the request is sent successfully if (connection.getResponseCode() == 200) { // Next, read the data using the input stream 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); }// Normally return {"errno":0,"error":"succ"}, this function is void, do not use this // Send data, and then submit with Handler to display successfully Message message = new Message(); message.what = SHOW_REQUEST; message.obj = response.toString(); handler.sendMessage(message); } // Finally close the HTTP connection connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }
(3) Acquiring text data by GET
public class OneNETHttpRequestGET extends Thread { private int SHOW_REQUEST = 0; private Handler handler; // First declare the device ID and apikey private static final String DeviceID = "25857699"; // Product key is recommended for personal use, device key will not have sufficient permissions in binary access private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM="; public OneNETHttpRequestGET(Handler handler) { this.handler = handler; } @Override public void run() { URL url; HttpURLConnection connection; try { // First new out a URL object, pass in the network address // Call the openConnection() method to get the HttpURLConnection object url = new URL("http://api.heclouds.com/devices/" + DeviceID + "/datastreams/" + "data1"); connection = (HttpURLConnection) url.openConnection(); // Here are some free customizations, such as setting connection timeouts, milliseconds of read timeouts, and some headers the server wants connection.setConnectTimeout(15000); connection.setReadTimeout(15000); connection.setRequestMethod("GET"); connection.setRequestProperty("api-key", ApiKey); // If the page responds correctly if (connection.getResponseCode() == 200) { // Next, read the data using the input stream 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); } // Read the data, then transfer it to Handler for display Message message = new Message(); message.what = SHOW_REQUEST; message.obj = response.toString(); handler.sendMessage(message); // Finally close the HTTP connection connection.disconnect(); } } catch (Exception e) { e.printStackTrace(); } } }
(4) Byte data acquisition using GET, but there are minor bug s that have not been successful
public class OneNETHttpRequestGETByte extends Thread { private int SHOW_REQUEST = 0; private Handler handler; // First declare the device ID and apikey private static final String DeviceID = "25857699"; // Product key is recommended for personal use, device key will not have sufficient permissions in binary access private static final String ApiKey = "QMrXbErb2t8h=E1mHORgxX20QVM="; public OneNETHttpRequestGETByte(Handler handler) { this.handler = handler; } @Override public void run() { URL url; HttpURLConnection connection; try { // First new out a URL object, pass in the network address // Call the openConnection() method to get the HttpURLConnection object url = new URL("http://api.heclouds.com/bindata/" + "25857699_15201013688560_data123"); connection = (HttpURLConnection) url.openConnection(); // Here are some free customizations, such as setting connection timeouts, milliseconds of read timeouts, and some headers the server wants connection.setConnectTimeout(15000); connection.setReadTimeout(15000); connection.setRequestMethod("GET"); connection.setRequestProperty("api-key", ApiKey); // If the page responds correctly if (connection.getResponseCode() == 200) { // Next, read the data using the input stream 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); } // Read the data, then transfer it to Handler for display Message message = new Message(); message.what = SHOW_REQUEST; message.obj = response.toString(); handler.sendMessage(message); // Finally close the HTTP connection connection.disconnect(); } } catch (Exception e) { e.printStackTrace(); } } }
For more technical articles, please follow my Personal Public Number