智能家居篇之---WIFI通信(ESP8266)

                  智能家居篇之---WIFI通信(ESP8266)

  智能家居所谓智能就是能够经过手机端来收集数据、分析数据、进而控制。那么要实现这样的一套系统,最重要也是最基本的要手机端与核心板创建通信。那么下面咱们来说一讲如何创建通信。

一、硬件部分:准备一个核心板,一个ESP8266wifi模块。java

二、软件部分:一台手机。android

首先咱们来讲一下这个ESP8266,这个在淘宝上很是便宜,10块左右,安信可的产品。这个WiFi模块已经作得很是的成熟,下面介绍一下它的基本使用,首先这个模块有三种模式:
1:STA 模式:ESP8266模块经过路由器链接互联网,手机或电脑经过互联网实现对设备的远程控制。
2:AP 模式:ESP8266模块做为热点,实现手机或电脑直接与模块通讯,实现局域网无线控制。
3:STA+AP 模式:两种模式的共存模式,便可以经过互联网控制可实现无缝切换,方便操做。

今天的实现用AP模式就够了,指令有下面这几个就够了:
一、设置wifi模式:AT+CWMODE=2
二、重启生效:AT+RST
三、启动多链接:AT+CIPMUX=1
四、创建server:AT+CIPSERVER=1
 服务器

安卓端代码编写:

一、添加一个异步处理类:异步

/**
 * Created by Layne_Yao on 2017/5/12.
 * CSDN:http://blog.csdn.net/Jsagacity
 */
 
public class SendAsyncTask extends AsyncTask<String, Void, Void> {
	
	//这里是链接ESP8266的IP和端口号,IP是经过指令在单片机开发板查询到,而端口号能够自行设置,也可使用默认的,333就是默认的
	private static final String IP = "192.168.4.1";
	private static final int PORT = 333;
 
 
 
	private Socket client = null;
	private PrintStream out = null;
 
 
	@Override
	protected Void doInBackground(String... params) {
		String str = params[0];
		try {
			client = new Socket(IP, PORT);
			client.setSoTimeout(5000);
			// 获取Socket的输出流,用来发送数据到服务端
			out = new PrintStream(client.getOutputStream());
			out.print(str);
			out.flush();
 
			if (client == null) {
				return null;
			} else {
				out.close();
				client.close();
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		}
 
		return null;
	}
	
}

二、手机端创建一个接受wifi模块传来得消息的服务器:ide

public class MobileServer implements Runnable {
	private ServerSocket server;
	private DataInputStream in;
	private byte[] receice;
 
	private Handler handler = new Handler();
 
	public MobileServer() {
	}
 
	public void setHandler(Handler handler) {
		this.handler = handler;
	}
 
	@Override
	public void run() {
 
		try {
			//5000是手机端开启的服务器的端口号,ESP8266进行TCP链接时使用的端口,而IP也是经过指令查询的联入设备的IP
			server = new ServerSocket(5000);
			while (true) {
				Socket client = server.accept();
				in = new DataInputStream(client.getInputStream());
				receice = new byte[50];
				in.read(receice);
				in.close();
				
				Message message = new Message();
				message.what = 1;
				message.obj = new String(receice);
				handler.sendMessage(message);
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			server.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

三、布局布局

<RelativeLayout 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"
    tools:context="com.itman.connectesp8266.MainActivity" >
 
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#fe9920"
        android:gravity="center"
        android:text="接收的内容" />
 
    <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="发送" />
 
    <TextView
        android:id="@+id/tv_send_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/bt_send"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:text="发送的内容" />
 
</RelativeLayout>

四、Mainactivitythis

<RelativeLayout 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"
    tools:context="com.itman.connectesp8266.MainActivity" >
 
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#fe9920"
        android:gravity="center"
        android:text="接收的内容" />
 
    <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="发送" />
 
    <TextView
        android:id="@+id/tv_send_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/bt_send"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:text="发送的内容" />
 
</RelativeLayout>

五、权限spa

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

这样咱们就能实现手机端发送指令到核心板,而后核心板再处理。

下面讲一下如何接收数据:.net

一、创建TCP链接:AT+CIPSTART=0,"TCP","192.168.4.2",5000(注意这时的ip是手机的ip)
二、发送信息:Sent to the Android3d

 

参考大神连接:http://www.javashuo.com/article/p-ypcuqxfk-gz.html