Facebook
From Rude Tamarin, 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 143
  1. /**
  2.  * @file main.cpp
  3.  * @brief rhomb.io SIM868 demo
  4.  * @author rhomb.io Software Team
  5.  *
  6.  * Version 1.0
  7.  *
  8.  * @copyright Copyright (c) 2019
  9.  */
  10.  
  11. #include <Arduino.h>
  12.  
  13. // Include rhio-pinmap and enable one MCU
  14. // #define S100_DUINO_UNO_v1_0
  15. // #define S100_DUINO_LEONARDO_v1_0
  16. // #define S200_DUINO_UNO_PRO_v1_0
  17. #define S200_DUINO_ZERO_v1_0
  18. // #define S100_DUINO_MEGA_v2_0
  19. // #define S100_ESP32_WROOM_32_v1_0
  20. // #define S100_ESP_WROOM_02_v1_0
  21. #include "rhio-pinmap.h"
  22.  
  23. // SIM868 pins on Socket 1
  24. #define GSM_PWREN  IO0
  25. #define GSM_PWRKEY IO1
  26. #define GPS_EN     IO2
  27. #define GSM_STATUS IO3
  28.  
  29. // SIM868 pins on Socket 2 (only Duino Mega)
  30. // #define GSM_PWREN  IO8
  31. // #define GSM_PWRKEY IO9
  32. // #define GPS_EN     IO10
  33. // #define GSM_STATUS IO11
  34.  
  35. // SIM868 apn parameters
  36. #define RH_apn "m2m.orange.es"
  37. #define RH_apnUser ""
  38. #define RH_apnPass ""
  39. #define RH_remoteServer "45.76.37.219"
  40. #define RH_remotePort "3136"
  41.  
  42. // Monitor serial (debug) and modem At commands
  43. #ifdef S200_DUINO_ZERO_v1_0
  44.   #define SerialMon SerialUSB
  45.   #define SerialAt Serial1 // Serial1 = UART-A
  46. #endif
  47.  
  48. #ifdef S100_DUINO_UNO_v1_0
  49.   #include SoftwareSerial
  50.   SoftwareSerial SoftSerial(IO6, IO7); // also may be SDA,SCL, MISO,MOSI and others
  51.   #define SerialMon SoftSerial
  52.   #define SerialAt Serial // Serial1 = UART-A
  53. #endif
  54.  
  55. #ifdef S100_DUINO_MEGA_v2_0
  56.   #define SerialMon Serial1 // UART-B
  57.   #define SerialAt Serial // UART-A
  58. #endif
  59.  
  60. // At commands responses will be allocated here
  61. u_int8_t signal_quality;
  62. char buffer[30];
  63.  
  64. void configureIos () {
  65.   pinMode(GSM_PWRKEY, OUTPUT);
  66.   pinMode(GSM_PWREN, OUTPUT);
  67.   pinMode(GPS_EN, OUTPUT);
  68.   pinMode(GSM_STATUS, INPUT);
  69. }
  70.  
  71. void configureSerial () {
  72.   SerialMon.begin(9600);
  73.   while (!SerialMon) {}
  74.  
  75.   SerialAt.begin(9600);
  76.   while (!SerialAt) {}
  77. }
  78.  
  79. /**
  80.  * @brief Turn on the modem
  81.  *
  82.  * To be able to use the SIM868 module we have to activate the PWREN line and
  83.  * make a pulse of 1.5 seconds in the PWRKEY line. The modem will have been
  84.  * turned on when the STATUS line goes to high level.
  85.  *
  86.  * This allows the MCU to control the power of the device, necessary for low
  87.  * power applications. By default the modem will be turned off and will only
  88.  * turn on if we perform the turn on routine as seen in this method.
  89.  *
  90.  * @return true if the modem has been turned on
  91.  * @return false
  92.  */
  93. bool powerOn () {
  94.   uint8_t gsm_status;
  95.  
  96.   digitalWrite(GSM_PWREN, HIGH);
  97.  
  98.   for (char i=0; i<5; i++) {
  99.     gsm_status = digitalRead(GSM_STATUS);
  100.     if (gsm_status == HIGH){
  101.       SerialMon.println(F("GSM HIGH!!"));
  102.       break;
  103.     } else {
  104.       SerialMon.println(F("GSM LOW!"));
  105.       digitalWrite(GSM_PWRKEY, HIGH);
  106.       delay(1500);
  107.       digitalWrite(GSM_PWRKEY, LOW);
  108.       delay(1500);
  109.     }
  110.   }
  111.  
  112.   if (!gsm_status) {
  113.     return false;
  114.   }
  115.  
  116.   // Turn on the GPS. It is not necessary for modem operation but this demo
  117.   // will make use of GPS.
  118.   digitalWrite(GPS_EN, HIGH);
  119.   SerialAt.println("AT+CGNSPWR=1");
  120.   delay(3000);
  121.  
  122.   // We obviate the result of enabling GPS
  123.   return true;
  124. }
  125.  
  126. void sendAt(const char * command){
  127.  
  128.   strcpy(buffer,"");
  129.   SerialAt.println(command);
  130.   uint32_t init=millis();
  131.  
  132.   int count=0;
  133.     while (SerialAt.available() || (millis() - init) < 3000)
  134.     {
  135.       buffer[count] = SerialAt.read();
  136.       count++;
  137.       delay(10);
  138.     }
  139.  
  140.       buffer[count]='\n';
  141.  
  142.   SerialMon.print("La respuesta es: ");
  143.   SerialMon.println(buffer);
  144.  
  145. }
  146.  
  147. void setup () {
  148.   configureIos();
  149.   configureSerial();
  150.  
  151.   if (!powerOn()) {
  152.     SerialMon.println("Fail to power on the modem. Is the SIM868 connected to the Socket 1?");
  153.     for (;;) {}
  154.   }
  155.  
  156.   sendAt("AT"); // test modem, should return "OK"
  157.   if(strcmp(buffer,"OK")){
  158.     sendAt("AT+SHUT");   //sets down any previous gprs conexions, should return "SHUT OK"
  159.     sendAt("AT+CIPMUX=0");   //sets single conexion mode, should return "OK"
  160.     sendAt("AT+CGATT=1");    //attaches GPRS Service, should return "OK"
  161.     sendAt("AT+CSTT=RH_apn,RH_apnUser,RH_apnPass");
  162.     sendAt("AT+CIICR"); //brings up the GPRS connexions, sometimes it takes a bit to respond, but should return "OK"
  163.     sendAt("AT+CIFSR");   //gives us our IP Address
  164.     sendAt("AT+CIPSTART=TCP,RH_remoteServer,RH_remotePort");
  165.   }
  166.  
  167. }
  168.  
  169. bool checkCreg () {
  170.   for (auto i = 0; i < 20; i++) {
  171.     sendAt("AT+CREG?");
  172.     if (strstr(buffer, ",1")) {
  173.       // tenemos CREG!!!
  174.       return true;
  175.     }
  176.     delay(1000);
  177.   }
  178.  
  179.   return false;
  180. }
  181.  
  182. void loop () {
  183.   // Get GPS. It will be necessary a few minutes before obtaining data.
  184.   // The important thing for this demo is not to get GPS data, but to check
  185.   // that we can send a command to the modem and get a response.
  186.   // SerialAt.println("AT+CGNSINF");   (Read GPS location data)    
  187.   // delay(1000);
  188.  
  189.   sendAt("AT");
  190.   if (strcmp(buffer, "OK")) {
  191.     SerialMon.println("El modem funciona!!");
  192.   } else {
  193.     Serial.println("Huston tenemos un problema");
  194.   }
  195.  
  196.   if (checkCreg()) {
  197.     SerialMon.println("Tenemos conexión a red!!");
  198.   }
  199.  
  200.   //sendAt("AT+CSQ");
  201.  
  202.  
  203.  
  204.   // wait for loop
  205.   delay(5000);
  206. }
  207.  
  208. //AT+CSQ (Comando AT que indica la calidad de la señal o la cobertura)
  209.