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!

Send SMS with an A6 GSM module

AI Thinker, the makers of my favorite micro controller, the ESP8266 have done it again with the A6 GSM module allowing us to connect to the GSM network and send sms, connect to the internet and even make phone calls!

What you’ll need:

  • A6 GSM moduleAn A6 module (This is an A6 mini bought you can find on ebay, aliexpress ect.. for 5 to 10€)
  • Sim card A Sim card with a valid subscription. Remember to remove the pin before starting by inserting it into a phone and deactivating the pin number.
  • Breadbord and jumper wires A breadboard and some jumper wires.
  • FDTI programer module An FTDI programer (2 to 5€ on Ebay ou aliexpress)
  • micro USB breakout board A micro usb breakout board get power to it all easily
  • Power supply A relatively powerful wall wart (here I recycled a 5V/2A power supply from a raspberry pi I had laying around ). Sending sms takes quite a lot of power for sending.
  • CoolTerm A computer with Windows and CoolTerm

Wiring

A6 wiring diagram
Wiring scematic of the A6 module with an FTDI

As you can see, I used my awesome artistic skills to come up with this horrible diagram!!

We need to power the module with our phone charger, join the FTDI and A6 module neutrals and then connect the TX of the of the RX pin, of the A6 and vice versa.

The finished setup
The finished setup

To the code!

Once you have pluged the FTDI in, CoolTerm should find the COM port on it’s own. You can also find it in windows device manager :

Peripherals

In CoolTerm, set the baudrate to 115200 and click on Connect. It takes a few seconds but it should come up with something like this (if not, try unpluging and pluging the A6 module power back in again) :

Connecting to the GSM network

+CREG: 5 means the module was able to conect to the network and is ready to go!

If nothing comes up, check all the connections again, especially that the RX/TX pins.

Snding an SMS

To send an SMS, go to Connection -> Send String ou Ctrl-T. The AT command (for attention) followed by a new line will return the current state :

send AT commandAT serial response

 The module should answer “ok”, however in some cases I got CME 58 errors. I think it means that it temporarily lost the signal, just sending AT again should work.

Next, we need to set the module to SMS mode :

AT+CMGF=1

followed with a new line:

Setting SMS mode

And finaly, to send the message :

AT+CMGS="003361234567"

(replacing the phone number with your’s) It will ask you to enter your message :

Sending SMS

Wait a few seconds and you should have youself a new message on your phone!

Traps

I had a few problems due the poor quality of my chinese jumper wires… I was also using a faulty usb extender cable.

Here is a document that helped me with certain errors and AT commands : GSM Developer Guide – GSM AT Commands – Rev A

Conclusion

Once all the wiring was in done and checked and the module answers, you only need 3 lines to send an sms! In the next article, I’ll try and send one from an arduino/ESP8266 instead of the computer.