Sending Messages via SMS or Telegram Using an ESP32 PLC 14 with Integrated 4G

Step-by-step guide to sending SMS messages and Telegram notifications from an ESP32 PLC 14 with integrated 4G using the NB library and Call Me Bot
June 18, 2026 by
Sending Messages via SMS or Telegram Using an ESP32 PLC 14 with Integrated 4G
Boot & Work Corp. S.L, Joan Bello

Introduction

The integration of 4G communication into ESP32-based PLCs opens up endless possibilities for IoT and industrial automation. In a previous blog, "How to Use 4G with the ESP32 PLC", we explained the initial setup for enabling 4G on the ESP32 PLC. This includes preparing the SIM card, configuring the APN, and initializing the NB (Narrowband IoT) library. If you're new to this process, we recommend starting with that post before diving into this guide.

In this article, we will focus on practical applications: sending SMS messages and Telegram notifications directly from the ESP32 PLC. The SMS example discussed here is readily available in the Industrial Shields library under File -> Examples -> Examples for 14 IOs PLC Family -> NB -> SendSMS.

Sending SMS Messages with an ESP32 PLC 14

The ability to send SMS messages is a valuable feature for industrial automation, whether for alarms, notifications, or remote commands. The following example demonstrates how to send SMS messages from the ESP32 PLC 14 using the NB library.

Code Example: SMS Sending

This example is included in the Industrial Shields library under File -> Examples -> Examples for 14 IOs PLC Family -> NB -> SendSMS. Here's the complete code:

arduino_secrets.h (dependency):

#define SECRET_PINNUMBER     ""
#define SECRET_APN           ""
#define SECRET_USERNAME      ""
#define SECRET_PASSWORD      ""

SMS Sending Example Code:

// Include the NB library
#include <NB.h>

// Uncomment to automatically configure the module baud rate
#define MAKE_AUTOBAUD 

#include "arduino_secrets.h" 

// PIN and APN details
const char PINNUMBER[] = SECRET_PINNUMBER;
const char APN[] = SECRET_APN;
const char USERNAME[] = SECRET_USERNAME;
const char PASSWORD[] = SECRET_PASSWORD;

// Initialize the library instance
NB nbAccess(true);
NB_SMS sms;

void setup() {
  Serial.begin(115200);
  while (!Serial);

  Serial.println("SMS Messages Sender");

  // Connection state
  bool connected = false;

  // Configure autobaud
#ifdef MAKE_AUTOBAUD
  if (nbAccess.autobaud()) {
    Serial.println("Autobaud completed successfully");
    nbAccess.shutdown();
  } else {
    Serial.println("Autobaud failed.");
    while (1);
  }
#endif

  // Connect to the network
  while (!connected) {
    if (nbAccess.begin(PINNUMBER, APN, USERNAME, PASSWORD) == NB_READY) {
      connected = true;
    } else {
      Serial.println("Not connected, retrying...");
      delay(1000);
    }
  }

  Serial.println("NB initialized");
}

void loop() {
  Serial.print("Enter a mobile number: ");
  char remoteNum[20];
  readSerial(remoteNum);

  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);

  sms.beginSMS(remoteNum);
  sms.print(txtMsg);
  sms.endSMS();
  Serial.println("Message sent!");
}

int readSerial(char result[]) {
  int i = 0;
  while (true) {
    while (Serial.available() > 0) {
      char inChar = Serial.read();
      if (inChar == '\n') {
        result[i] = '\0';
        return 0;
      }
      if (inChar != '\r') {
        result[i] = inChar;
        i++;
      }
    }
  }
}

With this code, you can easily send SMS messages by entering the recipient's phone number and the desired message via the Serial Monitor.

You can download the example here:

SendSMS

Sending Telegram Messages with Call Me Bot

Although the library does not currently include an example for sending Telegram messages, you can implement this functionality using the Call Me Bot service. Call Me Bot allows you to send Telegram messages without dealing with the complexity of the Telegram API. Before using this example, you must grant permissions to the bot through this authentication page.

