How To Automatically Convert Code In Arduino Gsm Library

I am using Arduino gsm shield. The code in Arduino GSM libraries is insufficient for my project. In the code, it only takes information from the serial port and sends sms. But what I need is to send an SMS to the number specified in the one-time code. I don't want to work on serial port. Please Help. Thanks in advance

 SMS sender

 This sketch, for the Arduino GSM shield,sends an SMS message
 you enter in the serial monitor. Connect your Arduino with the
 GSM shield and SIM card, open the serial monitor, and wait for
 the "READY" message to appear in the monitor. Next, type a
 message to send and press "return". Make sure the serial
 monitor is set to send a newline when you press return.

 * GSM shield
 * SIM card that can send SMS

 created 25 Feb 2012
 by Tom Igoe

 This example is in the public domain.


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

#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;

void setup() {
  // initialize serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  Serial.println("SMS Messages Sender");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while (notConnected) {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
      notConnected = false;
    } else {
      Serial.println("Not connected");

  Serial.println("GSM initialized");

void loop() {

  Serial.print("Enter a mobile number: ");
  char remoteNum[20];  // telephone number to send sms

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

  // send the message

  Read input serial
int readSerial(char result[]) {
  int i = 0;
  while (1) {
    while (Serial.available() > 0) {
      char inChar =;
      if (inChar == '\n') {
        result[i] = '\0';
        return 0;
      if (inChar != '\r') {
        result[i] = inChar;

If I understand you correctly, you want to send to a fixed number? If so, replace

  Serial.print("Enter a mobile number: ");
  char remoteNum[20];  // telephone number to send sms


char remoteNum[] = "0123456789"

If you also are in need to use a fixed text, you can do something similar.

Then -you- have to determine when the message needs to be sent, and set a flag that initiates to send your message and then clears the flag.

When the input condition occurs again, rinse & repeat.

I want not only the number but the message part fixed, but when I do both of them, it sends the same SMS over and over again. I want it to work one time when I want it[date=2021-05-22 timezone="Europe/Istanbul"] → 2021-05-22T21:00:00Z

It's very impolite to delete a post after somebody has replied !!

Your original post that @lastchancename replied to:

Sorry I'm new to the forum. Do you have any solution suggestions for my problem? thanks

Simplest is to put it in setup() and keep loop() empty. That guarantees a single run till you reset the board.

Maybe you should give some details about your project.

I want the touch sensor to send sms when the contact is cut, but it goes into a continuous loop and starts sending hundreds of messages in the example code.2021-05-22T21:00:00Z

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.