Advertisement
Guest User

gprs

a guest
Sep 16th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. #include <SoftwareSerial.h>;
  2. #include <TinyGPS.h>;
  3.  
  4. TinyGPS gps;
  5. typedef unsigned long int Timestamp;
  6. static const uint32_t GPSBaud = 9600;
  7. const int ldrPin = A5;
  8. typedef struct Time {
  9. unsigned int ans;
  10. unsigned int mois;
  11. unsigned int jour;
  12. unsigned int heure;
  13. unsigned int minut;
  14. unsigned int seconde;
  15. }Time;
  16. long int t;
  17. int last;
  18. int k=0;
  19. float lat,lng;
  20. float speedt;
  21. float altitudet;
  22. int year;
  23. byte month, day, hour, minute, second, hundredths;
  24. String date;
  25. SoftwareSerial ss(6,7); // Communication serie de GPS
  26. SoftwareSerial sim(2,3); // communication serie de GPRS
  27. //void updateSerial();
  28. void setup() {
  29. Serial.begin(9600);
  30. sim.begin(9600);
  31. ss.begin(9600);
  32. sim.listen();
  33. Serial.println("Initializing...");
  34. delay(2000);
  35. pinMode(8,OUTPUT); // Activation de GPRS
  36. digitalWrite(8, HIGH); // Activation de GPRS
  37. delay(4000);
  38. Serial.println("Begin SIM... Wait");
  39. delay(3000);
  40. //Lire les données de GPRS
  41. while(sim.available()){
  42. Serial.write(sim.read());
  43. }
  44.  
  45. sim.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\""); // type de connexion
  46. delay(1000);
  47. while(sim.available()){
  48. Serial.write(sim.read());
  49. }
  50. sim.println("AT+SAPBR=3,1,\"APN\",\"internet.tn\""); // APN de Oreedoo , si vous voulez le changer avec TT ou orange change l'APN
  51. delay(4000);
  52. while(sim.available()){
  53. Serial.write(sim.read());
  54. }
  55. sim.println("AT+SAPBR=3,1,\"USER\",\"internet\""); // Donnée de Oreedoo le USER pour connecter au APN
  56. delay(4000);
  57. while(sim.available()){
  58. Serial.write(sim.read());
  59. }
  60. sim.println("AT+SAPBR=3,1,\"PWD\",\"02\""); // Mot de passe de l'APN
  61. delay(4000);
  62. while(sim.available()){
  63. Serial.write(sim.read());
  64. }
  65. //Connexion au GPRS
  66. sim.println("AT+SAPBR=1,1");
  67. delay(2000);
  68. while(sim.available()){
  69. Serial.write(sim.read());
  70. }
  71. // LED Test
  72. pinMode(ldrPin, INPUT);
  73. int ldrStatus = analogRead(ldrPin);
  74. if (ldrStatus <= 400 ) {last=0;
  75. Serial.print("Its DARK, Turn on the LED : ");
  76. Serial.println(ldrStatus);}
  77. else {last =1;
  78. Serial.print("Its BRIGHT, Turn off the LED : ");
  79. Serial.println(ldrStatus);}
  80.  
  81. }
  82.  
  83. void loop() {
  84. int ldrStatus = analogRead(ldrPin);
  85. bool newData = false;
  86. unsigned long chars;
  87. unsigned short sentences, failed;
  88. // Lire les données de GPS
  89. ss.listen();
  90. // Si le GPS est disponible lire les données et les ecrire dans une variable c
  91. if (ss.available()){
  92. Serial.println("GPS Available");
  93. }
  94. else Serial.println("GPS Not Available");
  95. for (unsigned long start = millis(); millis() - start < 1000;)
  96. {
  97. while (ss.available())
  98. {
  99. char c = ss.read();
  100. if (gps.encode(c))
  101. newData = true;
  102. }
  103. // Si donnée bien recu lire Date , speed , latitude , longitude , altitude
  104. if (newData)
  105. {
  106. float flat, flon;
  107.  
  108. unsigned long age;
  109. gps.f_get_position(&flat, &flon, &age);
  110. lat = flat;
  111. lng = flon;
  112. altitudet = gps.f_altitude();
  113. speedt = gps.f_speed_kmph();
  114. Serial.print(speedt);
  115. Serial.print("----");
  116. Serial.println(altitudet);
  117. gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);
  118. date = String(day)+"-"+String(month)+"-"+String(year)+"/"+String(hour)+":"+String(minute)+":"+String(second);
  119. Time temps = {year,int(month),int(day),int(hour),int(minute),int(second)};
  120. t= Time_Convert_TO2TS(&temps);
  121. }
  122. }
  123. // Lire la communication GPRs
  124. sim.listen();
  125. if(sim.available())
  126. {
  127. Serial.write(sim.read());
  128. }
  129. if(Serial.available()){
  130. {
  131. sim.write(Serial.read());
  132. }
  133. }
  134. //Communication HTTP
  135. sim.println("AT+HTTPINIT");
  136. delay(2000);
  137. while(sim.available()){
  138. Serial.write(sim.read()); }
  139. //L'URL de serveur a communiqué avec
  140. sim.println("AT+HTTPPARA=\"URL\",\"http://us-central1-airtrack-d1869.cloudfunctions.net/SendData?data1="+String(lat,6)+"&data2="+String(lng,6)+"&data3="+String(altitudet,6)+"&data4="+String(speedt)+"&data5="+String(ldrStatus)+"&data6="+String(date)+"&data7="+String(t)+"&user="+String(t)+"\"");
  141. delay(500);
  142. while(sim.available()){
  143. Serial.write(sim.read());}
  144. // Test si la communication est bien recu
  145. sim.println("AT+HTTPACTION=0");
  146. delay(2000);
  147. while(sim.available()){
  148. Serial.write(sim.read());
  149. }
  150. // terminer la communication HTTP
  151. sim.println("AT+HTTPTERM");
  152. delay(2000);
  153. while(sim.available()){
  154. Serial.write(sim.read());
  155. }
  156.  
  157.  
  158. if (ldrStatus <= 300 && last == 0) {k++;
  159. Serial.println(k); }
  160. if (ldrStatus <= 300 & last == 1){
  161. Serial.print("Its DARK, Turn on the LED : ");
  162. Serial.println(ldrStatus);
  163. sim.println("AT+CMGF=1"); // Configuration TEXT mode
  164. updateSerial();
  165. sim.println("AT+CMGS=\"+21628382362\"");//changer le numéro avec le numéro que vous voulez envoyer un sms
  166. updateSerial();
  167. sim.print("CHANGE STATE : DARK,\nLatitude : "+String(lat,6)+" \nLongitude :"+String(lng,6)+" \nCheck you box please"); //text a envoyer
  168. updateSerial();
  169. sim.write(26);
  170. delay(30000);
  171. last=0;}
  172. if (ldrStatus > 250 & last == 1){k++;
  173. Serial.println(k);}
  174. if (ldrStatus > 250 & last == 0){
  175. Serial.print("Its BRIGHT, Turn off the LED : ");
  176. Serial.println(ldrStatus);
  177. sim.println("AT+CMGF=1"); // Configuring TEXT mode
  178. updateSerial();
  179. sim.println("AT+CMGS=\"+21628382362\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
  180. updateSerial();
  181. sim.print("CHANGE STATE : Bright ,\nLatitude : "+String(lat,6)+" \nLongitude :"+String(lng,6)+"\nCheck you box please"); //text content
  182. updateSerial();
  183. sim.write(26);
  184. delay(30000);
  185. last =1;}
  186. delay(1000);}
  187. void updateSerial()
  188. {
  189. delay(500);
  190. while (Serial.available())
  191. {
  192. sim.write(Serial.read());
  193. }
  194. while(sim.available())
  195. {
  196. Serial.write(sim.read());
  197. }
  198. }
  199.  
  200. // fonction pour convertir le temps de format date au format timestamp
  201. Timestamp Time_Convert_TO2TS(Time * time) {
  202. if (time) {
  203. int mth[12] = {0, 31, 59, 90, 120, 151, 181, 212, 273, 304, 334};
  204. int mthb[12] = {0, 31, 60, 91, 121, 152, 182, 213, 274, 305, 335};
  205. Timestamp timestamp =
  206. ( ( (!(time->ans % 4)) && (time->ans % 100) ) || (!(time->ans % 400)) )?
  207. ((((unsigned long int)( time->ans - 1970) / 4)) + (time->ans - 1970) * 365 + mthb[time->mois-1] + (time->jour - 1)) * 86400 + time->heure * 3600 + time -> minut * 60 + time -> seconde:
  208. ((((unsigned long int)( time->ans - 1970) / 4)) + (time->ans - 1970) * 365 + mth [time->mois-1] + (time->jour - 1)) * 86400 + time->heure * 3600 + time -> minut * 60 + time -> seconde;
  209. return timestamp;
  210. } else {
  211. return 0;
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement