编写一个简单的智能家居APP

简介

  • 这个APP是为了通过socket传递指令给树莓派,并从树莓派获取温湿度等信息而设计的,包含两个页面。

开头页面

  • hello页面。右上角放一个倒计时,倒计时结束后跳转到主页面。此页面类似一些APP的开头广告。
    在这里插入图片描述

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".helloActivity"
    android:background="@mipmap/hello">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="30dp"
        android:layout_marginRight="20dp"
        android:textSize="23sp"
        android:textColor="#E91E63"/>

</RelativeLayout>

代码

public class helloActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);

        final TextView text = (TextView)findViewById(R.id.text);
        final Handler handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                text.setText(msg.what + "s");
            }
        };

        final Intent i = new Intent(this,MainActivity.class);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    int i;
                    for (i=5;i>0;i--){
                        Message mag = new Message();
                        mag.what = i;
                        handler.sendMessage(mag);
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                startActivity(i);
                finish();
            }
        });
        t.start();
    }
}

主页面

  • 该页面包含两部分,上面的部分通过WebView访问树莓派摄像头的网页,显示摄像头画面。
  • 下面的部分显示从树莓派获取的温湿度和火焰信息和发送一些命令
    在这里插入图片描述

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <RelativeLayout
        android:id="@+id/RL1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@mipmap/mr">

        <WebView
            android:id="@+id/web"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="30dp"/>

        <Switch
            android:id="@+id/switch1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="摄像头"
            android:textColor="#001B96"
            android:textSize="18sp" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignRight="@id/RL1"
        android:layout_below="@id/RL1"
        android:background="@mipmap/home">

        <LinearLayout
            android:id="@+id/LL1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal"
            android:background="#739C27B0">

            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:padding="8dp"
                android:background="#E59C27B0"
                android:text="温度"
                android:textSize="27sp"
                android:textColor="#000"/>
            <TextView
                android:id="@+id/TMP"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="3"
                android:text="0℃"
                android:textSize="27sp"
                android:textColor="#000"/>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/LL2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@id/LL1"
            android:orientation="horizontal"
            android:background="#7C03A9F4">

            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:padding="8dp"
                android:background="#E803A9F4"
                android:text="湿度"
                android:textSize="27sp"
                android:textColor="#000"/>
            <TextView
                android:id="@+id/RH"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="3"
                android:text="0%"
                android:textSize="27sp"
                android:textColor="#000"/>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/LL3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@id/LL2"
            android:orientation="horizontal"
            android:background="#80FF5722">

            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:padding="8dp"
                android:background="#E7FF5722"
                android:text="火警"
                android:textSize="27sp"
                android:textColor="#000"/>
            <TextView
                android:id="@+id/fire"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="3"
                android:text="未检测到火焰"
                android:textSize="27sp"
                android:textColor="#000"/>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/LL4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="60dp"
            android:layout_below="@id/LL3"
            android:orientation="horizontal"
            >

            <Switch
                android:id="@+id/S1"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:padding="3dp"
                android:text="客厅灯"
                android:textSize="9sp"
                android:background="#BDFFC107"/>

            <Switch
                android:id="@+id/S2"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:padding="3dp"
                android:text="卧室灯"
                android:textSize="9sp"
                android:background="#BDFFC107"/>
            <Switch
                android:id="@+id/S3"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:padding="3dp"
                android:text="浴室灯"
                android:textSize="9sp"
                android:background="#BDFFC107"/>

            <Switch
                android:id="@+id/S4"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:padding="3dp"
                android:text="厨房灯"
                android:textSize="9sp"
                android:background="#BDFFC107"/>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/LL5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_below="@id/LL4"
            android:orientation="horizontal"
            >

            <Switch
                android:id="@+id/S5"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:padding="3dp"
                android:text="全部灯"
                android:textSize="9sp"
                android:background="#BDFFC107"/>

            <Switch
                android:id="@+id/S6"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:padding="3dp"
                android:text="报警"
                android:textSize="9sp"
                android:background="#BDFFC107"/>
            <Switch
                android:id="@+id/S7"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:padding="3dp"
                android:text="风扇"
                android:textSize="9sp"
                android:background="#BDFFC107"/>

            <Switch
                android:id="@+id/S8"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:padding="3dp"
                android:text="电磁锁"
                android:textSize="9sp"
                android:background="#BDFFC107"/>

        </LinearLayout>

    </RelativeLayout>



