1 、超声波测距
#define Echo D2
#define Trig D8
#define BEEP D5
void initChaoShengBo()
{
pinMode(Echo,INPUT);
pinMode(Trig,OUTPUT);
}
long getTime()
{
digitalWrite(Trig,HIGH);
delayMicroseconds(10);
digitalWrite(Trig,LOW);
return pulseIn(Echo,HIGH);//
}
void setup() {
// put your setup code here, to run once:
initChaoShengBo();
Serial.begin(115200);
pinMode(BEEP,OUTPUT);
digitalWrite(BEEP,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
//获取距离
long dis;
dis = getTime()/58;
Serial.print(dis);
Serial.println("cm");
if(dis<10)
{
digitalWrite(BEEP,LOW);
}
else
{
digitalWrite(BEEP,HIGH);
}
delay(500);
}
2、WIFI连接
连接wifi,通过手机tcp连接发送数据到womes D1,远程控制。
#include <ESP8266WiFi.h>
#define BEEP D5
char* ssid = "shao";
char* passwd = "15250057859";
int port = 8888; //端口号
WiFiServer server(port); // 设置服务器端口号
void initWifiSta()
{
WiFi.mode(WIFI_STA); // 设置STA模式
WiFi.begin(ssid, passwd); //连接网络
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(WiFi.localIP()); //通过串口打印wemos的IP地址
}
void initBeep()
{
pinMode(BEEP,OUTPUT);
digitalWrite(BEEP,HIGH);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//启动
initBeep();
initWifiSta();
server.begin();
}
void loop() {
// put your main code here, to run repeatedly:
char cmd;
WiFiClient client = server.available(); //服务初始化
while (client.connected())
{ //等待客户端连接
while (client.available() > 0)
{ //有数据到来,类似串口的那个函数
cmd = client.read();//读取数据
if(cmd =='1')
{
//Serial.println("ss hen shuai");
digitalWrite(BEEP,LOW);
}
else
{
digitalWrite(BEEP,HIGH);
// Serial.println("ss hai shi hen shuai");
}
}
}
}
3、WIFI避障小车
#include <ESP8266WiFi.h>
#define Dong1 D6
#define Dong2 D7
#define Zhuan1 D4
#define Zhuan2 D5
#define Echo D2
#define Trig D8
char* ssid = "shao";
char* passwd = "15250057859";
int port = 8888; //端口号
WiFiServer server(port); // 设置服务器端口号
void initChaoShengBo()
{
pinMode(Echo,INPUT);
pinMode(Trig,OUTPUT);
}
long getTime()
{
digitalWrite(Trig,HIGH);
delayMicroseconds(10);
digitalWrite(Trig,LOW);
return pulseIn(Echo,HIGH);//
}
void initWifiSta()
{
WiFi.mode(WIFI_STA); // 设置STA模式
WiFi.begin(ssid, passwd); //连接网络
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(WiFi.localIP()); //通过串口打印wemos的IP地址
}
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 ting()
{
digitalWrite(Dong1,LOW);
digitalWrite(Dong2,LOW);
}
void zuo()
{
digitalWrite(Zhuan1,HIGH);
digitalWrite(Zhuan2,LOW);
}
void you()
{
digitalWrite(Zhuan1,LOW);
digitalWrite(Zhuan2,HIGH);
}
void zheng()
{
digitalWrite(Zhuan1,HIGH);
digitalWrite(Zhuan2,HIGH);
}
void setup() {
// put your setup code here, to run once:
initL9110s();
initChaoShengBo();
Serial.begin(115200);
initWifiSta();
server.begin();
}
void loop() {
char cmd;
long dis;
int mark=0;
// put your main code here, to run repeatedly:
WiFiClient client = server.available(); //服务初始化
while (client.connected())
{
while (client.available() > 0)
{
cmd = client.read();
Serial.println(cmd);
dis = getTime()/58;
if(dis<15)
{
hou();
delay(200);
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 'd':
zheng();
break;
case 's':
ting();
break;
}
}
}
}
}