嵌入式 > 技术百科 > 详情

单片机wifi控制app

发布时间:2024-12-09 发布时间:
|

说起单片机wifi控制app,单片机方机的资料还容易找,对于app资料网上可以找得到的资料不多,就是找到了,也就是个APP,单片机wifi控制app配套的单片机C语言程序及手机APP源代码一般的都无法找得到,资料不全,给使用带来很大困难。目前,单片机wifi控制app,目前最常见到的是安桌手机的app,其编程用得比较多的是JAVA语言写的,其开发环境是Eclipse,对于基英语基础不好的人来说,比较容易上手的就是易语言编程环境E4A。这里我就贴出一个Eclipse开发环境下写的wifi控制app,下面是APP界面截图:

单片机wifi控制app
实物照片(照片中的APP是E4A写的,单片机的代码是一样的)


布局文件:

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:android1="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".ESP8266" >

        android:id="@+id/textView1"

        style="text-align:center"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="16dp"

        android:gravity="center"

        android:scrollHorizontally="true"

        android:text="@string/ljzt"

        android:textAppearance="?android:attr/textAppearanceLarge" />

   

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_below="@+id/textView1"

        android:layout_marginLeft="16dp"

        android:layout_marginTop="19dp"

        android:minWidth="128dip"

        android:scrollHorizontally="true"

        android:text="@string/open" />

   

        android:id="@+id/Button02"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/button1"

        android:layout_marginLeft="16dp"

        android:layout_marginTop="50dp"

        android:minWidth="128dip"

        android:scrollHorizontally="true"

        android:text="K1开" />

    

   

        android:id="@+id/Button03"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/button1"

        android:layout_marginLeft="170dp"

        android:layout_marginTop="50dp"

        android:minWidth="128dip"

        android:scrollHorizontally="true"

        android1:text="K1关" />

   

        android:id="@+id/Button04"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/Button02"

        android:layout_below="@+id/Button02"

        android:layout_marginTop="36dp"

        android:minWidth="128dip"

        android:scrollHorizontally="true"

        android:text="K2开" />

    

   

        android:id="@+id/Button06"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/Button04"

        android:layout_below="@+id/Button04"

        android:layout_marginTop="36dp"

        android:minWidth="128dip"

        android:scrollHorizontally="true"

        android:text="K3开" />

        

   

        android:id="@+id/Button07"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/Button05"

        android:layout_marginLeft="170dp"

        android:layout_marginTop="36dp"

        android:minWidth="128dip"

        android:scrollHorizontally="true"

        android:text="K3关" />

    

   

        android:id="@+id/Button05"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/Button02"

        android:layout_marginLeft="170dp"

        android:layout_marginTop="36dp"

        android:minWidth="128dip"

        android:scrollHorizontally="true"

        android:text="K2关" />

   

        android:id="@+id/Button01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/textView1"

        android:layout_marginLeft="170dp"

        android:layout_marginTop="19dp"

        android1:layout_alignParentLeft="true"

        android:minWidth="128dip"

        android:scrollHorizontally="true"

        android:text="@string/close" />

   

        android1:id="@+id/TextView02"

        style="text-align:center"

        android1:layout_width="wrap_content"

        android1:layout_height="wrap_content"

        android1:layout_alignParentLeft="true"

        android1:layout_alignParentRight="true"

        android1:layout_below="@+id/Button07"

        android:layout_marginTop="35dp"

        android1:gravity="center"

        android1:text="@string/ggnr3"

        android1:textAppearance="?android:attr/textAppearanceLarge" />

JV语言:

package com.example.esp8266_gpio;

import java.io.IOException;

import java.io.PrintStream;

import java.net.Socket;

import java.net.UnknownHostException;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView; 

import android.widget.Toast;

import android.os.Handler;  

public class ESP8266 extends Activity

{

Handler handler = new Handler();

 

  private final String SERVER_HOST_IP = "192.168.4.1";

 

  private final int SERVER_HOST_PORT = 5000;

  private Button button1;

  private Button Button01;

  private Button Button02;

  private Button Button03;

  private Button Button04;

  private Button Button05;

  private Button Button06;

  private Button Button07;

  private TextView textView1;

  private Socket socket;

  private PrintStream output;

  

  Runnable runnable = new Runnable() {

  @Override

  public void run() {

  try

     {

    socket.sendUrgentData(0xFF);

     }catch(Exception ex){

     closeSocket();//断开连接

     }

   handler.postDelayed(this, 1000);

  }

 };

  

 public void toastText(String message)

  {

    Toast.makeText(this, message, Toast.LENGTH_LONG).show();

  }

  public void handleException(Exception e, String prefix)

  {

    e.printStackTrace();

    toastText(prefix + e.toString());

  }

 

  @Override

  public void onCreate(Bundle savedInstanceState)

  {

 

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_esp8266);

     

    initView();

    

    button1.setOnClickListener(new Button.OnClickListener()

    {

      @Override

      public void onClick(View v)

      {

        initClientSocket();

      }

    });

    

    Button01.setOnClickListener(new Button.OnClickListener()

    {

      @Override

      public void onClick(View v)

      {

     closeSocket();//断开连接

      }

    });

    

    Button02.setOnClickListener(new Button.OnClickListener()

    {

      @Override

      public void onClick(View v)

      {

     try

     {

    socket.sendUrgentData(0xFF);

     }catch(Exception ex){

     closeSocket();//断开连接

     }

     sendMessage("GPIO0=0;");

     

      }

    });

    

    Button03.setOnClickListener(new Button.OnClickListener()

    {

      @Override

      public void onClick(View v)

      {

        sendMessage("GPIO0=1;");

      }

    });

    

    Button04.setOnClickListener(new Button.OnClickListener()

    {

      @Override

      public void onClick(View v)

      {

        sendMessage("GPIO0=2;");

      }

    });

    Button05.setOnClickListener(new Button.OnClickListener()

    {

      @Override

      public void onClick(View v)

      {

        sendMessage("GPIO0=3;");

      }

    });

            

    Button06.setOnClickListener(new Button.OnClickListener()

    {

      @Override

      public void onClick(View v)

      {

        sendMessage("GPIO0=4;");

      }

    });

    Button07.setOnClickListener(new Button.OnClickListener()

    {

      @Override

      public void onClick(View v)

      {

        sendMessage("GPIO0=5;");

      }

    });

          

  }

  

  public void initView()

  {

 button1 = (Button)findViewById(R.id.button1);

 Button01 = (Button)findViewById(R.id.Button01);

 Button02 = (Button)findViewById(R.id.Button02);

 Button03 = (Button)findViewById(R.id.Button03);

 Button04 = (Button)findViewById(R.id.Button04);

 Button05 = (Button)findViewById(R.id.Button05);  

 Button06 = (Button)findViewById(R.id.Button06);

 Button07 = (Button)findViewById(R.id.Button07);  

 

 textView1=(TextView)findViewById(R.id.textView1);

 

 Button01.setEnabled(false);

 Button02.setEnabled(false);

 Button03.setEnabled(false);

 Button04.setEnabled(false);

 Button05.setEnabled(false);  

 Button06.setEnabled(false);

 Button07.setEnabled(false);  

 textView1.setText("设备未连接");

  }

  public void closeSocket()

  {

    try

    {

      output.close();

      socket.close();

      button1.setEnabled(true);

 Button01.setEnabled(false);

 Button02.setEnabled(false);

 Button03.setEnabled(false);

 Button04.setEnabled(false);

 Button05.setEnabled(false);

 Button06.setEnabled(false);

 Button07.setEnabled(false);

 textView1.setText("设备未连接");

}

    catch (IOException e)

    {

      

}

  }

  

  private void initClientSocket()

  {

    try

    {

     

      socket = new Socket(SERVER_HOST_IP, SERVER_HOST_PORT);

     

      output = new PrintStream(socket.getOutputStream(), true, "utf-8");

      

      button1.setEnabled(false);

      Button01.setEnabled(true);

      Button02.setEnabled(true);

      Button03.setEnabled(true);

      Button04.setEnabled(true);

      Button05.setEnabled(true);

      Button06.setEnabled(true);

      Button07.setEnabled(true);

      textView1.setText("连接成功");

      handler.postDelayed(runnable, 1000);

    }

    catch (UnknownHostException e)

   {

      handleException(e, "unknown host exception: " + e.toString());

    }

    catch (IOException e)

    {

    toastText("连接错误!请检查是否连接硬件的wifi");

    }

  }

  

  private void sendMessage(String msg)

  {

    output.print(msg);

  }

  

  

}



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
瑞昱最新6端口超高速以太网络交换器单芯片适合于xDSL.PO...