智能农业项目小结

项目的实现原理
通过编写一个手机APP,然后通过它来获取农业基地的信息,然后根据信息反馈,来通过控制基地里面的设施来调节。这里只介绍手机APP的实现。首先呢,先编写好编写好APP的布局,以及点击事件的监听,其次,通过对局域网的访问来获取基地的数据信息,最后通过对服务端发送命令来打开设备。
项目实现
这里呢就不把项目全列出来了,就只介绍一下项目的核心内容。
1.编写好编写好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="com.example.zhang.zhinenglongye.Kongqixiangqing">



<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:gravity="center"
            android:text="空气详情"
            android:textColor="#7BCE44"
            android:textSize="30sp" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:src="@mipmap/fanhuianniu" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@mipmap/co2beijng"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:gravity="center"
            android:text="当前CO2浓度"
            android:textColor="#FFE29D02"
            android:textSize="20sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:gravity="center"
            android:text="CO2设定的范围"
            android:textColor="#FFE29D02"
            android:textSize="20sp" />


    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/pic1"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginLeft="30dp"
            android:src="@mipmap/dakaifengshan" />

        <ImageView
            android:id="@+id/pic2"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/pic1"
            android:src="@mipmap/dakaiguangzhao" />

        <ImageView
            android:id="@+id/pic3"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_below="@+id/pic1"
            android:layout_marginLeft="30dp"
            android:src="@mipmap/dakaishui" />

        <ImageView
            android:id="@+id/pic4"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_below="@+id/pic2"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/pic3"
            android:src="@mipmap/dakaibaojing" />
    </RelativeLayout>
</LinearLayout>


</RelativeLayout>

布局上主要是给出了四个按钮,分别实现服务端的风扇,水泵,蜂鸣,光照的功能。
2.通过对局域网的访问来获取基地的数据信息

String di="http://"+Izhi.id+":8890/type/jason/action/getSensor?username=admin";
               Httpurl.getByOkHttp(di, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                 String as=response.body().string();
                try {
                    JSONObject object = new JSONObject(as);
                  final   int a1 = object.getInt("co2");//获取农业基地的数据信息
                    final  int a2 = object.getInt("light");
                    final  int a3 = object.getInt("soilTemperature");
                    final  int a4 = object.getInt("soilHumidity");
                    final  int a5 = object.getInt("airTemperature");
                    final  int a6 = object.getInt("airHumidity");
                    getActivity().runOnUiThread(new Runnable() {
                        @Override//OkHttp不能进行UI操作.所以要单独建线程
                        public void run() {//将获取的信息显示给用户。这里的布局页面没有写
                            q1.setText(a1 + "");
                            q2.setText(a2 + "");
                            q3.setText(a3 + "");
                            q4.setText(a4 + "");
                            q5.setText(a5 + "");
                            q6.setText(a6 + "");
                        }
                    });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

写一个OkHttp的公共类

public class Httpurl {

    private static final String BASE="http://"+Izhi.id+":8890/type/jason/action/";
 private static OkHttpClient okHttpClient=new OkHttpClient();
 //用来获取数据信息
    public static void getByOkHttp(String url, Callback callback){
        Request request=new Request.Builder().url(getAbsoluteUrl(url)).build();
        okHttpClient.newCall(request).enqueue(callback);

    }
//用来发送JSON控制信息
    public static void postJsonOkHttp(String url, JSONObject jsonObject, Callback callback){
        MediaType mediaType=MediaType.parse("application/json;charset=utf-8");
        RequestBody requestBody=RequestBody.create(mediaType,jsonObject.toString());
        Request request=new Request.Builder().url(getAbsoluteUrl(url)).post(requestBody).build();
        okHttpClient.newCall(request).enqueue(callback);
    }
    private static  String getAbsoluteUrl(String url){
        return BASE+url;
    }

3.通过对服务端发送命令来打开设备

public class Kongqixiangqing extends AppCompatActivity {
private ImageView pic1;
    private ImageView pic2;
    private ImageView pic3;
    private ImageView pic4;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kongqixiangqing);
        pic1=findViewById(R.id.pic1);
        pic2=findViewById(R.id.pic2);
        pic3=findViewById(R.id.pic3);
        pic4=findViewById(R.id.pic4);
//这里用来判断进入时按钮的状态
        if(Izhi.shui==1){
            pic3.setImageResource(R.mipmap.dakaishui2);
        }else if(Izhi.shui==0){
            pic3.setImageResource(R.mipmap.dakaishui);
        }
        if(Izhi.deng==0){
            pic2.setImageResource(R.mipmap.dakaiguangzhao);
        }else if(Izhi.deng==1){
            pic2.setImageResource(R.mipmap.dakaiguangzhao2);
        }
        if(Izhi.bao==0){
            pic4.setImageResource(R.mipmap.dakaibaojing);
        }else if(Izhi.bao==1){
            pic4.setImageResource(R.mipmap.dakaibaojing2);
        }
        if(Izhi.feng==1){
            pic1.setImageResource(R.mipmap.dakaifengshan2);
        }else if(Izhi.feng==0){
            pic1.setImageResource(R.mipmap.dakaifengshan);
        }



        pic1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Jindu.tiao(Kongqixiangqing.this);
                            if (Izhi.feng == 0) {
                    JSONObject object = new JSONObject();
                    try {
                        object.put("Blower", "1");//使用OKHttp发送JSON命令
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Httpurl.postJsonOkHttp("control", object, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Jindu.tin();
                                    Toast.makeText(Kongqixiangqing.this, "加载失败", Toast.LENGTH_SHORT).show();
                                } });
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String as = response.body().string();
                            try {
                                JSONObject object = new JSONObject(as);
                                String pan = object.getString("result");
                                if (pan.equals("ok")) {
                                    Izhi.feng =1;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Jindu.tin();
                                            Toast.makeText(Kongqixiangqing.this, "加载成功", Toast.LENGTH_SHORT).show();
                                            pic1.setImageResource(R.mipmap.dakaifengshan2);
                                        }
                                    });
                                }else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Jindu.tin();
                                            Toast.makeText(Kongqixiangqing.this, "加载失败1", Toast.LENGTH_SHORT).show();
                                        }});}
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    JSONObject object = new JSONObject();
                    try {
                        object.put("Blower", "0");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Httpurl.postJsonOkHttp("control", object, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Jindu.tin();
                                    Toast.makeText(Kongqixiangqing.this, "加载失败", Toast.LENGTH_SHORT).show();
                                } });
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String as = response.body().string();
                            try {
                                JSONObject object = new JSONObject(as);
                                String pan = object.getString("result");
                                if (pan.equals("ok")) {
                                    Izhi.feng =0;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Jindu.tin();
                                            Toast.makeText(Kongqixiangqing.this, "加载成功", Toast.LENGTH_SHORT).show();
                                            pic1.setImageResource(R.mipmap.dakaifengshan);
                                        }
                                    });
                                }else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Jindu.tin();
                                            Toast.makeText(Kongqixiangqing.this, "加载失败1", Toast.LENGTH_SHORT).show();
                                        }});}
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }


            }
        });
        pic3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Jindu.tiao(Kongqixiangqing.this);
                             if (Izhi.shui == 0) {
                    JSONObject object = new JSONObject();
                    try {
                        object.put("WaterPump", "1");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Httpurl.postJsonOkHttp("control", object, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Jindu.tin();
                                    Toast.makeText(Kongqixiangqing.this, "加载失败", Toast.LENGTH_SHORT).show();
                                } });
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String as = response.body().string();
                            try {
                                JSONObject object = new JSONObject(as);
                                String pan = object.getString("result");
                                if (pan.equals("ok")) {
                                    Izhi.shui =1;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Jindu.tin();
                                            Toast.makeText(Kongqixiangqing.this, "加载成功", Toast.LENGTH_SHORT).show();
                                            pic3.setImageResource(R.mipmap.dakaishui2);
                                        }
                                    });
                                }else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Jindu.tin();
                                            Toast.makeText(Kongqixiangqing.this, "加载失败1", Toast.LENGTH_SHORT).show();
                                        }});}
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    JSONObject object = new JSONObject();
                    try {
                        object.put("WaterPump", "0");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Httpurl.postJsonOkHttp("control", object, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Jindu.tin();
                                    Toast.makeText(Kongqixiangqing.this, "加载失败", Toast.LENGTH_SHORT).show();
                                } });
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String as = response.body().string();
                            try {
                                JSONObject object = new JSONObject(as);
                                String pan = object.getString("result");
                                if (pan.equals("ok")) {
                                    Izhi.shui =0;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Jindu.tin();
                                            Toast.makeText(Kongqixiangqing.this, "加载成功", Toast.LENGTH_SHORT).show();
                                            pic3.setImageResource(R.mipmap.dakaishui);
                                        }
                                    });
                                }else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Jindu.tin();
                                            Toast.makeText(Kongqixiangqing.this, "加载失败1", Toast.LENGTH_SHORT).show();
                                        }});}
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }}

                });
                    pic2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Jindu.tiao(Kongqixiangqing.this);
                                                if (Izhi.deng == 0) {
                                JSONObject object = new JSONObject();
                                try {
                                    object.put("Roadlamp", "1");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                Httpurl.postJsonOkHttp("control", object, new Callback() {
                                    @Override
                                    public void onFailure(Call call, IOException e) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Jindu.tin();
                                                Toast.makeText(Kongqixiangqing.this, "加载失败", Toast.LENGTH_SHORT).show();
                                            } });
                                    }

                                    @Override
                                    public void onResponse(Call call, Response response) throws IOException {
                                        String as = response.body().string();
                                        try {
                                            JSONObject object = new JSONObject(as);
                                            String pan = object.getString("result");
                                            if (pan.equals("ok")) {
                                                Izhi.deng =1;
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Jindu.tin();
                                                        Toast.makeText(Kongqixiangqing.this, "加载成功", Toast.LENGTH_SHORT).show();
                                                        pic2.setImageResource(R.mipmap.dakaiguangzhao2);
                                                    }
                                                });
                                            }else {
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Jindu.tin();
                                                        Toast.makeText(Kongqixiangqing.this, "加载失败1", Toast.LENGTH_SHORT).show();
                                                    }});}
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            } else {
                                JSONObject object = new JSONObject();
                                try {
                                    object.put("Roadlamp", "0");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                Httpurl.postJsonOkHttp("control", object, new Callback() {
                                    @Override
                                    public void onFailure(Call call, IOException e) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Jindu.tin();
                                                Toast.makeText(Kongqixiangqing.this, "加载失败", Toast.LENGTH_SHORT).show();
                                            } });
                                    }

                                    @Override
                                    public void onResponse(Call call, Response response) throws IOException {
                                        String as = response.body().string();
                                        try {
                                            JSONObject object = new JSONObject(as);
                                            String pan = object.getString("result");
                                            if (pan.equals("ok")) {
                                                Izhi.deng =0;
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Jindu.tin();
                                                        Toast.makeText(Kongqixiangqing.this, "加载成功", Toast.LENGTH_SHORT).show();
                                                        pic2.setImageResource(R.mipmap.dakaiguangzhao);
                                                    }
                                                });
                                            }else {
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Jindu.tin();
                                                        Toast.makeText(Kongqixiangqing.this, "加载失败1", Toast.LENGTH_SHORT).show();
                                                    }});}
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            }
                        }
                    });

                    pic4.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Jindu.tiao(Kongqixiangqing.this);
                                               if (Izhi.bao == 0) {
                                JSONObject object = new JSONObject();
                                try {
                                    object.put("Buzzer", "1");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                Httpurl.postJsonOkHttp("control", object, new Callback() {
                                    @Override
                                    public void onFailure(Call call, IOException e) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Jindu.tin();
                                                Toast.makeText(Kongqixiangqing.this, "加载失败", Toast.LENGTH_SHORT).show();
                                            } });
                                    }

                                    @Override
                                    public void onResponse(Call call, Response response) throws IOException {
                                        String as = response.body().string();
                                        try {
                                            JSONObject object = new JSONObject(as);
                                            String pan = object.getString("result");
                                            if (pan.equals("ok")) {
                                                Izhi.bao =1;
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Jindu.tin();
                                                        Toast.makeText(Kongqixiangqing.this, "加载成功", Toast.LENGTH_SHORT).show();
                                                        pic4.setImageResource(R.mipmap.dakaibaojing2);
                                                    }
                                                });
                                            }else {
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Jindu.tin();
                                                        Toast.makeText(Kongqixiangqing.this, "加载失败1", Toast.LENGTH_SHORT).show();
                                                    }});}
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            } else {
                                JSONObject object = new JSONObject();
                                try {
                                    object.put("Buzzer", "0");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                Httpurl.postJsonOkHttp("control", object, new Callback() {
                                    @Override
                                    public void onFailure(Call call, IOException e) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Jindu.tin();
                                                Toast.makeText(Kongqixiangqing.this, "加载失败", Toast.LENGTH_SHORT).show();
                                            } });
                                    }

                                    @Override
                                    public void onResponse(Call call, Response response) throws IOException {
                                        String as = response.body().string();
                                        try {
                                            JSONObject object = new JSONObject(as);
                                            String pan = object.getString("result");
                                            if (pan.equals("ok")) {
                                                Izhi.bao =0;
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Jindu.tin();
                                                        Toast.makeText(Kongqixiangqing.this, "加载成功", Toast.LENGTH_SHORT).show();
                                                        pic4.setImageResource(R.mipmap.dakaibaojing);
                                                    }
                                                });
                                            }else {
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        Jindu.tin();
                                                        Toast.makeText(Kongqixiangqing.this, "加载失败1", Toast.LENGTH_SHORT).show();
                                                    }});}
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            }
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