Not able to send data from nodeMCU-8266 to localhost (database)

243 views Asked by At

When I try to send data from nodemcu it shows -1, refer bellow image [serial monitor][1] [1]: https://i.stack.imgur.com/3IokX.jpg

you can see my ip address [ip address][2] [2]: https://i.stack.imgur.com/srNo1.jpg

my post code

<?php
//Creates new record as per request
//Connect to database
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "sensor";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Database Connection failed: " . $conn->connect_error);
} else{
echo "success"."<br>";}

//Get current date and time
date_default_timezone_set('Asia/Kolkata');
$d = date("Y-m-d");
//echo " Date:".$d."<BR>";
$t = date("H:i:s");


if(!empty($_POST['sensor1']) || !empty($_POST['sensor2']))
{
    $sensorData1 = $_POST['sensor1'];
    $sensorData2 = $_POST['sensor2'];
    $sensorData3 = $_POST['sensor3'];
    $sensorData4 = $_POST['sensor4'];
    $sensorData5 = $_POST['sensor5'];
    $sensorData6 = $_POST['sensor6'];
    $sensorData7 = $_POST['sensor7'];


    $sql = "INSERT INTO logs (sensor1, sensor2,sensor3,sensor4,sensor5,sensor6,sensor7, Date, Time) 
    VALUES ('".$sensorData1."', '".$sensorData2."', '".$sensorData3."', '".$sensorData4."', 
    '".$sensorData5."', '".$sensorData6."', '".$sensorData7."', '".$d."', '".$t."')";


    if ($conn->query($sql) === TRUE) {
        echo "OK";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}




$conn->close();
?>

and my Arduino code

#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
const char *ssid = "mcu";  //ENTER YOUR WIFI ssid
const char *password = "123456789";  //ENTER YOUR WIFI password


void setup() {
connectWifi();
}


void loop() {
HTTPClient http;    //Declare object of class HTTPClient
String sensorData1,sensorData2,sensorData3,sensorData4,sensorData5,sensorData6,sensorData7, postData;
sensorData1="High";
sensorData2="High";
  sensorData3="High";
    sensorData4="High";
      sensorData5="High";
        sensorData6="High";
          sensorData7="High";
//Post Data
postData = "&sensor1=" +  sensorData1 + "&sensor2=" + sensorData2+ "&sensor3=" + sensorData3+  
"&sensor4=" + sensorData4+ "&sensor5=" + sensorData5+ "&sensor6=" + sensorData6+  "&sensor7=" + 
sensorData7;

 http.begin("http://192.168.137.1/esp8266/postData.php");              //change the ip to your 
 computer ip address
 http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type 
 header

 int httpCode = http.POST(postData);   //Send the request
 String payload = http.getString();    //Get the response payload

 Serial.println(httpCode);   //Print HTTP return code
 Serial.println(payload);    //Print request response payload

 http.end();  //Close connection

 delay(500);  //Post Data at every 5 seconds
 }
 //function to connect to wifi
 void connectWifi(){
 delay(1000);
 Serial.begin(115200);
 WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
 delay(1000);
 WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot
 WiFi.begin(ssid, password);     //Connect to your WiFi router
 Serial.println("");
 Serial.print("Connecting");
 // Wait for connection
 while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
 }
 //If connection successful show IP address in serial monitor 
 Serial.print("Connected to ");
 Serial.println(ssid);
 Serial.print("IP address: ");
 Serial.println(WiFi.localIP());  //IP address assigned to your ESP
 }

can you fin where is the mistake or missing part I am using php version: 7.4.10

0

There are 0 answers