智能家居篇之----收集数据、实时控制
在上一篇中说到wifi通讯,建立了通讯之后那么我们现在就可以实现安卓端与核心板之间的交流,那么我们现在来讲一下如何交流以及控制。(看这篇之前请先看上一篇wifi通讯)
以控制灯的亮灭举例:
安卓端代码:
1、布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.itman.connectesp8266.MainActivity" >
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#fe9920"
android:gravity="center"
android:text="LED"
android:textSize="30dp"
/>
<ImageView
android:id="@+id/open_close_led"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="125dp"
android:layout_marginTop="20dp"
android:src="@drawable/close_led"
/>
<ToggleButton
android:layout_marginTop="20dp"
android:id="@+id/tb"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:checked="true"
android:textOff="关掉" android:textOn="打开"
/>
<!--
<Button
android:id="@+id/bt_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="打开" />
<Button
android:id="@+id/bt_close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:text="关掉" />
-->
<TextView
android:id="@+id/tv_send_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tb"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_marginLeft="140dp"
android:text="发送的内容" />
</LinearLayout>
2、Led的Activity:
package com.gg.led;
import com.example.gg11_09_32.R;
import com.example.gg11_09_32.R.id;
import com.example.gg11_09_32.R.layout;
import com.gg.tcp.MobileServer;
import com.gg.tcp.SendAsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class Led extends ActionBarActivity implements OnClickListener {
private TextView tv_content, tv_send_text;
private ToggleButton tb;
private ImageView open_close_led;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
InitView();
//开启服务器
MobileServer mobileServer = new MobileServer();
mobileServer.setHandler(handler);
new Thread(mobileServer).start();
}
private void InitView() {
tv_content = (TextView) findViewById(R.id.tv_content);
tv_send_text = (TextView) findViewById(R.id.tv_send_text);
tb = (ToggleButton) findViewById(R.id.tb);
open_close_led = (ImageView) findViewById(R.id.open_close_led);
tb.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tb:
if(tb.isChecked()){
String str2 = "Close LED";
open_close_led.setImageResource(R.drawable.close_led);
new SendAsyncTask().execute(str2); //发送一个指令到核心板
tv_send_text.setText(str2);
}
else{
String str2 = "Open LED";
open_close_led.setImageResource(R.drawable.open_led);
new SendAsyncTask().execute(str2);
tv_send_text.setText(str2);
}
break;
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
//这里是收集核心板发来的数据。msg.obj是核心板发来的字符串,对字符串进行解析。
}
}
};
}
核心板代码(用keil4软件编写):