Hi everyone, I'm having some issues getting my code running and I'm not sure exactly sure what it wants me to do.
here is my code
//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 connection
sendData("AT+CIFSR\r\n", 1000, DEBUG); //show IP address
sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); //allow multiple connections
sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // start web server on port 80
}
//*********
//MAIN LOOP
//*********
void loop()
{
if (esp8266.available()) //verify incoming data
{
if (esp8266.find("+IPD,")) //if there is a message
{
String msg;
esp8266.find("?"); //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 response;
}
error messages are below
Arduino: 1.8.19 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"
sketch_mar10c:4:23: error: 'SoftwareSerial esp8266' redeclared as different kind of entity
4 | SoftwareSerial esp8266(3, 2); //RX pin = 3, TX pin = 2
| ^
In file included from C:\Users\gears\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\cores\esp8266/Arduino.h:42,
from sketch\sketch_mar10c.ino.cpp:1:
C:\Users\gears\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\cores\esp8266/core_esp8266_version.h:121:11: note: previous declaration 'namespace esp8266 { }'
121 | namespace esp8266 {
| ^~~~~~~
C:\Users\gears\Desktop\New folder\sketch_mar10c\sketch_mar10c.ino: In function 'void setup()':
sketch_mar10c:30:10: error: expected primary-expression before '.' token
30 | esp8266.begin(9600);
| ^
C:\Users\gears\Desktop\New folder\sketch_mar10c\sketch_mar10c.ino: In function 'void loop()':
sketch_mar10c:48:14: error: expected primary-expression before '.' token
48 | if (esp8266.available()) //verify incoming data
| ^
sketch_mar10c:50:16: error: expected primary-expression before '.' token
50 | if (esp8266.find("+IPD,")) //if there is a message
| ^
sketch_mar10c:53:14: error: expected primary-expression before '.' token
53 | esp8266.find("?"); //look for the message
| ^
sketch_mar10c:54:20: error: expected primary-expression before '.' token
54 | msg = esp8266.readStringUntil(' '); //read whole message
| ^
C:\Users\gears\Desktop\New folder\sketch_mar10c\sketch_mar10c.ino: In function 'String sendData(String, int, boolean)':
sketch_mar10c:130:10: error: expected primary-expression before '.' token
130 | esp8266.print(command);
| ^
sketch_mar10c:134:19: error: expected primary-expression before '.' token
134 | while (esp8266.available())
| ^
sketch_mar10c:136:23: error: expected primary-expression before '.' token
136 | char c = esp8266.read();
| ^
exit status 1
'SoftwareSerial esp8266' redeclared as different kind of entity