Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. void powerOn();
  2. void configure();
  3. bool sendCommand(char *cmd, char * validate, int time);
  4.  
  5. // configuración de pines para MEGA2560 con SIM868 en Socket II
  6. #define GSM_PWREN 47 // IO08
  7. #define GSM_PWRKEY 42 // IO09
  8. #define GPS_EN 45 // IO10
  9. #define GSM_STATUS 4 // IO11
  10.  
  11. // creamos un alias para Serial (monitor) y Serial1 (simcom)
  12. #define SerialMon Serial
  13. #define SerialAt Serial1
  14.  
  15. // cuando enviamos un comando al modem la respuesta se almacena aquí
  16. char buffer[250];
  17.  
  18. void setup () {
  19. // configuramos serials
  20. SerialMon.begin(115200);
  21. SerialAt.begin(115200);
  22. delay(1);
  23.  
  24. SerialMon.println("SIM869 TEST");
  25. SerialMon.println("");
  26.  
  27. // configuramos los pines conectados al modem
  28. configure();
  29. delay(1);
  30.  
  31. // encendemos el modem
  32. powerOn();
  33. delay(1);
  34.  
  35. // comando de prueba "AT", indicamos que la repsuesta debe contener OK para
  36. // validar como true, indicamos tambien que espere 100 milis antes de veriricar
  37. // si hay respuesta en el buffer SerialAt
  38. if (sendCommand((char *) "AT", (char *) "OK", 100)) {
  39. SerialMon.println("El comando AT se ha enviado correctamente");
  40. } else {
  41. SerialMon.println("Fallo en el comando AT, no ha devuelto OK");
  42. }
  43. delay(1);
  44.  
  45. // idem que en el comando anterior
  46. if (sendCommand((char *) "ATE0", (char *) "OK", 100)) {
  47. SerialMon.println("ATEO se ha enviado correctamente");
  48. } else {
  49. SerialMon.println("ATEO ha fallado!");
  50. }
  51. }
  52.  
  53. void loop () {
  54. // nada por aquí...
  55. }
  56.  
  57. /**
  58. * @brief configuramos entradas/salidas digitales
  59. */
  60. void configure() {
  61. pinMode(GSM_PWRKEY, OUTPUT);
  62. pinMode(GSM_PWREN, OUTPUT);
  63. pinMode(GPS_EN, OUTPUT);
  64. pinMode(GSM_STATUS, INPUT);
  65. }
  66.  
  67. /**
  68. * @brief Encendemos el modem dando pulsos HIGH de 1.5 secs en el pin GSM_PWRKEY
  69. *
  70. */
  71. void powerOn() {
  72. uint8_t gsm_status;
  73.  
  74. digitalWrite(GSM_PWREN, HIGH);
  75.  
  76. for (char i=0; i<5; i++) {
  77. gsm_status = digitalRead(GSM_STATUS);
  78. if (gsm_status == HIGH){
  79. SerialMon.println(F("GSM HIGH!!"));
  80. break;
  81. } else {
  82. SerialMon.println(F("GSM LOW!"));
  83. digitalWrite(GSM_PWRKEY, HIGH);
  84. delay(1500);
  85. digitalWrite(GSM_PWRKEY, LOW);
  86. delay(1500);
  87. }
  88. }
  89.  
  90. if (!gsm_status) {
  91. // No se ha podido encender el modem. Revisar que GSM_PWREN, GSM_STATUS y
  92. // GSM_PWRKEY son los pines correctos.
  93. return false;
  94. }
  95.  
  96. // si llegamos aquí el modem se ha encendido
  97. return true;
  98. }
  99.  
  100. bool sendCommand(char *cmd, char * validate, int time) {
  101. int count = 0;
  102. int indexPosition = 0;
  103.  
  104. // vaciamos el buffer del serial
  105. while (SerialAt.available()) SerialAt.read();
  106.  
  107. // vaciamos el buffer local
  108. strcpy(buffer, "");
  109.  
  110. // debug
  111. SerialMon.println(F(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"));
  112. SerialMon.print(F("SerialAt > ")); SerialMon.println(cmd);
  113.  
  114. // enviamos el comando al modem
  115. SerialAt.println(cmd);
  116. SerialAt.flush();
  117.  
  118. delay(time);
  119.  
  120. // Esperamos respuesta comprobando el buffer cada 400 millis
  121. while (!SerialAt.available()) {
  122. delay(400);
  123. SerialMon.println(F("trying again for SerialAt response"));
  124. count++;
  125. if (count > 25) {
  126. SerialMon.println(F("sendSerialAt failed"));
  127. return false;
  128. }
  129. }
  130.  
  131. // acomodamos la respuesta en buffer
  132. while(SerialAt.available() > 0) {
  133. buffer[indexPosition] = SerialAt.read();
  134. indexPosition++;
  135. delay(5);
  136. }
  137.  
  138. // debug
  139. SerialMon.println(buffer);
  140. SerialMon.println(F("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"));
  141.  
  142. // comprobamos si hay datos que validar
  143. if (validate != NULL && strlen(validate) >= 0) {
  144. if (strstr(buffer, validate)) {
  145. return true;
  146. }
  147. return false;
  148. }
  149.  
  150. return true;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement