1.使用串口服务器
private MD4017 md4017;
md4017=new MD4017(DataBusFactory.newSocketDataBus("192.168.1.85", 6001), new ConnectResultListener() {
@Override
public void onConnectResult(boolean b) {
System.out.println("连接情况:"+b);
}
});
class Myas extends AsyncTask<Void, ArrayList<String>,Void>{
@Override
protected Void doInBackground(Void... voids) {
while(true){
try {
ArrayList<String> arrayList = new ArrayList<>();
md4017.getVin((byte) 5, new MD4017ValListener() {
@Override
public void onVal(int[] ints) {
arrayList.add(String.format("%.2f", MD4017ValConvert.getRealValByType(Md4017VIN.TEM,ints[5])));
arrayList.add(String.format("%.2f", MD4017ValConvert.getRealValByType(Md4017VIN.HUM,ints[6])));
arrayList.add(String.format("%.2f", MD4017ValConvert.getRealValByType(Md4017VIN.LIGHT,ints[4])));
publishProgress(arrayList);
}
@Override
public void onFail(Exception e) {
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
protected void onProgressUpdate(ArrayList<String>... values) {
temp_tv.setText(values[0].get(0));
hum_tv.setText(values[0].get(1));
light_tv.setText(values[0].get(2));
}
}
2.使用新大陆云平台
public static final String TOKEN= "C8C13650535B55E3AA09AF027D8EA368646157D7BDCD7D9FE7B48ABE232951BA1925CDE17D7C41F59795C10BEEAA8461D4690CBDAD25E4D532A747375C456FF37B829DE1D8EC77061245940DDFBB0958C0DA1635482953C632C6E086615EDA1D00F5810794CDDC3DE94C1BD1858A05EA8289A977D8D2D4BF6911D556799E6EFBAEFB2280DACF0AEB074CBCA2831D247AD2D41404F8B4E877B225A9D67142EBEE7589CB540A03D19F8C68F0E82DB0FD3576C61A00D054ABEFE7878CDAC0A73A922A09000E4FE9AE166AA383810778EC93B070D23B0F0B19965601C1A5DCE55869";
class Myas2 extends AsyncTask<Void,ArrayList<String>,Void>{
@Override
protected Void doInBackground(Void... voids) {
String[] strings = new String[]{"m_temp","m_hum","m_light"};
while(true){
ArrayList<String> arrayList = new ArrayList<>();
for (String string : strings) {
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://api.nlecloud.com/devices/489733/Sensors?apiTag="+string).openConnection();
httpURLConnection.setRequestProperty("AccessToken",TOKEN);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
JSONObject jsonObject = new JSONObject(bufferedReader.readLine());
JSONObject jsonObject1 = jsonObject.getJSONObject("ResultObj");
arrayList.add(jsonObject1.getString("Value"));
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
System.out.println(arrayList.toArray());
publishProgress(arrayList);
}
}
@Override
protected void onProgressUpdate(ArrayList<String>... values) {
temp_tv.setText(values[0].get(0));
hum_tv.setText(values[0].get(1));
light_tv.setText(values[0].get(2));
}
}