SIM800L Resetting After Failing APN Check

I have a SIM800L which I have connected to an ESP32-C3. I'm using the following code:

#define TINY_GSM_MODEM_SIM800
#define SerialMon Serial
#define SerialAT Serial1
#define TINY_GSM_DEBUG SerialMon
#define GSM_PIN ""

#define NTP_SERVER "132.163.96.5"

#include <TinyGsmClient.h>

const char apn[] = "chatrweb.apn";
const char gprsUser[] = "";
const char gprsPass[] = "";

#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif

TinyGsmClient client(modem);

// ESP32 and SIM800l pins
#define MODEM_TX 1     // connected
#define MODEM_RX 2     // connected
#define MODEM_RST 3    // connected
#define MODEM_DTR 25    // not used
#define MODEM_RING 34   // not used


void setup() {
  SerialMon.begin(115200);
  delay(1000);
  pinMode(MODEM_RST, OUTPUT);
  digitalWrite(MODEM_RST, LOW);
  delay(100);                     // Keep low for 100ms
  digitalWrite(MODEM_RST, HIGH);  // Release reset pin
  delay(1000);                    // Wait for module to boot
  pinMode(MODEM_DTR, OUTPUT);
  digitalWrite(MODEM_DTR, HIGH);  // Keep modem awake
  pinMode(MODEM_RING, INPUT);     // Optional

  SerialMon.println("Wait ...");
  SerialAT.begin(115200, SERIAL_8N1, MODEM_TX, MODEM_RX);
  delay(3000);
  SerialMon.println("Initializing modem ...");
  modem.init();

  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem Info: ");
  SerialMon.println(modemInfo);

  if (GSM_PIN && modem.getSimStatus() != 3) {
    modem.simUnlock(GSM_PIN);
  }

  SerialMon.print("Waiting for network...");
  if (!modem.waitForNetwork()) {
    SerialMon.println(" fail");
    delay(10000);
    return;
  }
  SerialMon.println(" success");

  if (modem.isNetworkConnected()) {
    DBG("Network connected");
  }
  // IMEI
  String imei = modem.getIMEI();
  SerialMon.print("IMEI: ");
  SerialMon.println(imei);

  // Get operator name
  String operatorName = modem.getOperator();
  SerialMon.print("Operator: ");
  SerialMon.println(operatorName);

  // Signal quality (0–31, 99 = not known)
  int signalQuality = modem.getSignalQuality();
  SerialMon.print("Signal Quality (0-31): ");
  SerialMon.println(signalQuality);

  SerialMon.print("Connecting to APN: ");
  SerialMon.print(apn);
  if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
    SerialMon.println(" fail");
    ESP.restart();
  }
  SerialMon.println(" OK");
  if (modem.isGprsConnected()) {
    SerialMon.println("GPRS connected");
  }
  // Local IP
  IPAddress  localIP = modem.localIP();
  SerialMon.print("Local IP: ");
  SerialMon.println(localIP);
}

void loop() {
  static unsigned long lastPrint = 0;
  if (millis() - lastPrint > 5000) { // every 5 seconds
    int signal = modem.getSignalQuality();
    SerialMon.print("Signal Quality (0-31): ");
    SerialMon.println(signal);

    // Optional: Check if still connected
    if (!modem.isNetworkConnected()) {
      SerialMon.println("Network disconnected!");
    } else {
      SerialMon.println("Network OK");
    }

    lastPrint = millis();
  }

  delay(100);
}

When I connect the module to power (with an active SIM card installed) it blinks every second for a few seconds, then transitions to every 3 seconds. This is consistent with the module connecting to the network. When I run the code above, it connects and identifies all my network details etc.., but when it gets to Connecting to APN it fails and resets. Everything I've read points to a power issue, specifically that it requires up to a 2A draw when connecting.

