Project description
Code
Source Code For ESP8266 and Arduino Based IoT Vehicle Tracking System
c_cpp
1//Program to 2 3#include<SoftwareSerial.h> 4 5#include<LiquidCrystal.h> 6 7 8SoftwareSerial Serial1(2,3); //make RX arduino line is pin 2, make TX arduino line is pin 3. 9 10SoftwareSerial gps(10,11); 11 12LiquidCrystal lcd(13,12,9,8,7,6); 13 14 15boolean No_IP=false; 16 17String IP=""; 18 19String webpage=""; 20 21int i=0,k=0; 22 23int gps_status=0; 24 25String place="<p>1. Name: [ENTER UR NAME] </p>"; 26 27String num = "<p>2. Vehicle No: IND001</p>"; 28 29String cordinat="<p>Coordinates:</p>"; 30 31String latitude=""; 32 33String logitude=""; 34 35String gpsString=""; 36 37char *test="$GPGGA"; 38 39 40void setup() 41 42{ 43 44 Serial1.begin(9600); 45 46 Serial.begin(9600); 47 48 lcd.begin(16,2); 49 50 lcd.print("Vehicle Monitoring"); 51 52 lcd.setCursor(0,1); 53 54 lcd.print(" System "); 55 56 delay(2000); 57 58 lcd.clear(); 59 60 lcd.print("Connecting...."); 61 62 delay(1000); 63 64 connect_wifi("AT",1000); 65 66 connect_wifi("AT+CWMODE=3",1000); 67 68 connect_wifi("AT+CWQAP",1000); 69 70 connect_wifi("AT+RST",5000); 71 72 FindIP(5000); 73 74 if(!No_IP) 75 76 { 77 78 Serial.println("Connecting Wifi...."); 79 80 connect_wifi("AT+CWJAP=\\"[ENTER UR SSID NAME] \\",\\"[ENTER THE PASSWORD]\\"",7000); 81 //AT+CWJAP=”wifi_username”,”wifi_password” 82 83 } 84 85 else 86 87 { 88 89 } 90 91 Serial.println("Wifi Connected"); 92 93 lcd.clear(); 94 95 lcd.print("WIFI Connected"); 96 97 delay(2000); 98 99 lcd.clear(); 100 101 lcd.setCursor(5,0); 102 103 lcd.print("IP"); 104 105 lcd.setCursor(3,1); 106 107 lcd.print("ADDRESS"); 108 109 ip_add(); 110 111 delay(2000); 112 113 connect_wifi("AT+CIPMUX=1",100); 114 115 connect_wifi("AT+CIPSERVER=1,80",100); 116 117 Serial1.end(); 118 119 lcd.clear(); 120 121 lcd.print(" GPS DATA"); 122 123 delay(2000); 124 125 gps.begin(9600); 126 127 get_gps(); 128 129 coordinate_display(); 130 131 gps.end(); 132 133 Serial1.begin(9600); 134 135} 136 137 138void loop() 139 140{ 141 142 k=0; 143 144 Serial.println("Please Refresh Ur Page"); 145 146 while(k<1000) 147 148 { 149 150 k++; 151 152 while(Serial1.available()) 153 154 { 155 156 if(Serial1.find("0,CONNECT")) 157 158 { 159 160 Serial1.end(); 161 162 gps.begin(9600); 163 164 get_gps(); 165 166 gps.end(); 167 168 Serial1.begin(9600); 169 170 Serial1.flush(); 171 172 Serial.println("Start Printing"); 173 174 Send(); 175 176 coordinate_display(); 177 178 Serial.println("Done Printing"); 179 180 delay(5000); 181 182 lcd.clear(); 183 184 lcd.print("System Ready"); 185 186 delay(1000); 187 188 k=1200; 189 190 break; 191 192 } 193 194 } 195 196 delay(1); 197 198 } 199 200} 201 202 203void FindIP(int t1) 204 205{ 206 207 int t2=millis(); 208 209 while(t2+t1>millis()) 210 211 { 212 213 while(Serial1.available()>0) 214 215 { 216 217 if(Serial1.find("WIFI GOT IP")) 218 219 { 220 221 No_IP=true; 222 223 } 224 225 } 226 227 } 228 229} 230 231 232void ip_add() 233 234{ 235 236 IP=""; 237 238 char ch=0; 239 240 while(1) 241 242 { 243 244 Serial1.println("AT+CIFSR"); 245 246 while(Serial1.available()>0) 247 248 { 249 250 if(Serial1.find("STAIP,")) 251 252 { 253 254 delay(1000); 255 256 Serial.print("IP Address:"); 257 258 while(Serial1.available()>0) 259 260 { 261 262 ch=Serial1.read(); 263 264 if(ch=='+') 265 266 break; 267 268 IP+=ch; 269 270 } 271 272 } 273 274 if(ch=='+') 275 276 break; 277 278 } 279 280 if(ch=='+') 281 282 break; 283 284 delay(1000); 285 286 } 287 288 lcd.clear(); 289 290 lcd.print(IP); 291 292 lcd.setCursor(0,1); 293 294 lcd.print("Port: 80"); 295 296 Serial.print(IP); 297 298 Serial.print("Port:"); 299 300 Serial.println(80); 301 302 delay(1000); 303 304} 305 306 307void connect_wifi(String cmd, int t) 308 309{ 310 311 int temp=0,i=0; 312 313 while(1) 314 315 { 316 317 Serial.println(cmd); 318 319 Serial1.println(cmd); 320 321 while(Serial1.available()>0) 322 323 { 324 325 if(Serial1.find("OK")) 326 327 { 328 329 i=8; 330 331 } 332 333 } 334 335 delay(t); 336 337 if(i>5) 338 339 break; 340 341 i++; 342 343 } 344 345 if(i==8) 346 347 { 348 349 Serial.println("OK"); 350 351 } 352 353 else 354 355 { 356 357 Serial.println("Error"); 358 359 } 360 361 delay(1000); 362 363} 364 365 366void gps_Function() 367 368{ 369 370 gpsString=""; 371 372 while(1) 373 374 { 375 376 while (gps.available()>0) //Serial incoming data from GPS 377 378 { 379 380 char inChar = (char)gps.read(); 381 382 gpsString+= inChar; //store incoming data from GPS to temporary string str[] 383 384 i++; 385 386 if (i < 7) 387 388 { 389 390 if(gpsString[i-1] != test[i-1]) //check for right string 391 392 { 393 394 i=0; 395 396 gpsString=""; 397 398 } 399 400 } 401 402 if(inChar=='\r') 403 404 { 405 406 if(i>65) 407 408 { 409 410 gps_status=1; 411 412 break; 413 414 } 415 416 else 417 418 { 419 420 i=0; 421 422 } 423 424 } 425 426 } 427 428 if(gps_status) 429 430 break; 431 432 } 433 434} 435 436 437void get_gps() 438 439{ 440 441 gps_status=0; 442 443 int x=0; 444 445 while(gps_status==0) 446 447 { 448 449 gps_Function(); 450 451 int str_lenth=i; 452 453 latitude=""; 454 455 logitude=""; 456 457 coordinate2dec(); 458 459 i=0;x=0; 460 461 str_lenth=0; 462 463 } 464 465} 466 467 468void coordinate_display() 469 470{ 471 472 lcd.clear(); 473 474 lcd.print("LATITUTE:"); 475 476 lcd.print(latitude); 477 478 lcd.setCursor(0,1); 479 480 lcd.print("LONGITUTE:"); 481 482 lcd.print(logitude); 483 484 Serial.print("Latitude:"); 485 486 Serial.println(latitude); 487 488 Serial.print("Longitude:"); 489 490 Serial.println(logitude); 491 492} 493 494 495void coordinate2dec() 496 497{ 498 499 500 501 String lat_degree=""; 502 503 for(i=18;i<20;i++) //extract latitude from string 504 505 lat_degree+=gpsString[i]; 506 507 508 509 String lat_minut=""; 510 511 for(i=20;i<28;i++) 512 513 lat_minut+=gpsString[i]; 514 515 516 517 String long_degree=""; 518 519 for(i=30;i<33;i++) //extract longitude from string 520 521 long_degree+=gpsString[i]; 522 523 524 525 String long_minut=""; 526 527 for(i=33;i<41;i++) 528 529 long_minut+=gpsString[i]; 530 531 532 533 float minut= lat_minut.toFloat(); 534 535 minut=minut/60; 536 537 float degree=lat_degree.toFloat(); 538 539 latitude=degree+minut; 540 541 542 543 minut= long_minut.toFloat(); 544 545 minut=minut/60; 546 547 degree=long_degree.toFloat(); 548 549 logitude=degree+minut; 550 551} 552 553 554void Send() 555 556{ 557 558 559 560 webpage = "<h1>Welcome to Engineers Garage</h1><body bgcolor=f0f0f0>"; 561 562 webpage+=place; 563 564 webpage+=num; 565 566 webpage+=cordinat; 567 568 webpage+="<p>Latitude:"; 569 570 webpage+=latitude; 571 572 webpage+="</p>"; 573 574 webpage+="<p>Longitude:"; 575 576 webpage+=logitude; 577 578 webpage+="</p>"; 579 580 webpage+= "<a href=\\"http://maps.google.com/maps?&z=15&mrt=yp&t=k&q="; 581 582 webpage+=latitude; 583 584 webpage+='+'; //28.612953, 77.231545 //28.612953,77.2293563 585 586 webpage+=logitude; 587 588 webpage+="\\">Click Here for google map</a>"; 589 590 webdata(); 591 592 webpage=""; 593 594 while(1) 595 596 { 597 598 Serial.println("AT+CIPCLOSE=0"); 599 600 Serial1.println("AT+CIPCLOSE=0"); 601 602 while(Serial1.available()) 603 604 { 605 606 if(Serial1.find("0,CLOSE")) 607 608 { 609 610 return; 611 612 } 613 614 } 615 616 delay(500); 617 618 i++; 619 620 if(i>5) 621 622 { 623 624 i=0; 625 626 } 627 628 if(i==0) 629 630 break; 631 632 } 633 634} 635 636 637void webdata() 638 639{ 640 641 i=0; 642 643 while(1) 644 645 { 646 647 unsigned int l=webpage.length(); 648 649 Serial1.print("AT+CIPSEND=0,"); 650 651 Serial1.println(l+2); 652 653 Serial.println(l+2); 654 655 Serial.println(webpage); 656 657 Serial1.println(webpage); 658 659 while(Serial1.available()) 660 661 { 662 663 if(Serial1.find("OK")) 664 665 { 666 667 return; 668 669 } 670 671 } 672 673 i++; 674 675 if(i>5) 676 677 i=0; 678 679 if(i==0) 680 681 break; 682 683 delay(200); 684 685 } 686 687} 688
Downloadable files
Arduino and ESP8266 based IoT Vehicle Tracking Device Circuit
Arduino and ESP8266 based IoT Vehicle Tracking Device Circuit
Arduino and ESP8266 based IoT Vehicle Tracking Device Circuit
Arduino and ESP8266 based IoT Vehicle Tracking Device Circuit
Comments
Only logged in users can leave comments
TechnicalEngineer
4 Followers
•0 Projects
1
0