Code Example: Telegram Sending

Below is the code for sending Telegram messages using the Call Me Bot service:

arduino_secrets.h (dependency):

#define SECRET_PINNUMBER     ""
#define SECRET_APN           ""
#define SECRET_USERNAME      ""
#define SECRET_PASSWORD      ""

#define SECRET_USER          ""

Telegram Sending Example Code:

// Include the NB library
#include <NB.h>

// Uncomment to automatically configure the module baud rate
#define MAKE_AUTOBAUD 

#include "arduino_secrets.h"

// PIN and APN details
const char PINNUMBER[] = SECRET_PINNUMBER;
const char APN[] = SECRET_APN;
const char USERNAME[] = SECRET_USERNAME;
const char PASSWORD[] = SECRET_PASSWORD;

// Telegram USER (without '@')
const char USER[] = SECRET_USER;

// Initialize library instances
NBClient client;
GPRS gprs;
NB nbAccess(true);

// Server details
char server[] = "api.callmebot.com";
int port = 80;

void setup() {
  Serial.begin(115200);
  while (!Serial);

  Serial.println("Call Me Bot Messages Sender");

  // Connection state
  bool connected = false;

#ifdef MAKE_AUTOBAUD
  if (nbAccess.autobaud()) {
    Serial.println("Autobaud completed successfully");
    nbAccess.shutdown();
  } else {
    Serial.println("Autobaud could not configure the module correctly. Trying again...");
    if (nbAccess.autobaud()) {
      Serial.println("Autobaud completed successfully");
      nbAccess.shutdown();
    } else {
      Serial.println("Autobaud could not configure the module correctly. Blocking...");
      while(1);
    }
  }
#endif

  // Start NB module and attach GPRS
  while (!connected) {
    if ((nbAccess.begin(PINNUMBER, APN, USERNAME, PASSWORD) == NB_READY) && (gprs.attachGPRS() == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected, retrying...");
      delay(1000);
    }
  }

  Serial.println("NB and GPRS initialized");
}

void loop() {
  // Prompt user for a message
  Serial.print("Enter the message to send: ");
  char txtMsg[200];
  readSerial(txtMsg);

  // Encode the message for URL (replace spaces with '%20')
  String msgPayload = txtMsg;
  msgPayload.replace(" ", "%20");

  // Construct the GET request path
  char path[500];
  sprintf(path, "/text.php?source=web&user=@%s&text=%s", USER, msgPayload.c_str());

  Serial.println("Connecting to Call Me Bot...");

  if (client.connect(server, port)) {
    Serial.println("Connected to Call Me Bot server");

    // Make an HTTP GET request
    client.print("GET ");
    client.print(path);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
  } else {
    Serial.println("Connection failed");
    return;
  }

  // Wait for the server to respond, read response
  while (client.connected()) {
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }

  Serial.println();
  Serial.println("Disconnecting from server.");
  client.stop();
}

int readSerial(char result[]) {
  int i = 0;
  while (1) {
    while (Serial.available() > 0) {
      char inChar = (char)Serial.read();
      if (inChar == '\n') {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if (inChar != '\r') {
        result[i] = inChar;
        i++;
      }
    }
  }
}

With this code, you can easily send Telegram messages by entering the desired message via the Serial Monitor.

You can download the example here:

SendTelegram

Conclusion

The ESP32 PLC 14 with 4G connectivity proves to be a powerful tool for IoT and industrial automation. Using the built-in library, sending SMS messages is straightforward with the provided SendSMS example. Additionally, with a little customization, you can send Telegram notifications via the Call Me Bot service.

For those looking to explore more about the initial 4G setup, visit our previous guide: "How to Use 4G with the ESP32 PLC".

​Search in our Blog

Sending Messages via SMS or Telegram Using an ESP32 PLC 14 with Integrated 4G
Boot & Work Corp. S.L, Joan Bello June 18, 2026
Share this post
Tags

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.

PLC Comparison