主控芯片
esp8266 12E 这个就不赘述了
手机端软件
TCP连接 该软件作为tcp客户端连接esp8266 关于tcp,可在《计算机网络》中查找相关内容。 软件链接: 链接:https://pan.baidu.com/s/1KV4ZzJ0RxDDP3fqRV4JeIg 提取码:09jt
esp8266程序
将下述代码上传至esp8266,重新启动esp8266。
#include <ESP8266WiFi.h>
**
const char *ssid = "cxcx";
const char *psw = "12345678";**
char ch;
const int LocalPort = 666;
WiFiServer server(LocalPort);
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print("Setting soft_AP...");
if(WiFi.softAP(ssid, psw))
Serial.print("you can try to connect with cxcx");
else
Serial.print("Failed");
Serial.println();
server.begin();
Serial.printf("APIP is %s", WiFi.softAPIP().toString().c_str());
Serial.println();
Serial.printf("端口 is:%d", LocalPort);
gpio_init();
}
void gpio_init()
{
pinMode(D1,OUTPUT);
digitalWrite(D1,LOW);
pinMode(D3,OUTPUT);
digitalWrite(D3,LOW);
pinMode(D2,OUTPUT);
digitalWrite(D2,LOW);
pinMode(D4,OUTPUT);
digitalWrite(D4,LOW);
pinMode(D5,OUTPUT);
digitalWrite(D5,LOW);
pinMode(D6,OUTPUT);
digitalWrite(D6,LOW);
pinMode(D7,OUTPUT);
digitalWrite(D7,LOW);
pinMode(D8,OUTPUT);
digitalWrite(D8,LOW);
}
void loop()
{
WiFiClient client = server.available();
if(client)
{
client.write("Hello!Thank you for connecting with me.");
while(client.connected())
{
if(client.available())
{
ch = client.read();
Serial.println(ch);
if (ch=='a')
{
digitalWrite(D1,LOW);
digitalWrite(D3,HIGH);
digitalWrite(D2,LOW);
digitalWrite(D4,HIGH);
digitalWrite(D5,LOW);
digitalWrite(D6,HIGH);
digitalWrite(D7,LOW);
digitalWrite(D8,HIGH);
client.write("前进");
}
else if (ch=='b')
{
digitalWrite(D1,HIGH);
digitalWrite(D3,LOW);
digitalWrite(D2,HIGH);
digitalWrite(D4,LOW);
digitalWrite(D5,HIGH);
digitalWrite(D6,LOW);
digitalWrite(D7,HIGH);
digitalWrite(D8,LOW);
client.write("后退");
}
else if (ch=='c')
{
digitalWrite(D1,HIGH);
digitalWrite(D3,LOW);
digitalWrite(D2,LOW);
digitalWrite(D4,HIGH);
digitalWrite(D5,HIGH);
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
digitalWrite(D8,HIGH);
client.write("左转");
}
else if (ch=='d')
{
digitalWrite(D1,LOW);
digitalWrite(D3,HIGH);
digitalWrite(D2,HIGH);
digitalWrite(D4,LOW);
digitalWrite(D5,LOW);
digitalWrite(D6,HIGH);
digitalWrite(D7,HIGH);
digitalWrite(D8,LOW);
client.write("右转");
}
else if (ch=='t')
{
digitalWrite(D1,LOW);
digitalWrite(D3,LOW);
digitalWrite(D2,LOW);
digitalWrite(D4,LOW);
digitalWrite(D5,LOW);
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
digitalWrite(D8,LOW);
client.write("停止");
}
else
{
client.write("没有此指令");
}
}
}
}
client.stop();
}
手机端连接esp8266
打开软件 点击右上角连接 显示连接成功 切换至键盘页面 关于配置按键可自行按照代码配置
总结
esp8266在AP模式下作为服务端,而手机端需要在连接其wifi的前提下通过tcp连接软件进行客户端与服务端的连接,否则,将连接失败。要确保其处在同一局域网下。
|