</RelativeLayout>


代码

class switchChecked implements CompoundButton.OnCheckedChangeListener{
    String data;
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
        if (isChecked){
            switch (buttonView.getId()){
                case R.id.S1:data="LA1";break;
                case R.id.S2:data="LB1";break;
                case R.id.S3:data="LC1";break;
                case R.id.S4:data="LD1";break;
                case R.id.S5:data="L1";break;
                case R.id.S6:data="BEEP1";break;
            }
        }else {
            switch (buttonView.getId()){
                case R.id.S1:data="LA0";break;
                case R.id.S2:data="LB0";break;
                case R.id.S3:data="LC0";break;
                case R.id.S4:data="LD0";break;
                case R.id.S5:data="L0";break;
                case R.id.S6:data="BEEP0";break;
            }
        }

        new Thread(new Runnable(){
            public void run(){
                try {
                    Socket client = new Socket("192.168.8.110",6666);
                    OutputStream out = client.getOutputStream();
                    out.write(data.getBytes());
                    client.close();
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}

public class MainActivity extends AppCompatActivity {

    WebView web;
    Switch s;
    Switch s1;
    Switch s2;
    Switch s3;
    Switch s4;
    Switch s5;
    Switch s6;
    Switch s7;
    Switch s8;
    TextView TMP;
    TextView RH;
    TextView fire;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        s = (Switch)findViewById(R.id.switch1);
        s1 = (Switch)findViewById(R.id.S1);
        s2 = (Switch)findViewById(R.id.S2);
        s3 = (Switch)findViewById(R.id.S3);
        s4 = (Switch)findViewById(R.id.S4);
        s5 = (Switch)findViewById(R.id.S5);
        s6 = (Switch)findViewById(R.id.S6);
        s7 = (Switch)findViewById(R.id.S7);
        s8 = (Switch)findViewById(R.id.S8);
        TMP = (TextView)findViewById(R.id.TMP);
        RH = (TextView)findViewById(R.id.RH);
        fire = (TextView)findViewById(R.id.fire);
        web = (WebView)findViewById(R.id.web);



        s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               web.setWebViewClient(new WebViewClient(){
                    public boolean shouldOverrideUrlLoading(WebView view,String url){
                        view.loadUrl(url);
                        return true;
                    }
                });
                if (isChecked){
                    web.loadUrl("http://192.168.8.110:8080");
                }else {
                    web.loadUrl("https://www.baidu.com/");
                }
            }
        });

        final Handler handler = new Handler(){
            int data = 0;
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                data = msg.what;
                if(data == 0){
                    fire.setText("发现火焰!");
                }else {
                    RH.setText(((data&0xff)&0xff)+"."+((data >> 8) & 0x000000ff)+"%");
                    TMP.setText(((data >> 16)&0xff)+"."+((data >> 24)&0xff)+"℃");
                    fire.setText("未检测到火焰");
                }
            }
        };
        new Thread(new Runnable() {
            String cmd;
            int len;
            byte[] data = new byte[64];
            @Override
            public void run() {
                try {
                    while (true) {
                        cmd = "data";
                        Message mag = new Message();
                        Socket client = new Socket("192.168.8.110", 6666);
                        OutputStream out = client.getOutputStream();
                        InputStream in = client.getInputStream();
                        out.write(cmd.getBytes());
                        len = in.read(data);
                        mag.what= byteArrayToInt(data);
                        handler.sendMessage(mag);
                        client.close();
                        Thread.sleep(3000);
                    }
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        s1.setOnCheckedChangeListener(new switchChecked());
        s2.setOnCheckedChangeListener(new switchChecked());
        s3.setOnCheckedChangeListener(new switchChecked());
        s4.setOnCheckedChangeListener(new switchChecked());
        s5.setOnCheckedChangeListener(new switchChecked());
        s6.setOnCheckedChangeListener(new switchChecked());
    }


    public int byteArrayToInt(byte[] bytes){
        int value=0;
        //由高位到低位
        for(int i = 0; i < 4; i++) {
            int shift= (4-1-i) * 8;
            value +=(bytes[i] & 0x000000FF) << shift;//往高位游
        }
        return value;
    }
}

APP写的比较粗糙,希望各位大佬能多批评指正 ❤️

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

money的大雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值