Circuit Box- Download

Breaking

Sunday, April 7, 2024

ESP32 SIM800L: Send Text Messages (SMS Alert) with Sensor Readings

In this project we’re going to create an SMS notification system with the T-Call ESP32 SIM800L module that sends an SMS when sensor readings are above or below a certain threshold.

  1. Send an SMS with the TTGO T-Call ESP32 SIM800L: you’ll learn how to send a simple “Hello World” message.
  2. SMS Notification system with sensor readings: send a message every time the temperature readings cross the threshold value.
SMS Notification System with Sensor Readings using the T-Call ESP32 SIM800L Board
  • The ESP32 gets new temperature readings every 5 seconds
  • It checks if the temperature is above the threshold value
  • If it’s above the threshold value, it sends an alert SMS with the current temperature value
  • When the temperature goes below the threshold, it sends another alert
  • The ESP32 sends only one SMS every time the temperature readings cross the threshold value

You can modify this project for your specific case. For example, you may want to put the ESP32 in deep sleep mode and send an SMS every hour with the current temperature readings, etc.

You may also like: ESP32 Publish Data to Cloud without Wi-Fi (TTGO T-Call ESP32 SIM800L)

TTGO T-Call ESP32 SIM800L

The TTGO T-Call is a new ESP32 development board that combines a SIM800L GSM/GPRS module. You can get if for approximately $11.

TTGO T-Call ESP32 SIM800L Board

Besides Wi-Fi and Bluetooth, you can communicate with this ESP32 board using SMS, phone calls or you can connect it to the internet. In this tutorial, you’ll learn how to send an SMS notification.

For a complete overview of this board, you can read the following article: $11 TTGO T-Call ESP32 with SIM800L GSM/GPRS (Overview)

Prerequisites

1. ESP32 add-on Arduino IDE

We’ll program the ESP32 using Arduino IDE. So, you need to have the ESP32 add-on installed in your Arduino IDE. Follow the next tutorial, if you haven’t already.

2. Prepaid SIM Card (SMS plan)

To use the TTGO T-Call ESP32 SIM800L board, you need a nano SIM card. We recommend using a SIM card with a prepaid or monthly plan, so that you know exactly how much you’ll spend.

TTGO T-Call ESP32 SIM800L Insert nano SIM card

3. Libraries

For this project ,you also need to install the TinyGSM library to interface with the SIM800L module and the One Wire library by Paul Stoffregen and the Dallas Temperature library to get readings from the DS18B20 temperature sensor.

Installing the TinyGSM Library

Open your Arduino IDE and go to Sketch Include Library > Manage Libraries. The Library Manager should open. Search for TinyGSM. Select the TinyGSM library by Volodymyr Shymanskyy.

Installing TinyGSM library Arduino IDE

Installing DS18B20 Libraries

In the Arduino IDE Library Manager, type “onewire” in the search box and install OneWire library by Paul Stoffregen.

Install OneWire library by Paul Stoffregen in Arduino IDE

Then, search for “Dallas” and install DallasTemperature library by Miles Burton.

Install DallasTemperature library by Miles Burton in Arduino IDE

After installing the libraries, restart your Arduino IDE.

Parts Required

T-Call ESP32 SIM800L DS18B20 Temperature Sensor Circuit

To follow this tutorial you need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!


Note: we had some signal strength issues with the antenna that came with the board package, so we’ve used another antenna (as shown below) and all those connection problems were solved.

Alternative Antenna for TTGO T-Call ESP32 SIM800L Board

Send SMS with T-Call ESP32 SIM800L

In this section, we’ll show you how to send an SMS with the T-Call ESP32 SIM800L board. Let’s build a simple example that sends an “Hello from ESP32!” message to your smartphone.

Copy the following code to your Arduino IDE. But don’t upload it yet, you need to insert the recipient’s phone number in the code.

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-sim800l-send-text-messages-sms/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

// SIM card PIN (leave empty, if not defined)
const char simPIN[]   = "";

// Your phone number to send SMS: + (plus sign) and country code, for Portugal +351, followed by phone number
// SMS_TARGET Example for Portugal +351XXXXXXXXX
#define SMS_TARGET  "+351XXXXXXXXX"

// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800      // Modem is SIM800
#define TINY_GSM_RX_BUFFER   1024  // Set RX buffer to 1Kb

#include <Wire.h>
#include <TinyGsmClient.h>

// TTGO T-Call pins
#define MODEM_RST            5
#define MODEM_PWKEY          4
#define MODEM_POWER_ON       23
#define MODEM_TX             27
#define MODEM_RX             26
#define I2C_SDA              21
#define I2C_SCL              22

// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT  Serial1

// Define the serial console for debug prints, if needed
//#define DUMP_AT_COMMANDS

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

#define IP5306_ADDR          0x75
#define IP5306_REG_SYS_CTL0  0x00

bool setPowerBoostKeepOn(int en){
  Wire.beginTransmission(IP5306_ADDR);
  Wire.write(IP5306_REG_SYS_CTL0);
  if (en) {
    Wire.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
  } else {
    Wire.write(0x35); // 0x37 is default reg value
  }
  return Wire.endTransmission() == 0;
}

void setup() {
  // Set console baud rate
  SerialMon.begin(115200);

  // Keep power when running from battery
  Wire.begin(I2C_SDA, I2C_SCL);
  bool isOk = setPowerBoostKeepOn(1);
  SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));

  // Set modem reset, enable, power pins
  pinMode(MODEM_PWKEY, OUTPUT);
  pinMode(MODEM_RST, OUTPUT);
  pinMode(MODEM_POWER_ON, OUTPUT);
  digitalWrite(MODEM_PWKEY, LOW);
  digitalWrite(MODEM_RST, HIGH);
  digitalWrite(MODEM_POWER_ON, HIGH);

  // Set GSM module baud rate and UART pins
  SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
  delay(3000);

  // Restart SIM800 module, it takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  modem.restart();
  // use modem.init() if you don't need the complete restart

  // Unlock your SIM card with a PIN if needed
  if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
    modem.simUnlock(simPIN);
  }

  // To send an SMS, call modem.sendSMS(SMS_TARGET, smsMessage)
  String smsMessage = "Hello from ESP32!";
  if(modem.sendSMS(SMS_TARGET, smsMessage)){
    SerialMon.println(smsMessage);
  }
  else{
    SerialMon.println("SMS failed to send");
  }
}

void loop() {
  delay(1);
}

View raw code

How the Code Works

Insert you SIM card PIN in the following variable. If it’s not defined, you can leave this variable empty.

// SIM card PIN (leave empty, if not defined)
const char simPIN[] = "";

Then, add the phone number you want to send the SMS to. The number should be in international format, otherwise, it won’t work: (plus sign) and country code, for Portugal +351, followed by phone number.

Example for Portugal +351XXXXXXXXX

#define SMS_TARGET "+351XXXXXXXXXXXX"

Configure the TinyGSM library to work with the SIM800L modem:

#define TINY_GSM_MODEM_SIM800    // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024  // Set RX buffer to 1Kb

Include the following libraries:

#include <Wire.h>
#include <TinyGsmClient.h>

The following lines define the pins used by the SIM800L module:

#define MODEM_RST            5
#define MODEM_PWKEY          4
#define MODEM_POWER_ON       23
#define MODEM_TX             27
#define MODEM_RX             26
#define I2C_SDA              21
#define I2C_SCL              22

Initialize a serial communication to interact with the Serial Monitor and another to interact with the SIM800L module.

// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1

In the setup(), initialize the Serial Monitor.

SerialMon.begin(115200);

Setup the SIM800L pins in a proper state to operate:

pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);

Initialize a serial communication with the SIM800L module:

SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);

Initialize the SIM800L module and unlock the SIM card PIN if needed.

SerialMon.println("Initializing modem...");
modem.restart();
// use modem.init() if you don't need the complete restart

// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
  modem.simUnlock(simPIN);
}

To send an SMS, you just need to use the sendSMS() method on the modem object and pass as arguments the recipient’s phone number and the message.

modem.sendSMS(SMS_TARGET, smsMessage);

In this case, the message is “Hello from ESP32!“, but it can be replaced with other info like sensor data.

String smsMessage = "Hello from ESP32!";
if(modem.sendSMS(SMS_TARGET, smsMessage)){
  SerialMon.println(smsMessage);
}
else{
  SerialMon.println("SMS failed to send");
}

Demonstration

With the nano SIM card inserted in the module, upload the code to your T-Call ESP32 SIM800L board.

Go to Tools Board and select ESP32 Dev Module. Go to Tools > Port and select the COM port your board is connected to. Finally, press the upload button to upload the code to your board.

Upload Code to Arduino IDE

Note: at the moment, there isn’t a board for the T-Call ESP32 SIM800L, but we’ve selected the ESP32 Dev Module and it’s been working fine.

After uploading the code, open the Serial Monitor at a baud rate of 115200 to see what’s going on.

Initializing the T-Call ESP32 SIM800L Board Serial Monitor Arduino IDE

After a few seconds, you should receive an SMS on the recipient’s phone number.

No comments:

Post a Comment