需要的包和硬件:
c51独立模块,esp8266一个,串口助手,org.eclipse.paho.client.mqttv3-1.2.0.jar的包
c51代码:
#include <reg52.h>
void delay(unsigned int time);
void connect_init();
void init();
char receive[];
int i;
void send(char *date);
sbit led1 = P1^0;
char *p = "AT+CIPSTART=0,\"TCP\",\"192.168.4.2\",5000\r\n";
char *q = "AT+CIPSEND=0,19\r\n";
char *cs = "hello work\r\n";
void main(){
init();
delay(500);
connect_init();
send(p);
delay(500);
send(q);
delay(500);
send(cs);
while(1);
}
void delay(unsigned int time){
while(time--);
}
//设置波特率9600
void init(){
SCON = 0x50;
TMOD = 0x20;
PCON |= 0x80;
TH1 = 0xfa;
TL1 = 0xfa;
EA = 1;
ES = 1;
TR1 = 1;
}
//exp8266每次重启需要输入的值
void connect_init(){
char *a = "AT+CIPMUX=1\r\n";
char *b = "AT+CIPSERVER=1,8080\r\n";
while(*a!='\0'){
SBUF = *a;
while(!TI);
TI = 0;
a++;
}
//加延迟防止上面数据exp8266没有识别运行完就进入下一条数据
delay(500);
while(*b!='\0'){
SBUF = *b;
while(!TI);
TI = 0;
b++;
}
}
//这个函数是用来向exp8266串口发送数据
void send(char *date){
delay(500);
while(*date != '\0'){
SBUF = *date;
while(!TI);
TI = 0;
date++;
}
}
esp8266的配置(记得最后在用串口助手配置的时候一定要加回车!!!!)
先AT+RST 重启
AT+CWMODE=2 开始AP模式吧exp8266做为热点
AT+CIPSERVER=1,8080 建立服务器(8080是我设置的端口)
AT+CWSAP=“ESP8266”,“0123456789”,4,0 设置wifi名为ESP8266,密码为0123456789。4是加密方式。0是信道)
在AT+UART=9600,8,1,0,0 用AT指令配置exp8266的波特率为9600(9600这个值在这里可以更改,比如想要多少波特率就改多少波特率)
AT+RST 更新重启配置
AT+CIPMUX=1 启动多连接(这个指令每次exp8266重启要输入一遍,上面我用c51初始化了这里在跟c51串口连接时可以不用在串口助手里打一遍)
AT+CIPSERVER=1 建立server(这个指令每次exp8266重启也要输入一遍)
AT+CIPSTART=0,"TCP","192.168.4.2",5000 建立TCP连接
安卓的配置
安卓对应main的页面
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_content"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="C51接收的内容" />
<TextView
android:id="@+id/tv_send_text"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/bt_send"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:text="发送的内容" />
</LinearLayout>
<Button
android:id="@+id/bt_send"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/tv_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="发送" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
安卓主程序
package com.example.c51;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity<MattClient, handler> extends AppCompatActivity implements View.OnClickListener {
private TextView tv_send_text,tv_content;
private Button bt_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//*************************************************************//
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);
bt_send = (Button) findViewById(R.id.bt_send);
bt_send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_send:
String str = "hello work";
new yichang().execute(str);
tv_send_text.setText(str);
break;
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
tv_content.setText("WiFi模块发送的:" + msg.obj);
Toast.makeText(MainActivity.this, "接收到信息", Toast.LENGTH_LONG)
.show();
}
}
};
}
第一个类
package com.example.c51;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MobileServer implements Runnable{
private ServerSocket server;
private DataInputStream in;
private Handler handler = new Handler();
public MobileServer(){
}
public void setHandler(Handler handler){
this.handler = handler;
}
@Override
public void run() {
try {
server = new ServerSocket(5000);
while (true){
Socket client = server.accept();
in = new DataInputStream(client.getInputStream());
byte[] receice;
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();
}
}
}
第二个类
package com.example.c51;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class SendAsyncTask extends AsyncTask<String,Void,Void> {
private static final String IP = "192.168.4.1";
private static final int PORT = 8080;
private Socket client = null;
private PrintStream out = null;
protected Void doInBackground(@NonNull String... params){
String str = params[0];
try {
client = new Socket(IP,PORT);
client.setSoTimeout(5000);
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;
}
}
结果图: