新大陆获取传感器数据的两种方式

新大陆获取传感器数据的两种方式

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));
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值