Connection of GSM module SIM800L to ESP8266/ESP32/Arduino

Module SIM800L

In the previous article I have considered in detail the relatively expensive (color of the sea Wave:-)) Board module SIM800C and its connection to ESP32/ESP8266. Circuit board SIM800C I did not like. In my opinion there is no sense To overpay for this module, it is better to take the inexpensive "red" module SIM800C/L and a bit of it to improve, adding in the schematic technician:

  • Resistive Divider for RX module input for level matching. The Calculation of the divisor in the previous article.
  • DC-DC step down converter. The scheme used inexpensive module Mini-360 for $0.3.
  • Several Tanks for filtering DC-DC pulsations are step down converter-A.

At least this module gets into the mounting board without any problems, as the distance between the "comb" connectors multiple 2.54 mm. The Nuances of working with SIM800 are described in detail in the previous article.

Wiring Diagram SIM800L to ESP32

Since The SIM800L module I have used in expensive equipment that should work guaranteed, I have added tanks according to the recommendations of datasheet.

The Tantalum capacitor on the module is split. I have added an electrolytic capacitor for 2200 Μf and the rest of the tanks by datasheet.

The ESP32 Microcontroller In this example was fed from the USB port of the laptop.

Because One port is used for data transfer and the second is for reception, you can use a resistive divider instead of a level converter. But there is one risk involved in this way. If all to dissolve correctly, but thus programmatically, at specification of numbers of pins for RX and TX, it is possible to confuse them and to burn TXD port of Chip SIM800. Therefore, in case of such economy it is necessary to write code very carefully. 🙂

If you do the conversion of levels in a serious way, then you need to use the board of the level converter, supplied by the microcontroller support voltage 3, 3V, and from the side of the SIM800L the reference voltage removed from the simple chain resistor and stabilitron. Her calculation is here. Stabilitrons happen on 2.7 V and 3 V.

The Datasheet module Specifies the maximum level of the logical unit at the RX input-3.1 B (with a minimum of 2.1 V).  Accordingly, 2.7 V will be enough, and 3 V is already risky. If you look at the datasheet voltage to exit the reference voltage SIM800L

Just the minimum value of 2.7 V. Immediately say-I have not tried this scheme, limited to a simple option with resistive divider.

Connect SIM800L to ESP32 Devkit 38 pin
Connect SIM800L to ESP32 Devkit 38 pin
Mini 360 DC-DC step down converter
Mini 360 DC-DC step down converter

Mini-360 DC-DC Converter is not the best option. It normally withstands the load, SIM800L loaded with it stably, but the quality of the variable resistor is very mediocre. In the Case of vibrations in the production room where the equipment will be placed, the output voltage may "depart" from the set value. So This DC-DC converter will be on the menu for something more quality.

However, the module is tiny, so it is easily placed together with the tanks (except electrolyte) and resistors under the module SIM800L. The GSM module was dampened on the connector.

Power supply 12V, 3A

A 12 V power supply unit Was used as a power supply on 3A. For the price of 4 USD-just a magical quality. I Took it here.

Immediately Note that the powerful 4-port USB charging AUKEY 5V, by 2.4 A on each USB port connected to the power supply for breadboard-A, the load did not pull. At loading of the module there is a peak power consumption and the voltage on the GSM module woke up to 3 V. The Module did not even startanul really. There May be a problem in the BP for Breadboard. He even had no load to give less than 5 V.

Code to work with SIM800L on ESP32

The Code was tested on the 38-inch MH-ET Live ESP32 Devkit 38 pins. This piece of code in the project is in the form of a *. cpp file, so simply rename the Init_GSM_SIM800L () function to setup () and add the loop () function.

#include "GSM_SIM800L. H"
/*
 * There are three serial ports on the ESP known as U0UXD, U1UXD and U2UXD.
 * 
 * U0UXD is used to communicate with the ESP32 for programming and during reset/boot.
 * U1UXD is unused and can be used for your projects. Some boards Use this port for SPI Flash access though
 * U2UXD is unused and can be used for your projects.
 * 
*/
#define TINY_GSM_MODEM_SIM800

#define DEBUG/enable DEBUG mode

#include < TinyGsmClient. h.
 
#define RXD2 16
#define TXD2 17

#define Max_Modem_Reboots 5

#define APN_NAME "internet.beeline.ru"
#define APN_USER "Beeline"
#define APN_PSWD "Beeline"

#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 38400

Set your CAD pin (optional)
#define SIM_PIN ""

Define your board pin here
#define LED_PIN 27

TinyGsm modemGSM (SERIAL2);

int Modem_Reboots_Counter = 0;

Command must be without AT prefix
/* String sendAT (String command)
{
  modemGSM. sendAT (GF (command));
  if (modemGSM. waitResponse (GF (GSM_NL))! = 1) {
      return "Empty";
  }

  String res = modemGSM. Stream. readStringUntil (' n ');
  modemGSM. waitResponse ();
  return res;
}*/

Command must be without AT prefix
String sendAT (String command)
{
  String response = "";
  Serial2. println ("AT" + command);
  while (! Serial2. Available ());
 
  Response = Serial2. readString ();
 
  return response;
}
  
String printAT (String command, String message = "")
{
  String res = "";
  if (Message! = "")
  {
    Serial. println (message);
  }
  
  if (Command! = "")
  {
    Serial. println ("AT" + command);
    res = sendAT (command);
    Serial. println (RES);
  }
  return res;  
}

void RestartGSMModem ()
{
    Serial. println ("Restarting GSM...");
    if (! modemGSM. Restart ())
    {
      Serial. println ("Tfaille. :-(rn ");
      ESP. restart ();
    } 
    if (Modem_Reboots_Counter < Max_Modem_Reboots)
    {
      Init_GSM_SIM800L ();
    }
    Modem_Reboots_Counter + +;
}

String GSMSignalLevel (int level)
{
  Switch (level)
  {
    case 0:
      Return "-115 dBm or less";
    case 1:
      Return "-111 dBm";
    Case 31:
      Return "-52 dBm or greater";
    Case 99:
      Return "not known or not detectable";
    default:   
         if (Level > 1 & & Level < 31)
           Return "-110... -54 dBm ";
  }
  return "Unknown";
}

String GSMRegistrationStatus (RegStatus State)
{
  Switch (state)
  {
    Case REG_UNREGISTERED:
      Return "Not registered, MT is not currently searching a new operator to register to";
    Case REG_SEARCHING:
      Return "Not registered, but MT is currently searching a new operator to register to";
    Case REG_DENIED:
      Return "Registration denied";
    Case REG_OK_HOME:
      Return "Registered, home Network";
    Case REG_OK_ROAMING:
      Return "Registered, roaming";
    Case REG_UNKNOWN:
      return "Unknown"; 
  }
  return "Unknown";
}

String SwapLocation (String location)
{
   int i = location. indexOf (', ');
   Int j = location. indexOf (', ', i + 1);
   String longitude = location. substring (i + 1, j); 
   i = location. indexOf (', ', J + 1);
   String latitude = location. substring (j + 1, i); 
   Return latitude + "," + longitude;
}

void Init_GSM_SIM800L () {

  Serial2. Begin (9600, SERIAL_8N1, RXD2, TXD2);
  Serial. println ("Serial GSM Txd is on pin:" + String (TXD2));
  Serial. println ("Serial GSM Rxd is on pin:" + String (RXD2));

  pinMode (LED_PIN, OUTPUT);

  Delay (3000);

  TinyGsmAutoBaud (Serial2, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);

  String info = modemGSM. getModemInfo ();
  Serial. println (info);
 
  if (! modemGSM. Restart ())
  {
    RestartGSMModem ();
  }
  Else
  {
    Serial. println ("Modem restart OK");
  }

  printAT ("+ CMEE = 2", "Enable + CME ERROR: < Err > result code and use verbose < err > values.");
  printAT ("+ COPS =?", "Show List of available operators")

  if (modemGSM. getSimStatus ()! = 3)//Printt ("AT + CPIN?", "Check PIN Code Status"). startsWith ("+ CME ERROR:"))
  {
    Serial. println ("Check PIN code for the SIM.");
    if (SIM_PIN! = "")
    {
     Serial. println ("Try to unlock SIM PIN.");
      modemGSM. simUnlock (SIM_PIN);
      Delay (3000);
      if (modemGSM. getSimStatus ()! = 3)
      {
        RestartGSMModem ();
      }
    }
  }
 
  if (! modemGSM. waitForNetwork ()) 
  {
    Serial. println ("Failed to connect to network");
    RestartGSMModem ();
  }
  Else
  {
    RegStatus registration = modemGSM. getRegistrationStatus ();
    Serial. println ("Registration[" + GSMRegistrationStatus(registration) + "]:"); 
    Serial. println ("Modem network OK");
  }
  
  Serial. println (modemGSM. gprsConnect (APN_NAME, APN_USER, APN_PSWD)? "GPRS Connect OK": "GPRS Connection failed");

  BOOL stateGPRS = modemGSM. isGprsConnected ();
  if (! stateGPRS)
  {
    RestartGSMModem ();
  }
  
  String state = stateGPRS? "Connected": "Not Connected";
  Serial. println ("GPRS Status:" + state);
  Serial. println ("CCID:" + modemGSM. getSimCCID ());
  Serial. println ("IMEI:" + modemGSM. getIMEI ());
  Serial. println ("Operator:" + modemGSM. getOperator ());

  IPAddress local = modemGSM. localIP ();
  Serial. println ("Local IP:" + Local. toString ());

  int csq = modemGSM. getSignalQuality ();
  if (CSQ = = 0)
  {
    Serial. println ("Signal quality is 0. Restart modem. ");
    RestartGSMModem ();
  }
  Serial. println ("Signal quality:" + GSMSignalLevel (CSQ) + [" + String(csq) + "]"");

  int battLevel = modemGSM. getBattPercent ();
  Serial. println ("Battery level:" + String (battLevel) + "%");

  float battVoltage = modemGSM. getBattVoltage ()/1000.0 F;
  Serial. println ("Battery voltage:" + String (battVoltage));

  String gsmLoc = modemGSM. getGsmLocation ();
  Serial. println ("GSM location:" + gsmLoc);
  Serial. println ("GSM location:" + SwapLocation (gsmLoc));
}

When You run the code, we have the following:

Spread the love
This entry was posted in IT Recipies. Bookmark the permalink.