基于esp266的安卓软件(TCP连接)控制小车

2021-07-20 16:05:08 浏览数 (1)

主控芯片

esp8266 12E

这个就不赘述了

esp8266 12E实物图片esp8266 12E实物图片

手机端软件

TCP连接

该软件作为tcp客户端连接esp8266

关于tcp,可在《计算机网络》中查找相关内容。

软件链接:

链接:https://pan.baidu.com/s/1KV4ZzJ0RxDDP3fqRV4JeIg

提取码:09jt

esp8266程序

将下述代码上传至esp8266,重新启动esp8266。

代码语言:javascript复制
#include <ESP8266WiFi.h>

**//设置WiFi名称和密码  const:常量
const char *ssid = "cxcx";
const char *psw  = "12345678";**
//定义ch(字符)存储手机发送的值
char ch;   
//定义端口,启动tcp服务
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();
        //client.write("已收到请稍等n");
        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

打开软件

点击右上角连接

显示连接成功

切换至键盘页面,关于配置按键可自行按照代码配置

小车图片

演示视频:

https://www.bilibili.com/video/BV1C64y1z7LS/

总结

github项目地址 : https://github.com/CXmmcx/car_wifi

esp8266在AP模式下作为服务端,而手机端需要在连接其wifi的前提下通过tcp连接软件进行客户端与服务端的连接,否则,将连接失败。要确保其处在同一局域网下。

0 人点赞