Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SoftwareSerial.h"
- #define countof(a) (sizeof(a) / sizeof(a[0]))
- #define A6_OK 0
- #define A6_NOTOK 1
- #define A6_TIMEOUT 2
- #define A6_FAILURE 3
- #define D0 0
- #define D10 10 //Tx
- #define D11 11 //Rx
- #define A6_CMD_TIMEOUT 2000
- #define A6_CMD_TIMEOUT_LOCS 8000
- struct SMSmessage {
- String number;
- String date;
- String message;
- };
- // Instantiate the library with TxPin, RxPin.
- SoftwareSerial A6conn(D10, D11);
- int unreadSMSLocs[30] = {0};
- int unreadSMSNum = 0;
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- // ++++ Reboot the module by setting the specified pin HIGH, then LOW. The pin should +++
- // be connected to a P-MOSFET, not the A6's POWER pin.
- void powerCycle(int pin) {
- Serial.println("\n\t\t\tPower-cycling module...");
- pinMode(pin, OUTPUT);
- digitalWrite(pin, LOW);
- delay(2000);
- pinMode(pin, OUTPUT);
- digitalWrite(pin, HIGH);
- // Give the module some time to settle.
- Serial.println("\t\t\t Done, waiting for the module to initialize...");
- delay(20000);
- Serial.println("Done.");
- A6conn.flush();
- }
- // ++++++++++++++++++ Read some data from the A6 in a non-blocking manner +++++++++++++++
- String read() {
- String reply = "";
- if (A6conn.available() ) {
- reply = A6conn.readString();
- }
- return reply;
- }
- //+++++++++++++++++++++++++++ Wait for response +++++++++++++++++++++++++++++++++++++++++
- byte A6waitFor(const char *resp1, const char *resp2, int timeout, String *response) {
- unsigned long entry = millis();
- int count = 0;
- String reply = "";
- byte retVal = 99;
- do {
- reply += A6conn.readStringUntil('\n');
- Serial.println("REPLY: " + reply);
- yield(); //Passes control to other tasks when called. Ideally yield() should be used in functions that will take awhile to complete.
- } while (((reply.indexOf(resp1) + reply.indexOf(resp2)) == -2) && ((millis() - entry) < timeout)); // do wjile not strings found and timeout not done
- if (reply.equals("") == false) {
- Serial.print("Reply in " + String(millis() - entry) + "ms. ");
- }
- if (response != NULL) {
- *response = reply;
- }
- if ((millis() - entry) >= timeout) {
- retVal = A6_TIMEOUT;
- Serial.println("Timed out.");
- } else {
- if (reply.indexOf(resp1) + reply.indexOf(resp2) > -2) {
- Serial.println("Reply OK. <" + reply + ">");
- retVal = A6_OK;
- } else {
- Serial.println("Reply NOT OK.");
- retVal = A6_NOTOK;
- }
- }
- return retVal;
- }
- // +++++++++++ Get the real time from the modem. Time will be returned as yy/MM/dd,hh:mm:ss+XX +++++++
- String getRealTimeClock() {
- String response = "";
- // Issue the command and wait for the response.
- A6command("AT+CCLK?", "OK", "yy", A6_CMD_TIMEOUT, 1, &response);
- int respStart = response.indexOf("+CCLK: \"") + 8;
- response.setCharAt(respStart - 1, '-');
- return response.substring(respStart, response.indexOf("\""));
- }
- //++++++++++++++++++++++++++ Issue a command +++++++++++++++++++++++++++++++++++++++++++
- byte A6command(const char *command, const char *resp1, const char *resp2, int timeout, int repetitions, String *response) {
- byte returnValue = A6_NOTOK;
- byte count = 0;
- // Get rid of any buffered output.
- A6conn.flush();
- while (count < repetitions && returnValue != A6_OK) {
- Serial.print("\nIssuing command: ");
- Serial.println(command);
- A6conn.println(command);
- if (A6waitFor(resp1, resp2, timeout, response) == A6_OK) {
- returnValue = A6_OK;
- } else {
- returnValue = A6_NOTOK;
- }
- count++;
- }
- return returnValue;
- }
- //++++++++++++++++++++++++++++++ DetectRate +++++++++++++++++++++++++++++++++++++++++++++
- long detectRate() {
- unsigned long rate = 0;
- unsigned long rates[] = {9600, 115200};
- //Try to autodetect rate
- Serial.println("\t\t\tAutodetecting connection rate...");
- for (int i = 0; i < countof(rates); i++) {
- rate = rates[i];
- A6conn.begin(rate);
- Serial.print("\nTrying rate ");
- Serial.print(String(rate));
- Serial.println("...");
- delay(100);
- if (A6command("AT", "OK", "+CME", 3000, 2, NULL) == A6_OK) {
- return rate;
- }
- }
- Serial.println("\n\t\t\tError. Couldn't detect the rate.");
- return A6_NOTOK;
- }
- //+++++++++++++++++++++++++++++++++ SetRate +++++++++++++++++++++++++++++++++++++++++++++
- char setRate(long baudRate) {
- int state = 0;
- state = detectRate();
- if (state == A6_NOTOK) {
- return A6_NOTOK;
- }
- // The rate is already the desired rate, return.
- //if (rate == baudRate) return OK;
- delay(3000);
- Serial.println("\n\t\t\tSetting baud rate on the module.");
- return A6_OK;
- }
- // +++++++++++++++++++++++++++++++++++ Set the SMS charset. ++++++++++++++++++++++++++++++++++++++++
- byte setSMScharset(String charset) {
- char buffer[30];
- String buf = ("AT+CSCS=\"" + charset + "\"");
- buf.toCharArray(buffer, 30);
- return A6command(buffer, "OK", "yy", A6_CMD_TIMEOUT, 2, NULL);
- }
- // +++++++++++++ Set the volume for the speaker. level should be a number between 5 and ++++++++++++
- // 8 inclusive.
- void etVol(byte level) {
- char buffer[30];
- // level should be between 5 and 8.
- level = min(max(level, 5), 8);
- String buf = "AT+CLVL=" + level;
- buf.toCharArray(buffer, 30);
- A6command(buffer, "OK", "yy", A6_CMD_TIMEOUT, 2, NULL);
- }
- // ++++++++ Enable the speaker, rather than the headphones. Pass 0 to route audio through +++++++++++
- // headphones, 1 through speaker.
- void enableSpeaker(byte enable) {
- char buffer[30];
- // enable should be between 0 and 1.
- enable = min(max(enable, 0), 1);
- String buf = "AT+SNFS=" + enable;
- buf.toCharArray(buffer, 30);
- A6command(buffer, "OK", "yy", A6_CMD_TIMEOUT, 2, NULL);
- }
- // ++++++++++ Initialize the software serial connection and change the baud rate from the ++++++++++
- // default (autodetected) to the desired speed.
- byte Begin(long baudRate) {
- String response = "";
- A6conn.flush();
- if (A6_OK != setRate(baudRate)) {
- return A6_NOTOK;
- }
- // Factory reset.
- if (A6_OK != A6command("AT&F", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL)) {
- return A6_FAILURE;
- }
- // Echo off.
- if (A6_OK != A6command("ATE0", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL)) {
- return A6_FAILURE;
- }
- // // Switch audio to headset.
- // enableSpeaker(0);
- // Set caller ID on.
- if (A6_OK != A6command("AT+CLIP=1", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL)) {
- return A6_FAILURE;
- }
- // Set SMS to text mode.
- if (A6_OK != A6command("AT+CMGF=1", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL)) {
- return A6_FAILURE;
- }
- // Turn SMS indicators off.
- if (A6_OK != A6command("AT+CNMI=1,0", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL)) {
- return A6_FAILURE;
- }
- // Set SMS storage to the GSM modem.
- if (A6_OK != A6command("AT+CPMS=ME,ME,ME", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL))
- // This may sometimes fail, in which case the modem needs to be
- // rebooted.
- {
- return A6_FAILURE;
- }
- // Set SMS character set.
- if( A6_OK != setSMScharset("UCS2"))
- {
- return A6_FAILURE;
- }
- // AT+CSDH Show Text Mode Parameters (For SMS)
- if (A6_OK != A6command("AT+CSDH=1", "OK", "yy", A6_CMD_TIMEOUT, 2, NULL))
- {
- return A6_FAILURE;
- }
- // BEGIN OK
- return A6_OK;
- }
- //++++++++++++++++++++++ Block until module is ready ++++++++++++++++++++++++++++++++++++
- byte blockUntilModuleReady(long baudRate) {
- byte response = A6_NOTOK;
- while (A6_OK != response) {
- response = Begin(baudRate);
- // This means the modem has failed to initialize and we need to reboot
- // it.
- if (A6_FAILURE == response) {
- return A6_FAILURE;
- }
- delay(1000);
- Serial.println("\n\t\t\t Waiting for module to be ready...\n");
- }
- return A6_OK;
- }
- // ++++++++++++++++++++++ Get the strength of the GSM signal. ++++++++++++++++++++++++++++
- int getSignalStrength() {
- String response = "";
- uint32_t respStart = 0;
- int strength, error = 0;
- // Issue the command and wait for the response.
- A6command("AT+CSQ", "OK", "+CSQ", A6_CMD_TIMEOUT, 2, &response);
- respStart = response.indexOf(",");
- Serial.println(response);
- if (respStart < 0) {
- return 0;
- }
- // sscanf(response.substring(respStart).c_str(), "+CSQ: %d,%d",
- // strength =
- // Bring value range 0..31 to 0..100%, don't mind rounding..
- strength = (strength * 100) / 31;
- return strength;
- }
- // ++++++++++++++++++++++++++++++++ Send an SMS. +++++++++++++++++++++++++++++++++++++++++
- byte sendSMS(String number, String text) {
- char ctrlZ[2] = { 0x1a, 0x00 };
- char buffer[100];
- if (text.length() > 159) {
- // We can't send messages longer than 160 characters.
- return A6_NOTOK;
- }
- Serial.print("Sending SMS to ");
- Serial.print(number);
- Serial.println("...");
- String buf = "AT+CMGS=\"" + number + "\"";
- buf.toCharArray(buffer, 30);
- A6command(buffer, ">", "yy", A6_CMD_TIMEOUT, 2, NULL);
- delay(100);
- A6conn.println(text.c_str());
- A6conn.println(ctrlZ);
- A6conn.println();
- return A6_OK;
- }
- // +++++++++++++++++++++++ RECEIVE SMS: RECEIVE an SMS. +++++++++++++++++++++++++++++++++
- // Retrieve the number and locations of all SMS messages.
- int getSMSLocsOfType(int* buf, int maxItems, String type) {
- String seqStart = "+CMGL: ";
- String response = "";
- String command = "AT+CMGL=\"";
- command += type;
- command += "\"";
- // Issue the command and wait for the response.
- char buffer[30];
- command.toCharArray(buffer, 30);
- //byte status = A6command(buffer, "\xff\r\nOK\r\n", "\r\nOK\r\n", A6_CMD_TIMEOUT, 2, &response);
- byte status = A6command(buffer, "+CMGL", "yy", A6_CMD_TIMEOUT_LOCS, 2, &response);
- int seqStartLen = seqStart.length();
- int responseLen = response.length();
- int index, occurrences = 0;
- // Start looking for the +CMGL string.
- for (int i = 0; i < (responseLen - seqStartLen); i++) {
- // If we found a response and it's less than occurrences, add it.
- if (response.substring(i, i + seqStartLen) == seqStart && occurrences < maxItems) {
- // Parse the position out of the reply.
- sscanf(response.substring(i, i + 12).c_str(), "+CMGL: %u,%*s", &index);
- buf[occurrences] = index;
- occurrences++;
- }
- }
- return occurrences;
- }
- // ++++++++++++++++++++++++++++++++++++ Retrieve the number and locations of unread SMS messages.
- int getUnreadSMSLocs(int* buf, int maxItems) {
- return getSMSLocsOfType(buf, maxItems, "REC UNREAD");
- }
- // ++++++++++++++++++++++++++++++++++++ Retrieve the number and locations of all SMS messages.
- int getSMSLocs(int* buf, int maxItems) {
- return getSMSLocsOfType(buf, maxItems, "ALL");
- }
- // ++++++++++++++++++++++++++++++++++++ Return the SMS at index. ++++++++++++++++++++++++++++++++++++++
- SMSmessage readSMS(int index) {
- String response = "";
- char buffer[30];
- // Issue the command and wait for the response.
- String command = "AT+CMGR=" + index;
- command.toCharArray(buffer, 30);
- // A6command(buffer, "\xff\r\nOK\r\n", "\r\nOK\r\n", A6_CMD_TIMEOUT, 2, &response);
- A6command(buffer, "OK", "+CMGR=", A6_CMD_TIMEOUT, 2, &response);
- char message[200];
- char number[50];
- char date[50];
- char type[10];
- int respStart = 0, matched = 0;
- SMSmessage sms = (const struct SMSmessage) {
- "", "", ""
- };
- // Parse the response if it contains a valid +CLCC.
- respStart = response.indexOf("+CMGR");
- if (respStart >= 0) {
- // Parse the message header.
- matched = sscanf(response.substring(respStart).c_str(), "+CMGR: \"REC %s\",\"%s\",,\"%s\"\r\n", type, number, date);
- sms.number = String(number);
- sms.date = String(date);
- // The rest is the message, extract it.
- sms.message = response.substring(strlen(type) + strlen(number) + strlen(date) + 24, response.length() - 8);
- }
- return sms;
- }
- // ++++++++++++++++++++++++++++++++++++ Delete the SMS at index. ++++++++++++++++++++++++++++++++++++
- byte deleteSMS(int index) {
- char buffer[20];
- String command = "AT+CMGD=" + index;
- command.toCharArray(buffer, 20);
- return A6command(buffer, "OK", "yy", A6_CMD_TIMEOUT, 2, NULL);
- }
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- void setup() {
- Serial.begin(9600);
- while (A6_OK != blockUntilModuleReady(9600)) {
- delay(100); // spaeter reboot
- }
- // Serial.println("\n\t\t\tStrength of GSM Signal: " + String(getSignalStrength()) +"%.");
- }
- void loop() {
- int SMSbuffer[30]={0};
- SMSmessage actSMS;
- delay (3000);
- // int SMSCount = getUnreadSMSLocs(*SMSbuffer, 30);
- int SMSCount = getSMSLocs(*SMSbuffer, 30);
- for( int i=0; i<30; i++){
- Serial.println("Buffer:" +String(SMSbuffer[i]) );
- }
- return;
- // if (SMSCount > 0) { // For Schleife mit Anzahl
- actSMS = readSMS(0);
- Serial.println("Number: " + actSMS.number);
- Serial.println("Date: " + actSMS.date);
- Serial.println("Message: " + actSMS.message);
- // deleteSMS(0); // Replay OK ändern
- actSMS = readSMS(1);
- Serial.println("Number: " + actSMS.number);
- Serial.println("Date: " + actSMS.date);
- Serial.println("Message: " + actSMS.message);
- // deleteSMS(0); // Replay OK ändern
- // while (true);
- // }
- return;
- String myNumber = "06630 0";
- sendSMS(myNumber, "TestNeu");
- delay(15000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement