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 ""
#define SECRET_PINNUMBER ""
#define SECRET_APN ""
#define SECRET_USERNAME ""
#define SECRET_PASSWORD ""
SMS Sending Example Code:
#include <NB.h>
#include "arduino_secrets.h"
const char PINNUMBER[] = SECRET_PINNUMBER;
const char APN[] = SECRET_APN;
const char USERNAME[] = SECRET_USERNAME;
const char PASSWORD[] = SECRET_PASSWORD;
NB nbAccess(true);
NB_SMS sms;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("SMS Messages Sender");
bool connected = false;
#ifdef MAKE_AUTOBAUD
if (nbAccess.autobaud()) {
Serial.println("Autobaud completed successfully");
nbAccess.shutdown();
} else {
Serial.println("Autobaud failed.");
while (1);
}
#endif
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++;
}
}
}
}
#include <NB.h>
#include "arduino_secrets.h"
const char PINNUMBER[] = SECRET_PINNUMBER;
const char APN[] = SECRET_APN;
const char USERNAME[] = SECRET_USERNAME;
const char PASSWORD[] = SECRET_PASSWORD;
NB nbAccess(true);
NB_SMS sms;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("SMS Messages Sender");
bool connected = false;
#ifdef MAKE_AUTOBAUD
if (nbAccess.autobaud()) {
Serial.println("Autobaud completed successfully");
nbAccess.shutdown();
} else {
Serial.println("Autobaud failed.");
while (1);
}
#endif
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 ""
#define SECRET_PINNUMBER ""
#define SECRET_APN ""
#define SECRET_USERNAME ""
#define SECRET_PASSWORD ""
#define SECRET_USER ""
Telegram Sending Example Code:
#include <NB.h>
#include "arduino_secrets.h"
const char PINNUMBER[] = SECRET_PINNUMBER;
const char APN[] = SECRET_APN;
const char USERNAME[] = SECRET_USERNAME;
const char PASSWORD[] = SECRET_PASSWORD;
const char USER[] = SECRET_USER;
NBClient client;
GPRS gprs;
NB nbAccess(true);
char server[] = "api.callmebot.com";
int port = 80;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Call Me Bot Messages Sender");
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
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() {
Serial.print("Enter the message to send: ");
char txtMsg[200];
readSerial(txtMsg);
String msgPayload = txtMsg;
msgPayload.replace(" ", "%20");
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");
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;
}
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++;
}
}
}
}
#include <NB.h>
#include "arduino_secrets.h"
const char PINNUMBER[] = SECRET_PINNUMBER;
const char APN[] = SECRET_APN;
const char USERNAME[] = SECRET_USERNAME;
const char PASSWORD[] = SECRET_PASSWORD;
const char USER[] = SECRET_USER;
NBClient client;
GPRS gprs;
NB nbAccess(true);
char server[] = "api.callmebot.com";
int port = 80;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Call Me Bot Messages Sender");
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
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() {
Serial.print("Enter the message to send: ");
char txtMsg[200];
readSerial(txtMsg);
String msgPayload = txtMsg;
msgPayload.replace(" ", "%20");
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");
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;
}
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".