Facebook
From Queen Tortoise, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 42
  1.  
  2. // Monitor serial (debug) and modem At commands
  3. #define SerialMon SerialUSB
  4. #define SerialAt Serial1 // Serial1 = UART-A
  5.  
  6. // At commands responses will be allocated here
  7. u_int8_t signal_quality;
  8. char buffer[1000];
  9.  
  10. void configureIos () {
  11.   pinMode(GSM_PWRKEY, OUTPUT);
  12.   pinMode(GSM_PWREN, OUTPUT);
  13.   pinMode(GPS_EN, OUTPUT);
  14.   pinMode(GSM_STATUS, INPUT);
  15. }
  16.  
  17. void configureSerial () {
  18.   SerialMon.begin(38400);
  19.   while (!SerialMon) {}
  20.  
  21.   SerialAt.begin(38400);
  22.   while (!SerialAt) {}
  23. }
  24.  
  25. /**
  26.  * @brief Turn on the modem
  27.  *
  28.  * To be able to use the SIM868 module we have to activate the PWREN line and
  29.  * make a pulse of 1.5 seconds in the PWRKEY line. The modem will have been
  30.  * turned on when the STATUS line goes to high level.
  31.  *
  32.  * This allows the MCU to control the power of the device, necessary for low
  33.  * power applications. By default the modem will be turned off and will only
  34.  * turn on if we perform the turn on routine as seen in this method.
  35.  *
  36.  * @return true if the modem has been turned on
  37.  * @return false
  38.  */
  39. bool powerOn () {
  40.   uint8_t gsm_status;
  41.  
  42.   digitalWrite(GSM_PWREN, HIGH);
  43.  
  44.   for (char i=0; i<5; i++) {
  45.     gsm_status = digitalRead(GSM_STATUS);
  46.     if (gsm_status == HIGH){
  47.       SerialMon.println(F("GSM HIGH!!"));
  48.       break;
  49.     } else {
  50.       SerialMon.println(F("GSM LOW!"));
  51.       digitalWrite(GSM_PWRKEY, HIGH);
  52.       delay(1500);
  53.       digitalWrite(GSM_PWRKEY, LOW);
  54.       delay(1500);
  55.     }
  56.   }
  57.  
  58.   if (!gsm_status) {
  59.     return false;
  60.   }
  61.  
  62.   // Turn on the GPS. It is not necessary for modem operation but this demo
  63.   // will make use of GPS.
  64.   digitalWrite(GPS_EN, HIGH);
  65.   SerialAt.println("AT+CGNSPWR=1");
  66.   delay(3000);
  67.  
  68.   // We obviate the result of enabling GPS
  69.   return true;
  70. }
  71.  
  72. void sendAt(const char * command){
  73.   memset(buffer, 0, sizeof buffer);
  74.   SerialMon.println(command);
  75.   SerialAt.println(command);
  76.   uint32_t init=millis();
  77.  
  78.   int count=0;
  79.   while ((millis() - init) < 5000) {
  80.     delay(1);
  81.     if (!SerialAt.available()) continue;
  82.  
  83.     while (SerialAt.available()) {
  84.       buffer[count] = (char) SerialAt.read();
  85.       count++;
  86.       delay(10);
  87.     }
  88.  
  89.     buffer[count]='\n';
  90.     break; // hemos acabado, salimos del while
  91.   }
  92.  
  93.   SerialMon.print("La respuesta es: ");
  94.   SerialMon.println(buffer);
  95. }
  96.  
  97. void setup () {
  98.   configureIos();
  99.   configureSerial();
  100.  
  101.   if (!powerOn()) {
  102.     SerialMon.println("Fail to power on the modem. Is the SIM868 connected to the Socket 1?");
  103.     for (;;) {}
  104.   }
  105.  
  106.   sendAt("AT"); // test modem, should return "OK"
  107.   if(strcmp(buffer,"OK")){
  108.     sendAt("AT+SHUT");   //sets down any previous gprs conexions, should return "SHUT OK"
  109.     sendAt("AT+CIPMUX=0");   //sets single conexion mode, should return "OK"
  110.     sendAt("AT+CGATT=1");    //attaches GPRS Service, should return "OK"
  111.     sendAt("AT+CSTT=RH_apn,RH_apnUser,RH_apnPass");
  112.     sendAt("AT+CIICR"); //brings up the GPRS connexions, sometimes it takes a bit to respond, but should return "OK"
  113.     sendAt("AT+CIFSR");   //gives us our IP Address
  114.     sendAt("AT+CIPSTART=TCP,RH_remoteServer,RH_remotePort");
  115.   }
  116.  
  117. }
  118.  
  119. bool checkCreg () {
  120.   for (auto i = 0; i < 20; i++) {
  121.     sendAt("AT+CSQ");
  122.     sendAt("AT+CREG?");
  123.     if (strstr(buffer, ",1") || strstr(buffer, ",5")) {
  124.       // tenemos CREG!!!
  125.       return true;
  126.     }
  127.     delay(1000);
  128.   }
  129.  
  130.   return false;
  131. }
  132.  
  133. void loop () {
  134.   // Get GPS. It will be necessary a few minutes before obtaining data.
  135.   // The important thing for this demo is not to get GPS data, but to check
  136.   // that we can send a command to the modem and get a response.
  137.   // SerialAt.println("AT+CGNSINF");   (Read GPS location data)
  138.   // delay(1000);
  139.  
  140.   sendAt("AT");
  141.   if (strcmp(buffer, "OK")) {
  142.     SerialMon.println("El modem funciona!!");
  143.   } else {
  144.     SerialMon.println("Huston tenemos un problema");
  145.   }
  146.  
  147.   if (checkCreg()) {
  148.     SerialMon.println("Tenemos conexiĂłn a red!!");
  149.   }
  150.  
  151.   //sendAt("AT+CSQ");
  152.  
  153.  
  154.  
  155.   // wait for loop
  156.   delay(5000);
  157. }
  158.  
  159. //AT+CSQ (Comando AT que indica la calidad de la señal o la cobertura)
  160.