Wemos D1 接入wifi(STA模式)
原型
#include <ESP8266WiFi.h>
char* ssid = "gcc-tech";
char* passwd = "18650711783";
void initWifiSta()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(WiFi.localIP());
delay(500);
}
C/S模式
架设Wemos为ftp服务器
int port = 8888;
WiFiServer server(port);
void loop() {
WiFiClient client = server.available();
while (client.connected()) {
while (client.available() > 0) {
cmd = client.read();
}
}
}
L9110s步进电机控制器
通过串口来调试小车上下左右的代码
#define Dong1 D6
#define Dong2 D7
#define Zhuan1 D3
#define Zhuan2 D4
void initL9110s()
{
pinMode(Dong1,OUTPUT);
pinMode(Dong2,OUTPUT);
pinMode(Zhuan1,OUTPUT);
pinMode(Zhuan2,OUTPUT);
}
void qian()
{
digitalWrite(Dong1,HIGH);
digitalWrite(Dong2,LOW);
}
void hou()
{
digitalWrite(Dong1,LOW);
digitalWrite(Dong2,HIGH);
}
void zuo()
{
digitalWrite(Zhuan1,LOW);
digitalWrite(Zhuan2,HIGH);
}
void you()
{
digitalWrite(Zhuan1,HIGH);
digitalWrite(Zhuan2,LOW);
}
void setup()
{
initL9110s();
Serial.begin(115200);
}
void loop()
{
char cmd;
if(Serial.available() > 0)
{
cmd = Serial.read();
switch(cmd)
{
case 'q': qian();break;
case 'h': hou();break;
case 'z': zuo();break;
case 'y': you();break;
}
}
}
整体项目的电路连接
Android开发环境搭建
一. 编程软件 Eclipse AD下载,解压。 二. 安卓模拟器(夜神),适合 没安卓手机的同学,下载,双击像普通程序一样安装。
Android导入程序包
项目整合以及在安卓模拟器上运行演示
项目整合:实现小车方向控制,如果小车距离障碍物的距离小于10厘米 ,并后退一定的距离 。 Arduino完整编程代码:
#include<ESP8266WiFi.h>
#define Echo D2
#define Trig D8
#define Dong1 D6
#define Dong2 D7
#define Zhuan1 D4
#define Zhuan2 D5
char* ssid = "Tenda_D82600";
char* passwd = "123456789aa";
int port = 8888;
WiFiServer server(port);
long getTime()
{
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
return pulseIn(Echo, HIGH);
}
void initChaoShengBo()
{
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
}
void initWifiSta()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(WiFi.localIP());
}
void dzoutput() {
pinMode(Dong1, OUTPUT);
pinMode(Dong2, OUTPUT);
pinMode(Zhuan1, OUTPUT);
pinMode(Zhuan2, OUTPUT);
}
void qian() {
digitalWrite(Dong1, HIGH);
digitalWrite(Dong2, LOW);
}
void hou() {
digitalWrite(Dong2, HIGH);
digitalWrite(Dong1, LOW);
}
void ting() {
digitalWrite(Dong1, LOW);
digitalWrite(Dong2, LOW);
}
void zuo() {
digitalWrite(Zhuan1, HIGH);
digitalWrite(Zhuan2, LOW);
}
void you() {
digitalWrite(Zhuan2, HIGH);
digitalWrite(Zhuan1, LOW);
}
void zheng() {
digitalWrite(Zhuan1, LOW);
digitalWrite(Zhuan2, LOW);
}
void setup() {
dzoutput();
initChaoShengBo();
Serial.begin(115200);
initWifiSta();
server.begin();
}
void loop() {
char cmd;
long dis;
int mark = 0;
WiFiClient client = server.available();
while (client.connected()) {
while (client.available() > 0) {
cmd = client.read();
dis = getTime() / 58;
if (dis < 10) {
hou();
delay(500);
ting();
mark = 1;
} else {
mark = 0;
}
if (mark == 0) {
switch (cmd) {
case 'q':
qian();
break;
case 'h':
hou();
break;
case 'z':
zuo();
break;
case 'y':
you();
break;
case 's':
ting();
break;
case 'd':
zheng();
break;
}
}
}
}
}
java完整编程代码: ContrlActivity.java:
package com.gec.bluetoothcarlink;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class ContrlActivity extends Activity {
Button qian;
Button hou;
Button zuo;
Button you;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_contrl);
initView();
}
void initView() {
qian = (Button) findViewById(R.id.btnqian);
hou = (Button) findViewById(R.id.btnhou);
zuo = (Button) findViewById(R.id.btnzuo);
you = (Button) findViewById(R.id.btnyou);
qian.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
new NetUtils("q").sendMessage();
break;
case MotionEvent.ACTION_UP: {
new NetUtils("s").sendMessage();
break;
}
}
return false;
}
});
hou.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
new NetUtils("h").sendMessage();
break;
case MotionEvent.ACTION_UP: {
new NetUtils("s").sendMessage();
break;
}
}
return false;
}
});
zuo.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
new NetUtils("z").sendMessage();
break;
case MotionEvent.ACTION_UP: {
new NetUtils("d").sendMessage();
break;
}
}
return false;
}
});
you.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
new NetUtils("y").sendMessage();
break;
case MotionEvent.ACTION_UP: {
new NetUtils("d").sendMessage();
break;
}
}
return false;
}
});
}
}
NetUtils.java:
package com.gec.bluetoothcarlink;
import java.io.OutputStream;
import java.net.Socket;
import android.os.Handler;
public class NetUtils {
public String message;
public String reTurnMes;
public Handler handler;
public NetUtils(String message) {
this.message = message;
}
public NetUtils(String message, Handler handler) {
this.message = message;
this.handler = handler;
}
public void sendMessage() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Socket client = new Socket("192.168.1.106", 8888);
OutputStream out = client.getOutputStream();
out.write(message.getBytes());
out.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
activity_contrl_xml:
<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:background="@drawable/test"
tools:context=".ContrlActivity" >
<LinearLayout
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btnqian"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@drawable/btn_back"
android:text="前进"
android:layout_margin="10dp"
android:textColor="#ffffff" />
<Button
android:id="@+id/btnhou"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:background="@drawable/btn_back"
android:text="后退"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnzuo"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_weight="1"
android:background="@drawable/btn_back"
android:text="左转"
android:layout_margin="10dp"
android:textColor="#ffffff" />
<Button
android:id="@+id/btnyou"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:background="@drawable/btn_back"
android:text="右转"
android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
演示视频
0.56 ZMj:/ WiFi避障小车项目完成 https://v.douyin.com/Nknb8RY/ 复制此链接,打开Dou音搜索,直接观看视频!
|