转载android客服端+eps8266+单片机+路由器之远程控制系统

Posted IT足迹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了转载android客服端+eps8266+单片机+路由器之远程控制系统相关的知识,希望对你有一定的参考价值。

android客服端+eps8266+单片机+路由器做了一个远程控制的系统,因为自己是在实验室里,所以把实验室的门,灯做成了远程控制的。

控制距离有多远------只能说很远很远,只要你手机能上网的地方,不对应该是只要能打电话的地方,不对应该是只要是移动网(我用的是移动的卡)覆盖的地方,

这篇只说明怎么样才能实现远程通信(在路由器上怎样设置,wifi模块eps8266怎样设置),最后会贴上单片机,android的源码

请事先参考我的前几篇文章

android之WIFI小车编程详述

android 之TCP客户端编程

ESP8266使用详解

 

有人的设置方法介绍

http://www.usr.cn/Faq/146.html

http://www.usr.cn/Faq/148.html

 

另外

ESP8266使用详解中介绍过它的连接路由器的模式,让它连接上路由器后需要做一些设置

 

 

我做的手机tcp客服端

 

关于上面的远程 与 近程连接

远程连接:

当手机是2G,3G,4G上网的时候,或者,连接了wifi模块所连接的路由器的时候,或者所连接的路由器桥接了wifi模块所连接的路由器。

远程连接服务器ip是路由器的公网ip

 

对于通信端口号是设置wifi模块所监听的端口号(它通信的端口号)

unsigned char code CIPSERVER[]="AT+CIPSERVER=1,10000\\r\\n";//开启服务器模式,端口号10000

 近程连接:

因为我设置了wifi模块AP 兼 Station 模式,所以模块会发出无线信号,我连接它的wifi信号也能完成通信,只不过连接它的wifi信号后,要连接的ip地址为192.168.4.1

好了附上单片机程序,本来我设置了返回数据,但是因为我的灯和门并没有加检测所以后来就去掉了

复制代码
#define _MAIN_C_
#include "include.h"
sbit L1 = P1^0;
sbit L2 = P1^1;
sbit L3 = P1^2;
sbit L4 = P1^3;
sbit kaiguan = P1^4;
sbit led1 = P1^5;
sbit led2 = P1^6;
//AP
unsigned char code CWMODE2[]="AT+CWMODE=2\\r\\n";     //设置模块的工作的模式为AP模式

//多连接ap+station模式
unsigned char code CWMODE3[]="AT+CWMODE=3\\r\\n";     //设置模块的工作的模式为ap+station模式
unsigned char code CWJAP[]="AT+CWJAP=\\"TP-LINK_9750\\",\\"56565888\\"\\r\\n";//设置连接的路由器

