Hallo. i used esp8266 and gps modul. the value data sensor can insert into local database. but when i try to post value data to 000webhost database. the value cannot insert and i got error on serial monitor "500 Internal Server Error". may you help me to solve my error?
there is my arduino ide code:
#include <TinyGPS++.h> // library for GPS module
#include <SoftwareSerial.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <SPI.h>
SoftwareSerial gpsSerial(4,5);//rx,tx The serial connection to the GPS device
TinyGPSPlus gps; // The TinyGPS++ object
//SoftwareSerial ss(4, 5); //
const char* ssid = "myssid"; //ssid of your wifi
const char* password = "mypass"; //password of your wifi
const char* host = "https://coba.000webhostapp.com";
float latitude = 20;
float longitude = 20;
WiFiClient client;
void setup(){
Serial.begin(115200);
delay(10);
Serial.begin(9600);
gpsSerial.begin(9600); // connect gps sensor
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
//server.begin();
Serial.println("Server started");
Serial.print(WiFi.localIP());
delay(1000);
Serial.println("connecting...");
}
void loop(){
while(1)
{
while (gpsSerial.available() > 0)
{ gps.encode(gpsSerial.read()); }
if (gps.location.isUpdated())
{
latitude=gps.location.lat();
longitude=gps.location.lng();
break;
}
}
Serial.print("LATITUDE="); Serial.println(latitude,6);
Serial.print("LONGITUDE="); Serial.println(longitude,6);
delay(1000);
if(!client.connect(host, 80)){
Serial.println("connection Failed");
return;
}
//Creating URI
String url = "/coba.php?";
url += "latitude=";
url += latitude;
url += "&longitude=";
url += longitude;
Serial.print("Requesting URL :");
Serial.println(url);
//This will send the request to the server and print them
client.print(String("GET /") + url + "HTTP/1.1\r\n" +
"HOST :" + host + "\r\n" +
"Connection: close \r\n\r\n");
//"Accept: *" + "/" + "*\r\n" +
//"Content-Length: " + latitude.length() + longitude.length() +"\r\n" +
//"Content-Type: aplication/x-www-form-urlencoded\r\n" +
unsigned long timeout = millis();
while(client.available() == 0){
if(millis() - timeout > 5000){
Serial.println(">>>Client Timeout");
client.stop();
return;
}
}
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("Closing Connection");
delay(10000);
}
there is my php code:
<?php
$latitude = (float) $_GET['latitude'];
$longitude = (float) $_GET['longitude'];
$servername = "localhost";
$username = "myusername";
$password = "mypass";
$database = "mydb";
try {
$connect = new PDO("$servername, $dbname", $username, $password);
// set the PDO error mode to exception
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO lokasi (latitude, longitude)
VALUES ('".$latitude."', '".$longitude."')";
// use exec() because no results are returned
$connect->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . " " . $e->getMessage();
}
$connect = null;
?>