Here's some details about the system:

  1. I'm using a Dell laptop PS which is rated for 20V and 4.5A. I cut the jack off the end and soldered it to a buck converter rated at a 3A max. The buck converter is dialed down to 4V output. I've tried it with various voltages from 3.8V - 4.2V with the same results detailed above.

  2. There is a 2200uF 10V electrolytic cap across the Vcc and GND pins. I've also tried 470uF and 1000uF.

  3. I'm using an antenna like this one with a u.fl to SMA adapter cable:

  4. Buck converter, GMS module, and ESP32 all share a common ground.

Does anybody see any issue with the power supply I'm using?

What power supply are you using for the SIM800L?

Hello @jools1976

the blink pattern suggests the modem is already connected to the network. And it probably got the correct APN automatically from the network.

Have you tried to omit (e.g. comment out) the connecting to APN step in your code? Does that work or does it then fail on later steps?

Thanks
Felix

Either the power supply or buck or both can't handle the 2A load transient.
Adding gigantic capacitors can actually make things worse.

19V Dell laptop PS with the jack cut off and soldered into a buck converter, which is set to output 4V. ESP32 is being powered by the micro USB cable which I'm also using to upload the code.

See post #4

That worked. Thanks! Some weirdness though following that.

If I should be starting a new post just let me know and I happily will.

So once the above code got me connected and I could verify signal strength etc... I switched to this so that I could send SMS messages:

#define TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>
#include "config.h"

#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif

String extractPhoneNumber(String response);
void handleSms(String number, String msg);

TinyGsmClient client(modem);

bool isReceivingMessage = false, pinReq = false;
String received_message, sender_number, reply_message;

void setup() {
  SerialMon.begin(115200);
  delay(1000);
  pinMode(ACTION_PIN, OUTPUT);
  digitalWrite(ACTION_PIN, LOW);
  pinMode(MODEM_RST, OUTPUT);
  digitalWrite(MODEM_RST, LOW);
  delay(100);
  digitalWrite(MODEM_RST, HIGH);
  delay(1000);
  pinMode(MODEM_DTR, OUTPUT);
  digitalWrite(MODEM_DTR, HIGH);
  pinMode(MODEM_RING, INPUT);

  SerialMon.println("Wait ...");

  // Initialize Serial communication with SIM800L
  SerialAT.begin(115200, SERIAL_8N1, MODEM_TX, MODEM_RX);
  delay(3000);

  // Restart and Initialize Modem
  SerialMon.println("Initializing modem ...");
  modem.restart();
  delay(3000);
  modem.init();

  /* Unlock SIM if needed
  if (GSM_PIN && modem.getSimStatus() != 3) {
    modem.simUnlock(GSM_PIN);
  }*/

  // Print Modem Info
  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem Info: ");
  SerialMon.println(modemInfo);

  // Connect to Network
  SerialMon.print("Waiting for network...");
  if (!modem.waitForNetwork()) {
    SerialMon.println(" fail");
    delay(10000);
    return;
  }
  SerialMon.println(" success");

  if (modem.isNetworkConnected()) {
    SerialMon.println("Network connected");
  }

  // Print Network Details
  String imei = modem.getIMEI();
  SerialMon.print("IMEI: ");
  SerialMon.println(imei);

  String operatorName = modem.getOperator();
  SerialMon.print("Operator: ");
  SerialMon.println(operatorName);

  int signalQuality = modem.getSignalQuality();
  SerialMon.print("Signal Quality (0-31): ");
  SerialMon.println(signalQuality);

  // Enable Network Time Sync
  SerialMon.println("Enabling network time synchronization...");
  modem.sendAT("+CLTS=1");
  delay(500);
  modem.sendAT("&W");
  delay(500);

  // Restart to apply Time Sync settings
  modem.restart();
  delay(3000);
  modem.waitForNetwork();

  // Set SMS mode to Text Mode
  modem.sendAT("+CMGF=1");
  delay(1000);

  // Configure how new SMS are handled (send directly to Serial)
  SerialAT.print("AT+CNMI=2,2,0,0,0\r");
  delay(1000);

  // --- Send Startup SMS ---
  String initialMessage = "DEVICE READY";
  SerialMon.print("Sending SMS to ");
  SerialMon.println(ADMIN_NUMBER);

  bool success = modem.sendSMS(ADMIN_NUMBER, initialMessage);
  if (success) {
    SerialMon.println("SMS sent successfully!");
  } else {
    SerialMon.println("SMS failed to send.");
  }
}

