在前面的课程中,都是用红外的方式控制小车,在本课中,将通过wifi方式,用手机app控制小车。手机连接ESP8266 wifi模块发射的wifi信号,把控制指令发送给esp8266,esp8266再通过串口方式发送给arduino控制器,最终实现控制小车目的。
将esp8266 uart wifi shield插在arduino uno上,esp8266 uart wifi shield不仅是wifi模块同时还是arduino uno的扩展板,引出了arduino uno的所有GPIO引脚。用母对母杜邦线将L298N、循迹模块、红外接收模块与esp8266 uart wifi shield连接起来
esp8266 uart wifi shield | L298N |
D5 | ENA |
D7 | IN1 |
D8 | IN2 |
D9 | IN3 |
D10 | IN4 |
D6 | ENB |
左边循迹模块 | |
循迹模块 | esp8266 uart wifi shield |
VCC | 5V |
GND | GND |
DO | D2 |
AO | 不接 |
右边循迹模块 | |
循迹模块 | esp8266 uart wifi shield |
VCC | 5V |
GND | GND |
DO | D3 |
AO | 不接 |
esp8266 uart wifi shield | 红外接收模块 |
S | D4 |
+ | 5V |
– | GND |
将esp8266 uart wifi shield上拨码开关拨到”12″位置。
下载示例代码并解压,用arduino IDE打开lesson-4.ino,下面对部分代码作简单说明。
void do_Uart_Tick() { char Uart_Date=0; if(Serial.available()) { size_t len = Serial.available(); uint8_t sbuf[len + 1]; sbuf[len] = 0x00; Serial.readBytes(sbuf, len); //parseUartPackage((char*)sbuf); memcpy(buffUART + buffUARTIndex, sbuf, len);//ensure that the serial port can read the entire frame of data buffUARTIndex += len; preUARTTick = millis(); if(buffUARTIndex >= MAX_PACKETSIZE - 1) { buffUARTIndex = MAX_PACKETSIZE - 2; preUARTTick = preUARTTick - 200; } } if(buffUARTIndex > 0 && (millis() - preUARTTick >= 100))//APP send flag to modify the obstacle avoidance parameters { //data ready buffUART[buffUARTIndex] = 0x00; Uart_Date=buffUART[0]; buffUARTIndex = 0; } switch (Uart_Date) //serial control instructions { case '2': Drive_Status=MANUAL_DRIVE; Drive_Num=GO_ADVANCE;Serial.println("forward"); break; case '4': Drive_Status=MANUAL_DRIVE; Drive_Num=GO_LEFT; Serial.println("turn left");break; case '6': Drive_Status=MANUAL_DRIVE; Drive_Num=GO_RIGHT; Serial.println("turn right");break; case '8': Drive_Status=MANUAL_DRIVE; Drive_Num=GO_BACK; Serial.println("go back");break; case '5': Drive_Status=MANUAL_DRIVE; Drive_Num=STOP_STOP;Serial.println("stop");break; case '1': Drive_Status=AUTO_DRIVE_LF; Serial.println("line follow...");break; default:break; } }
上面的函数用于接收APP发过来的指令,同时向APP反馈信息。
step1 :用USB线将arduino 和PC连接起来,选择正确的板卡型号和端口将其烧录到arduino uno中。将esp8266 uart wifi shield上拨码开关拨到”ON”位置。esp8266 uart wifi shield与arduino之间通过串口连接在一起。
step 2:下载并安装APP
app下载链接:https://osoyoo.com/driver/car.apk.
app源码链接:https://osoyoo.com/driver/BTcar-master.zip
目前,手机app只支持安卓,并且,提供手机APP源码,可以根据自己需要,修改源码,个性化定制app。小车还支持蓝牙控制,可以自己在esp8266 uart wifi shield上安装一个蓝牙模块,通过APP与蓝牙模块通讯控制小车。
step 3 打开电池盒电源,打开手机wifi,找到名叫“DoitWIFI_Config”的热点并连接
step 4 Open app>> select WiFi mode>> then you can controller the robot car through wifi
There are two working mode: manual control and tracking. Users can switch freely among two working mode.
1) In manual control mode, you can click buttons (^) (V) (<) (>) to control the Robot car to move forward and backward, turn right and left. Meanwhile, the APP can observe the car real time movement.
2) Click “tracking” button of App to switch the current mode to tracking mode. The Robot car will move forward along the black line in white background. Meanwhile, the APP can observe the car real time movement. Press “||” button to stop moving and click other button to change Robot car working mode. To learn more about this mode, please review our lesson 3.
其余按钮为预留功能,可以自己开发。
DownLoad Url osoyoo.com