#include <ESP8266WiFi.h>
#define Dong1 D6
#define Dong2 D7
#define Zhuan1 D4
#define Zhuan2 D5
#define Echo D2
#define Trig D8
char* ssid = "126587"; //WiFi热点名称
char* passwd = "12345678"; //WiFi热点密码
int port = 8888;
WiFiServer server(port);//设置服务端口号
long getTime()
{
digitalWrite(Trig, HIGH); //*******************
delayMicroseconds(10); //发送一个10微秒的脉冲
digitalWrite(Trig, LOW); //************
return pulseIn(Echo, HIGH); //波传输过程echo高电平,计算高电平时间就获取波传输的时间,单位是微秒
}
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 initChaoShengBo()
{
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
}
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, LOW);
digitalWrite(Zhuan2, LOW);
}
void setup() {
// put your setup code here, to run once:
initL9110s();
initChaoShengBo();
Serial.begin(115200);
initWifiSta();
server.begin();
}
void loop() {
// put your main code here, to run repeatedly:
char cmd;
long dis;
int mark = 0;
WiFiClient client = server.available();//服务器初始化
while (client.connected()) { //等待客户端接入
while (client.available() > 0) {
cmd = client.read();
Serial.println(cmd);
dis = getTime() / 58;
if (dis < 10) {
hou();
delay(300);
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'p':
zheng();
break;
}
}
}
}
}