// Monitor serial (debug) and modem At commands #define SerialMon SerialUSB #define SerialAt Serial1 // Serial1 = UART-A // At commands responses will be allocated here u_int8_t signal_quality; char buffer[1000]; void configureIos () { pinMode(GSM_PWRKEY, OUTPUT); pinMode(GSM_PWREN, OUTPUT); pinMode(GPS_EN, OUTPUT); pinMode(GSM_STATUS, INPUT); } void configureSerial () { SerialMon.begin(38400); while (!SerialMon) {} SerialAt.begin(38400); while (!SerialAt) {} } /** * @brief Turn on the modem * * To be able to use the SIM868 module we have to activate the PWREN line and * make a pulse of 1.5 seconds in the PWRKEY line. The modem will have been * turned on when the STATUS line goes to high level. * * This allows the MCU to control the power of the device, necessary for low * power applications. By default the modem will be turned off and will only * turn on if we perform the turn on routine as seen in this method. * * @return true if the modem has been turned on * @return false */ bool powerOn () { uint8_t gsm_status; digitalWrite(GSM_PWREN, HIGH); for (char i=0; i<5; i++) { gsm_status = digitalRead(GSM_STATUS); if (gsm_status == HIGH){ SerialMon.println(F("GSM HIGH!!")); break; } else { SerialMon.println(F("GSM LOW!")); digitalWrite(GSM_PWRKEY, HIGH); delay(1500); digitalWrite(GSM_PWRKEY, LOW); delay(1500); } } if (!gsm_status) { return false; } // Turn on the GPS. It is not necessary for modem operation but this demo // will make use of GPS. digitalWrite(GPS_EN, HIGH); SerialAt.println("AT+CGNSPWR=1"); delay(3000); // We obviate the result of enabling GPS return true; } void sendAt(const char * command){ memset(buffer, 0, sizeof buffer); SerialMon.println(command); SerialAt.println(command); uint32_t init=millis(); int count=0; while ((millis() - init) < 5000) { delay(1); if (!SerialAt.available()) continue; while (SerialAt.available()) { buffer[count] = (char) SerialAt.read(); count++; delay(10); } buffer[count]='\n'; break; // hemos acabado, salimos del while } SerialMon.print("La respuesta es: "); SerialMon.println(buffer); } void setup () { configureIos(); configureSerial(); if (!powerOn()) { SerialMon.println("Fail to power on the modem. Is the SIM868 connected to the Socket 1?"); for (;;) {} } sendAt("AT"); // test modem, should return "OK" if(strcmp(buffer,"OK")){ sendAt("AT+SHUT"); //sets down any previous gprs conexions, should return "SHUT OK" sendAt("AT+CIPMUX=0"); //sets single conexion mode, should return "OK" sendAt("AT+CGATT=1"); //attaches GPRS Service, should return "OK" sendAt("AT+CSTT=RH_apn,RH_apnUser,RH_apnPass"); sendAt("AT+CIICR"); //brings up the GPRS connexions, sometimes it takes a bit to respond, but should return "OK" sendAt("AT+CIFSR"); //gives us our IP Address sendAt("AT+CIPSTART=TCP,RH_remoteServer,RH_remotePort"); } } bool checkCreg () { for (auto i = 0; i < 20; i++) { sendAt("AT+CSQ"); sendAt("AT+CREG?"); if (strstr(buffer, ",1") || strstr(buffer, ",5")) { // tenemos CREG!!! return true; } delay(1000); } return false; } void loop () { // Get GPS. It will be necessary a few minutes before obtaining data. // The important thing for this demo is not to get GPS data, but to check // that we can send a command to the modem and get a response. // SerialAt.println("AT+CGNSINF"); (Read GPS location data) // delay(1000); sendAt("AT"); if (strcmp(buffer, "OK")) { SerialMon.println("El modem funciona!!"); } else { SerialMon.println("Huston tenemos un problema"); } if (checkCreg()) { SerialMon.println("Tenemos conexión a red!!"); } //sendAt("AT+CSQ"); // wait for loop delay(5000); } //AT+CSQ (Comando AT que indica la calidad de la señal o la cobertura)