手机wifi控制单片机C语言,单片机wifi控制app
说起单片机wifi控制app,单片机方机的资料还容易找,对于app资料网上可以找得到的资料不多,就是找到了,也就是个APP,单片机wifi控制app配套的单片机C语言程序及手机APP源代码一般的都无法找得到,资料不全,给使用带来很大困难。目前,单片机wifi控制app,目前最常见到的是安桌手机的app,其编程用得比较多的是JAVA语言写的,其开发环境是Eclipse,对于基英语基础不好的人来说,比较容易上手的就是易语言编程环境E4A。这里我就贴出一个Eclipse开发环境下写的wifi控制app,下面是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);
}
}
手机wifi控制单片机C语言,单片机wifi控制app相关推荐
- 单片机c语言 步进电机,步进电机控制(单片机C语言).doc
步进电机控制(单片机C语言) 模块二 简单应用实例调试 任务2 步进电机控制(H22) 任务要求 用单片机P1端口控制步进电机,编写程序输出脉冲序列到P1口,控制步进电机正转.反转,加速,减速. 二. ...
- 步进电机编写单4拍或4-8拍方式的汇编或c语言控制程序.,51单片机C语言和汇编控制28BYJ48步进电机程序...
本文提供三个51单片机控制步进电机正反转的程序,2个C程序,1个汇编,. 步进电机正反转程序 28BYJ48,,,,四相五线制,,用拍的脉冲... 电机先正转一周,在反转一周,停止.. 本程序利用 S ...
- 独立键盘控制风火轮c语言,单片机项目教程--C语言版(十二五)(高职高专)...
单片机项目教程--C语言版(十二五)(高职高专) 作 者:周坚 编著 出版时间:2013年03月 定 价:26.00 I S B N :9787811247817 所属分类: 大中专教材 &n ...
- 单片机io口的控制实验c语言,单片机io口控制实验报告
<单片机io口控制实验报告>由会员分享,可在线阅读,更多相关<单片机io口控制实验报告(5页珍藏版)>请在人人文库网上搜索. 1.单片机io口控制实验报告 精品文档,仅供参考单 ...
- 通过按键控制二极管c语言,单片机一个按键控制一个发光二极管
满意答案 vs2026 2013.08.31 采纳率:58% 等级:12 已帮助:13269人 以下是用C语言实现的: #include sbit KEY=P1^0; sbit LED=P1^1 ...
- sbit单片机c语言,单片机C语言开发sbit使用方法.doc
单片机 C语音开发 sbit使用方法·· 1.bit和sbit都是C51扩展的变量类型. bit和int char之类的差不多,只不过char=8位, bit=1位而已.都是变量,编译器在编译过程中分 ...
- 整点报时 单片机 c语言,单片机整点报时编程
① 通过加入若干语句,对mg及ms变量进行控制,实现每当一秒钟到来时mg自动加1:当mg=10(即经历10秒钟)时mg自动清零,同时ms自动加1:当ms=2(即经历20秒)时ms自动清零,最终实现标准 ...
- 万年历单片机c语言,单片机+lcd12864液晶万年历C程序
闹铃的界面,喇叭出现铃声. 生日倒计时提醒功能. 全部完整的源代码下载:http://www.51hei.com/bbs/dpj-20391-1.html 下面是12864.h文件:/*------- ...
- 单片机c语言实验,单片机实验C语言编程.doc
单片机实验C语言编程.doc 下载提示(请认真阅读)1.请仔细阅读文档,确保文档完整性,对于不预览.不比对内容而直接下载带来的问题本站不予受理. 2.下载的文档,不会出现我们的网址水印. 3.该文档所 ...
最新文章
- api工程IOS学习:在IOS开发中使用GoogleMaps SDK
- ​计算产业如何加速突破?鲲鹏开发者技术沙龙带来新答案
- MySQL DATE_FORMATE函数内置字符集的坑
- 3月12日云栖精选夜读:操作阿里云Kibana
- 运行Android应用时提示ADB是否存在于指定路径问题
- 把经典的ABAP webdynpro应用配置到SAP Fiori Launchpad里
- tomcat使用ssl_使用SSL和Spring Security保护Tomcat应用程序的安全
- ubuntu+eclipse+svn
- ArangoDB教程(二)-AQL语句使用,图使用,结合WEB界面端
- 《Mastering OpenCV》--3.Markless AR.无标识式AR (1)
- 广告代码(弹窗和富媒体)
- 微信小程序登录后,用户名显示微信用户,头像显示灰色,用户自己的头像和名称无法正常显示的问题(附解决方案)
- jquery判断是否按下Enter(回车)和TAB键
- 【英语学习】关于音标的汇总图分享
- 计算思维应用于计算机学科,【计算机教学论文】计算机教学中的计算思维培养(共2667字)...
- 06.设计模式之观察者模式
- java怎么修改支付宝步数_支付宝怎么修改运动步数?刷步数最新方法
- NFC模块PN532使用
- 物联网技术(基本概述说明)
- 蓝牙技术|蓝牙Mesh在照明网络上的应用