fastoch13

Untitled

Apr 26th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.26 KB | None | 0 0
  1. #include <NMEAGPS.h>  //parsing the comma seperated values to be more human readable
  2. #include <stdlib.h>   //the lib is needed for the floatToString() helper function
  3. #include <NeoSWSerial.h>  //it is used instead of SoftwareSerial
  4.  
  5. //NOTE: THE CODE WILL ONLY WORK IF YOU HAVE ALREADY UNLOCKED YOUR SIM CARD - SO THE PHONE WON'T ASK FOR PIN CODE WHENEVER YOU USE IT
  6.  
  7. //if there are more devices, the server must differentiate them by the deviceID
  8. int deviceId = 13;
  9.  
  10. //SoftwareSerial variables
  11. static const int gpsRX = 3, gpsTX = 4;
  12. static const uint32_t gpsBaud = 9600;
  13. static const int simRX = 10, simTX = 11;
  14. static const uint32_t simBaud = 9600;
  15.  
  16. //SoftwareSerial instances
  17. NeoSWSerial gpsPort(gpsRX, gpsTX);
  18. NeoSWSerial Sim800l(simRX, simTX);
  19.  
  20. //GPS instance
  21. NMEAGPS gps;
  22. gps_fix fix;
  23.  
  24. //for sending data to a remote webserver
  25. String ipAddress = ""; //this is the ip address of our server - e.g. "123.123.123.123"
  26. String APN = ""; //check your Internet provider's website for the APN e.g. "internet"
  27.  
  28. //helper variables for waitUntilResponse() function
  29. String response = "";
  30. static long maxResponseTime = 5000;
  31. unsigned long lastTime;
  32.  
  33. //The frequency of http requests (seconds)
  34. int refreshRate = 15; //SECONDS
  35. //variables for a well-scheduled loop - in which the sendData() gets called every 15 secs (refresh rate)
  36. unsigned long last;
  37. unsigned long current;
  38. unsigned long elapsed;
  39.  
  40. //if there is an error in sendLocation() function after the GPRS Connection is setup - and the number of errors exceeds 3 - the system reboots. (with the help of the reboot pin)
  41. int maxNumberOfErrors = 3;
  42. boolean gprsConnectionSetup = false;
  43. boolean reboot = false;
  44. int errorsEncountered = 0; //number of errors encountered after gprsConnection is set up - if exceeds the max number of errors the system reboots
  45. int resetPin = 12;
  46.  
  47. //if any error occurs with the gsm module or gps module, the corresponding LED will light up - until they don't get resolved
  48. int sim800Pin = 2; //error pin
  49. int gpsPin = 6; //error pin
  50.  
  51.  
  52.  
  53. //a helper function which converts a float to a string with a given precision
  54. String floatToString(float x, byte precision = 2) {
  55.   char tmp[50];
  56.   dtostrf(x, 0, precision, tmp);
  57.   return String(tmp);
  58. }
  59.  
  60.  
  61. void setup(){
  62.  
  63.   //setup resetPin
  64.   digitalWrite(resetPin, HIGH);
  65.   delay(200);
  66.   pinMode(resetPin, OUTPUT);
  67.  
  68.   //init
  69.   Serial.begin(9600);
  70.   gpsPort.begin(gpsBaud);
  71.   Sim800l.begin(simBaud);
  72.  
  73.   pinMode(sim800Pin, OUTPUT);
  74.   pinMode(gpsPin, OUTPUT);
  75.  
  76.   Sim800l.write(27); //Clears buffer for safety
  77.   Serial.println("Beginning...");
  78.   delay(15000); //Waiting for Sim800L to get signal
  79.   Serial.println("SIM800L should have booted up");
  80.  
  81.   Sim800l.listen(); //The GSM module and GPS module can't communicate with the arduino board at once - so they need to get focus once we need them
  82.   setupGPRSConnection(); //Enable the internet connection to the SIM card
  83.   Serial.println("Connection set up");
  84.  
  85.   gpsPort.listen();
  86.  
  87.   last = millis();
  88.  
  89. }
  90.  
  91. void loop(){
  92.  
  93.   current = millis();
  94.   elapsed += current - last;
  95.   last = current;
  96.   Serial.println(elapsed);
  97.   while (gps.available(gpsPort)) {
  98.       fix = gps.read();
  99.     }
  100.   if(elapsed >= (refreshRate * 1000)) {
  101.     sendData();
  102.     elapsed -= (refreshRate * 1000);
  103.   }
  104.  
  105.   if ((gps.statistics.chars < 10)) {
  106.      //no gps detected (maybe wiring)
  107.      Serial.println("NO GPS DETECTED OR BEFORE FIRST HTTP REQUEST");
  108.   }
  109.  
  110.   if(reboot){
  111.     digitalWrite(resetPin, LOW);  
  112.   }
  113.  
  114. }
  115.  
  116. void sendData(){
  117.  Serial.println("data to be sent");
  118.   if (fix.valid.location) {
  119.     digitalWrite(gpsPin, LOW);
  120.     String lat = floatToString(fix.latitude(), 5);
  121.     String lon = floatToString(fix.longitude(), 5);
  122.     sendLocation(lat, lon);
  123.   } else {
  124.     sendLocation("-1", "-1");
  125.     digitalWrite(gpsPin, HIGH);  
  126.   }
  127. }
  128.  
  129. void setupGPRSConnection(){
  130.  Sim800l.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\""); //Connection type: GPRS
  131.  waitUntilResponse("OK");
  132.  Sim800l.println("AT+SAPBR=3,1,\"APN\",\"" + APN + "\""); //We need to set the APN which our internet provider gives us
  133.  waitUntilResponse("OK");
  134.  Sim800l.println("AT+SAPBR=1,1"); //Enable the GPRS
  135.  waitUntilResponse("OK");
  136.  Sim800l.println("AT+HTTPINIT"); //Enabling HTTP mode
  137.  waitUntilResponse("OK");
  138.  gprsConnectionSetup = true;
  139. }
  140.  
  141. //ERROR handler - exits if error arises or a given time exceeds with no answer - or when everything is OK
  142. void waitUntilResponse(String resp){
  143.   lastTime = millis();
  144.   response="";
  145.   String totalResponse = "";
  146.   while(response.indexOf(resp) < 0 && millis() - lastTime < maxResponseTime)
  147.   {
  148.     readResponse();
  149.     totalResponse = totalResponse + response;
  150.     Serial.println(response);
  151.   }
  152.  
  153.   if(totalResponse.length() <= 0)
  154.   {
  155.     Serial.println("NO RESPONSE");
  156.     digitalWrite(sim800Pin, HIGH);
  157.     if (gprsConnectionSetup == true){
  158.       Serial.println("error");
  159.       errorsEncountered++;
  160.     }
  161.   }
  162.   else if (response.indexOf(resp) < 0)
  163.   {
  164.     if (gprsConnectionSetup == true){
  165.       Serial.println("error");
  166.       errorsEncountered++;
  167.     }
  168.     Serial.println("UNEXPECTED RESPONSE");
  169.     Serial.println(totalResponse);
  170.     digitalWrite(sim800Pin, HIGH);
  171.   }else{
  172.     Serial.println("SUCCESSFUL");
  173.     digitalWrite(sim800Pin, LOW);
  174.     errorsEncountered = 0;
  175.   }
  176.  
  177.   //if there are more errors or equal than previously set ==> reboot!
  178.   if (errorsEncountered>= maxNumberOfErrors){
  179.     reboot = true;
  180.   }
  181. }
  182.  
  183. //the function - which is responsible for sending data to the webserver
  184. void sendLocation(String lat, String lon){
  185.  Sim800l.listen();
  186.  //The line below sets the URL we want to connect to
  187.  Sim800l.println("AT+HTTPPARA=\"URL\", \"http://" + ipAddress +  "/log_info.php?dev_id=13&lat=" + lat + "&lon=" + lon + "\"");
  188.  waitUntilResponse("OK");
  189.  //GO
  190.  Sim800l.println("AT+HTTPACTION=0");
  191.  waitUntilResponse("200");
  192.  Serial.println("Location sent");
  193.  gpsPort.listen();
  194. }
  195.  
  196. void readResponse(){
  197.   response = "";
  198.   while(response.length() <= 0 || !response.endsWith("\n"))
  199.   {
  200.     tryToRead();
  201.     if(millis() - lastTime > maxResponseTime)
  202.     {
  203.       return;
  204.     }
  205.   }
  206. }
  207.  
  208. void tryToRead(){
  209.   while(Sim800l.available()){
  210.     char c = Sim800l.read();  //gets one byte from serial buffer
  211.     response += c; //makes the string readString
  212.   }
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment