Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void powerOn();
- void configure();
- bool sendCommand(char *cmd, char * validate, int time);
- // configuración de pines para MEGA2560 con SIM868 en Socket II
- #define GSM_PWREN 47 // IO08
- #define GSM_PWRKEY 42 // IO09
- #define GPS_EN 45 // IO10
- #define GSM_STATUS 4 // IO11
- // creamos un alias para Serial (monitor) y Serial1 (simcom)
- #define SerialMon Serial
- #define SerialAt Serial1
- // cuando enviamos un comando al modem la respuesta se almacena aquí
- char buffer[250];
- void setup () {
- // configuramos serials
- SerialMon.begin(115200);
- SerialAt.begin(115200);
- delay(1);
- SerialMon.println("SIM869 TEST");
- SerialMon.println("");
- // configuramos los pines conectados al modem
- configure();
- delay(1);
- // encendemos el modem
- powerOn();
- delay(1);
- // comando de prueba "AT", indicamos que la repsuesta debe contener OK para
- // validar como true, indicamos tambien que espere 100 milis antes de veriricar
- // si hay respuesta en el buffer SerialAt
- if (sendCommand((char *) "AT", (char *) "OK", 100)) {
- SerialMon.println("El comando AT se ha enviado correctamente");
- } else {
- SerialMon.println("Fallo en el comando AT, no ha devuelto OK");
- }
- delay(1);
- // idem que en el comando anterior
- if (sendCommand((char *) "ATE0", (char *) "OK", 100)) {
- SerialMon.println("ATEO se ha enviado correctamente");
- } else {
- SerialMon.println("ATEO ha fallado!");
- }
- }
- void loop () {
- // nada por aquí...
- }
- /**
- * @brief configuramos entradas/salidas digitales
- */
- void configure() {
- pinMode(GSM_PWRKEY, OUTPUT);
- pinMode(GSM_PWREN, OUTPUT);
- pinMode(GPS_EN, OUTPUT);
- pinMode(GSM_STATUS, INPUT);
- }
- /**
- * @brief Encendemos el modem dando pulsos HIGH de 1.5 secs en el pin GSM_PWRKEY
- *
- */
- void 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) {
- // No se ha podido encender el modem. Revisar que GSM_PWREN, GSM_STATUS y
- // GSM_PWRKEY son los pines correctos.
- return false;
- }
- // si llegamos aquí el modem se ha encendido
- return true;
- }
- bool sendCommand(char *cmd, char * validate, int time) {
- int count = 0;
- int indexPosition = 0;
- // vaciamos el buffer del serial
- while (SerialAt.available()) SerialAt.read();
- // vaciamos el buffer local
- strcpy(buffer, "");
- // debug
- SerialMon.println(F(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"));
- SerialMon.print(F("SerialAt > ")); SerialMon.println(cmd);
- // enviamos el comando al modem
- SerialAt.println(cmd);
- SerialAt.flush();
- delay(time);
- // Esperamos respuesta comprobando el buffer cada 400 millis
- while (!SerialAt.available()) {
- delay(400);
- SerialMon.println(F("trying again for SerialAt response"));
- count++;
- if (count > 25) {
- SerialMon.println(F("sendSerialAt failed"));
- return false;
- }
- }
- // acomodamos la respuesta en buffer
- while(SerialAt.available() > 0) {
- buffer[indexPosition] = SerialAt.read();
- indexPosition++;
- delay(5);
- }
- // debug
- SerialMon.println(buffer);
- SerialMon.println(F("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"));
- // comprobamos si hay datos que validar
- if (validate != NULL && strlen(validate) >= 0) {
- if (strstr(buffer, validate)) {
- return true;
- }
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement