Advertisement
Utshaw

GPS_GSM_merged

Jul 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. #include<SoftwareSerial.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include <TinyGPS++.h>
  5.  
  6. static const uint32_t GPSBaud = 9600;
  7.  
  8. // The TinyGPS++ object
  9. TinyGPSPlus gps;
  10.  
  11. SoftwareSerial GPRS(7,8); //Rx , Tx
  12. SoftwareSerial GPS(9,10); //Rx , Tx
  13.  
  14. int lockPin = 12;
  15. int unlockPin = 11;
  16. unsigned char buffer[64];
  17. unsigned char bufferReader[64];
  18. int count = 0;
  19. int bufferReaderCounter = 0;
  20. float floatLat = 22.113;
  21. float floatLng = 33.991;
  22. double gpslat;
  23. double gpslng;
  24.  
  25. void(* resetFunc) (void) = 0; //declare reset function @ address 0
  26.  
  27.  
  28. void GPS_parse() // run over and over
  29. {
  30. while(1){
  31. if (GPS.available()){
  32. // Serial.write(Serial1.read());
  33.  
  34. //Serial.println("Data");
  35. gps.encode(GPS.read());
  36. if (gps.location.isUpdated()){
  37. Serial.println("------------------------------------------------------");
  38. // Latitude in degrees (double)
  39. Serial.print("Latitude= ");
  40. Serial.print(gps.location.lat(), 6);
  41. gpslat = gps.location.lat();
  42. // Longitude in degrees (double)
  43. Serial.print(" Longitude= ");
  44. Serial.println(gps.location.lng(), 6);
  45. gpslng = gps.location.lng();
  46. // Raw latitude in whole degrees
  47. Serial.print("Raw latitude = ");
  48. Serial.print(gps.location.rawLat().negative ? "-" : "+");
  49. Serial.println(gps.location.rawLat().deg);
  50. // ... and billionths (u16/u32)
  51. Serial.println(gps.location.rawLat().billionths);
  52.  
  53. // Raw longitude in whole degrees
  54. Serial.print("Raw longitude = ");
  55. Serial.print(gps.location.rawLng().negative ? "-" : "+");
  56. Serial.println(gps.location.rawLng().deg);
  57. // ... and billionths (u16/u32)
  58. Serial.println(gps.location.rawLng().billionths);
  59.  
  60. // Year (2000+) (u16)
  61. Serial.print("Year = ");
  62. Serial.println(gps.date.year());
  63. // Month (1-12) (u8)
  64. Serial.print("Month = ");
  65. Serial.println(gps.date.month());
  66. // Day (1-31) (u8)
  67. Serial.print("Day = ");
  68. Serial.println(gps.date.day());
  69.  
  70. // Raw time in HHMMSSCC format (u32)
  71. Serial.print("Raw time in HHMMSSCC = ");
  72. Serial.println(gps.time.value());
  73.  
  74. // Hour (0-23) (u8)
  75. Serial.print("Hour = ");
  76. Serial.println(gps.time.hour());
  77. // Minute (0-59) (u8)
  78. Serial.print("Minute = ");
  79. Serial.println(gps.time.minute());
  80. // Second (0-59) (u8)
  81. Serial.print("Second = ");
  82. Serial.println(gps.time.second());
  83.  
  84.  
  85. // Speed in meters per second (double)
  86. Serial.print("Speed in m/s = ");
  87. Serial.println(gps.speed.mps());
  88.  
  89. // Altitude in feet (double)
  90. Serial.print("Altitude in feet = ");
  91. Serial.println(gps.altitude.feet());
  92.  
  93. // Number of satellites in use (u32)
  94. Serial.print("Number os satellites in use = ");
  95. Serial.println(gps.satellites.value());
  96. Serial.println("------------------------------------------------------");
  97.  
  98. delay(1000);
  99. break;
  100. }
  101. }
  102. }
  103. }
  104.  
  105. void GSMinit(){
  106. floatLat++;
  107. floatLng++;
  108. //Serial.println("In GSMInit");
  109. GPS_parse();
  110. char charLat[10];
  111. char charLng[10];
  112.  
  113. char main[100];
  114. Serial.println(gpslat);
  115. //temporarily holds data from vals
  116.  
  117. //4 is mininum width, 3 is precision; float value is copied onto buff
  118. dtostrf(gpslat, 6, 5, charLat);
  119. dtostrf(gpslng, 6, 5, charLng);
  120.  
  121. char * writeQuery = "AT+HTTPPARA=\"URL\",\"http://198.211.96.87/v1/index_Cycle.php/gpsloc?lat=";
  122. strcpy(main,writeQuery);
  123. strcat(main,charLat);
  124. strcat(main,"&lng=");
  125. strcat(main, charLng);
  126. strcat(main,"&bk=1\"\r\n");
  127.  
  128. Serial.write(main);
  129.  
  130. //char * writeQuery = "AT+HTTPPARA=\"URL\",\"http://198.211.96.87/v1/index_Cycle.php/gpsloc?lat=29.64&lng=11.33&bk=1\"\r\n";
  131. GPRS.write(main);
  132. delay(500);
  133.  
  134. GPRS.write("AT+CREG?\r\n");
  135. delay(500);
  136.  
  137. GPRS.write("AT+CGATT=1\r\n"); // Attach to GPRS
  138. delay(500);
  139.  
  140. GPRS.write("AT+SAPBER=3,1,\"APN\",\"gpinternet\"\r\n"); // To query the GPRS context
  141. delay(200);
  142.  
  143. GPRS.write("AT+SAPBR=1,1\r\n"); // Open a GPRS context
  144. delay(500);
  145.  
  146. GPRS.write("AT+HTTPACTION=1\r\n");
  147. delay(1000);
  148.  
  149.  
  150. while(GPRS.available())
  151. {
  152. buffer[count++] = GPRS.read();
  153. if(count == 64) break;
  154. }
  155. //Serial.println(buffer);
  156. Serial.write(buffer , count);
  157. clearBufferArray();
  158. count = 0;
  159. Serial.write("\r\n ... ... \r\n");
  160.  
  161. }
  162.  
  163.  
  164. void setup() {
  165.  
  166. pinMode(LED_BUILTIN, OUTPUT);//blink led for testing
  167. pinMode(lockPin, OUTPUT);
  168. pinMode(unlockPin, OUTPUT);
  169.  
  170. // put your setup code here, to run once:
  171. GPRS.begin(9600);
  172. GPS.begin(9600);
  173.  
  174. Serial.begin(9600);
  175.  
  176. GPRS.write("AT\r\n");
  177. delay(200);
  178.  
  179. GPRS.write("AT+HTTPINIT\r\n");
  180. delay(200);
  181.  
  182. GPRS.write("AT+HTTPPARA=\"CID\",1\r\n");
  183. delay(500);
  184.  
  185.  
  186.  
  187. }
  188.  
  189. void loop() {
  190. // put your main code here, to run repeatedly:
  191. GSMinit();
  192. while(1){
  193.  
  194. Serial.println("MASTER SANDWICH----------------------------------");
  195. count=0;
  196. bufferReaderCounter = 0;
  197.  
  198. while(GPRS.available())
  199. {
  200. //Serial.println("Available --> ");
  201. //buffer[count++] = GPRS.read();
  202. //Serial.println(buffer[count-1]);
  203. if(GPRS.read() == '+'){
  204. while(GPRS.available()){
  205. buffer[count++] = GPRS.read();
  206. Serial.println(buffer[count-1]);
  207. }
  208. }
  209. else{
  210. continue;
  211. }
  212. if(count == 64) break;
  213. }
  214.  
  215.  
  216. char *p;
  217. p = strtok(buffer, ",");
  218. p = strtok(NULL, ",");
  219. p = strtok(NULL, ",");
  220.  
  221.  
  222.  
  223. if(buffer[0] != NULL){
  224. //int i;
  225. //sscanf(p, "%d", &i);
  226. Serial.print("Found buffer array size: ");
  227. //Serial.println(i);
  228. GPRS.flush();
  229.  
  230. char * readContent = "AT+HTTPREAD=0,";
  231. strcat(p, "\r\n");
  232. strcat(readContent, p);
  233. Serial.print("Before write");
  234. GPRS.write(readContent);
  235. delay(500);
  236.  
  237.  
  238. char flag = 'f';
  239. while(flag == 'f' && !GPRS.available()){
  240.  
  241. }
  242. flag = 't';
  243.  
  244. char temp[20];
  245. char newTemp[33];
  246. int ntemp = 0;
  247. int itemp = 0;
  248. while(GPRS.available()){
  249. char rr = GPRS.read();
  250. if(rr >= 'a' && rr <= 'z'){
  251. newTemp[ntemp++] = rr;
  252. }
  253. }
  254.  
  255.  
  256. newTemp[ntemp] = '\0';
  257. temp[itemp] = '\0';
  258. bufferReader[bufferReaderCounter] = '\0';
  259.  
  260. Serial.println("DEF");
  261. Serial.write(bufferReader,bufferReaderCounter);
  262. Serial.println("DEF");
  263. char* pch = strstr(bufferReader,"unlock"); // find "unlock" inside bufferReader
  264.  
  265.  
  266.  
  267. if( strstr(bufferReader,"unlock") != NULL){
  268. Serial.println("Got unlock inside bufferedReader ");
  269. digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
  270. delay(1000); // wait for a second
  271. digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
  272. delay(1000); // wait for a second
  273. }
  274.  
  275. Serial.println("ABCDEF");
  276. Serial.write(bufferReader,bufferReaderCounter);
  277. Serial.println("ABCDEF");
  278. Serial.println(temp);
  279. Serial.println("ABCDEF");
  280. if(temp[0] == 'u'){
  281. Serial.println("Unlock hoiseeeeeeee");
  282. }else if(temp[0] == 'l'){
  283. Serial.println("Lock hoiseeeeeeee");
  284. }else if(temp[0] == 'n'){
  285. Serial.println("No changed hoiseeeeeeee");
  286. }
  287. Serial.println("ABCDEF");
  288.  
  289. Serial.println("SANDWICH");
  290. if(strcmp(temp, "unlock") == 0){
  291. Serial.println("Sandwiched unlock");
  292. digitalWrite(unlockPin, HIGH);
  293. digitalWrite(lockPin, LOW);
  294.  
  295. }else if(strcmp(temp, "lock") == 0){
  296. Serial.println("Sandwiched lock");
  297.  
  298. digitalWrite(unlockPin, LOW);
  299. digitalWrite(lockPin, HIGH);
  300.  
  301.  
  302. }else if(strcmp(temp, "nock") == 0){
  303. Serial.println("Sandwiched nock");
  304. }
  305. Serial.println("SANDWICH");
  306.  
  307.  
  308.  
  309.  
  310. Serial.println("EXTRA SANDWICH");
  311. Serial.write(newTemp, ntemp);
  312. if(strcmp(newTemp, "unlock") == 0){
  313. Serial.println("Sandwiched unlock");
  314. digitalWrite(unlockPin, HIGH);
  315. digitalWrite(lockPin, LOW);
  316. break;
  317.  
  318. }else if(strcmp(newTemp, "lock") == 0){
  319. Serial.println("Sandwiched lock");
  320.  
  321. digitalWrite(unlockPin, LOW);
  322. digitalWrite(lockPin, HIGH);
  323. break;
  324.  
  325. }else if(strcmp(newTemp, "nock") == 0){
  326. Serial.println("Sandwiched nock");
  327. break;
  328. }
  329. Serial.println("EXTRA SANDWICH");
  330.  
  331. Serial.println("-----T ");
  332. Serial.println("A----");
  333. //clearBufferReaderArray();
  334. //bufferReaderCounter = 0;
  335.  
  336. }
  337. Serial.write(buffer , count);
  338. // Serial.println(i);
  339. clearBufferArray();
  340. count = 0;
  341.  
  342. if(Serial.available()){
  343. //Serial.println("Got data...");
  344. byte b = Serial.read();
  345. if( b == '*')
  346. GPRS.write(0x1a);
  347. else
  348. GPRS.write(b);
  349. }
  350.  
  351. Serial.println("MASTER SANDWICH END----------THE END :( --------------------------------");
  352. }
  353.  
  354. delay(6000);
  355. resetFunc();
  356. }
  357.  
  358. void clearBufferArray()
  359. {
  360. for(int i = 0; i< count ; i++)
  361. {
  362. buffer[i] = NULL;
  363.  
  364. }
  365.  
  366. }
  367.  
  368.  
  369.  
  370. void clearBufferReaderArray()
  371. {
  372. for(int i = 0; i< bufferReaderCounter ; i++)
  373. {
  374. bufferReader[i] = NULL;
  375.  
  376. }
  377.  
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement