I'm trying to send my latitude and longitude values to my xampp sql database.
- How do I check if my server is actually connected?
- Assuming it connects fine, why isnt my database updating with new rows?
Arduino code:
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <SPI.h>
// GPS Variables
static const int TXPin = 5; //(RX GPS -> D1(MCU)/TX(MCU)
static const int RXPin = 4; //(TX GPS -> D2(MCU)/RX(MCU)
static const uint32_t GPSBaud = 9600;
int i = 0, x = 0, loop1 = 0;
float latitude;
float longitude;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
//WiFi Variables
const char* ssid = "Pretty Fly for a Wifi";
const char* password = "!153Rolleston153!";
//WiFiServer server(80);
//Server Variables
//IPAddress ip(192,168,0,11);
const char* host = "192.168.0.11";
//char server[] = "192.168.0.11";
IPAddress server(192,168,0,11);
WiFiClient client;
void setup()
{
//Starts GPS modules
Serial.begin(115200);
ss.begin(GPSBaud);
// Connect to WiFi network
connectwifi();
// Start the server
//server.begin();
//Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
Serial.println("Setup Complete.");
}
void loop()
{
while (ss.available() < 0)
{
}
// This sketch displays information every time a new sentence is correctly encoded.
while (ss.available() > 0)
if (gps.encode(ss.read()))
{
displayInfo();
}
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while (true);
}
i++;
if (x == 10)
while (1) {
Serial.println("LOOP STUCK");
}
}
void displayInfo()
{
Serial.print(F("Location: "));
if (gps.location.isValid())
{
Serial.println("Number of Satellites: ");
Serial.println(gps.satellites.value());
latitude = gps.location.lat();
longitude = gps.location.lng();
Serial.print(gps.location.lat(), 6);
Serial.print(F(", "));
Serial.print(gps.location.lng(), 6);
if (client.connect(server, 80))
{
Serial.println("\nConnected to database...");
client.print("GET /send_data.php?");
Serial.print("GET /send_data.php?");
client.print("latitude=");
Serial.print("latitude=");
client.print(latitude);
Serial.print(latitude);
client.print("&longitude=");
Serial.print("&longitude=");
client.print(longitude);
Serial.print(longitude);
client.println(" HTTP/1.1");
client.println("Host: localhost"); // SERVER ADDRESS
client.println("Content-Type: text/php" );
client.println("Connection: close");
client.println();
client.println();
client.stop();
x++;
}
else
{
Serial.println("\nERROR - Cannot connect to Database");
}
delay(500);
}
else
{
Serial.print(F("INVALID"));
}
Serial.println();
}
void connectwifi() {
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("\nAttempting to Connect..");
}
//Acknowledge the connection
Serial.println("");
Serial.printf("\nConnected to the host : % s...", host);
}
/
PHP code:
<?php
//require("config.php");
$dbHost = "localhost";
$dbUsername = "root";
$dbPassword = '';
$dbName = "map1";
// Opens a connection to a MySQL server
$connection = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName);
if (!$connection) {
die('Not connected : Ah shit ' . mysqli_error());
}
//Set the active MySQL database
$db_selected = mysqli_select_db($connection, $dbName );
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error());
}
//$request = $_GET['request'];
//$sql = "INSTERT INTO request.request (request)
//VALUES ('$request', '$request', '$request', '$request', '$request')";
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
//TEST to see if values received.
$file = "testfile.txt";
file_put_contents($file,$latitude);
file_put_contents($file,$longitude);
$myfile = fopen("testfile.txt", "w");
fwrite($myfile,$latitude);
fwrite($myfile,"\n");
fwrite($myfile,$longitude);
fwrite($myfile,"\n");
fclose($myfile);
//INSERT into SQL database
$sql = "INSERT INTO markers (name, address, lat, lng, type)
VALUES ('GPS', 'GPS','".$GET["latitude"]."','".$_GET["longitude"]."', 'GPS')";
// VALUES ('GPSname', 'GPSaddress','$latitude','$longitude', 'GPStype')";
//
//Checks to see if new record made
if (mysqli_query($connection, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . mysqli_error($connection);
}
mysqli_close($connection);
?>
Also; is there a specific folder my arduino code document should be placed in? Do I place it in htdocs also, or does it not matter?