void loop() {
  // Listen for data from the Modem
  while (SerialAT.available()) {
    String response = SerialAT.readStringUntil('\n');
    response.trim();

    // Check if the response is an incoming SMS
    if (response.startsWith("+CMT: ")) {
      sender_number = extractPhoneNumber(response);
      isReceivingMessage = true;
    }
    // If the previous line was "+CMT", this line is the actual message content
    else if (isReceivingMessage) {
      received_message = response;
      isReceivingMessage = false;
      received_message.trim();
      received_message.toLowerCase();  // Make case-insensitive

      handleSms(sender_number, received_message);
    }
  }
}

// Function to handle the logic of incoming messages
void handleSms(String number, String msg) {
  SerialMon.println("New Incoming Message!");
  SerialMon.print("From: ");
  SerialMon.println(number);
  SerialMon.print("Message: ");
  SerialMon.println(msg);

  // Check for "status" command
  if (msg == "initiate" && pinReq == false) {
    SerialMon.println("'status' command received. Preparing reply...");
    pinReq = true;

    // Construct the reply message
    reply_message = "ENTER INITIATION PIN:\n";

    // Send the reply
    SerialMon.print("Sending reply: ");
    SerialMon.println(reply_message);

    bool success = modem.sendSMS(number, reply_message);
    if (success) {
      SerialMon.println("Reply sent successfully!");
    } else {
      SerialMon.println("Failed to send reply.");
    }
  } else if (msg == "17771" && pinReq == true) {
    reply_message = "PIN VALID - ACTIONING NOW\n";
    modem.sendSMS(number, reply_message);
    delay(3000);
    digitalWrite(ACTION_PIN, HIGH);
  } else if (msg != "17771" && pinReq == true) {
    reply_message = "INVALID PIN\n";
    modem.sendSMS(number, reply_message);
    pinReq = false;
    reply_message = "DEVICE READY\n";
    modem.sendSMS(number, reply_message);
  }
  // Handle unknown commands
  else {
    SerialMon.println("Unrecognized command.");
    reply_message = "Unknown command.";
    modem.sendSMS(number, reply_message);
  }

  SerialMon.println();
}

// Helper function to extract phone number from the raw AT response
String extractPhoneNumber(String response) {
  int startIndex = response.indexOf("\"") + 1;
  int endIndex = response.indexOf("\",", startIndex);
  return response.substring(startIndex, endIndex);
}

With the following config file:

//config.h

#define ADMIN_NUMBER "/*my phone number was in here*/" 

// SIM Card PIN (Leave empty if your SIM doesn't have a PIN)
#define GSM_PIN ""

// APN Settings (Check your mobile carrier's website)
//const char apn[] = "internet"; 
//const char gprsUser[] = "";
//const char gprsPass[] = "";

#define MODEM_TX 1
#define MODEM_RX 2
#define MODEM_RST 3
#define MODEM_DTR 25
#define MODEM_RING 34
#define ACTION_PIN 10
#define SerialMon Serial
#define SerialAT Serial1
#define TINY_GSM_DEBUG SerialMon

It compiled fine and would work exactly as I wanted it to, with one exception. If I replied to the "DEVICE READY" SMS right away, everything was fine. I could do this over and over and over again without fail, as long as I didn't wait too long. If I waited any longer than say 20 seconds or so following the "DEVICE READY" message, the module would start flashing every second and nothing would work. I would have to disconnect the power and reset it, which would get it working again.

With the PS being rated for 4.5A shouldn't it easily handle the 2A draw? The buck converter is rated for 3A, so that should be good too right?