Thank you! I have currently managed to get my TinyGPS++ code working as required and written a working "Send_data.php" file that also works correctly, and adds rows to my database.
My challenge now, is to get my arduino sending data to the database.
Would somebody explain the basic standard footprint code for using arduino to send to a database?
Here is my code with my attempts to send to database (broken currently)
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <SPI.h>
//#include <Ethernet.h>
#include <WiFi.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;
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
char hostserver[] = "192.168.0.11";
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 20UL * 1000UL;
WiFiClient client;
void setup()
{
//Starts GPS modules
Serial.begin(115200);
ss.begin(GPSBaud);
// 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("\nAttempting to Connect..");
}
//Acknowledge the connection
Serial.println("");
Serial.println("WiFi connected to");
Serial.println(ssid);
// 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 (client.available())
{
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
Serial.println("\nTest: ");
Serial.println(1);
delay(500);
// 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++;
}
void displayInfo()
{
Serial.print(F("Location: "));
if (gps.location.isValid())
{
latitude = gps.location.lat();
longitude = gps.location.lng();
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.print(gps.location.lng(), 6);
}
else
{
Serial.print(F("INVALID"));
}
Serial.println();
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
delay(1000);
if (client.connect(hostserver, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("GET /send_data.php?latitude=");
client.print(latitude);
client.print("&longitude=");
client.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();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
delay(10000);
}