The esp32,localhost web server is connected as a lan network using a switch.The network has established successfully.But when i run the code it displays http request code is -1.Can anyone help me with this issue?
arduino code
#include <EthernetENC.h>
#include <Ethernet.h>
#include <SPI.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <ESP32Lib.h>
#include <lwip/tcpip.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // MAC address of ESP32
IPAddress ip(192, 168, 43, 20); // IP address of ESP32
IPAddress serverIP(192, 168, 43, 129);// IP address of the local web server
const int serverPort = 443;
const char* serverPath = "http://192.168.43.10/Hardware%20Project%20Web%20site%20(1)/Hardware%20Project%20Web%20site/input.php";
EthernetClient client;
HTTPClient httpClient;
void setup() {
//Ethernet.init(15);
tcpip_init(NULL, NULL);
Ethernet.begin(mac, ip);
Serial.begin(115200);
delay(3000);
// Wait for Ethernet to connect
Serial.println("Ethernet connected");
Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
HTTPClient http;
if (client.connect("192,168,43,10", serverPort)) {
Serial.println("Connected to server");
// Send HTTP GET request
http.begin(serverPath);
//Serial.println(http.begin(serverPath));
Serial.println("test 2");
//Serial.println("httpCode " + String(httpCode));
//int httpCode = http.GET();
int httpCode = http.GET();
Serial.println("httpCode " + httpCode);
if (httpCode == HTTP_CODE_OK) {
// Read the response as a string
String response = http.getString();
Serial.println("httpCode " + String(httpCode));
// Parse the JSON data
DynamicJsonDocument jsonDocument(1024);
deserializeJson(jsonDocument, response);
// Access the message field
const char* message = jsonDocument["message"];
// Print the message
Serial.print("Received message: ");
Serial.println(message);
} else {
Serial.print("HTTP request failed with error code: ");
Serial.println(httpCode);
}
http.end();
client.stop();
} else {
Serial.println("Failed to connect to server");
}
delay(3);
}
PHP CODE
<?php
$host="127.0.0.1:3325";
$root="root@";
$dbPass="";
$dbName="alertproject";
$conn=mysqli_connect($host,$root,$dbPass,$dbName);
//$ip = "192.168.43.15"; // Replace with the IP address of your ESP32
//$port = 80;
$message1="no message to print\n";
if(isset($_POST['submit'])){
$check=1;
$message=$_POST['message'];
$file=$_FILES['image'];
$fileName=$_FILES['image']['name'];
$fileTmpName=$_FILES['image']['tmp_name'];
$fileError=$_FILES['image']['error'];
$fileType=$_FILES['image']['type'];
$fileSize=$_FILES['image']['size'];
$user_id=$_SESSION['user_id'];
$floorNo=$_POST['floorNo'];
$fileExt= explode('.',$fileName);
$fileActExt= strtolower(end($fileExt));
$allowed = array('jpg','jpeg','png','pdf');
if($fileName!=""){
if(in_array($fileActExt,$allowed)){
if($fileError==0){
if($fileSize<1000000){
$fileNameNew= uniqid('',true).".".$fileActExt;
$fileDestination='uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName,$fileDestination);
}
else{
echo "<script> alert('ur file is too big!') </script>";
}
}
else{
echo "<script> alert('There was an error uploding ur file!') </script>";
}
}
else{
echo "<script> alert('you cannot upload files of this type!') </script>";
}
}
$stmt1=$conn->prepare("UPDATE update_message SET message=?,image=?"); //updating values in update message table
$stmt1->bind_param("ss",$message,$fileDestination);
$stmt1->execute();
$stmt=$conn->prepare("insert into message(userId,message,image,floorNo)
values(?,?,?,?)"); //inserting values in message tables
// code to pass data to esp32 board using json
//$myObj=(object)array();
//$myObj=new stdClass();
// $myObj=(object)array();
// $sql="SELECT message,image FROM update_message WHERE id=1";
// foreach($conn->query($sql) as $row){
// $myObj->message=$row['message'];
// $myObj->image=$row['image'];
// $myJSON = json_encode($myObj);
// }
// if(!empty($message)){
// $response = [
// 'message' => "jagath",
// //'image' => $image1
// ];
// // Send JSON response
// //header('Content-Type: application/json');
// echo json_encode($response);
// }
$stmt2 = $conn->prepare("SELECT message, image FROM update_message WHERE id = 1");
$stmt2->execute();
$stmt2->bind_result($message1, $image1);
$stmt2->fetch();
$stmt2->close();
// $conn->close();
$stmt->bind_param("isss",$user_id,$message,$fileDestination,$floorNo);
$stmt->execute();
//header("location:web_inform.php");//if there are no outputs or line spaces in the programme we can use header other wise following code
//echo '<script>window.location.href = "web_inform.php";</script>';
$stmt->close();
//$conn->close();
// $response = [
// 'message' => "malinda",
// //'image' => $image1
// ];
// // Send JSON response
// //header('Content-Type: application/json');
// echo json_encode($response);
// //echo '<script>window.location.href = "web_inform.php";</script>';
}
$stmt4 = $conn->prepare("SELECT message, image FROM update_message WHERE id = 1");
$stmt4->execute();
$stmt4->bind_result($message1, $image1);
$stmt4->fetch();
$stmt4->close();
$conn->close();
$response = [
'message' => $message1,
];
// // Send the response as JSON
// //header('Content-Type: application/json');
echo json_encode($response);
?
**the output**
<?php
$host="127.0.0.1:3325";
$root="root@";
$dbPass="";
$dbName="alertproject";
$conn=mysqli_connect($host,$root,$dbPass,$dbName);
//$ip = "192.168.43.15"; // Replace with the IP address of your ESP32
//$port = 80;
$message1="no message to print\n";
if(isset($_POST['submit'])){
$check=1;
$message=$_POST['message'];
$file=$_FILES['image'];
$fileName=$_FILES['image']['name'];
$fileTmpName=$_FILES['image']['tmp_name'];
$fileError=$_FILES['image']['error'];
$fileType=$_FILES['image']['type'];
$fileSize=$_FILES['image']['size'];
$user_id=$_SESSION['user_id'];
$floorNo=$_POST['floorNo'];
$fileExt= explode('.',$fileName);
$fileActExt= strtolower(end($fileExt));
$allowed = array('jpg','jpeg','png','pdf');
if($fileName!=""){
if(in_array($fileActExt,$allowed)){
if($fileError==0){
if($fileSize<1000000){
$fileNameNew= uniqid('',true).".".$fileActExt;
$fileDestination='uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName,$fileDestination);
}
else{
echo "<script> alert('ur file is too big!') </script>";
}
}
else{
echo "<script> alert('There was an error uploding ur file!') </script>";
}
}
else{
echo "<script> alert('you cannot upload files of this type!') </script>";
}
}
$stmt1=$conn->prepare("UPDATE update_message SET message=?,image=?"); //updating values in update message table
$stmt1->bind_param("ss",$message,$fileDestination);
$stmt1->execute();
$stmt=$conn->prepare("insert into message(userId,message,image,floorNo)
values(?,?,?,?)"); //inserting values in message tables
// code to pass data to esp32 board using json
//$myObj=(object)array();
//$myObj=new stdClass();
// $myObj=(object)array();
// $sql="SELECT message,image FROM update_message WHERE id=1";
// foreach($conn->query($sql) as $row){
// $myObj->message=$row['message'];
// $myObj->image=$row['image'];
// $myJSON = json_encode($myObj);
// }
// if(!empty($message)){
// $response = [
// 'message' => "jagath",
// //'image' => $image1
// ];
// // Send JSON response
// //header('Content-Type: application/json');
// echo json_encode($response);
// }
$stmt2 = $conn->prepare("SELECT message, image FROM update_message WHERE id = 1");
$stmt2->execute();
$stmt2->bind_result($message1, $image1);
$stmt2->fetch();
$stmt2->close();
// $conn->close();
$stmt->bind_param("isss",$user_id,$message,$fileDestination,$floorNo);
$stmt->execute();
//header("location:web_inform.php");//if there are no outputs or line spaces in the programme we can use header other wise following code
//echo '<script>window.location.href = "web_inform.php";</script>';
$stmt->close();
//$conn->close();
// $response = [
// 'message' => "malinda",
// //'image' => $image1
// ];
// // Send JSON response
// //header('Content-Type: application/json');
// echo json_encode($response);
// //echo '<script>window.location.href = "web_inform.php";</script>';
}
$stmt4 = $conn->prepare("SELECT message, image FROM update_message WHERE id = 1");
$stmt4->execute();
$stmt4->bind_result($message1, $image1);
$stmt4->fetch();
$stmt4->close();
$conn->close();
$response = [
'message' => $message1,
];
// // Send the response as JSON
// //header('Content-Type: application/json');
echo json_encode($response);
The Output
HTTP request failed with error code: -1
Connected to server
test 2