#include <ESP8266WiFi.h> #define Dong1 D6 #define Dong2 D7 #define Zhuan1 D4 #define Zhuan2 D5 #define Echo D2 #define Trig D8 char* ssid = "CMCC-401";//“wifi热点名称” char* passwd = "15066903226"; //”wifi热点密码” 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); // 设置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() { // put your main code here, to run repeatedly: char cmd; int mark =0; long dis; WiFiClient client =server.available();//服务器初始化 while(client.connected())//等待服务器连接 { while(client.available()>0)//有数据到来,类似串口那个函数 { cmd =client.read();//读取数据 Serial.print(cmd); dis=getTime()/58; if(dis<20) { 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 's': ting(); break; case 'd': zheng(); break; } } } } }