"I will come back in the evening and give all the details. I wanted to know first if anyone would be interested in helping to improve a program written in C++."
Added after 10 [hours] 1 [minutes]:
CODE FOR THE WIFI
#include
#include
#include
// WiFi network settings
const char *ssid = "NameYourNetwork";
const char *password = "PasswordYourNetwork";
// Pin settings for the L298N motor controller
const int motor1A = D1;
const int motor1B = D2;
const int motor2A = D3;
const int motor2B = D4;
WiFiServer server(80);
WiFiClient client;
void setup() {
// Initialisation of motor pins
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
// Connection to WiFi network
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to network ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Initialise server
server.begin();
}
void loop() {
// Listen for client connection
client = server.available();
if (client) {
while (client.connected()) {
// Read the first byte of data (command) sent by the remote
if (client.available()) {
char command = client.read();
executeCommand(command);
}
}
client.stop();
}
}
// Function to execute the corresponding command
void executeCommand(char command) {
switch (command) {
case 'F':
forward();
break;
case 'B':
backward();
break;
case 'L':
left();
break;
case 'R':
right();
break;
case 'S':
stop();
break;
default:
break;
}
}
// Functions controlling the movement of the car
void forward() {
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}
void backward() {
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}
void left() {
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}
void right() {
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}
void stop() {
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
}
CODE FOR PILOT
#include
#include
// WiFi network settings
const char *ssid = "NameYourNetwork";
const char *password = "PasswordYourNetwork";
// pin settings for buttons
const int button1 = D1;
const int button2 = D2;
const int button3 = D3;
const int button4 = D4;
WiFiClient client;
void setup() {
// Initialisation of button pins
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
// Connection to WiFi network
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to network ");
Serial.println(ssid);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
Serial.println("Hotspot started");
}
void loop() {
if (client.connect("address_ip_selfie", 80)) {
if (digitalRead(button1) == LOW) {
client.write('F'); // Button 1 - drive forward
} else if (digitalRead(button2) == LOW) {
client.write('B'); // Button 2 - going backwards
} else if (digitalRead(button3) == LOW) {
client.write('L'); // Button 3 - turning left
} else if (digitalRead(button4) == LOW) {
client.write('R'); // Button 4 - turn right
} else {
client.write('S'); // No buttons pressed - stop
}
delay(100); // Delay to prevent network congestion
client.stop();
}
}
The boards establish a connection with each other, but when the buttons are clicked, the motors do not respond.