基于Android studio设计的APP通过esp8266在AP模式下控制STM32单片机LED灯

一、单片机硬件:mini板f103rct6、 显示屏TFT2.8寸、wifi模块esp8266(正点原子旗舰店购买)

关键代码:

模块AT指令配置进入ap模式:

相应的指令执行:

软件Android Studio4.13版本

二、主要代码:

package com.example.LED;
import android.content.Context;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class MainActivity extends Activity implements View.OnClickListener
{
   
Button lj, hkai, hguan,lkai,lguan;
   
EditText et1, et2;
   
private String url;
   
private int dk;
   
private Socket socket = null;
   
private String str;
   
private Context context;
   
@Override
   
protected void onCreate(Bundle savedInstanceState)
    {
       
super.onCreate(savedInstanceState);
        setContentView(
R.layout.activity_main);
       
context = this;
        init();
    }
   
private void init()
    {
       
et1 = (EditText) findViewById(R.id.ip);
       
et2 = (EditText) findViewById(R.id.port);
       
lj = (Button) findViewById(R.id.lj);
       
hkai = (Button) findViewById(R.id.hkai);
       
hguan = (Button) findViewById(R.id.hguan);
       
lkai = (Button) findViewById(R.id.lkai);
       
lguan = (Button) findViewById(R.id.lguan);
       
lj.setOnClickListener(this);
       
hkai.setOnClickListener(this);
        
hguan.setOnClickListener(this);
       
lkai.setOnClickListener(this);
       
lguan.setOnClickListener(this);
    }
   
@Override
   
public void onClick(View v)
    {
       
switch (v.getId())
        {
           
case R.id.lj:
               
url = et1.getText().toString().trim();
               
String text = et2.getText().toString().trim();
               
if (TextUtils.isEmpty(url))
                {
                   
Toast.makeText(context, "ip地址不能为空",
                           
Toast.LENGTH_SHORT).show();
                   
return;
                }
               
if (TextUtils.isEmpty(text))
                {
                   
Toast.makeText(context, "端口不能为空",
                           
Toast.LENGTH_SHORT).show();
                    
return;
                }
               
dk = Integer.parseInt(text);
               
str = "";
               
new ServerThreadTCP().start();
               
break;
           
case R.id.hkai:
               
if (socket != null)
                {
                    
str = "HK";
                   
new ServerThreadTCP().start();
                }
else
               
{
                   
Toast.makeText(context, "请先建立socket连接",
                           
Toast.LENGTH_SHORT).show();
                }
               
break;
           
case R.id.hguan:
               
if (socket != null)
                {
                   
str = "HG";
                   
new ServerThreadTCP().start();
                }
else
               
{
                   
Toast.makeText(context, "请先建立socket连接",
                           
Toast.LENGTH_SHORT).show();
                }
               
break;
           
case R.id.lkai:
               
if (socket != null)
                {
                   
str = "LK";
                    
new ServerThreadTCP().start();
                }
else
               
{
                   
Toast.makeText(context, "请先建立socket连接",
                           
Toast.LENGTH_SHORT).show();
                }
               
break;
           
case R.id.lguan:
               
if (socket != null)
                {
                   
str = "LG";
                   
new ServerThreadTCP().start();
                }
else
               
{
                   
Toast.makeText(context, "请先建立socket连接",
                            
Toast.LENGTH_SHORT).show();
                }
               
break;
        }
    }
   
class ServerThreadTCP extends Thread
   
{ public void run()
        {
OutputStream outputStream = null;
           
InputStream inputStream = null;
           
try {
               
if (socket == null){
                   
socket new Socket(url, dk);
                }
                outputStream =
socket.getOutputStream();
                inputStream =
socket.getInputStream();
               
byte data[] = str.getBytes();
                outputStream.write(
data, 0, data.length);
                outputStream.flush();
               
byte buffer[] = new byte[1024 * 4];
               
int temp = 0;
               
while ((temp = inputStream.read(buffer)) != -1)
                {
                   
System.out.println(new String(buffer, 0, temp));
                }
            }
catch (Exception e)
            {
System.out.println(e);
            }
finally
                           
{
                
try {
                    inputStream.close();
                    outputStream.close();
                   
socket.close();
                }
catch (IOException e)
                                   {
                    e.printStackTrace();
                }
            }
        }
    }
}

三、参考书籍:

单片机与物联网技术应用实战教程/王李冬版

四、作品展示

五、资料下载

猜你喜欢

转载自blog.csdn.net/th971212/article/details/123977792