unsigned char code RST[]="AT+RST\\r\\n";                     //重启模块使AT+CWMODE模式生效
unsigned char code CIPMUX[]="AT+CIPMUX=1\\r\\n";            //启动多连接
unsigned char code CIPSERVER[]="AT+CIPSERVER=1,10000\\r\\n";//开启服务器模式,端口号10000
//微秒延时
/*
void us_delay(unsigned char t) 
{  
    while(t--); 
} */
//ms秒延时
void Delay_ms(unsigned long ms)
{
    unsigned char i, j,k;
    for(k=0;k<ms;k++)
    {
        _nop_();
        _nop_();
        i = 22;
        j = 128;
        do
        {
            while (--j);
        } while (--i);
    }
}
void delay1s(void)   //误差 -0.00000000024us
{
    unsigned char a,b,c;
    for(c=95;c>0;c--)
        for(b=26;b>0;b--)
            for(a=185;a>0;a--);
}
//ESP8266设置
void ESP8266_Set(unsigned char *puf)
{
    while(*puf!=\'\\0\')
    {
        Send_Uart(*puf);
        puf++;
    }
}

//多连接AP模式
void ManyConnect_AP()
{
    P1=0xff;
    //模式ap
    while(1)
    {
        ESP8266_Set(CWMODE2);//返回ok
        delay1s();
        if(Usart_AT_flage ==1)
        {
            if(strstr(Usart_Receive, "OK") )
            {
                Usart_AT_flage = 0;
                L1 = 0;
                break;
            }                
        }
  }
    //复位
    ESP8266_Set(RST);//返回一大溜,不用判断返回
    delay1s();
    delay1s();
    //多连接
    while(1)
    {
        ESP8266_Set(CIPMUX);//返回ok
        delay1s();
        if(Usart_AT_flage ==1)
        {
            if(strstr(Usart_Receive, "OK") )
            {
                Usart_AT_flage = 0;
                L2 = 0;
                break;
            }                
        }
  }
    //启动TCP服务
    while(1)
    {
        ESP8266_Set(CIPSERVER);//返回ok,多了也返回ok
        delay1s();
        if(Usart_AT_flage ==1)
        {
            if(strstr(Usart_Receive, "OK") )
            {
                Usart_AT_flage = 0;
                L3 = 0;
                break;
            }                
        }
  }
}

//多连接AP+Station 模式
void ManyConnect_ap_Station()
{
    P1=0xff;
    //模式3
    while(1)
    {
        ESP8266_Set(CWMODE3);//返回ok
        delay1s();
        if(Usart_AT_flage ==1)//接收完成
        {
            if(strstr(Usart_Receive, "OK") )
            {
                Usart_AT_flage = 0;
                L1 = 0;
                break;
            }                
        }
  }
    //连接路由
    while(1)
    {
        //复位
        ESP8266_Set(RST);//返回一大溜,不用判断返回
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        ESP8266_Set(CWJAP);//返回ok
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        delay1s();
        if(Usart_AT_flage ==1)
        {
            if(strstr(Usart_Receive, "OK") )
            {
                Usart_AT_flage = 0;
                L2 = 0;
                break;
            }    
        }
  }
    //多连接
    while(1)
    {
        ESP8266_Set(CIPMUX);//返回ok
        delay1s();
        if(Usart_AT_flage ==1)
        {
            if(strstr(Usart_Receive, "OK") )
            {
                Usart_AT_flage = 0;
                L3 = 0;
                break;
            }                
        }
  }
    //启动TCP服务
    while(1)
    {
        ESP8266_Set(CIPSERVER);//返回ok,多了也返回ok
        delay1s();
        if(Usart_AT_flage ==1)
        {
            if(strstr(Usart_Receive, "OK") )
            {
                Usart_AT_flage = 0;
                L4 = 0;
                break;
            }                
        }
  }
}

void delay1s500ms(void)   //误差 -0.000000000341us
{
    unsigned char a,b,c;
    for(c=123;c>0;c--)
        for(b=212;b>0;b--)
            for(a=25;a>0;a--);
}

void main()
{
    int i;
    InitUART();
    while(1)
    {
        //ManyConnect_AP();//多连接AP模式
        ManyConnect_ap_Station();//多连接AP+station模式
        while(1)
        {
            //由于消息的开头是+IP  故做此判断,00000000000000000号
            if((Usart_Receive[0]==\'+\')&&(Usart_Receive[1]==\'I\')&&(Usart_Receive[2]==\'P\'))
            {
                if((Usart_Receive[3]==\'D\')&&(Usart_Receive[5]==\'0\')&&(Usart_Receive[6]==\',\'))
                {
                    if(Usart_Receive[9]==\'1\')
                    {
                        kaiguan = 0;
                        delay1s500ms();
                        kaiguan = 1;
                        delay1s();
                        for(i = 0 ; i<20; i++)
                        {
                            Usart_Receive[i]=\' \';
                        }
                        
                        //ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        //delay1s();
                        //ESP8266_Set("1");//向服务器发送数据
                        
                        //delay1s();
                        //ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        //delay1s();
                        //ESP8266_Set("1");//向服务器发送数据
                    }
                    if(Usart_Receive[9]==\'2\')
                    {
                        kaiguan = 0;
                        delay1s500ms();
                        kaiguan = 1;
                        delay1s();
                        for(i = 0 ; i<20; i++)
                        {
                            Usart_Receive[i]=\' \';
                        }
                        //ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        //delay1s();
                        //ESP8266_Set("2");//向服务器发送数据
                        
                        //delay1s();
                        //ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        //delay1s();
                        //ESP8266_Set("2");//向服务器发送数据
                    }
                    if(Usart_Receive[9]==\'3\')
                    {
                        led1 = 0;
                        delay1s();
                        delay1s();
                        //ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        //delay1s();
                        //ESP8266_Set("3");//向服务器发送数据
                        
                        //ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        //delay1s();
                        //ESP8266_Set("3");//向服务器发送数据
                    }
                    if(Usart_Receive[9]==\'4\')
                    {
                        led1 = 1;
                        delay1s();
                        delay1s();
                        /*
                        ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        delay1s();
                        ESP8266_Set("4");//向服务器发送数据
                        
                        ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        delay1s();
                        ESP8266_Set("4");//向服务器发送数据*/
                    }
                    if(Usart_Receive[9]==\'5\')
                    {
                        led2 = 0;
                        delay1s();
                        delay1s();
                        /*
                        ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        delay1s();
                        ESP8266_Set("5");//向服务器发送数据
                        
                        ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        delay1s();
                        ESP8266_Set("5");//向服务器发送数据/*/
                    }
                    if(Usart_Receive[9]==\'6\')
                    {
                        led2 = 1;
                        delay1s();
                        delay1s();
                        /*
                        ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        delay1s();
                        ESP8266_Set("6");//向服务器发送数据
                        
                        ESP8266_Set("AT+CIPSEND=0,1\\r\\n");
                        delay1s();
                        ESP8266_Set("6");//向服务器发送数据*/
                    }
                }
            }
                //由于消息的开头是+IP  故做此判断,111111111111111111111号
            if((Usart_Receive[0]==\'+\')&&(Usart_Receive[1]==\'I\')&&(Usart_Receive[2]==\'P\'))
            {
                if((Usart_Receive[3]==\'D\')&&(Usart_Receive[5]==\'1\')&&(Usart_Receive[6]==\',\'))
                {
                    if(Usart_Receive[9]==\'1\')
                    {
                        kaiguan = 0;
                        delay1s500ms();
                        kaiguan = 1;
                        delay1s();
                        for(i = 0 ; i<20; i++)
                        {
                            Usart_Receive[i]=\' \';
                        }
                    }
                    if(Usart_Receive[9]==\'2\')
                    {
                        kaiguan = 0;
                        delay1s500ms();
                        kaiguan = 1;
                        delay1s();
                        for(i = 0 ; i<20; i++)
                        {
                            Usart_Receive[i]=\' \';
                        }
                        
                    }
                    if(Usart_Receive[9]==\'3\')
                    {
                        led1 = 0;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'4\')
                    {
                        led1 = 1;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'5\')
                    {
                        led2 = 0;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'6\')
                    {
                        led2 = 1;
                        
                        delay1s();delay1s();
                    }
                }
            }
                //由于消息的开头是+IP  故做此判断,222222222222222号
            if((Usart_Receive[0]==\'+\')&&(Usart_Receive[1]==\'I\')&&(Usart_Receive[2]==\'P\'))
            {
                if((Usart_Receive[3]==\'D\')&&(Usart_Receive[5]==\'2\')&&(Usart_Receive[6]==\',\'))
                {
                    if(Usart_Receive[9]==\'1\')
                    {
                        kaiguan = 0;
                        delay1s500ms();
                        kaiguan = 1;
                        delay1s();
                        for(i = 0 ; i<20; i++)
                        {
                            Usart_Receive[i]=\' \';
                        }
                    }
                    if(Usart_Receive[9]==\'2\')
                    {
                        kaiguan = 0;
                        delay1s500ms();
                        kaiguan = 1;
                        delay1s();
                        for(i = 0 ; i<20; i++)
                        {
                            Usart_Receive[i]=\' \';
                        }
                    }
                    if(Usart_Receive[9]==\'3\')
                    {
                        led1 = 0;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'4\')
                    {
                        led1 = 1;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'5\')
                    {
                        led2 = 0;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'6\')
                    {
                        led2 = 1;
                        
                        delay1s();delay1s();
                    }
                }
            }
                //由于消息的开头是+IP  故做此判断,00000000000000000号
            if((Usart_Receive[0]==\'+\')&&(Usart_Receive[1]==\'I\')&&(Usart_Receive[2]==\'P\'))
            {
                if((Usart_Receive[3]==\'D\')&&(Usart_Receive[5]==\'3\')&&(Usart_Receive[6]==\',\'))
                {
                    if(Usart_Receive[9]==\'1\')
                    {
                        kaiguan = 0;
                        delay1s500ms();
                        kaiguan = 1;
                    delay1s();
                        for(i = 0 ; i<20; i++)
                        {
                            Usart_Receive[i]=\' \';
                        }
                    }
                    if(Usart_Receive[9]==\'2\')
                    {
                        kaiguan = 0;
                        delay1s500ms();
                        kaiguan = 1;
                        delay1s();
                        for(i = 0 ; i<20; i++)
                        {
                            Usart_Receive[i]=\' \';
                        }
                    }
                    if(Usart_Receive[9]==\'3\')
                    {
                        led1 = 0;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'4\')
                    {
                        led1 = 1;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'5\')
                    {
                        led2 = 0;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'6\')
                    {
                        led2 = 1;
                        
                        delay1s();delay1s();
                    }
                }
            }
                //由于消息的开头是+IP  故做此判断,444444444444444444号
            if((Usart_Receive[0]==\'+\')&&(Usart_Receive[1]==\'I\')&&(Usart_Receive[2]==\'P\'))
            {
                if((Usart_Receive[3]==\'D\')&&(Usart_Receive[5]==\'0\')&&(Usart_Receive[6]==\',\'))
                {
                    if(Usart_Receive[9]==\'1\')
                    {
                        kaiguan = 0;
                        delay1s500ms();
                        kaiguan = 1;
                        delay1s();
                        for(i = 0 ; i<20; i++)
                        {
                            Usart_Receive[i]=\' \';
                        }
                    }
                    if(Usart_Receive[9]==\'2\')
                    {
                        kaiguan = 0;
                        delay1s500ms();
                        kaiguan = 1;
                        delay1s();
                        for(i = 0 ; i<20; i++)
                        {
                            Usart_Receive[i]=\' \';
                        }
                    }
                    if(Usart_Receive[9]==\'3\')
                    {
                        led1 = 0;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'4\')
                    {
                        led1 = 1;
                        
                    delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'5\')
                    {
                        led2 = 0;
                        
                        delay1s();delay1s();
                    }
                    if(Usart_Receive[9]==\'6\')
                    {
                        led2 = 1;
                        
                        delay1s();delay1s();
                    }
                }
            }
            
        }
    }
}
复制代码

复制代码
#define _USART_C_
#include "include.h"
unsigned char Usart_Receive[20]={0};
unsigned char Usart_Cnt=0;
bit Usart_AT_flage;
bit flage;
bit Command_Flag;

void InitUART(void)
{
    TMOD = 0x20;
    SCON = 0x50;
    TH1 = 0xFD;
    TL1 = TH1;
    PCON = 0x00;
    EA = 1;
    ES = 1;
    TR1 = 1;
}

void Send_Uart(unsigned char value) 
{
    ES=0;  //关闭串口中断  
    TI=0;   //清发送完毕中断请求标志位   
    SBUF=value; //发送  
    while(TI==0); //等待发送完毕   
    TI=0;   //清发送完毕中断请求标志位   
    ES=1;  //允许串口中断  
}

void UARTInterrupt(void) interrupt 4
{
    if(RI)
    {
        RI=0;
        Usart_Receive[Usart_Cnt]=SBUF;//接收串口数据
        Usart_Cnt++;//
        //返回数据以回车结尾,有回车,而且数据个数大于2,说明接收到了数据
        if(Usart_Receive[Usart_Cnt-2]==\'\\r\' && Usart_Receive[Usart_Cnt-1]==\'\\n\' && Usart_Cnt >= 2)
        {
            Usart_Cnt = 0;//接收数据计数清零
            Usart_AT_flage = 1;//数据接收成功标志位
        }
        else if(Usart_Cnt > 20)//
        {
            Usart_Cnt = 0;
        }    
    }
}
复制代码

 

 

复制代码
#ifndef __USART_H_
#define __USART_H_


#ifndef _USART_C_
#define _USART_ex_ extern
#else
#define _USART_ex_
#endif

_USART_ex_ unsigned char Usart_Receive[20];
_USART_ex_ unsigned char Usart_Cnt;
_USART_ex_ bit Usart_AT_flage;
_USART_ex_ bit Command_Flag;
_USART_ex_ unsigned char UsartData;
_USART_ex_ void InitUART(void);//串口初始化
_USART_ex_ void Send_Uart(unsigned char value); 
#endif
复制代码

#include <REGX52.H>
#include "USART.h"
#include <string.h>
#include <intrins.h>

完了,,,,,,,

android代码

 

复制代码
package com.laboratory_control.yang;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;


import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

    String imei;//手机imei号
    long long_mima;
    String string_mima;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private CheckBox rememberpass;
    static Socket socket = null;//定义socket
    Button shenqing_button;//申请密钥
    Button LandingButton;//登陆按钮
    Button FarButton;//远程连接按钮
    Button NearButton;//进程连接按钮
    EditText PasswordEditText;//密码输入框
    EditText FarIpEditText;//远程ip输入框
    EditText NearIpEditText;//近程ip输入框
    EditText PortText;//定义端口输入框
    static InputStream inputStream=null;//定义输入流
    static OutputStream outputStream=null;//定义输出流
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        rememberpass = (CheckBox) findViewById(R.id.cb_mima);
        
        shenqing_button = (Button) findViewById(R.id.shenqingButton);//申请密钥
        LandingButton = (Button) findViewById(R.id.dengluButton);//登陆
        FarButton = (Button) findViewById(R.id.yuanButton);//远程按钮
        NearButton = (Button) findViewById(R.id.jinButton);//进程按钮
        
        FarIpEditText = (EditText) findViewById(R.id.IPEditText);//远程ip输入框
        NearIpEditText = (EditText) findViewById(R.id.jinIPEditText);//进程ip输入框
        PortText = (EditText) findViewById(R.id.PORTEditText);//通信端口
        PasswordEditText = (EditText) findViewById(R.id.mimaEditText);//登陆密码
        
        boolean isRemember = pref.getBoolean("cb_mima", false);//得到cb_mima文件存的值,得不到会返回false
        if (isRemember) //如果上次选择了保存密码
        {
            //Toast.makeText(MainActivity.this, "123", Toast.LENGTH_SHORT).show();
            String password = pref.getString("password", "");//取出密码
            PasswordEditText.setText(password);//把密码输入到密码输入框
            rememberpass.setChecked(true);//选中记住密码
        }
        
        shenqing_button.setOnClickListener(shenqing_buttonListener);
        FarButton.setOnClickListener(FarButtonListener);//远程监听
        NearButton.setOnClickListener(NearButtonListener);//进程监听
        LandingButton.setOnClickListener(LandingButtonListener);//登陆监听
        
        /** 
         * 获取IMEI号,IESI号,手机型号 
         */  
        getInfo();
        
        /**
         * 为完全退出应用程序而加的代码
         */
        ExitApplication.getInstance().addActivity(this);
    }
    /**
     * 申请密钥监听
     */
    private OnClickListener shenqing_buttonListener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this,"请把"+imei+"提供给管理员",Toast.LENGTH_LONG).show();
        }
    };
    /**
     * 远程按钮连接监听
     */
    private OnClickListener FarButtonListener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //启动远程连接线程
            FarConnect_Thread farconnect_Thread = new FarConnect_Thread();
            farconnect_Thread.start();
        }
    };
    /**
     * //近程按钮连接监听
     */
    private OnClickListener NearButtonListener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            NearConnect_Thread nearconnect_Thread = new NearConnect_Thread();
            nearconnect_Thread.start();
        }
    };
    /**
     * 
     * 近程连接线程
     *
     */
    class NearConnect_Thread extends Thread//继承Thread
    {
        public void run()//重写run方法
        {
            try 
            {
                if(true)
                //if (socket == null) 
                {
                    //用InetAddress方法获取ip地址
                    InetAddress ipAddress = InetAddress.getByName(NearIpEditText.getText().toString());
                    int port =Integer.valueOf(PortText.getText().toString());//获取端口号 
                    socket = new Socket(ipAddress, port);//创建连接地址和端口    
                    if (socket !=null) 
                    {
                        runOnUiThread(new Runnable()
                        {
                            public void run() 
                            {    
                                // TODO Auto-generated method stub
                                Toast.makeText(MainActivity.this,"已成功连接!", Toast.LENGTH_SHORT).show();
                            }                        
                        });
                    }
                }
                /*
                else
                {
                    runOnUiThread(new Runnable()
                    {
                        public void run() 
                        {    
                            // TODO Auto-generated method stub
                            Toast.makeText(MainActivity.this,"成功连接!输入密码后即可登陆", Toast.LENGTH_SHORT).show();
                        }                        
                    });
                }*/
            } 
            catch (Exception e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    /**
     * 
     * 远程连接线程
     *
     */
        
    class FarConnect_Thread extends Thread//继承Thread
    {
        public void run()//重写run方法
        {
            try 
            {
                if(true)
                //if (socket == null) 
                {
                    //用InetAddress方法获取ip地址
                    InetAddress ipAddress = InetAddress.getByName(FarIpEditText.getText().toString());
                    int port =Integer.valueOf(PortText.getText().toString());//获取端口号 
                    socket = new Socket(ipAddress, port);//创建连接地址和端口    
                    if (socket !=null) 
                    {
                        runOnUiThread(new Runnable()
                        {
                            public void run() 
                            {    
                                // TODO Auto-generated method stub
                                Toast.makeText(MainActivity.this,"成功连接!输入密码后即可登陆", Toast.LENGTH_SHORT).show();
                            }                        
                        });
                    }
                }
                /*
                else
                {
                    runOnUiThread(new Runnable()
                    {
                        public void run() 
                        {    
                            // TODO Auto-generated method stub
                            Toast.makeText(MainActivity.this,"已经连接!", Toast.LENGTH_SHORT).show();
                        }                        
                    });
                }*/
            } 
            catch (Exception e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    /**
     * 登陆监听,启动控制Activity(活动)
     */
    private OnClickListener LandingButtonListener =  new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //if(true)
            String password = PasswordEditText.getText().toString();//得到输入框输入的密码
            
            if (socket !=null)//连接成功
            {
                if (PasswordEditText.getText().toString().equals(string_mima))//密码正确 
                {
                     editor = pref.edit();
                     if (rememberpass.isChecked()) 
                     {
                        editor.putBoolean("cb_mima", true);
                        editor.putString("password", password);
                     }
                     else 
                     {
                         editor.clear();
                     }
                     editor.commit();
                     Intent intent = new Intent(); 
                     intent.setClass(MainActivity.this, Control.class);
                     MainActivity.this.startActivity(intent);
                }
                else 
                {
                    runOnUiThread(new Runnable()
                    {
                        public void run() 
                        {    
                            // TODO Auto-generated method stub
                            Toast.makeText(MainActivity.this,"已连接,请输入正确密码!", Toast.LENGTH_SHORT).show();
                        }                        
                    });
                }
            } 
            else 
            {
                runOnUiThread(new Runnable()
                {
                    public void run() 
                    {    
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this,"请先连接!", Toast.LENGTH_SHORT).show();
                    }                        
                });
            }
        }
    };
    /**
     * 退出提示框
     */
    protected void dialog() { 
         AlertDialog.Builder builder = new Builder(MainActivity.this); 
         builder.setMessage("亲,确定要退出吗"); 
         builder.setTitle("提示"); 
         builder.setPositiveButton("确定", 
          new android.content.DialogInterface.OnClickListener() { 
           @Override
           public void onClick(DialogInterface dialog, int which) { 
           dialog.dismiss(); 
           MainActivity.this.finish(); 
           } 
          }); 
         builder.setNegativeButton("取消", 
          new android.content.DialogInterface.OnClickListener() { 
           @Override
           public void onClick(DialogInterface dialog, int which) { 
           dialog.dismiss(); 
           
           } 
          }); 
         builder.create().show(); 
        } 
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) { 
         if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
          dialog(); 
          return false; 
         } 
         return false; 
        }
        /** 
         * 获取IMEI号,IESI号,手机型号 
         */  
        private void getInfo() {  
               TelephonyManager mTm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);  
               imei = mTm.getDeviceId(); 
               
               String aaaa = imei.substring(0,6);
               
               long_mima = Integer.parseInt(aaaa);
               long_mima = long_mima/3+666;//做运算当作登陆密码
               string_mima = ""+long_mima;
               
               //String imsi = mTm.getSubscriberId();  
               //String mtype = android.os.Build.MODEL; // 手机型号  
               //String mtyb= android.os.Build.BRAND;//手机品牌  
               //String numer = mTm.getLine1Number(); // 手机号码,有的可得,有的不可得  
               
               Log.i("text", "手机IMEI号:"+imei);
               Log.i("456", string_mima);
           }  
}
复制代码

复制代码
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/dd2"
    tools:context="com.laboratory_control.yang.MainActivity" >

    <!-- IP地址 -->
    <TextView
        android:text="远程连接服务器IP:"
        android:id="@+id/IPTextView"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="20dp"
        
        />
    <!-- 远程IP地址输入框 -->
    <EditText 
        android:text=""
        android:id="@+id/IPEditText"
        android:layout_height="35dp"
        android:layout_width="match_parent"
        android:layout_marginTop="25dp"
        android:background="@android:color/white"
        />
     <!-- 近程IP地址 -->
    <TextView
        android:text="近程连接服务器IP:"
        android:id="@+id/jinTextView"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="20dp"
        android:layout_marginTop="70dp"
        />
    <!-- 近程IP地址输入框 -->
    <EditText 
        android:text="192.168.4.1"
        android:id="@+id/jinIPEditText"
        android:layout_height="35dp"
        android:layout_width="match_parent"
        android:layout_marginTop="95dp"
        android:background="@android:color/white"
        />
   
    <!-- 端口号 -->
    <TextView
        android:text="通信端口号:"
        android:id="@+id/PORTTextView"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="20dp"
        android:layout_marginTop="140dp"
        />
    <!-- 端口号输入框 -->
    <EditText 
        android:text="10000"
        android:id="@+id/PORTEditText"
        android:layout_height="35dp"
        android:layout_width="fill_parent"
        android:layout_marginTop="165dp"
        android:background="@android:color/white"
        />
   <!-- 密码 -->
    <TextView 
        android:text="登陆密码:"
        android:id="@+id/mimaTextView"
        android:layout_height="wra

以上是关于转载android客服端+eps8266+单片机+路由器之远程控制系统的主要内容,如果未能解决你的问题,请参考以下文章

Python网络编程UDP服务器与客服端简单例子

哪为IT大神帮个忙,Android客服端如何能够控制视图层监听的数据传输到服务端?

(4)优化TCP编写 客服端上传图片,服务端给客服端发消息问好

CXF 客服端调用报错

升讯威在线客服系统客服端英文界面的实现方法

升讯威在线客服系统客服端英文界面的实现方法