Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TinyGPSPlus.h>
- #include <WiFi.h>
- #include <HTTPClient.h>
- /*
- This sample code demonstrates the normal use of a TinyGPSPlus (TinyGPSPlus) object.
- It requires the use of SoftwareSerial, and assumes that you have a
- 4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
- */
- int validFlag = 0;
- const char* ssid = "network";
- const char* password = "123456789";
- static const uint32_t GPSBaud = 9600;
- const char* SERVER_NAME = "http://team3ie20.atwebpages.com/gpsdata.php";
- //ESP32_API_KEY is the exact duplicate of, ESP32_API_KEY in config.php file
- //Both values must be same
- String ESP32_API_KEY = "Ad5F10jkBM0";
- // The TinyGPSPlus object
- TinyGPSPlus gps;
- unsigned long previousMillis = 0;
- long interval = 30000;
- // The serial connection to the GPS device
- void setup()
- {
- Serial.begin(115200);
- Serial2.begin(GPSBaud);
- Serial.println(F("FullExample.ino"));
- Serial.println(F("An extensive example of many interesting TinyGPSPlus features"));
- Serial.print(F("Testing TinyGPSPlus library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
- Serial.println(F("by Mikal Hart"));
- Serial.println();
- Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
- Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"));
- Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
- WiFi.begin(ssid, password);
- Serial.println("Connecting");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to WiFi network with IP Address: ");
- Serial.println(WiFi.localIP());
- }
- void loop()
- {
- if (WiFi.status() == WL_CONNECTED) {
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis > interval) {
- previousMillis = currentMillis;
- //Send an HTTP POST request every 30 seconds
- sendGpsToServer();
- }
- }
- //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
- else {
- Serial.println("WiFi Disconnected");
- }
- static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
- printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
- printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
- printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
- printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
- printInt(gps.location.age(), gps.location.isValid(), 5);
- printDateTime(gps.date, gps.time);
- printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
- printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
- printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
- printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
- unsigned long distanceKmToLondon =
- (unsigned long)TinyGPSPlus::distanceBetween(
- gps.location.lat(),
- gps.location.lng(),
- LONDON_LAT,
- LONDON_LON) / 1000;
- printInt(distanceKmToLondon, gps.location.isValid(), 9);
- double courseToLondon =
- TinyGPSPlus::courseTo(
- gps.location.lat(),
- gps.location.lng(),
- LONDON_LAT,
- LONDON_LON);
- printFloat(courseToLondon, gps.location.isValid(), 7, 2);
- const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
- printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
- printInt(gps.charsProcessed(), true, 6);
- printInt(gps.sentencesWithFix(), true, 10);
- printInt(gps.failedChecksum(), true, 9);
- Serial.println();
- smartDelay(1000);
- if (millis() > 5000 && gps.charsProcessed() < 10)
- Serial.println(F("No GPS data received: check wiring"));
- }
- // This custom version of delay() ensures that the gps object
- // is being "fed".
- static void smartDelay(unsigned long ms)
- {
- unsigned long start = millis();
- do
- {
- while (Serial2.available())
- gps.encode(Serial2.read());
- } while (millis() - start < ms);
- }
- static void printFloat(float val, bool valid, int len, int prec)
- {
- if (!valid)
- {
- while (len-- > 1)
- Serial.print('*');
- Serial.print(' ');
- }
- else
- {
- Serial.print(val, prec);
- int vi = abs((int)val);
- int flen = prec + (val < 0.0 ? 2 : 1); // . and -
- flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
- for (int i = flen; i < len; ++i)
- Serial.print(' ');
- }
- smartDelay(0);
- }
- static void printInt(unsigned long val, bool valid, int len)
- {
- char sz[32] = "*****************";
- if (valid)
- sprintf(sz, "%ld", val);
- sz[len] = 0;
- for (int i = strlen(sz); i < len; ++i)
- sz[i] = ' ';
- if (len > 0)
- sz[len - 1] = ' ';
- Serial.print(sz);
- smartDelay(0);
- }
- static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
- {
- if (!d.isValid())
- {
- Serial.print(F("********** "));
- }
- else
- {
- char sz[32];
- sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
- Serial.print(sz);
- }
- if (!t.isValid())
- {
- Serial.print(F("******** "));
- }
- else
- {
- char sz[32];
- sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
- Serial.print(sz);
- }
- printInt(d.age(), d.isValid(), 5);
- smartDelay(0);
- }
- static void printStr(const char *str, int len)
- {
- int slen = strlen(str);
- for (int i = 0; i < len; ++i)
- Serial.print(i < slen ? str[i] : ' ');
- smartDelay(0);
- }
- int sendGpsToServer()
- {
- //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
- //Can take up to 60 seconds
- boolean newData = false;
- for (unsigned long start = millis(); millis() - start < 2000;) {
- while (Serial2.available()) {
- if (gps.encode(Serial2.read())) {
- if (gps.location.isValid() == 1) {
- validFlag = 1;
- newData = true;
- break;
- }
- else {
- validFlag = 0;
- newData = true;
- break;
- }
- }
- }
- }
- //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
- //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
- //If newData is true
- if (true) {
- newData = false;
- //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
- String latitude, longitude;
- //float altitude;
- //unsigned long date, time, speed, satellites;
- if (validFlag == 1) {
- latitude = String(gps.location.lat(), 6); // Latitude in degrees (double)
- longitude = String(gps.location.lng(), 6); // Longitude in degrees (double)
- }
- else{
- latitude ="3.3";
- longitude ="3.3";
- }
- //altitude = gps.altitude.meters(); // Altitude in meters (double)
- //date = gps.date.value(); // Raw date in DDMMYY format (u32)
- //time = gps.time.value(); // Raw time in HHMMSSCC format (u32)
- //speed = gps.speed.kmph();
- Serial.print("Latitude= ");
- Serial.print(latitude);
- Serial.print(" Longitude= ");
- Serial.println(longitude);
- //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
- //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
- HTTPClient http;
- //domain name with path
- http.begin(SERVER_NAME);
- //Specify content-type header
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- //----------------------------------------------
- //HTTP POST request data
- String gps_data;
- gps_data = "api_key=" + ESP32_API_KEY;
- gps_data += "&lat=" + latitude;
- gps_data += "&lng=" + longitude;
- Serial.print("gps_data: ");
- Serial.println(gps_data);
- //----------------------------------------------
- //----------------------------------------------
- //Send HTTP POST Request and get response
- int httpResponseCode = http.POST(gps_data);
- String httpResponseString = http.getString();
- //----------------------------------------------
- //--------------------------------------------------------------------------------
- // If you need an HTTP request with a content type: text/plain
- //http.addHeader("Content-Type", "text/plain");
- //int httpResponseCode = http.POST("Hello, World!");
- // If you need an HTTP request with a content type: application/json, use the following:
- //http.addHeader("Content-Type", "application/json");
- //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
- //--------------------------------------------------------------------------------
- //----------------------------------------------
- if (httpResponseCode > 0) {
- Serial.print("HTTP Response code: ");
- Serial.println(httpResponseCode);
- Serial.println(httpResponseString);
- }
- //----------------------------------------------
- else {
- Serial.print("Error on HTTP request - Error code: ");
- Serial.println(httpResponseCode);
- Serial.println(httpResponseString);
- }
- //----------------------------------------------
- // Free resources
- http.end();
- //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment