arduino wifi无线小车原型测试代码

Xylona ·
更新时间:2024-09-21
· 530 次阅读

这几天倒腾手机app控制arduino wifi小车,测试代码写完了,做个记录防止忘记。

/****wifi无线遥控车测试代码 基本实现 1 基于ESP8266的uno与esp8266以及手机的通信(信号传递) 2 舵机控制对舵机的控制 3 对LN2003步进驱动的控制(有问题需要调试) 4手机与esp8266采用的tcp协议通信,可使用java 实现桌面以及android客户端,也可网上下载tcp网络调试app ****/ #include //舵机控制 #include //步进电机控制 #include //软串口 因为 uno实际 tx(0脚) Rx(1脚)就各一个,为了不在调试的时候出现冲突,就使用软串口 #define DEBUG true //boolean变量 #define STEPS 64 //电机一圈所规定得步数,自己去取摸索 Stepper stepper(STEPS, 2, 3, 4, 5); //给ln2003驱动指定四个输入引脚 SoftwareSerial mySerial(13, 12); // RX, TX //软串口 13 12相当于功能转移 String data = ""; //接收到手机发过来的数据 int flag = 0; //接受完毕的信号 int LED = 9; //9引脚接led灯的正极 void inits(); //函数使用前的申明 Servo myservo; //duoji //定义舵机 void servoSport(Servo myservo, char deg); //函数使用前的申明 void motorSport(char dir); //函数使用前的申明 void setup() { Serial.begin(115200); //开始,设置波特率 115200 esp8266 默认的波特率 Serial.println("Hello,serial!"); mySerial.begin(115200); //设置波特率 mySerial.println("AT"); pinMode(LED, OUTPUT); //点亮led灯前的申明 inits(); //esp8266的初始化操作,为了防止断电后无法自动开启tcp服务 myservo.attach(7);//7controller servo //设置引脚7为了控制舵机 stepper.setSpeed(290);//motor //电机一圈所规定得步数,自己去取摸索 } void loop() { while (mySerial.available() > 0) //判断串口中有无数据 { data += char(mySerial.read()); //将接收到的int数据转换为char 然后追加到字符串中 delay(5); //延时 防止数据太多没有接受完毕 flag = 1; //接受完毕标志 } if (flag == 1) { Serial.println(data); Serial.println("----------start----------"); Serial.println(data[data.length() - 1]); if (data[data.length() - 1] == '1') { //判断手机发送给esp8266的tcp数据最后一位是什么 相当于控制信号,当然此处也可以做字符串截取处理,来完成更复杂的控制任务 digitalWrite(LED, HIGH); //信号为1的时候 点亮灯 servoSport(myservo, '1'); //控制舵机旋转 } else if (data[data.length() - 1] == '0') { digitalWrite(LED, LOW); servoSport(myservo, '0'); } else if (data[data.length() - 1] == '2') { motorSport('1'); //控制电机旋转 } else if (data[data.length() - 1] == '3') { motorSport('0'); } Serial.println("--------end------------"); data = ""; flag = 0; //将接受标志变为0 } if (Serial.available()) { mySerial.write(Serial.read()); } } /* 初始化esp8266 */ void inits() { sendCommand(mySerial, "AT+RST\r\n", 1000, DEBUG); sendCommand(mySerial, "AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections sendCommand(mySerial, "AT+CIPSERVER=1,8080\r\n", 1000, DEBUG); // turn on TCP server on port 8080 } /* uno 向 8266发送数据 参考官网写法 */ String sendData(SoftwareSerial esp8266, String command, const int timeout, boolean debug) { String response = ""; int dataSize = command.length(); char data[dataSize]; command.toCharArray(data, dataSize); esp8266.write(data, dataSize); // send the read character to the esp8266 if (debug) { Serial.write(data, dataSize); } long int time = millis(); while ( (time + timeout) > millis()) { while (esp8266.available()) { char c = esp8266.read(); // read the next character. response += c; } } if (debug) { Serial.print(response); } return response; } /* uno 向 8266发送数据 参考官网写法 */ String sendCommand(SoftwareSerial esp8266, String command, const int timeout, boolean debug) { String response = ""; esp8266.print(command); // send the read character to the esp8266 long int time = millis(); while ( (time + timeout) > millis()) { while (esp8266.available()) { // The esp has data so display its output to the serial window char c = esp8266.read(); // read the next character. response += c; } } if (debug) { Serial.print(response); } return response; } ///控制舵机转动 void servoSport(Servo myservo, char deg) { if (deg == '1') { myservo.write(90); delay(15); } else if (deg == '0') { myservo.write(0); delay(15); } } //控制电机转动 void motorSport(char dir) { if (dir == '1') { stepper.step(2048); //4步模式下旋转一周用2048 步。 delay(500); } else if (dir == '0') { stepper.step(-2048); //4步模式下旋转一周用2048 步。 delay(500); } }
作者:101tx



测试 arduino

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章