安装最新的Arduino IDE。没有图书馆与esp-8266模块或电机控制通信需要。
请检查你的波特率设置ESP8266正确的代码。
下载Arduino代码(为。INO)和你的无线路由器的SSID和yyyyy路由器密码更换xxxxx。连接Arduino板到您的计算机的USB端口和上传代码。
将USB电缆连接到Arduino板并继续下一步。
//include libraries
#include <SoftwareSerial.h>
SoftwareSerial esp8266(3, 2); //RX pin = 3, TX pin = 2
//definition of variables
#define DEBUG true //show messages between ESP8266 and Arduino in serial port
int state = 5; //define initial state of the robot (5 = stand-by)
//define motor pins
const int motor1Pin1 = 5;
const int motor1Pin2 = 6;
const int motor2Pin1 = 9;
const int motor2Pin2 = 10;
//define motor speed
int motorSpeed = 150; //motor speed (PWM)
//*****
//SETUP
//*****
void setup()
{
//set pin modes
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
//start communication
Serial.begin(9600);
esp8266.begin(9600);
sendData("AT+RST\r\n", 2000, DEBUG); //reset module
sendData("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
sendData("AT+CWJAP=\"XXXXX\",\"YYYYY\"\r\n", 2000, DEBUG); //connect wi-fi network (replace XXXXX by your Wi-Fi router SSID and YYYYY by its password
delay(5000); //wait for connecti在
SendData(“+ cifsr \r\n”、1000、调试);//显示IP地址
SendData(“+ cipmux = 1 \r\n”、1000、调试);//允许多个连接
SendData(“+ cipserver = 80 \r\n”,1000、调试);/ /在80端口启动
}
/ *********
/主回路
/ *********
无效loop()
{
Web服务器如果(ESP8266。available())/ /验证输入的数据
{
如果(ESP8266。找到(“+ IPD,”))/如果有一个消息
{
字符串msg;
ESP8266。找到(“?”; //look for the message
msg = esp8266.readStringUntil(' '); //read whole message
String command = msg.substring(0, 3); //first 3 characters = command
Serial.println(command);
//move forward
if(command == "cm1") {
state = 1;
}
//move backward
if(command == "cm2") {
state = 2;
}
//turn right
if(command == "cm3") {
state = 3;
}
//turn left
if(command == "cm4") {
state = 4;
}
//do nothing
if(command == "cm5") {
state = 5;
}
}
}
//STATE 1: move forward
if (state == 1) {
analogWrite(motor1Pin1, motorSpeed);
digitalWrite(motor1Pin2, LOW);
analogWrite(motor2Pin1, motorSpeed);
digitalWrite(motor2Pin2, LOW);
}
//STATE 2: move backward
if (state == 2) {
digitalWrite(motor1Pin1, LOW);
analogWrite(motor1Pin2, motorSpeed);
digitalWrite(motor2Pin1, LOW);
analogWrite(motor2Pin2, motorSpeed); }
//STATE 3: move right
if (state == 3) {
analogWrite(motor1Pin1, motorSpeed);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
analogWrite(motor2Pin2, motorSpeed);
}
//STATE 4: move left
if (state == 4) {
digitalWrite(motor1Pin1, LOW);
analogWrite(motor1Pin2, motorSpeed);
analogWrite(motor2Pin1, motorSpeed);
digitalWrite(motor2Pin2, LOW);
}
//STATE 5: do nothing
if (state == 5) {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
}
//*******************
//Auxiliary functions
//*******************
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp8266.available())
{
char c = esp8266.read();
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return re
响应;
代码解释:
该代码使用一个串行端口的Arduino和ESP8266之间的通信,和另一个Arduino和计算机之间的通信。一旦Arduino Uno只有一个串口,softwareseial图书馆是用来创建一个辅助端口,使用数字引脚2和3。
/ /包括图书馆
#包括<< softwareserial。H >
softwareserial ESP8266(3,2);// RX引脚= 3,TX引脚= 2
安装时,串行通信必须开始,其波特率定义(一个Arduino和串口监控,与其他匹配ESP8266速度)。注意到我ESP8266设置为9600 kbps。默认情况下,大多数这些模块来115200kbps,但softwareserial图书馆不能以这样的速度,你必须改变它的波特率工作。我的9600 kbps的不错。
在这个项目中我没有使用一个特定的库与Wi-Fi模块的通信。相反,只有以下普通AT命令(在ESP8266默认固件定义一套指令)进行:
-
在RST:复位ESP8266模块
-
在cwmode:设置模块站模式或接入点
-
在cwjap:连接Wi-Fi网络的SSID和密码了
-
在cipmux:多连接或单一的连接设置模块
-
在cipserver:开始在一个给定的端口/启动通信的Web服务器
串行。开始(9600);
ESP8266。开始(9600);
SendData(”+ RST \r\n”、2000、调试);/ /复位模块
SendData(“+ cwmode = 1 \r\n”、1000、调试);//设置站模式
发送数据(“+ cwjap =“xxxxx \”、\“yyyyy“\r\n”、2000、调试);/ /连接无线xxxxx(代替你的Wi-Fi路由器SSID和yyyyy的Wi-Fi网络密码
延迟(5000);//等待连接
SendData(”在cifsr \r\n”、1000、调试);//显示IP地址
SendData(“+ cipmux = 1 \r\n”、1000、调试);//允许多个连接
SendData(“+ cipserver = 80 \r\n”、1000、调试);启动Web服务器/ /
一个辅助功能(发送)用于发送数据(从Arduino ESP8266),读串口监控显示反应。
字符串(字符串发送命令,const int超时,布尔调试)
{
字符串响应=“”;
ESP8266。打印(命令);
时间变量= millis();
而((时间+超时)> millis())
{
而(ESP8266。available())
{
char c = ESP8266。read();
响应+ = C;
} }如果
(调试)
{
串口打印(。
使用上面的代码使得Arduino复位模块,加入一个网络,等待一段连接,并显示其IP地址和启动Web服务器。之后,主循环将开始,和单片机将等待命令。
无效loop()
{
如果(ESP8266。available())/ /验证输入的数据
{
如果(ESP8266。找到(“+ IPD,”))//如果有一个消息
{
字符串msg;
ESP8266。找到(“?”;//查找的邮件
味精= ESP8266。readstringuntil();//读整个消息
字符串命令=味精。substring(0,3);//前3个字符=命令
系列。println(命令);
/前进
(= =“如果命令CM1”){
状态= 1;}
/向后移动
如果(命令= =“厘米”){
状态= 2;}
/右转
如果(命令= =“厘米”){
状态= 3;}
/转左
如果(命令= =“CM4”){
状态= 4;}
/做什么
如果(= =“CM5”命令){
状态= 5;
定义了五种可能的命令(CM1 CM5)。每当Arduino接收的命令,在进入五个可能的状态(向前、向后移动,移动,左移和支持),并继续,直到收到不同的命令。
每个国家确定电机引脚的信号。我用digitalwrite(引脚,低)当我想设置一个PIN 0V和analogWrite(引脚,motospeed)当我想打开一个销。允许我用值来改变电机的转速,使机器人移动速度。
//STATE 1: move forward
if (state == 1) {
analogWrite(motor1Pin1, motorSpeed);
digitalWrite(motor1Pin2, LOW);
analogWrite(motor2Pin1, motorSpeed);
digitalWrite(motor2Pin2, LOW);
}
//STATE 2: move backward
if (state == 2) {
digitalWrite(motor1Pin1, LOW);
analogWrite(motor1Pin2, motorSpeed);
digitalWrite(motor2Pin1, LOW);
analogWrite(motor2Pin2, motorSpeed); }
//STATE 3: move right
if (state == 3) {
analogWrite(motor1Pin1, motorSpeed);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
analogWrite(motor2Pin2, motorSpeed);
}
//STATE 4: move left
if (state == 4) {
digitalWrite(motor1Pin1, LOW);
analogWrite(motor1Pin2, motorSpeed);
analogWrite(motor2Pin1, motorSpeed);
digitalWrite(motor2Pin2, LOW);
}
//STATE 5: do nothing
if (state == 5) {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
注意,发动机工作3~6V。一旦一个5V电源使用,可以调节电机的平均电压(PWM)在3~5V,从而改变它的速度。它不会让你很好的控制机器人的速度。