通过WiFi进行环境监控的App(Android Studio)

需求分析

        本次需要做一个App,该App可以通过WiFi获取温湿度、烟雾浓度和一氧化碳的环境参数,并在界面上显示出来,能够评判空气质量,同时,在App内也可以实现这些环境参数的阈值更改功能,并向32单片机上的WiFi模块回传这些阈值参数,当参数超过阈值时手机通过震动发出报警。

        本App通过WiFi与32单片机进行通信,采用的是TCP协议。WiFi模块会不断地向App发送以空格为间隔的 7 个数据,一共21个字节,如“20 80 10 10 40 20 20 ”,把这串数据以空格拆分并依次赋值给温度、湿度、烟雾浓度、一氧化碳浓度、温度阈值、烟雾阈值和一氧化碳阈值。发送阈值时,也以空格为间隔发送,只不过空格是在前面,如“ 40 20 20”。

App界面效果

通过WiFi进行环境监控的App(Android Studio)_第1张图片

各种功能实现

        权限的申请

        android:usesCleartextTraffic="false"用于明文传输






        显示当前的参数

/******显示当前环境的参数******/     
textView_humidity.setText(Integer.toString(data_humidity) + "%RH");
textView_temp.setText(Integer.toString(data_temp) + " ℃");
textView_smog.setText(Integer.toString(data_smog));
textView_co.setText(Integer.toString(data_co));

