So essentially, I have my esp8266 and I am trying to open an http server, however, whenever i put the ip address of http://10.0.0.146/, it just says site cannot be reached or something along the lines of that. Basically, my project is sending commands from the ip server to my robot which will do tasks, but the main problem I have currently is actually opening the ip server. Also, it opens once in a while, but usually it just says "this site cannot be reached. Any help would be appreciated. Thank you!! (im new to the forum so lmk if i didnt post correctly)
#include "WiFiEsp.h"
#include "SoftwareSerial.h"
SoftwareSerial softserial(4, 5); // RX, TX
char ssid[] = " "; // your network SSID (name)
char pass[] = " "; // your network password
int status = WL_IDLE_STATUS;
int reqCount = 0; // Number of requests received
WiFiEspServer server(80);
void setup() {
// Initialize serial for debugging
Serial.begin(9600);
// Initialize serial for ESP module
softserial.begin(115200);
softserial.write("AT+CIOBAUD=9600\r\n");
softserial.write("AT+RST\r\n");
softserial.begin(9600);
// Initialize ESP module
WiFi.init(&softserial);
// Check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true) ;
}
// Attempt to connect to Wi-Fi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
}
Serial.println("You're connected to the network");
printWifiStatus();
// Start the web server on port 80
server.begin();
}
void loop() {
// Listen for incoming clients
WiFiEspClient client = server.available();
if (client) {
Serial.println("New client");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// If an HTTP request ends
if (c == '\n' && currentLineIsBlank) {
Serial.println("Sending response");
// Send HTML form with buttons to control the robot
client.print(
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n"
"\r\n"
"<!DOCTYPE HTML>\r\n"
"<html>\r\n"
"<h1>Control Robot</h1>\r\n"
"<form action=\"/moveForward\" method=\"GET\">\r\n"
"<input type=\"submit\" value=\"Move Forward\">\r\n"
"</form>\r\n"
"<form action=\"/stop\" method=\"GET\">\r\n"
"<input type=\"submit\" value=\"Stop\">\r\n"
"</form>\r\n"
"</html>\r\n");
// Check the incoming request for a command and send it to the robot
String request = client.readStringUntil('\r');
if (request.indexOf("/moveForward") != -1) {
sendCommandToRobot("MOVE_FORWARD");
} else if (request.indexOf("/stop") != -1) {
sendCommandToRobot("STOP");
}
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(10); // Allow time for data to be sent
client.stop();
Serial.println("Client disconnected");
}
}
void sendCommandToRobot(String command) {
// Send the command to Robot Arduino through serial (pins 1 and 0)
Serial.println(command); // Debugging output
Serial.println("Command sent to Robot: " + command);
}
void printWifiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("To see this page, open a browser to http://");
Serial.println(WiFi.localIP());
}