Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>;
- #include <TinyGPS.h>;
- TinyGPS gps;
- typedef unsigned long int Timestamp;
- static const uint32_t GPSBaud = 9600;
- const int ldrPin = A5;
- typedef struct Time {
- unsigned int ans;
- unsigned int mois;
- unsigned int jour;
- unsigned int heure;
- unsigned int minut;
- unsigned int seconde;
- }Time;
- long int t;
- int last;
- int k=0;
- float lat,lng;
- float speedt;
- float altitudet;
- int year;
- byte month, day, hour, minute, second, hundredths;
- String date;
- SoftwareSerial ss(6,7); // Communication serie de GPS
- SoftwareSerial sim(2,3); // communication serie de GPRS
- //void updateSerial();
- void setup() {
- Serial.begin(9600);
- sim.begin(9600);
- ss.begin(9600);
- sim.listen();
- Serial.println("Initializing...");
- delay(2000);
- pinMode(8,OUTPUT); // Activation de GPRS
- digitalWrite(8, HIGH); // Activation de GPRS
- delay(4000);
- Serial.println("Begin SIM... Wait");
- delay(3000);
- //Lire les données de GPRS
- while(sim.available()){
- Serial.write(sim.read());
- }
- sim.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\""); // type de connexion
- delay(1000);
- while(sim.available()){
- Serial.write(sim.read());
- }
- sim.println("AT+SAPBR=3,1,\"APN\",\"internet.tn\""); // APN de Oreedoo , si vous voulez le changer avec TT ou orange change l'APN
- delay(4000);
- while(sim.available()){
- Serial.write(sim.read());
- }
- sim.println("AT+SAPBR=3,1,\"USER\",\"internet\""); // Donnée de Oreedoo le USER pour connecter au APN
- delay(4000);
- while(sim.available()){
- Serial.write(sim.read());
- }
- sim.println("AT+SAPBR=3,1,\"PWD\",\"02\""); // Mot de passe de l'APN
- delay(4000);
- while(sim.available()){
- Serial.write(sim.read());
- }
- //Connexion au GPRS
- sim.println("AT+SAPBR=1,1");
- delay(2000);
- while(sim.available()){
- Serial.write(sim.read());
- }
- // LED Test
- pinMode(ldrPin, INPUT);
- int ldrStatus = analogRead(ldrPin);
- if (ldrStatus <= 400 ) {last=0;
- Serial.print("Its DARK, Turn on the LED : ");
- Serial.println(ldrStatus);}
- else {last =1;
- Serial.print("Its BRIGHT, Turn off the LED : ");
- Serial.println(ldrStatus);}
- }
- void loop() {
- int ldrStatus = analogRead(ldrPin);
- bool newData = false;
- unsigned long chars;
- unsigned short sentences, failed;
- // Lire les données de GPS
- ss.listen();
- // Si le GPS est disponible lire les données et les ecrire dans une variable c
- if (ss.available()){
- Serial.println("GPS Available");
- }
- else Serial.println("GPS Not Available");
- for (unsigned long start = millis(); millis() - start < 1000;)
- {
- while (ss.available())
- {
- char c = ss.read();
- if (gps.encode(c))
- newData = true;
- }
- // Si donnée bien recu lire Date , speed , latitude , longitude , altitude
- if (newData)
- {
- float flat, flon;
- unsigned long age;
- gps.f_get_position(&flat, &flon, &age);
- lat = flat;
- lng = flon;
- altitudet = gps.f_altitude();
- speedt = gps.f_speed_kmph();
- Serial.print(speedt);
- Serial.print("----");
- Serial.println(altitudet);
- gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);
- date = String(day)+"-"+String(month)+"-"+String(year)+"/"+String(hour)+":"+String(minute)+":"+String(second);
- Time temps = {year,int(month),int(day),int(hour),int(minute),int(second)};
- t= Time_Convert_TO2TS(&temps);
- }
- }
- // Lire la communication GPRs
- sim.listen();
- if(sim.available())
- {
- Serial.write(sim.read());
- }
- if(Serial.available()){
- {
- sim.write(Serial.read());
- }
- }
- //Communication HTTP
- sim.println("AT+HTTPINIT");
- delay(2000);
- while(sim.available()){
- Serial.write(sim.read()); }
- //L'URL de serveur a communiqué avec
- 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)+"\"");
- delay(500);
- while(sim.available()){
- Serial.write(sim.read());}
- // Test si la communication est bien recu
- sim.println("AT+HTTPACTION=0");
- delay(2000);
- while(sim.available()){
- Serial.write(sim.read());
- }
- // terminer la communication HTTP
- sim.println("AT+HTTPTERM");
- delay(2000);
- while(sim.available()){
- Serial.write(sim.read());
- }
- if (ldrStatus <= 300 && last == 0) {k++;
- Serial.println(k); }
- if (ldrStatus <= 300 & last == 1){
- Serial.print("Its DARK, Turn on the LED : ");
- Serial.println(ldrStatus);
- sim.println("AT+CMGF=1"); // Configuration TEXT mode
- updateSerial();
- sim.println("AT+CMGS=\"+21628382362\"");//changer le numéro avec le numéro que vous voulez envoyer un sms
- updateSerial();
- sim.print("CHANGE STATE : DARK,\nLatitude : "+String(lat,6)+" \nLongitude :"+String(lng,6)+" \nCheck you box please"); //text a envoyer
- updateSerial();
- sim.write(26);
- delay(30000);
- last=0;}
- if (ldrStatus > 250 & last == 1){k++;
- Serial.println(k);}
- if (ldrStatus > 250 & last == 0){
- Serial.print("Its BRIGHT, Turn off the LED : ");
- Serial.println(ldrStatus);
- sim.println("AT+CMGF=1"); // Configuring TEXT mode
- updateSerial();
- sim.println("AT+CMGS=\"+21628382362\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
- updateSerial();
- sim.print("CHANGE STATE : Bright ,\nLatitude : "+String(lat,6)+" \nLongitude :"+String(lng,6)+"\nCheck you box please"); //text content
- updateSerial();
- sim.write(26);
- delay(30000);
- last =1;}
- delay(1000);}
- void updateSerial()
- {
- delay(500);
- while (Serial.available())
- {
- sim.write(Serial.read());
- }
- while(sim.available())
- {
- Serial.write(sim.read());
- }
- }
- // fonction pour convertir le temps de format date au format timestamp
- Timestamp Time_Convert_TO2TS(Time * time) {
- if (time) {
- int mth[12] = {0, 31, 59, 90, 120, 151, 181, 212, 273, 304, 334};
- int mthb[12] = {0, 31, 60, 91, 121, 152, 182, 213, 274, 305, 335};
- Timestamp timestamp =
- ( ( (!(time->ans % 4)) && (time->ans % 100) ) || (!(time->ans % 400)) )?
- ((((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:
- ((((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;
- return timestamp;
- } else {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement