safwan092

Untitled

Feb 20th, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2. #include <WiFi.h>
  3. #include <HTTPClient.h>
  4. /*
  5. This sample code demonstrates the normal use of a TinyGPSPlus (TinyGPSPlus) object.
  6. It requires the use of SoftwareSerial, and assumes that you have a
  7. 4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
  8. */
  9. int validFlag = 0;
  10. const char* ssid = "network";
  11. const char* password = "123456789";
  12. static const uint32_t GPSBaud = 9600;
  13.  
  14. const char* SERVER_NAME = "http://team3ie20.atwebpages.com/gpsdata.php";
  15.  
  16. //ESP32_API_KEY is the exact duplicate of, ESP32_API_KEY in config.php file
  17. //Both values must be same
  18. String ESP32_API_KEY = "Ad5F10jkBM0";
  19. // The TinyGPSPlus object
  20. TinyGPSPlus gps;
  21. unsigned long previousMillis = 0;
  22. long interval = 30000;
  23. // The serial connection to the GPS device
  24.  
  25.  
  26. void setup()
  27. {
  28. Serial.begin(115200);
  29. Serial2.begin(GPSBaud);
  30.  
  31. Serial.println(F("FullExample.ino"));
  32. Serial.println(F("An extensive example of many interesting TinyGPSPlus features"));
  33. Serial.print(F("Testing TinyGPSPlus library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  34. Serial.println(F("by Mikal Hart"));
  35. Serial.println();
  36. Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
  37. Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"));
  38. Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
  39. WiFi.begin(ssid, password);
  40. Serial.println("Connecting");
  41. while (WiFi.status() != WL_CONNECTED) {
  42. delay(500);
  43. Serial.print(".");
  44. }
  45. Serial.println("");
  46. Serial.print("Connected to WiFi network with IP Address: ");
  47. Serial.println(WiFi.localIP());
  48. }
  49.  
  50. void loop()
  51. {
  52. if (WiFi.status() == WL_CONNECTED) {
  53. unsigned long currentMillis = millis();
  54. if (currentMillis - previousMillis > interval) {
  55. previousMillis = currentMillis;
  56. //Send an HTTP POST request every 30 seconds
  57. sendGpsToServer();
  58. }
  59. }
  60. //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
  61. else {
  62. Serial.println("WiFi Disconnected");
  63. }
  64.  
  65. static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  66.  
  67. printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  68. printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
  69. printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  70. printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  71. printInt(gps.location.age(), gps.location.isValid(), 5);
  72. printDateTime(gps.date, gps.time);
  73. printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  74. printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  75. printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  76. printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
  77.  
  78. unsigned long distanceKmToLondon =
  79. (unsigned long)TinyGPSPlus::distanceBetween(
  80. gps.location.lat(),
  81. gps.location.lng(),
  82. LONDON_LAT,
  83. LONDON_LON) / 1000;
  84. printInt(distanceKmToLondon, gps.location.isValid(), 9);
  85.  
  86. double courseToLondon =
  87. TinyGPSPlus::courseTo(
  88. gps.location.lat(),
  89. gps.location.lng(),
  90. LONDON_LAT,
  91. LONDON_LON);
  92.  
  93. printFloat(courseToLondon, gps.location.isValid(), 7, 2);
  94.  
  95. const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
  96.  
  97. printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
  98.  
  99. printInt(gps.charsProcessed(), true, 6);
  100. printInt(gps.sentencesWithFix(), true, 10);
  101. printInt(gps.failedChecksum(), true, 9);
  102. Serial.println();
  103.  
  104. smartDelay(1000);
  105.  
  106. if (millis() > 5000 && gps.charsProcessed() < 10)
  107. Serial.println(F("No GPS data received: check wiring"));
  108. }
  109.  
  110. // This custom version of delay() ensures that the gps object
  111. // is being "fed".
  112. static void smartDelay(unsigned long ms)
  113. {
  114. unsigned long start = millis();
  115. do
  116. {
  117. while (Serial2.available())
  118. gps.encode(Serial2.read());
  119. } while (millis() - start < ms);
  120. }
  121.  
  122. static void printFloat(float val, bool valid, int len, int prec)
  123. {
  124. if (!valid)
  125. {
  126. while (len-- > 1)
  127. Serial.print('*');
  128. Serial.print(' ');
  129. }
  130. else
  131. {
  132. Serial.print(val, prec);
  133. int vi = abs((int)val);
  134. int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  135. flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  136. for (int i = flen; i < len; ++i)
  137. Serial.print(' ');
  138. }
  139. smartDelay(0);
  140. }
  141.  
  142. static void printInt(unsigned long val, bool valid, int len)
  143. {
  144. char sz[32] = "*****************";
  145. if (valid)
  146. sprintf(sz, "%ld", val);
  147. sz[len] = 0;
  148. for (int i = strlen(sz); i < len; ++i)
  149. sz[i] = ' ';
  150. if (len > 0)
  151. sz[len - 1] = ' ';
  152. Serial.print(sz);
  153. smartDelay(0);
  154. }
  155.  
  156. static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
  157. {
  158. if (!d.isValid())
  159. {
  160. Serial.print(F("********** "));
  161. }
  162. else
  163. {
  164. char sz[32];
  165. sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
  166. Serial.print(sz);
  167. }
  168.  
  169. if (!t.isValid())
  170. {
  171. Serial.print(F("******** "));
  172. }
  173. else
  174. {
  175. char sz[32];
  176. sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
  177. Serial.print(sz);
  178. }
  179.  
  180. printInt(d.age(), d.isValid(), 5);
  181. smartDelay(0);
  182. }
  183.  
  184. static void printStr(const char *str, int len)
  185. {
  186. int slen = strlen(str);
  187. for (int i = 0; i < len; ++i)
  188. Serial.print(i < slen ? str[i] : ' ');
  189. smartDelay(0);
  190. }
  191.  
  192. int sendGpsToServer()
  193. {
  194. //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
  195. //Can take up to 60 seconds
  196. boolean newData = false;
  197. for (unsigned long start = millis(); millis() - start < 2000;) {
  198. while (Serial2.available()) {
  199. if (gps.encode(Serial2.read())) {
  200. if (gps.location.isValid() == 1) {
  201. validFlag = 1;
  202. newData = true;
  203. break;
  204. }
  205. else {
  206. validFlag = 0;
  207. newData = true;
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
  214.  
  215. //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
  216. //If newData is true
  217. if (true) {
  218. newData = false;
  219.  
  220. //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
  221. String latitude, longitude;
  222. //float altitude;
  223. //unsigned long date, time, speed, satellites;
  224. if (validFlag == 1) {
  225. latitude = String(gps.location.lat(), 6); // Latitude in degrees (double)
  226. longitude = String(gps.location.lng(), 6); // Longitude in degrees (double)
  227. }
  228. else{
  229. latitude ="3.3";
  230. longitude ="3.3";
  231. }
  232. //altitude = gps.altitude.meters(); // Altitude in meters (double)
  233. //date = gps.date.value(); // Raw date in DDMMYY format (u32)
  234. //time = gps.time.value(); // Raw time in HHMMSSCC format (u32)
  235. //speed = gps.speed.kmph();
  236.  
  237. Serial.print("Latitude= ");
  238. Serial.print(latitude);
  239. Serial.print(" Longitude= ");
  240. Serial.println(longitude);
  241. //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
  242.  
  243. //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
  244. HTTPClient http;
  245.  
  246. //domain name with path
  247. http.begin(SERVER_NAME);
  248.  
  249. //Specify content-type header
  250. http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  251.  
  252. //----------------------------------------------
  253. //HTTP POST request data
  254. String gps_data;
  255. gps_data = "api_key=" + ESP32_API_KEY;
  256. gps_data += "&lat=" + latitude;
  257. gps_data += "&lng=" + longitude;
  258.  
  259. Serial.print("gps_data: ");
  260. Serial.println(gps_data);
  261. //----------------------------------------------
  262.  
  263. //----------------------------------------------
  264. //Send HTTP POST Request and get response
  265. int httpResponseCode = http.POST(gps_data);
  266. String httpResponseString = http.getString();
  267. //----------------------------------------------
  268.  
  269. //--------------------------------------------------------------------------------
  270. // If you need an HTTP request with a content type: text/plain
  271. //http.addHeader("Content-Type", "text/plain");
  272. //int httpResponseCode = http.POST("Hello, World!");
  273.  
  274. // If you need an HTTP request with a content type: application/json, use the following:
  275. //http.addHeader("Content-Type", "application/json");
  276. //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
  277. //--------------------------------------------------------------------------------
  278.  
  279. //----------------------------------------------
  280. if (httpResponseCode > 0) {
  281. Serial.print("HTTP Response code: ");
  282. Serial.println(httpResponseCode);
  283. Serial.println(httpResponseString);
  284. }
  285. //----------------------------------------------
  286. else {
  287. Serial.print("Error on HTTP request - Error code: ");
  288. Serial.println(httpResponseCode);
  289. Serial.println(httpResponseString);
  290. }
  291. //----------------------------------------------
  292. // Free resources
  293. http.end();
  294. //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
  295.  
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment