Hey, I am tring to store data in mysql database.. From ESP8266 with GPS Neo 6M.. I am Beginner. I cant see any error every thing works fine. even get httpresponcode 200 each time cant get where is the issue please help me!!
Arduino Code
#include <TinyGPS++.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <SoftwareSerial.h>
TinyGPSPlus gps;
SoftwareSerial SerialGPS(4, 5);
const char* ssid = "***";
const char* password = "***";
float Latitude , Longitude;
int year , month , date, hour , minute , second, id = 1;
String DateString , TimeString , LatitudeString , LongitudeString;
const char* serverName = "http://****/lut2ms/sensor-data.php";
String apiKeyValue = "tPmAT5Ab3j7F9";
WiFiServer server(80);
void setup()
{
Serial.begin(9600);
SerialGPS.begin(9600);
Serial.println();
Serial.print("Connecting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
void loop()
{
while (SerialGPS.available() > 0)
if (gps.encode(SerialGPS.read()))
{
if (gps.location.isValid())
{
Latitude = gps.location.lat();
LatitudeString = String(Latitude , 6);
Longitude = gps.location.lng();
LongitudeString = String(Longitude , 6);
}
if (gps.date.isValid())
{
DateString = "";
date = gps.date.day();
month = gps.date.month();
year = gps.date.year();
if (date < 10)
DateString = '0';
DateString += String(date);
DateString += " / ";
if (month < 10)
DateString += '0';
DateString += String(month);
DateString += " / ";
if (year < 10)
DateString += '0';
DateString += String(year);
}
if (gps.time.isValid())
{
TimeString = "";
hour = gps.time.hour()+ 5; //adjust UTC
minute = gps.time.minute();
second = gps.time.second();
if (hour < 10)
TimeString = '0';
TimeString += String(hour);
TimeString += " : ";
if (minute < 10)
TimeString += '0';
TimeString += String(minute);
TimeString += " : ";
if (second < 10)
TimeString += '0';
TimeString += String(second);
}
}
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + "sss" + "&location=" + LatitudeString + "&value1=" + LongitudeString + "&value2=" + DateString + "&value3=" + TimeString + "";
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
delay(30000);
}
Php Code
<?php
include "config/config.php";
$api_key_value = "tPmAT5Ab3j7F9";
$api_key= $sensor = $location = $value1 = $value2 = $value3 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$api_key = test_input($_POST["api_key"]);
if($api_key == $api_key_value) {
$sensor = test_input($_POST["sensor"]);
$location = test_input($_POST["location"]);
$value1 = test_input($_POST["value1"]);
$value2 = test_input($_POST["value2"]);
$value3 = test_input($_POST["value3"]);
$sql = "INSERT INTO SensorData (sensor, location, value1, value2, value3)
VALUES ('" . $sensor . "', '" . $location . "', '" . $value1 . "', '" . $value2 . "', '" . $value3 . "')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
else {
echo "Wrong API Key provided.";
}
}
else {
echo "No data posted with HTTP POST.";
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>