/******显示当先阈值******/
editText_temp.setText(Integer.toString(temp_yu_setting));
editText_smog.setText(Integer.toString(smog_yu_setting));
editText_co.setText(Integer.toString(co_yu_setting));
textView_temp_yu.setText(Integer.toString(temp_yu));
textView_smog_yu.setText(Integer.toString(smog_yu));
textView_co_yu.setText(Integer.toString(co_yu));

        设置温度加减功能关键代码

 /******设置温度加减功能******/
        button_decline_temp = findViewById(R.id.decline_button_of_temp);
        button_decline_temp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isInt = true;
                try {
                    int tempInput = Integer.parseInt(editText_temp.getText().toString());
                    if (tempInput >= 1 && tempInput <= 99) {
                        temp_yu = tempInput;
                    } else {
                        Toast.makeText(MainActivity.this, "请输入 1~99 之间的整数", Toast.LENGTH_SHORT).show();
                        editText_temp.setText(String.valueOf(temp_yu));
                    }
                } catch (NumberFormatException e) {
                    isInt = false;//转化失败
                    Toast.makeText(MainActivity.this, "请输入一个整数", Toast.LENGTH_SHORT).show();
                }
                if (isInt) {//如果没有发生异常
                    if (temp_yu > 1) {
                        temp_yu--;
                        editText_temp.setText(String.valueOf(temp_yu));
                    } else {
                        editText_temp.setText(String.valueOf(temp_yu));
                    }
                } else {
                    editText_temp.setText(String.valueOf(temp_yu));
                }
            }
        });

        连接按钮底层代码

        // 连接按钮底层代码
        button_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = editText_ip.getText().toString();
                String c = editText_port.getText().toString();
                if ("".equals(a) || "".equals(c)) {
                    Toast.makeText(MainActivity.this, "请输入ip和端口号", Toast.LENGTH_SHORT).show();
                    receive.append("请输入ip和端口号" + "\r\n");
                } else {
                    b = Integer.parseInt(c);
                    lianjie = new connectthread();
                    lianjie.start();
                }
            }
        });

        发送数据按键底层代码

        // 发送数据 按键底层代码
        button_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //子线程中进行网络操作
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (socket != null) {
                            try {
                                String text_temp = " " + editText_temp.getText().toString();//温度栏的阈值
                                String text_smog = " " + editText_smog.getText().toString();//烟雾栏的阈值
                                String text_co = " " + editText_co.getText().toString();//CO栏的阈值
                                lianjie.outputStream.write((text_temp + text_smog + text_co + "\r\n").getBytes());
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MainActivity.this, "已发送", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else {
                            runOnUiThread(new Runnable()//不允许其他线程直接操作组件,用提供的此方法可以
                            {
                                public void run() {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(MainActivity.this, "请先建立连接", Toast.LENGTH_SHORT).show();
                                    receive.append("请先建立连接" + "\r\n");
                                }
                            });
                        }
                    }
                }).start();
            }
        });

        清除按钮功能

        //清除按钮功能
        button_clear = findViewById(R.id.clear_button);
        button_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                receive.setText("");
            }
        });

        子线程中进行网络相关操作

    //子线程中进行网络相关操作
    // 联网子线程
    class connectthread extends Thread {

        OutputStream outputStream = null;
        InputStream inputStream = null;

        @SuppressWarnings("InfiniteLoopStatement")
        @Override
        public void run() {

            //连接
            try {
                socket = new Socket(a, b);
                runOnUiThread(new Runnable()//不允许其他线程直接操作组件,用提供的此方法可以
                {
                    public void run() {
                        Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
                        receive.append("连接成功" + "\r\n");
                    }
                });
            } catch (UnknownHostException e) {
                runOnUiThread(new Runnable()//不允许其他线程直接操作组件,用提供的此方法可以
                {
                    public void run() {
                        Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                        receive.append("连接失败" + "\r\n");
                    }
                });
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable()//不允许其他线程直接操作组件,用提供的此方法可以
                {
                    public void run() {
                        Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                        receive.append("连接失败" + "\r\n");
                    }
                });
            }
            if (socket != null) {
                //获取输出流对象
                try {
                    outputStream = socket.getOutputStream();
                    outputStream.write(123);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    do {
                        final byte[] buffer = new byte[1024];//创建接收缓冲区
                        inputStream = socket.getInputStream();
                        final int len = inputStream.read(buffer);//数据读出来,并且返回数据的长度
                        runOnUiThread(new Runnable()//不允许其他线程直接操作组件,用提供的此方法可以
                        {
                            public void run() {
                                String receivedData = new String(buffer, 0, len);

                                // 拆分字符串为子字符串数组
                                String[] tokens = receivedData.split(" ");

                                //获取环境参数
                                data_humidity = Integer.parseInt(tokens[0]);
                                data_temp = Integer.parseInt(tokens[1]);
                                data_smog = Integer.parseInt(tokens[2]);
                                data_co = Integer.parseInt(tokens[3]);

                                //获取环境阈值
                                temp_yu = Integer.parseInt(tokens[4]);
                                smog_yu = Integer.parseInt(tokens[5]);
                                co_yu = Integer.parseInt(tokens[6]);

                                //显示环境参数
                                textView_humidity.setText(Integer.toString(data_humidity) + "%RH");
                                textView_temp.setText(Integer.toString(data_temp) + " ℃");
                                textView_smog.setText(Integer.toString(data_smog));
                                textView_co.setText(Integer.toString(data_co));

                                //显示环境阈值
                                textView_temp_yu.setText(Integer.toString(temp_yu));
                                textView_smog_yu.setText(Integer.toString(smog_yu));
                                textView_co_yu.setText(Integer.toString(co_yu));

                                receive.append(receivedData + "\r\n");

                                /*空气质量判断并显示*/
                                if (data_co < 25 && data_smog < 25 && data_temp <= 25 && data_temp >=15)
                                    quality = 1;
                                else if (data_co > 35 || data_smog > 35 || data_temp > 35)
                                    quality = 3;
                                else quality = 2;
                                switch (quality) {
                                    case 1:
                                        textView_quality.setText("优");
                                        textView_quality.setTextColor(Color.rgb(0, 205, 0));
                                        break;
                                    case 2:
                                        textView_quality.setText("良");
                                        textView_quality.setTextColor(Color.YELLOW);
                                        break;
                                    case 3:
                                        textView_quality.setText("差");
                                        textView_quality.setTextColor(Color.RED);
                                        break;
                                    default:
                                        break;
                                }

                                /*阈值判断*/
                                peizhi();

                            }
                        });
                    } while (true);
                } catch (IOException ignored) {
                }
            }
        }
    }

阈值判断并震动的函数

public void peizhi() {

        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);//定义震动

        if ( data_temp >= temp_yu ){
            textView_temp.setTextColor(Color.RED);
            vibrator.vibrate(1000);
        }
        else{
            textView_temp.setTextColor(Color.GRAY);
            vibrator.cancel();
        }

        if (data_smog >= smog_yu){
            textView_smog.setTextColor(Color.RED);
            vibrator.vibrate(1000);
        }
        else{
            textView_smog.setTextColor(Color.GRAY);
            vibrator.cancel();
        }

        if (data_co >= co_yu){
            textView_co.setTextColor(Color.RED);
            vibrator.vibrate(1000);
        }
        else{
            textView_co.setTextColor(Color.GRAY);
            vibrator.cancel();
        }
    }

XML文件




    

        

            

                

            

            

                

                    

                    
                

                

                    

                    

                    

                    

                

                

                    

                    
                

                

                    

                    
                

                

            

            

                

                    

                        

                        

工程文件

该工程的项目文件已上传,需要的话可以下载使用。

https://download.csdn.net/download/nazonomaster/87553478https://download.csdn.net/download/nazonomaster/87553478

你可能感兴趣的:(android,studio,android,物联网,stm32)