Go Down

Topic: Data not reaching the database  (Read 133 times) previous topic - next topic

Harsh1004

Jul 11, 2019, 09:44 pm Last Edit: Jul 11, 2019, 09:48 pm by Harsh1004
hii everyone i've been working on nodemcu and taking ultrasonic sensor values from serial monitor of arduino uno's serial monitor and trying to send the data from node mcu to my created database by WAMP but the values are not reaching at the database help me with that

HERE IS THE NODE MCU CODE
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
char ssid[] = "Nodemcu";
char pass[] = "yonexcarbonex";
const char* host = "192.168.43.57";
void setup() {
 
  Serial.begin(115200);

  while (!Serial) {
    ;
  }
  WiFi.begin(ssid, pass); 

  while (WiFi.status() != WL_CONNECTED) { 

    delay(500);
    Serial.println("Waiting for connection");
  }
  Serial.println("WiFi connected");
    Serial.println("IP Address");
    Serial.println(WiFi.localIP());
}
void loop() {
  if (Serial.available()) {
    Serial.read();
  }
 
  String postData;
  String val = String (Serial.read());
  if (WiFi.status() == WL_CONNECTED) {
      Serial.println("connected");
    HTTPClient http;
    http.begin("http://192.168.43.57/UltraValues/index.php");
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    postData = "inches" + val;
    //    getData = "inches" + val;
    //    Link = "http://192.168.56.1:3306/UltraValues/index.php" + getData;
    //    http.begin(Link);
    int httpCode = http.POST(postData);
    String payload = http.getString();

    Serial.println(httpCode);
    Serial.println(payload);

    http.end();
  }]
  }
  else {

    Serial.println("Error in WiFi connection");

  }

  delay(30000); 
}






HERE IS THE PHP CODE
<?php
//Creates new record as per request
    //Connect to database
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "ultravalue";
 
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Database Connection failed: " . $conn->connect_error);
    } else {
        echo "Connected";
    }
 
 
    if(!empty($_POST['val']))
    {
       $val = $_POST['val'];
       

 
       $sql = "INSERT INTO value (val)
              VALUES ('$val')";
 
      if ($conn->query($sql) === TRUE) {
          echo "OK";
      } else {
          echo "Error: " . $sql . "<br>" . $conn->error;
      }
   }else{
        echo "Value not going";
    }
 
 
   $conn->close();
?>

Thee_Captain

I would test my HTTP Server endpoint from something other than my Arduino to see that my endpoint is working as it should. Then work on the Arduino code to use that endpoint. You can use a web browser to test your endpoints with a GET request by typing the URI in the location bar.

Once you are sure your endpoint is responding properly to your requests, you can then concentrate on making your Arduino sketch properly make an HTTP request.

If you want to test a post request you can get a REST client to do more advanced HTTP requests. I use Advanced REST Client.
Throw a little karma my way. What goes around comes around.

Go Up