Le tout assemblé et branché

Send SMS with an ESP8266 and an A6 GSM module

This article takes off from where I left off in the previous one (https://www.twiggotronix.com/en/send-sms-with-an-a6-gsm-module/), I recommend you read that one first.

In the previous article, we managed to send SMSs with an FTDI programmer board. This time we’re going to replace it with a microcontroller (ESP8266 in this case but it should work with just about any arduino).

What you’ll need:

More or less the same as for the previous article, plus a microcontroller. I used a WEMOS D1 mini :

Wemos D1 mini
Wemos D1 mini

I buy them straight from WEMOS Electronics, Delivery tends to be a bit long, but they’re cheap and really reliable.

Wiring

I made a bit of an effort this time 😉 and did the diagram out in fritzing :

Wiring diagram
Wiring diagram

I added a wire between D0 on the D1 and the RST pin on the A6 that will allow us to restart the module when we’re ready. Then I just added power and hooked up D1 and D2 respectiverly to UTX and URX on the GSM module.

At this point, I thought it would be a good idea to make things a bit more permanent and broke out a perfboard. This will make sure I don’t get errors due to wiring… You can however just leave everything on the breadboard.

Perfboard from above
Perfboard from above
Perfboard from bellow
Perfboard from bellow (sorry if my soldering job makes you nauseous…)
The assembled kit
The assembled kit

To the code!

Initialisation

Communication between our GSM module and our microcontroller is done using the standard softwareSerial librairy:

#include <SoftwareSerial.h>

#define RX_PIN D1 // Green wire
#define TX_PIN D2 // Orange wire
#define RESET_PIN D0 // Yellow wire

SoftwareSerial a6Serial(RX_PIN, TX_PIN);

void setup() {
    Serial.begin(9600);
    a6Serial.begin(115200); //The A6's defaul baudrate is 115200
    delay(500);
}
void loop() {}

Then we can restart our A6 module. This step is probably not necessary but allows us to keep track of the GSM module’s state.

void resetModule() {
    digitalWrite(RESET_PIN, HIGH);
    delay(200);
    digitalWrite(RESET_PIN, LOW); 
}

void setup() {
    Serial.begin(9600);
    a6Serial.begin(115200); //The A6's defaul baudrate is 115200
    delay(500);

    pinMode(RESET_PIN, OUTPUT);
    resetModule(); //restart the GSM module
}

To finish setting everything up, we need to wait for the module to connect to the GSM network, and then set SMS mode :

void waitUntilReady()
{
  Serial.println("Waiting for network");
  while(state == "initing" || state == "sms_mode") {  
    if (a6Serial.available()) {
      String recievedA6 = a6Serial.readString();
      Serial.println(recievedA6);
      if(state == "initing" && recievedA6.indexOf("+CREG: 5") >= 0) {
        Serial.println("Mode sms");
        state = "sms_mode";
        delay(2000);
        a6Serial.println("AT+CMGF=1");  
      } else if(state == "sms_mode" && recievedA6.indexOf("OK") >= 0) {
        state = "ready";
        Serial.println("Ready!");
      }
    }
    yield(); // hands controll back to the ESP8266 to avoid wifi connexion loss
  }
}

Send an SMS

As seen in the previous article, sending an SMS is done using AT commands:

followed by the the message and Ctrl-Z:

void sendSms(String number, String message) {
  if(state == "ready") {
    Serial.println("Sending \"" + message + "\" to " + number);
    a6Serial.print("AT+CMGS=\"");
    a6Serial.print(number);
    a6Serial.write(0x22);
    a6Serial.write(0x0D);  // Return carriage in hexadecimal
    a6Serial.write(0x0A);  // New line in hexadecimal
    delay(2000);
    a6Serial.print(message);
    delay(500);
    a6Serial.println(char(26));// Control-Z in hexadecimal
  }
}

Just for the example, I send an SMS using a custom sendSMS command to the ESP8266 over  Serial :

void loop() {
  if(Serial.available()) {
    String received = Serial.readString();
    received.trim();
    if(state == "ready") {
      if(received == "sendSms") {
        sendSms(PHONE_NUMBER, MESSAGE);
      }
    }
  }
  if (a6Serial.available()) {
      String recievedA6 = a6Serial.readString();
      Serial.println("[A6] " + recievedA6);
  }
}

The result is :

Result of sending an SMS
Result of sending an SMS

The full code

#include <Arduino.h>
#include <SoftwareSerial.h>

#define RX_PIN D1 // Green wire
#define TX_PIN D2 // Orange wire
#define RESET_PIN D0 // Yellow wire

#define PHONE_NUMBER "+336XXXXXXXXX" // Replace with your country code (+33 is for France) and phone number
#define MESSAGE "Whoop whoop"

SoftwareSerial a6Serial(RX_PIN, TX_PIN);
String state = "initing";

void resetModule() {
    digitalWrite(RESET_PIN, HIGH);
    delay(200);
    digitalWrite(RESET_PIN, LOW); 
}

void waitUntilReady()
{
  Serial.println("Waiting for network");
  while(state == "initing" || state == "sms_mode") {  
    if (a6Serial.available()) {
      String recievedA6 = a6Serial.readString();
      Serial.println(recievedA6);
      if(state == "initing" && recievedA6.indexOf("+CREG: 5") >= 0) {
        Serial.println("Sms mode");
        state = "sms_mode";
        delay(2000);
        a6Serial.println("AT+CMGF=1");  
      } else if(state == "sms_mode" && recievedA6.indexOf("OK") >= 0) {
        state = "ready";
        Serial.println("Ready!");
      }
    }
    yield(); // hands controll back to the ESP8266 to avoid wifi connexion loss
  }
}

void sendSms(String number, String message) {
  if(state == "ready") {
    Serial.println("Sending \"" + message + "\" to " + number);
    a6Serial.print("AT+CMGS=\"");
    a6Serial.print(number);
    a6Serial.write(0x22);
    a6Serial.write(0x0D);  // Return carriage in hexadecimal
    a6Serial.write(0x0A);  // New line in hexadecimal
    delay(2000);
    a6Serial.print(message);
    delay(500);
    a6Serial.println(char(26));// Control-Z in hexadecimal
  }
}

void setup() {
    Serial.begin(9600);
    a6Serial.begin(115200); //The A6's defaul baudrate is 115200
    delay(500);

    pinMode(RESET_PIN, OUTPUT);
    resetModule(); //restart the GSM module
    waitUntilReady(); //Wait for the GSM network
}

void loop() {
  if(Serial.available()) {
    String received = Serial.readString();
    received.trim();
    if(state == "ready") {
      if(received == "sendSms") {
        sendSms(PHONE_NUMBER, MESSAGE);
      }
    }
  }
  if (a6Serial.available()) {
      String recievedA6 = a6Serial.readString();
      Serial.println("[A6] " + recievedA6);
  }
}

Conclusion

We now have a small module that allows us to send SMSs! Now all we need to do is to turn it into an even more connected thing!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>