智能农业大致思路和用到的知识点

布局很简单,用viewpager和fragment实现页面的滑动和点击跳转,绑定按钮的id;

要获取到传感器上的值显示到app上,这里我们需要使用到网络,首先添加权限:

 <uses-permission android:name="android.permission.INTERNET"/>

添加依赖:

   compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'com.google.code.gson:gson:2.8.2'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'

如果使用AscncHttp,那么获取的速度会慢一点,Okhttp比它快。

新建类HttpUtil,公共需要用到的东西:

public class HttpUtil {
  private static final String basicUrl = "http://192.168.1.100:8890/type/jason/action/";
  private static AsyncHttpClient client = new AsyncHttpClient();
    public static void get (String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
        client.get(url,params,responseHandler);
    }
    public static void post(Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
        client.post(context,url,entity,contentType,responseHandler);
    }

其中地址是智能农业公共能用到的部分。

okhttp也是新建类:

public class Okhttp {
     private static OkHttpClient client = new OkHttpClient();
     public static void sendOkHttpRequest(String address,okhttp3.Callback callback){
      Request request = new Request.Builder().url(address).build();
      client.newCall(request).enqueue(callback);
    }
public static void postJsonByOkHttp(String url, JSONObject jsonObject,okhttp3.Callback callback){
        MediaType mediaType = MediaType.parse("application/json;Charset=UTF-8");
        RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString());
        Request request = new Request.Builder().url(url).post(requestBody).build();
        client.newCall(request).enqueue(callback);
    }
 }

获取传感器的值:

        RequestParams params = new RequestParams();
        params.put("username","admin");
        HttpUtil.get(basicUrl+"getSensor", params, new TextHttpResponseHandler() {
            @Override
           public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
               
       }         @Override
           public void onSuccess(int statusCode, Header[] headers, String responseString) {

                
                 Gson gson = new Gson();
                App app = gson.fromJson(responseString,App.class);
                 airHumidity = app.getAirHumidity();
                PM25 = app.getPM25();
                 airTemperature = app.getAirTemperature();
                 soilTemperature = app.getSoilTemperature();
               co2 = app.getCo2();
                 soilHumidity = app.getSoilHumidity();
                 light = app.getLight();
 
                 co2Tv.setText(co2+"");
                 lightTv.setText(light+"");
                soilTempTv.setText(soilTemperature+"");
                 soilWetnessTv.setText(soilHumidity+"");
                 airTempTv.setText(airTemperature+"");
                airWetnessTv.setText(airHumidity+"");
          }
       });

其他页面获取值以此类推。

okhttp加快速度:

        String relativeUrl = intent.getStringExtra("IP");
        String basicUrl2 = "http://"+relativeUrl+":8890/type/jason/action/getSensor?username=admin";
        Log.e(TAG, "getAppValues: "+basicUrl2 );
        Okhttp.sendOkHttpRequest(basicUrl2, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                });
            }
              @Override
            public void onResponse(Call call, final Response response) throws IOException {

                flag++;
               String responseString = null;
                try {
                    responseString = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Gson gson = new Gson();
                App app = gson.fromJson(responseString,App.class);
                airHumidity = app.getAirHumidity();
                PM25 = app.getPM25();
                airTemperature = app.getAirTemperature();
                soilTemperature = app.getSoilTemperature();
                co2 = app.getCo2();
                soilHumidity = app.getSoilHumidity();
                light = app.getLight();
                 getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        co2Tv.setText(co2+"");
                        lightTv.setText(light+"");
                        soilTempTv.setText(soilTemperature+"");
                        soilWetnessTv.setText(soilHumidity+"");
                        airTempTv.setText(airTemperature+"");
                        airWetnessTv.setText(airHumidity+"");
                  }
                });
  

新建类便于每个按钮的状态的获取:

public class Status {

    public static int WaterPump = 0;
    public static int Blower = 0;
    public static int Roadlamp = 0;
    public static int Buzzer = 0;
 }
初始值设为0。

获取每个按钮当时的状态,如果是开着的则返回数字1,是关着的返回数字0,进行打开操作:

   final JSONObject[] jsonObject = {new JSONObject()};
        try {
            jsonObject[0].put("Blower", 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Okhttp.postJsonByOkHttp(basicUrl + "control", jsonObject[0], new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
              @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseString = response.body().string();
                try {
                    JSONObject jsonObject1 = new JSONObject(responseString);
                    if (jsonObject1.getString("result").equals("ok")) {
                        Status.Blower = 1;
                        DeviceControl.result = 1;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
           }
        });

关闭操作:

  final JSONObject[] jsonObject = {new JSONObject()};
        try {
            jsonObject[0].put("Blower", 0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Okhttp.postJsonByOkHttp(basicUrl + "control", jsonObject[0], new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseString = response.body().string();
                try {
                    JSONObject jsonObject1 = new JSONObject(responseString);
                    if (jsonObject1.getString("result").equals("ok")) {
                        Status.Blower = 0;
                        DeviceControl.result = 0;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

还有点击开始或者关闭之后更换图片:

    if (Status.Blower == 0) {
            openFanImg.setImageResource(R.mipmap.dakaifengshan);
            DeviceControl.result = 0;
        } else if (Status.Blower == 1) {
            openFanImg.setImageResource(R.mipmap.dakaifengshan2);
            DeviceControl.result = 1;
        }

其他按钮也是以此类推。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值