ESP8266模块三种模式:
1、STA模式(客户端模式): ESP8266模块通过路由器连接互联网,手机或电脑通过互联网实现对设备的远程控制
2、AP 模式(接入点模式):ESP8266 模块作为热点,手机或电脑直接与模块连接,实现局域网无线控制
3、STA+AP模式(两种模式并存):ESP8266 模块即可作为一个客户端连接区域网内的路由,也可以设置成是一个服务器
本文主要介绍一下ESP8266WiFi模块与Andriod实现数据通信的AP模式通讯。
1.ESP8266模块设定:
(1)准备阶段:
操作步骤:
1、选择对应串口,波特率为115200;
2、硬件接线完成后,将RST接低电平重置,观察串口接收到ready;
3、串口调试助手输入AT。
注意:你手中的模块是哪个厂家的,我这款是上海海枫的。在出厂的时候已经默认了初始参数,你们可以到开发手册查看一下。
发送+++,三秒之内发送a,返回ok进入AT指令配置。
因为我手上这款模块已经完成了初始化配置,所以不再需要使用AT指令配置。
其他款模块可以参考以下指令:
1.AT //观察是否回传OK
2.AT+CWMODE=2 //配置成AP模式
3.AT+CWSAP="ESP8266","123456789",11,0 //配置WiFi名称、密码等
4.AT+CIPMUX=1 //开启多连接
5.AT+CIPSERVER=1,8080 //开启TCP服务器,端口号设置成8080
6.AT+CIFSR //查询模块自身的IP地址
2.Android手机端APP设置:
操作步骤:
1、打开手机WiFi,连接ESP8266WiFi热点;
2、手机APP输入ESP8266WiFi模块IP地址;
3、点击连接,可从串口助手上观察到手机端APP的IP地址、CONNECT。
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit_ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="请输入IP地址"
android:text="10.10.100.000"
android:inputType="none"
android:textSize="20sp"></EditText>
<EditText
android:id="@+id/edit_port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入端口号"
android:text="8899"
android:inputType="number"
android:textSize="20sp"></EditText>
<TextView
android:id="@+id/send_text_dis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:hint="接收到的消息"
android:textColor="@color/colorPrimaryDark"
android:textSize="20sp"></TextView>
<EditText
android:id="@+id/send_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="1234"
android:hint="请输入要发送的内容"
android:textSize="20sp"></EditText>
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="发送"
android:textSize="25sp"></Button>
<Button
android:id="@+id/btn_turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="连接"
android:textSize="25sp"></Button>
</LinearLayout>
MainActivity:
package com.example.esp8266;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "hello";
private TextView mSend_text;
private TextView mSend_text_dis;
private Button mBtn_send;
private Socket mSocket;
private PrintStream out;
private ConnectThread mConnectThread;
private String mip = "192.168.4.1";
private int mport = 8089;
private Button mBtn_turn;
private BufferedReader mBufferedReader;
private String readStrng;
private String tempStrng;
private myHandler mHandler = new myHandler();
private Timer mTimer;
private ReceiveDataTask mReceiveDataTask;
private EditText mText_ip;
private EditText mText_port;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
@Override
protected void onStart() {
super.onStart();
startTimer();
}
@Override
protected void onStop() {
super.onStop();
stopTimer();
}
private class myHandler extends Handler {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1212:
mSend_text_dis.setText(readStrng);//接收
break;
}
}
}
private void initView() {
mSend_text_dis = findViewById(R.id.send_text_dis);
mSend_text = findViewById(R.id.send_text);
mBtn_send = findViewById(R.id.btn_send);
mBtn_turn = findViewById(R.id.btn_turn);
mText_ip = findViewById(R.id.edit_ip);
mText_port = findViewById(R.id.edit_port);
mBtn_send.setOnClickListener(this);
mBtn_turn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_turn:
if (mSocket == null || !mSocket.isConnected()) {
if(!TextUtils.isEmpty(mText_ip.getText()) && !TextUtils.isEmpty(mText_port.getText())){
mip = mText_ip.getText().toString();
mport = Integer.parseInt(mText_port.getText().toString());
Log.i(TAG, "onClick: "+mport);
mConnectThread = new ConnectThread(mip, mport);
mConnectThread.start();
}else if(TextUtils.isEmpty(mText_ip.getText())){
Toast.makeText(this,"请输入IP地址",Toast.LENGTH_SHORT).show();
}else if(TextUtils.isEmpty(mText_port.getText())){
Toast.makeText(this,"请输入端口号",Toast.LENGTH_SHORT).show();
}
}
if (mSocket != null && mSocket.isConnected()) {
try {
mSocket.close();
mSocket = null;
mBtn_turn.setText("连接");
Toast.makeText(this, "连接已断开", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case R.id.btn_send:
final String str = mSend_text.getText().toString();
if (str != null && out != null) {
new Thread(new Runnable() {
@Override
public void run() {
out.print(str);//发送
out.flush();
}
}).start();
}
break;
}
}
private class ConnectThread extends Thread {
private String ip;
private int port;
public ConnectThread(String ip, int port) {
this.ip = ip;
this.port = port;
}
@Override
public void run() {
try {
mSocket = new Socket(ip, port);
out = new PrintStream(mSocket.getOutputStream());
mBufferedReader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
runOnUiThread(new Runnable() {
@Override
public void run() {
mBtn_turn.setText("断开");
Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
}
});
}
}
}
private class ReceiveDataTask extends TimerTask {
@Override
public void run() {
try {
if (mBufferedReader != null && (mBufferedReader.ready())) {
char[] readbuff = new char[30];
byte[] readByte = new byte[30];
mBufferedReader.read(readbuff, 0, readbuff.length);
tempStrng = String.valueOf(readbuff);
readByte = tempStrng.getBytes();
readStrng = new String(readByte, 0, readByte.length, "GB2312");
Message message = Message.obtain();
message.what = 1212;
mHandler.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void startTimer() {
Log.i(TAG, "startTimer:");
if (mTimer == null) {
mTimer = new Timer();
}
if (mReceiveDataTask == null) {
mReceiveDataTask = new ReceiveDataTask();
}
mTimer.schedule(mReceiveDataTask, 0, 10);
}
private void stopTimer() {
Log.i(TAG, "stopTimer: ");
if (mReceiveDataTask != null) {
mReceiveDataTask.cancel();
mReceiveDataTask = null;
}
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
}
}
APP效果图:
标签:Toast,数据通信,void,private,new,import,ESP8266WiFi,null,Android From: https://blog.51cto.com/u_15887619/6365724