Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.73 KB | None | 0 0
  1. #include "SoftwareSerial.h"
  2.  
  3. #define countof(a) (sizeof(a) / sizeof(a[0]))
  4. #define A6_OK 0
  5. #define A6_NOTOK 1
  6. #define A6_TIMEOUT 2
  7. #define A6_FAILURE 3
  8.  
  9. #define D0 0
  10. #define D10 10 //Tx
  11. #define D11 11 //Rx
  12.  
  13. #define A6_CMD_TIMEOUT 2000
  14. #define A6_CMD_TIMEOUT_LOCS 8000
  15.  
  16. struct SMSmessage {
  17.   String number;
  18.   String date;
  19.   String message;
  20. };
  21.  
  22. // Instantiate the library with TxPin, RxPin.
  23. SoftwareSerial A6conn(D10, D11);
  24.  
  25. int unreadSMSLocs[30] = {0};
  26. int unreadSMSNum = 0;
  27.  
  28. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  29. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  30. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  31. // ++++ Reboot the module by setting the specified pin HIGH, then LOW. The pin should +++
  32. // be connected to a P-MOSFET, not the A6's POWER pin.
  33. void powerCycle(int pin) {
  34.   Serial.println("\n\t\t\tPower-cycling module...");
  35.  
  36.   pinMode(pin, OUTPUT);
  37.   digitalWrite(pin, LOW);
  38.  
  39.   delay(2000);
  40.  
  41.   pinMode(pin, OUTPUT);
  42.   digitalWrite(pin, HIGH);
  43.  
  44.   // Give the module some time to settle.
  45.   Serial.println("\t\t\t Done, waiting for the module to initialize...");
  46.   delay(20000);
  47.   Serial.println("Done.");
  48.  
  49.   A6conn.flush();
  50. }
  51. // ++++++++++++++++++ Read some data from the A6 in a non-blocking manner +++++++++++++++
  52. String read() {
  53.   String reply = "";
  54.   if (A6conn.available() ) {
  55.     reply = A6conn.readString();
  56.   }
  57.   return reply;
  58. }
  59. //+++++++++++++++++++++++++++ Wait for response +++++++++++++++++++++++++++++++++++++++++
  60. byte A6waitFor(const char *resp1, const char *resp2, int timeout, String *response) {
  61.   unsigned long entry = millis();
  62.   int count = 0;
  63.   String reply = "";
  64.   byte retVal = 99;
  65.  
  66.   do {
  67.     reply += A6conn.readStringUntil('\n');
  68.     Serial.println("REPLY: " + reply);
  69.     yield(); //Passes control to other tasks when called. Ideally yield() should be used in functions that will take awhile to complete.
  70.   } while (((reply.indexOf(resp1) + reply.indexOf(resp2)) == -2) && ((millis() - entry) < timeout)); // do wjile not strings found and timeout not done
  71.  
  72.   if (reply.equals("") == false) {
  73.     Serial.print("Reply in " + String(millis() - entry) + "ms. ");
  74.   }
  75.  
  76.   if (response != NULL) {
  77.     *response = reply;
  78.   }
  79.  
  80.   if ((millis() - entry) >= timeout) {
  81.     retVal = A6_TIMEOUT;
  82.     Serial.println("Timed out.");
  83.   } else {
  84.     if (reply.indexOf(resp1) + reply.indexOf(resp2) > -2) {
  85.       Serial.println("Reply OK. <" + reply + ">");
  86.       retVal = A6_OK;
  87.     } else {
  88.       Serial.println("Reply NOT OK.");
  89.       retVal = A6_NOTOK;
  90.     }
  91.   }
  92.   return retVal;
  93. }
  94. // +++++++++++ Get the real time from the modem. Time will be returned as yy/MM/dd,hh:mm:ss+XX +++++++
  95. String getRealTimeClock() {
  96.   String response = "";
  97.  
  98.   // Issue the command and wait for the response.
  99.   A6command("AT+CCLK?", "OK", "yy", A6_CMD_TIMEOUT, 1, &response);
  100.   int respStart = response.indexOf("+CCLK: \"") + 8;
  101.   response.setCharAt(respStart - 1, '-');
  102.  
  103.   return response.substring(respStart, response.indexOf("\""));
  104. }
  105.  
  106. //++++++++++++++++++++++++++ Issue a command +++++++++++++++++++++++++++++++++++++++++++
  107.  
  108. byte A6command(const char *command, const char *resp1, const char *resp2, int timeout, int repetitions, String *response) {
  109.   byte returnValue = A6_NOTOK;
  110.   byte count = 0;
  111.  
  112.   // Get rid of any buffered output.
  113.   A6conn.flush();
  114.  
  115.   while (count < repetitions && returnValue != A6_OK) {
  116.     Serial.print("\nIssuing command: ");
  117.     Serial.println(command);
  118.  
  119.     A6conn.println(command);
  120.  
  121.     if (A6waitFor(resp1, resp2, timeout, response) == A6_OK) {
  122.       returnValue = A6_OK;
  123.     } else {
  124.       returnValue = A6_NOTOK;
  125.     }
  126.     count++;
  127.   }
  128.   return returnValue;
  129. }
  130. //++++++++++++++++++++++++++++++ DetectRate +++++++++++++++++++++++++++++++++++++++++++++
  131. long detectRate() {
  132.   unsigned long rate = 0;
  133.   unsigned long rates[] = {9600, 115200};
  134.  
  135.   //Try to autodetect rate
  136.   Serial.println("\t\t\tAutodetecting connection rate...");
  137.   for (int i = 0; i < countof(rates); i++) {
  138.     rate = rates[i];
  139.  
  140.     A6conn.begin(rate);
  141.     Serial.print("\nTrying rate ");
  142.     Serial.print(String(rate));
  143.     Serial.println("...");
  144.  
  145.     delay(100);
  146.     if (A6command("AT", "OK", "+CME", 3000, 2, NULL) == A6_OK) {
  147.       return rate;
  148.     }
  149.   }
  150.   Serial.println("\n\t\t\tError. Couldn't detect the rate.");
  151.   return A6_NOTOK;
  152. }
  153. //+++++++++++++++++++++++++++++++++ SetRate +++++++++++++++++++++++++++++++++++++++++++++
  154. char setRate(long baudRate) {
  155.   int state = 0;
  156.  
  157.   state = detectRate();
  158.   if (state == A6_NOTOK) {
  159.     return A6_NOTOK;
  160.   }
  161.  
  162.   // The rate is already the desired rate, return.
  163.   //if (rate == baudRate) return OK;
  164.   delay(3000);
  165.   Serial.println("\n\t\t\tSetting baud rate on the module.");
  166.   return A6_OK;
  167. }
  168. // +++++++++++++++++++++++++++++++++++ Set the SMS charset. ++++++++++++++++++++++++++++++++++++++++
  169. byte setSMScharset(String charset) {
  170.   char buffer[30];
  171.   String buf = ("AT+CSCS=\"" + charset + "\"");
  172.  
  173.   buf.toCharArray(buffer, 30);
  174.   return A6command(buffer, "OK", "yy", A6_CMD_TIMEOUT, 2, NULL);
  175. }
  176. // +++++++++++++ Set the volume for the speaker. level should be a number between 5 and ++++++++++++
  177. // 8 inclusive.
  178. void etVol(byte level) {
  179.   char buffer[30];
  180.  
  181.   // level should be between 5 and 8.
  182.   level = min(max(level, 5), 8);
  183.  
  184.   String buf = "AT+CLVL=" + level;
  185.   buf.toCharArray(buffer, 30);
  186.  
  187.   A6command(buffer, "OK", "yy", A6_CMD_TIMEOUT, 2, NULL);
  188. }
  189. // ++++++++ Enable the speaker, rather than the headphones. Pass 0 to route audio through +++++++++++
  190. // headphones, 1 through speaker.
  191. void enableSpeaker(byte enable) {
  192.   char buffer[30];
  193.  
  194.   // enable should be between 0 and 1.
  195.   enable = min(max(enable, 0), 1);
  196.  
  197.   String buf = "AT+SNFS=" + enable;
  198.   buf.toCharArray(buffer, 30);
  199.  
  200.   A6command(buffer, "OK", "yy", A6_CMD_TIMEOUT, 2, NULL);
  201. }
  202. // ++++++++++ Initialize the software serial connection and change the baud rate from the ++++++++++
  203. // default (autodetected) to the desired speed.
  204.  
  205. byte Begin(long baudRate) {
  206.   String response = "";
  207.  
  208.   A6conn.flush();
  209.  
  210.   if (A6_OK != setRate(baudRate)) {
  211.     return A6_NOTOK;
  212.   }
  213.  
  214.   // Factory reset.
  215.   if (A6_OK != A6command("AT&F", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL)) {
  216.     return A6_FAILURE;
  217.   }
  218.  
  219.   // Echo off.
  220.   if (A6_OK != A6command("ATE0", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL)) {
  221.     return A6_FAILURE;
  222.   }
  223.  
  224. //  // Switch audio to headset.
  225. //  enableSpeaker(0);
  226.  
  227.   // Set caller ID on.
  228.   if (A6_OK != A6command("AT+CLIP=1", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL)) {
  229.     return A6_FAILURE;
  230.   }
  231.  
  232.   // Set SMS to text mode.
  233.   if (A6_OK != A6command("AT+CMGF=1", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL)) {
  234.     return A6_FAILURE;
  235.   }
  236.  
  237.   // Turn SMS indicators off.
  238.   if (A6_OK != A6command("AT+CNMI=1,0", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL)) {
  239.     return A6_FAILURE;
  240.   }
  241.  
  242.   // Set SMS storage to the GSM modem.
  243.   if (A6_OK != A6command("AT+CPMS=ME,ME,ME", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL))
  244.     // This may sometimes fail, in which case the modem needs to be
  245.     // rebooted.
  246.   {
  247.     return A6_FAILURE;
  248.   }
  249.  
  250.   // Set SMS character set.
  251.   if( A6_OK != setSMScharset("UCS2"))
  252.   {
  253.     return A6_FAILURE;    
  254.   }
  255.  
  256.   // AT+CSDH Show Text Mode Parameters (For SMS)
  257.     if (A6_OK != A6command("AT+CSDH=1", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL))
  258.   {
  259.     return A6_FAILURE;
  260.   }
  261.  
  262.   // BEGIN OK
  263.   return A6_OK;
  264. }
  265. //++++++++++++++++++++++ Block until module is ready ++++++++++++++++++++++++++++++++++++
  266. byte blockUntilModuleReady(long baudRate) {
  267.   byte response = A6_NOTOK;
  268.   while (A6_OK != response) {
  269.     response = Begin(baudRate);
  270.     // This means the modem has failed to initialize and we need to reboot
  271.     // it.
  272.     if (A6_FAILURE == response) {
  273.       return A6_FAILURE;
  274.     }
  275.     delay(1000);
  276.     Serial.println("\n\t\t\t Waiting for module to be ready...\n");
  277.   }
  278.   return A6_OK;
  279. }
  280.  
  281. // ++++++++++++++++++++++ Get the strength of the GSM signal. ++++++++++++++++++++++++++++
  282. int getSignalStrength() {
  283.   String response = "";
  284.   uint32_t respStart = 0;
  285.   int strength, error  = 0;
  286.  
  287.   // Issue the command and wait for the response.
  288.   A6command("AT+CSQ", "OK", "+CSQ", A6_CMD_TIMEOUT, 2, &response);
  289.  
  290.   respStart = response.indexOf(",");
  291.   Serial.println(response);
  292.   if (respStart < 0) {
  293.     return 0;
  294.   }
  295.  
  296.   //  sscanf(response.substring(respStart).c_str(), "+CSQ: %d,%d",
  297.  // strength =
  298.  
  299.   // Bring value range 0..31 to 0..100%, don't mind rounding..
  300.   strength = (strength * 100) / 31;
  301.   return strength;
  302. }
  303.  
  304. // ++++++++++++++++++++++++++++++++ Send an SMS. +++++++++++++++++++++++++++++++++++++++++
  305. byte sendSMS(String number, String text) {
  306.   char ctrlZ[2] = { 0x1a, 0x00 };
  307.   char buffer[100];
  308.  
  309.   if (text.length() > 159) {
  310.     // We can't send messages longer than 160 characters.
  311.     return A6_NOTOK;
  312.   }
  313.  
  314.   Serial.print("Sending SMS to ");
  315.   Serial.print(number);
  316.   Serial.println("...");
  317.  
  318.   String buf = "AT+CMGS=\"" + number + "\"";
  319.   buf.toCharArray(buffer, 30);
  320.  
  321.   A6command(buffer, ">", "yy", A6_CMD_TIMEOUT, 2, NULL);
  322.   delay(100);
  323.   A6conn.println(text.c_str());
  324.   A6conn.println(ctrlZ);
  325.   A6conn.println();
  326.  
  327.   return A6_OK;
  328. }
  329.  
  330. // +++++++++++++++++++++++ RECEIVE SMS: RECEIVE an SMS. +++++++++++++++++++++++++++++++++
  331.  
  332. // Retrieve the number and locations of all SMS messages.
  333. int getSMSLocsOfType(int* buf, int maxItems, String type) {
  334.   String seqStart = "+CMGL: ";
  335.   String response = "";
  336.  
  337.   String command = "AT+CMGL=\"";
  338.   command += type;
  339.   command += "\"";
  340.  
  341.   // Issue the command and wait for the response.
  342.   char buffer[30];
  343.   command.toCharArray(buffer, 30);
  344.  
  345.   //byte status = A6command(buffer, "\xff\r\nOK\r\n", "\r\nOK\r\n", A6_CMD_TIMEOUT, 2, &response);
  346.   byte status = A6command(buffer, "+CMGL", "yy", A6_CMD_TIMEOUT_LOCS, 2, &response);
  347.  
  348.   int seqStartLen = seqStart.length();
  349.   int responseLen = response.length();
  350.   int index, occurrences = 0;
  351.  
  352.   // Start looking for the +CMGL string.
  353.   for (int i = 0; i < (responseLen - seqStartLen); i++) {
  354.     // If we found a response and it's less than occurrences, add it.
  355.    
  356.     if (response.substring(i, i + seqStartLen) == seqStart && occurrences < maxItems) {
  357.       // Parse the position out of the reply.
  358.       sscanf(response.substring(i, i + 12).c_str(), "+CMGL: %u,%*s", &index);
  359.  
  360.       buf[occurrences] = index;
  361.       occurrences++;
  362.     }
  363.   }
  364.   return occurrences;
  365. }
  366.  
  367. // ++++++++++++++++++++++++++++++++++++ Retrieve the number and locations of unread SMS messages.
  368. int getUnreadSMSLocs(int* buf, int maxItems) {
  369.   return getSMSLocsOfType(buf, maxItems, "REC UNREAD");
  370. }
  371.  
  372. // ++++++++++++++++++++++++++++++++++++ Retrieve the number and locations of all SMS messages.
  373. int getSMSLocs(int* buf, int maxItems) {
  374.   return getSMSLocsOfType(buf, maxItems, "ALL");
  375. }
  376. // ++++++++++++++++++++++++++++++++++++ Return the SMS at index. ++++++++++++++++++++++++++++++++++++++
  377. SMSmessage readSMS(int index) {
  378.   String response = "";
  379.   char buffer[30];
  380.  
  381.   // Issue the command and wait for the response.
  382.  
  383.   String command = "AT+CMGR="  + index;
  384.   command.toCharArray(buffer, 30);
  385.   // A6command(buffer, "\xff\r\nOK\r\n", "\r\nOK\r\n", A6_CMD_TIMEOUT, 2, &response);
  386.   A6command(buffer, "OK", "+CMGR=", A6_CMD_TIMEOUT, 2, &response);
  387.  
  388.   char message[200];
  389.   char number[50];
  390.   char date[50];
  391.   char type[10];
  392.   int respStart = 0, matched = 0;
  393.   SMSmessage sms = (const struct SMSmessage) {
  394.     "", "", ""
  395.   };
  396.  
  397.   // Parse the response if it contains a valid +CLCC.
  398.   respStart = response.indexOf("+CMGR");
  399.   if (respStart >= 0) {
  400.     // Parse the message header.
  401.     matched = sscanf(response.substring(respStart).c_str(), "+CMGR: \"REC %s\",\"%s\",,\"%s\"\r\n", type, number, date);
  402.     sms.number = String(number);
  403.     sms.date = String(date);
  404.     // The rest is the message, extract it.
  405.     sms.message = response.substring(strlen(type) + strlen(number) + strlen(date) + 24, response.length() - 8);
  406.   }
  407.   return sms;
  408. }
  409.  
  410. // ++++++++++++++++++++++++++++++++++++ Delete the SMS at index. ++++++++++++++++++++++++++++++++++++
  411. byte deleteSMS(int index) {
  412.   char buffer[20];
  413.  
  414.   String command = "AT+CMGD="  + index;
  415.   command.toCharArray(buffer, 20);
  416.   return A6command(buffer, "OK", "yy", A6_CMD_TIMEOUT, 2, NULL);
  417. }
  418.  
  419. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  420. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  421. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  422. void setup() {
  423.   Serial.begin(9600);
  424.   while (A6_OK != blockUntilModuleReady(9600)) {
  425.     delay(100); // spaeter reboot
  426.   }
  427.  // Serial.println("\n\t\t\tStrength of GSM Signal: " + String(getSignalStrength()) +"%.");
  428. }
  429.  
  430.   void loop() {
  431.     int SMSbuffer[30]={0};
  432.     SMSmessage actSMS;
  433.  
  434.     delay (3000);
  435.  //   int SMSCount = getUnreadSMSLocs(*SMSbuffer, 30);
  436.  int SMSCount = getSMSLocs(*SMSbuffer, 30);
  437.    
  438.     for( int i=0; i<30; i++){
  439.     Serial.println("Buffer:" +String(SMSbuffer[i]) );    
  440.     }
  441.  
  442.  
  443.  
  444.    
  445.     return;
  446.   //  if (SMSCount > 0) {  // For Schleife mit Anzahl
  447.       actSMS = readSMS(0);
  448.       Serial.println("Number: " + actSMS.number);
  449.       Serial.println("Date: " + actSMS.date);
  450.       Serial.println("Message: " + actSMS.message);
  451.      // deleteSMS(0); // Replay OK ändern
  452.            actSMS = readSMS(1);
  453.       Serial.println("Number: " + actSMS.number);
  454.       Serial.println("Date: " + actSMS.date);
  455.       Serial.println("Message: " + actSMS.message);
  456.      // deleteSMS(0); // Replay OK ändern
  457.  //     while (true);
  458.    // }
  459.  
  460.  
  461.  
  462.  
  463.     return;
  464.     String myNumber = "06630 0";
  465.  
  466.     sendSMS(myNumber, "TestNeu");
  467.  
  468.  
  469.     delay(15000);
  470.  
  471.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement