I have an Ardunio UNO (not sure if needed)
I have an ESP8266 NODE MCU unit
I have a NEO6MV2 GPS module
Basically, I want to receive a location via my GPS module, and then fill in a table in an SQL database that I created on xampp/phpmyadmin with the data received via GPS. I need a loop, so it keeps receiving the data and updating the table with new locations.
How might I go about this?
The ultimate aim would be to then present this as a marker on a google API on a website - I've built the second phase (my website takes from the sql database and presents marker on API) - I just need to get the data there!
If you search my name and SQL or WAMP or PHP.. you'll see many posts where I have provided sketches, php code and table set-up on how to start to logging data to a MySQL database.
What server side scripting language are you using to talk to the database? PHP? Other?
You'll need a GPS library to parse the NMEA sentences from the ublox NEO-6M device. Comparisons of the 4 major libraries can be found here (performance), here (RAM usage) and here (program size).
Regardless of which library you use, be sure to read the suggestions on the Installation and Troubleshooting pages.
Avoid the String class (use C strings, aka char arrays) and calling delay; this will get you started on a good path.
I am currently trying to write a code that outputs the latitude and longitude to the serial monitor in order to test if my GPS module and NODEMCU module are working together correctly.
Here is my code:
#include <ESP8266WiFi.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
TinyGPSPlus gps;
static const int RXPin = 4;
static const int TXPin = 5;
SoftwareSerial ss (RXPin,TXPin);
const char* ssid = "Ryan's iPhone";
const char* password = "ryanwifi";
int i;
float latitude = gps.location.lat();
float longitude = gps.location.lng();
unsigned long age = 0;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
ss.begin(9600);
// 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..");
}
Serial.println("");
Serial.println("WiFi connected");
// 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("/");
}
void checkGPS(){
if(gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
}
}
void loop() {
latitude = gps.location.lat();
longitude = gps.location.lng();
Serial.println("\n\n\nTest: ");
Serial.println(i);
Serial.println("\nNumber of satellies: ");
Serial.println(gps.satellites.value());
Serial.println("\nLatitude: ");
Serial.println(latitude);
Serial.println("\nLongitude: ");
Serial.println(longitude);
i++;
delay(1000);
}
But something isnt working correctly; the output for satellites, long and lat are all 0.
Where am I going wrong here?
Sidenote; When connecting to the nodemcu - for clarity - which pins should the TX of the GPS module go to? and the RX of the GPS module go to - on the nodemcu?
#include <ESP8266WiFi.h>
#include <NMEAGPS.h>
#include <SoftwareSerial.h>
NMEAGPS gps; // the parser
gps_fix fix; // the results of the parser, all GPS fields
uint16_t fixCount = 0;
static const int RXPin = 4;
static const int TXPin = 5;
SoftwareSerial gpsPort (RXPin,TXPin);
const char* ssid = "Ryan's iPhone";
const char* password = "ryanwifi";
unsigned long age = 0;
//WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
gpsPort.begin(9600);
// 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..");
}
Serial.println("");
Serial.println("WiFi connected");
// 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("/");
}
void checkGPS()
{
static bool warningPrinted = false;
if (not warningPrinted and (millis() > 5000) and (gps.statistics.chars < 10))
{
Serial.println(F("No GPS detected: check wiring."));
warningPrinted = true;
}
}
void loop()
{
// Check for and parse any available GPS characters
if (gps.available( gpsPort )) {
// Once per second, a complete structure of GPS fields is ready.
fix = gps.read();
// Remember how many fixes we have received. This can be
// used for a 1Hz "clock".
fixCount++;
Serial.println("\n\n\nTest: ");
Serial.println( fixCount );
Serial.println("\nNumber of satellies: ");
if (fix.valid.satellites)
Serial.println( fix.satellites );
Serial.println("\nLatitude: ");
if (fix.valid.location)
Serial.println( fix.latitude(), 6 );
Serial.println("\nLongitude: ");
if (fix.valid.location)
Serial.println( fix.longitude(), 6 );
}
checkGPS();
}
Note how it only prints when a new GPS fix has arrived, after all sentences have arrived (RMC+GGA+ etc.). Other libraries do not work that way... you will get one update for each sentence (RMC, then GGA, etc.).
Because the sketch never sits at a delay, doing nothing, the sketch can do other things will each GPS gradually arrives. 9600 is really sloooow when your processor is running at 16MHz (or faster!). Don't make the processor twiddle its thumbs with delay.
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);
}