安卓控制新大陆云平台(三)
前言
在第二次的基础上,增加了简单的自动控制,舵机的使用,温度的获取等等
使用C#封装了部分API 部分API接口
部分封装的安卓API会在后续给出
布局文件
由于界面不需要多么原生,这里仅做简单使用即可.
我这里给出了跟第二次不同的布局代码
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="阙值"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="100dp"android:text="最小值:"/><EditTextandroid:id="@+id/minTemp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="20"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="最大值:"/><EditTextandroid:id="@+id/maxTemp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="30"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="自动"/><Buttonandroid:id="@+id/auto"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="100dp"android:text="开启"/><Buttonandroid:id="@+id/noauto"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="关闭"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="舵机"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="100dp"android:text="水平值:"/><TextViewandroid:id="@+id/currentX"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="0"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="50dp"android:text="垂直值:"/><TextViewandroid:id="@+id/currentY"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="0"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="水平控制"/><SeekBarandroid:id="@+id/seekBarX"android:layout_width="match_parent"android:layout_height="wrap_content"android:max="180"android:layout_marginLeft="40dp"android:layout_marginRight="50dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:text="垂直控制"/><SeekBarandroid:id="@+id/seekBarY"android:layout_width="match_parent"android:layout_height="wrap_content"android:max="180"android:layout_marginLeft="40dp"android:layout_marginRight="50dp"/></LinearLayout>
可见,都是基本的UI组件堆砌而成,如果对UI组件有什么问题,请参考:
https://blog.csdn.net/qq_40318498/article/details/89609248
控制界面
这里,针对第二次末尾提到的问题,如下
这里,我给出了解决办法,在NCallBack类中,添加一个无参的构造方法,如下:
public NCallBack() {}
即可解决问题.
舵机的控制
舵机的控制跟灯泡,风扇控制一样,只需要给出对应的参数即可,如
control("10439","steeringengine0",i);//10439设备标识符,steeringengine0舵机标识符,i是角度,0-180即可(整型)
自动控制
自动控制,简单的说就是运行一个线程,当温度高于上阙值时,开风扇,关灯;当温度低于下阙值时,开灯,关风扇;其他情况关灯和关风扇
温度的获取
由于温度的获取跟控制不相关,因此,我们必须查找官方给的SDK,查看跟传感器相关的类
参数设备Id,传感器标识符,回调函数,这里我们再看看回调函数.
有个onResponse和onFailure方法,一个是请求成功的返回,一个是请求失败的返回,
现在我们来查看新大陆云平台获取到了格式,如下
json类型,因此我们必须对请求到的数据进行json解析.
相关代码
package com.example.newland;import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;import com.google.gson.Gson;import org.json.JSONException;
import org.json.JSONObject;import cn.com.newland.nle_sdk.responseEntity.SensorInfo;
import cn.com.newland.nle_sdk.responseEntity.User;
import cn.com.newland.nle_sdk.responseEntity.base.BaseResponseEntity;
import cn.com.newland.nle_sdk.util.NCallBack;
import cn.com.newland.nle_sdk.util.NetWorkBusiness;import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;public class MenuActivity extends AppCompatActivity {private EditText EquimentID; //设备idprivate EditText MinTemp,MaxTemp; //最小,最大温度private TextView CurrentTemp; //当前温度private Button OpenLight,CloseLight; //开关灯private Button OpenFan,CloseFan; //开关风扇private Button GetTemp; //获取温度private Button Auto,Noauto; //是否自动控制...private SeekBar SeekBarX,SeekBarY; //拖动条private TextView CurrentSeekBarX,CurrentSeekBarY; //当前拖动条x,yprivate boolean isAuto = false; //标志位.private double tem;private NetWorkBusiness netWorkBusiness;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_menu);init();OpenLight.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {control("10439","ctrl",1); //灯.}});CloseLight.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {control("10439","ctrl",0); //灯.}});OpenFan.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {control("10439","defense",1); //fan}});CloseFan.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {control("10439","defense",0);}});GetTemp.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {getTemperature();}});Auto.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//自动控制isAuto = true; //运行线程...//线程.Thread1 th = new Thread1();new Thread(th).start();}});Noauto.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {isAuto = false;}});SeekBarX.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {@Overridepublic void onProgressChanged(SeekBar seekBar, int i, boolean b) {//获取值..String a = Integer.toString(i);CurrentSeekBarX.setText(a);control("10439","steeringengine1",i);}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {}});SeekBarY.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {@Overridepublic void onProgressChanged(SeekBar seekBar, int i, boolean b) {String a = Integer.toString(i);CurrentSeekBarY.setText(a);control("10439","steeringengine0",i);}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {}});}void init(){EquimentID = (EditText)findViewById(R.id.equimentID); //设备idCurrentTemp = (TextView) findViewById(R.id.currentTemp); //当前温度OpenLight = (Button) findViewById(R.id.openLight); //开灯CloseLight = (Button) findViewById(R.id.closeLight); //关灯OpenFan = (Button) findViewById(R.id.openFan); //开风扇CloseFan = (Button) findViewById(R.id.closeFan); //关风扇GetTemp = (Button)findViewById(R.id.getTemp); //获取温度Auto = (Button)findViewById(R.id.auto); //自动控制..Noauto = (Button)findViewById(R.id.noauto); //关闭自动控制.MinTemp = (EditText)findViewById(R.id.minTemp); //最小温度...MaxTemp = (EditText)findViewById(R.id.maxTemp); //最大温度...SeekBarX = (SeekBar)findViewById(R.id.seekBarX); //水平拖动条SeekBarY = (SeekBar)findViewById(R.id.seekBarY);CurrentSeekBarX = (TextView)findViewById(R.id.currentX);CurrentSeekBarY = (TextView)findViewById(R.id.currentY);Bundle bundle = getIntent().getExtras();String accessToken = bundle.getString("accessToken"); //获得传输秘钥netWorkBusiness = new NetWorkBusiness(accessToken,"http://api.nlecloud.com:80/"); //进行登录连接}public void control(String id,String apiTag,Object value){//设备id,标识符,值.netWorkBusiness.control(id, apiTag, value, new Callback<BaseResponseEntity>() {@Overridepublic void onResponse(@NonNull Call<BaseResponseEntity> call,@NonNull Response<BaseResponseEntity> response) {BaseResponseEntity<User> baseResponseEntity = response.body(); //获得返回体if (baseResponseEntity==null){Toast.makeText(MenuActivity.this,"请求内容为空",Toast.LENGTH_SHORT).show();}}@Overridepublic void onFailure(Call<BaseResponseEntity> call, Throwable t) {Toast.makeText(MenuActivity.this,"请求出错 " + t.getMessage(),Toast.LENGTH_SHORT).show();}});}public void getTemperature(){//获取温度...//id和设备标识符写死netWorkBusiness.getSensor("10439", "temperature", new NCallBack<BaseResponseEntity<SensorInfo>>() {@Overridepublic void onResponse(final Call<BaseResponseEntity<SensorInfo>> call, final Response<BaseResponseEntity<SensorInfo>> response) {BaseResponseEntity baseResponseEntity = response.body();if (baseResponseEntity!=null){//获取到了内容,使用json解析.//JSON 是一种文本形式的数据交换格式,它比XML更轻量、比二进制容易阅读和编写,调式也更加方便;解析和生成的方式很多,Java中最常用的类库有:JSON-Java、Gson、Jackson、FastJson等final Gson gson=new Gson();JSONObject jsonObject=null;String msg=gson.toJson(baseResponseEntity);try {jsonObject = new JSONObject(msg); //解析数据.JSONObject resultObj = (JSONObject) jsonObject.get("ResultObj");String aaa=resultObj.getString("Value");tem=Double.valueOf(aaa).intValue();CurrentTemp.setText(tem+"℃");} catch (JSONException e) {e.printStackTrace();}}}@Overrideprotected void onResponse(BaseResponseEntity<SensorInfo> response) {}public void onFailure(final Call<BaseResponseEntity<SensorInfo>> call, final Throwable t) {Toast.makeText(MenuActivity.this,"温度获取失败", Toast.LENGTH_SHORT).show();}});}class Thread1 implements Runnable{@Overridepublic void run() {while(true){//获取温度...getTemperature(); //并显示出来int currentTemp = (int)tem;int min = Double.valueOf(MinTemp.getText().toString()).intValue();int max = Double.valueOf(MaxTemp.getText().toString()).intValue();if (currentTemp>max && isAuto){//开风扇.control("10439","defense",1);control("10439","ctrl",0); //灯.}else if (currentTemp<min && isAuto){//小于报警开灯.,关风扇.control("10439","ctrl",1); //灯.control("10439","defense",0);}else{control("10439","ctrl",0); //灯.control("10439","defense",0);}try{Thread.sleep(1000); //希望不用使用Handler处理,使用handler加快处理速度吗?} catch (InterruptedException e) {e.printStackTrace();}}}}
}
结果
下面展示安卓获取到的新大陆示意图(跟上文不是一个项目)。
问题
1.获取到的温度有延迟,自动控制不能实时控制.
2.代码冗杂度过高,需要优化(本人安卓新手,只是调用API接口获取数据)
期待下次更新,如有问题,请下方留言评论。
安卓控制新大陆云平台(三)相关推荐
- 安卓控制新大陆云平台(一)
前言 由于要参加比赛,因此必须学会用安卓来控制新大陆云平台,后期还会用C#控制,最后更新STM32部分代码 新大陆云平台官网:http://www.nlecloud.com/ 本章主要先介绍如何写一个 ...
- Android——新大陆云平台篇
新大陆云平台篇 简介 代码分析 全部代码 简介 主要功能:温湿度传感器,灯,风扇与网关ZIGBBE自组网,然后网关将数据上传至云平台,上位机程序(android)通过云平台实时获取数据 1:获取温湿度 ...
- Android——新大陆云平台配置(2)
云平台登陆代码详解 开启应用 登陆云平台 开启应用 效果如图所示: 首先位Switch控件建造一些自定义view,在drawable文件夹下建立六个文件,分别位:open_track,open_thu ...
- Android网络请求-新大陆物联网云平台请求通信工程-使用Post登录新大陆云平台获取Token-物联网竞赛-物联网数据通信
目录 一.概述 二.代码与实现 三.总结归纳 一.概述 本文将通过Android Studio原生安卓环境,通过Post网络请求与新大陆物联网云平台官网进行通信,实现获取用户Token. Need:A ...
- 新大陆云平台app制作(史上最简单)
笔记目录 一.App Inventor介绍 二.访问原理 三.逻辑设计 1.页面设计 2.登录 3.数据解析 4.传感器数据获取 因为比赛原因,需要做一款app来读取新大陆物联网平台的数据,官方给的资 ...
- 新大陆云平台使用笔记
云平台API使用 1.登陆 1.1 登陆调用api 1.2返回JSON值2.查询单个项目 2.1 api 2.2查询单个项目返回JSON值 3.模糊查询项目 3.1 模糊查询项目api 3.2 模糊查 ...
- mfp 服务器控制中心,云平台管理中心
产品特点: ● 快速配置简单易用 云管理服务器可自动配置并发现网络云节点,无需复杂配置即可快速完成云平台搭建.支持Web访问,使用者通过浏览器即可轻松完成全平台的管理. ● 云显示管理 云管理服务器可 ...
- 新大陆物联网-Android实现网关功能-连接云平台并上传传感器数据-获取执行器指令并执行-Android网关开发-通信-数据上传云平台-JAVA原理讲解-免费云平台使用-竞赛2022国赛真题
目录 一.任务要求 二.开发环境 三.网关上线 四.数据上传与命令下发 五.JSON命令解析思路 六.总结 一.任务要求 我们将要实现的效果是:Android开发平板与Lora板进行有线串口通信,解析 ...
- 搭建云平台(一) 云平台基础服务部署
最近因课程要求,自己动手搭了一个OpenStack云平台,我将整个过程分了六篇博客.我使用了两个CentOS的虚拟机,一个作为计算节点,一个作为控制节点,整体过程比较繁杂,有心人可以细心看一看 1.修 ...
- 无线开关量收发模块、模拟量无线收发模块、无线液位采集传输控制系统、无线压力传感器、云平台远程监控、本地监控、无线西门子plc在污水处理方案中的应用
无线开关量收发模块.模拟量无线收发模块.无线液位采集传输控制系统.无线压力传感器.云平台远程监控.本地监控.无线西门子plc在污水处理方案中的应用 项目背景 污水处理是指为使污水达到排入某 ...
最新文章
- solidworks完全卸载安装高版本
- 用JS查看修改CSS样式(cssText,attribute('style'),currentStyle,getComputedStyle)
- Linux下crontab(自动重启)的格式备忘
- 鸿蒙开发者社区入口,鸿蒙OS 社区
- 垃圾回收算法与实现系列-学习GC之前的准备工作
- oracle取时间间隔天,如何掌握 Oracle 中的时间间隔型数据(转)
- Openfire Meetings插件是一个包含各种Jitsi项目(如VideoBridge和Meet)的实现
- 73 ----空间曲线的投影、投影柱面与投影曲线的方程、二元函数的等值线、等高线的性质
- 手机电子邮件设置exchange方式登录163邮箱
- [每日一题] 73. 电话号码(字符串、set)
- DIV+CSS布局基本流程及实例介绍
- java 中查询余额怎么写_如何调用中国移动余额查询的接口 用java 求大神指点一下。...
- 如何将Vufroria 、ARCore和ARkit结合使用
- 王建然之第三类永动机
- 利用CSS让图片围绕中心旋转
- 2022年郑州轻工业新生赛题目-打死我也不说
- 感谢lj对我的安(chao)慰(feng)
- 影响艾默生流量计测量误差的主要原因
- dSPACE Mid-Size Simulator Introduction
- python 爬虫基础学习