#include <ESP8266WiFi.h>
#define Dong1 D6
#define Dong2 D7
#define Zhuan1 D4
#define Zhuan2 D5
#define Echo D2
#define Trig D8
char* ssid = "126587";
char* passwd = "12345678";
int port = 8888;
WiFiServer server(port);
long getTime()
{
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
return pulseIn(Echo, HIGH);
}
void initWifiSta()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passwd);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(WiFi.localIP());
}
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() {
initL9110s();
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();
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;
}
}
}
}
}