Advertisement
Guest User

Untitled

a guest
May 25th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include <Adafruit_FONA.h>
  2. #include <SoftwareSerial.h>
  3. #include <TinyGPS++.h>
  4.  
  5. #define FONA_RX 4
  6. #define FONA_TX 5
  7. #define FONA_RST 9
  8. #define GPS_RX 2
  9. #define GPS_TX 3
  10. #define BTN_PIN 8
  11. #define TIMER_LED 15000
  12. #define TIMER_SMS 30000
  13.  
  14. static const uint32_t GPSBaud = 9600;
  15. TinyGPSPlus gps;
  16.  
  17.  
  18. SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
  19. SoftwareSerial *fonaSerial = &fonaSS;
  20. SoftwareSerial gpsSS(GPS_RX, GPS_TX);
  21.  
  22. Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
  23.  
  24.  
  25. uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
  26. uint8_t type;
  27.  
  28. long lastEventMillis = 0;
  29.  
  30. boolean btn = true; // attention que le bouton fonctionne à l'envers : HIGH = pas pressé et LOW = pressé
  31.  
  32. void setup() {
  33.   while (!Serial);
  34.   Serial.begin(9600);
  35.  
  36.   pinMode(BTN_PIN, INPUT);
  37.   digitalWrite(BTN_PIN, HIGH); //pullup interne
  38. }
  39.  
  40. void connectGSM() {
  41.   fonaSerial->begin(9600);
  42.   if (! fona.begin(*fonaSerial)) {
  43.     Serial.println(F("FONA Introuvable"));
  44.     while (1);
  45.   }
  46.  
  47.   type = fona.type();
  48.   Serial.println(F("FONA is OK"));
  49.   Serial.print(F("Found "));
  50.  
  51.   char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
  52.   uint8_t imeiLen = fona.getIMEI(imei);
  53.   if (imeiLen > 0) {
  54.     Serial.print("Module IMEI: "); Serial.println(imei);
  55.   }
  56. }
  57.  
  58. void sendSMS(char* pos) {
  59.   connectGSM();
  60.   delay(2000);
  61.  
  62.   uint8_t n = 0;
  63.   while( n != 1)
  64.   {
  65.     n = fona.getNetworkStatus();
  66.   }
  67.  
  68.   /*char* str;
  69.   Serial.println(strlen(pos));
  70.   Serial.println(sizeof (String));
  71.   pos.toCharArray(str, 140, 0);
  72.   */
  73.   Serial.println(pos);
  74.   if (
  75.       fona.sendSMS("+32495256268", pos)
  76.      )
  77.   {
  78.     Serial.println("Failed");
  79.   } else {
  80.     //lastEventMillis = millis();
  81.     Serial.println("Sent!");
  82.   }
  83.  
  84. }
  85.  
  86. String getPosition() {
  87.   gpsSS.begin(GPSBaud);
  88.  
  89.   double lat = 0;
  90.   double lng = 0;
  91.   double spd = 0;
  92.  
  93.   while(lat == 0 && lng == 0)
  94.   {
  95.     while (gpsSS.available())
  96.       gps.encode(gpsSS.read());
  97.    
  98.     //TODO: add a timeout
  99.     lat = gps.location.lat();
  100.     lng = gps.location.lng();
  101.     spd = gps.speed.kmph();
  102.   }
  103.  
  104.   return String(lat, 6) + " - " + String(lng, 6)+ " - " + String(spd,6);
  105. }
  106.  
  107. void loop() {
  108.         /*uint8_t n = fona.getNetworkStatus();
  109.         Serial.print(F("Network status "));
  110.         Serial.print(n);
  111.         Serial.print(F(": "));
  112.         if (n == 0) Serial.println(F("Not registered"));
  113.         if (n == 1) Serial.println(F("Registered (home)"));
  114.         if (n == 2) Serial.println(F("Not registered (searching)"));
  115.         if (n == 3) Serial.println(F("Denied"));
  116.         if (n == 4) Serial.println(F("Unknown"));
  117.         if (n == 5) Serial.println(F("Registered roaming"));
  118.         */
  119.  
  120.        
  121.         String pos = String("HELLO"); //getPosition();
  122.         delay(2000);
  123.  
  124.   connectGSM();
  125.   delay(2000);
  126.  
  127.   uint8_t n = 0;
  128.   while( n != 1)
  129.   {
  130.     n = fona.getNetworkStatus();
  131.     Serial.println(n);
  132.   }
  133.  
  134.   char str[141] = {0};
  135.   pos.toCharArray(str, 10, 0);
  136.  
  137.   Serial.println(pos);
  138.   if (
  139.       fona.sendSMS("+32495256268", str)
  140.      )
  141.   {
  142.     Serial.println("Failed");
  143.   } else {
  144.     //lastEventMillis = millis();
  145.     Serial.println("Sent!");
  146.   }
  147.        
  148.         while(1);
  149.         delay(20000);
  150.  
  151.         /*if(lastEventMillis + 25000 < millis())
  152.           fonaSerial->begin(9600);
  153.  
  154.         if(btn && !digitalRead(BTN_PIN)) { // si la variable btn est vrai (le bouton n'a jamais été pressé) et que la lecture du bouton est fausse (le bouton est pressé)
  155.           btn = false;
  156.           // btn est false, le bouton est maintenant considéré comme pressé jusqu'au reset
  157.         }
  158.  
  159.         if (btn && lastEventMillis + TIMER_LED < millis() ){
  160.           // TODO : allumer la led après 15s
  161.         }
  162.  
  163.         if (btn && n == 1 && lastEventMillis + TIMER_SMS < millis() ){
  164.  
  165.           //TODO : lire gps
  166.         //  while (gpsSS.available() > 0)
  167.         //    gps.encode(gpsSS.read());
  168.  
  169.         //  double lat = gps.location.lat();
  170.         //  double lon = gps.location.lng();
  171.  
  172.         //  Serial.println(lat);
  173.         //  Serial.println(lon);
  174.  
  175.           // if (!fona.sendSMS("+32472555810", "caca")) {
  176.           //   Serial.println("Failed");
  177.           // } else {
  178.           //   lastEventMillis = millis();
  179.           //   Serial.println("Sent!");
  180.           // }
  181.         }*/
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement