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